Skip to content

Commit 292d3bb

Browse files
committed
feat: corrigindo indeces incorretos
1 parent b802f77 commit 292d3bb

File tree

9 files changed

+234
-228
lines changed

9 files changed

+234
-228
lines changed

tp01/src/data/ArquivoEpisode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ArquivoEpisode() throws Exception {
4444
public int create(Episode e) throws Exception {
4545
int id = super.create(e);
4646
indiceIndiretoNome.create(new PairNameID(e.getName(), id));
47-
relacaoNN.create(new PairIDFK(e.getId(), e.getFkSerie()));
47+
relacaoNN.create(new PairIDFK(e.getFkSerie(), e.getId()));
4848
return id;
4949
}
5050

@@ -176,9 +176,9 @@ public boolean update(Episode novaEpisodio) throws Exception {
176176
if (super.update(novaEpisodio)) {
177177
if (!e.getName().equals(novaEpisodio.getName())) {
178178
indiceIndiretoNome.delete(new PairNameID(e.getName(), e.getId()));
179-
relacaoNN.delete(new PairIDFK(e.getId(), e.getFkSerie()));
179+
relacaoNN.delete(new PairIDFK(e.getFkSerie(), e.getId()));
180180
indiceIndiretoNome.create(new PairNameID(novaEpisodio.getName(), novaEpisodio.getId()));
181-
relacaoNN.create(new PairIDFK(e.getId(), e.getFkSerie()));
181+
relacaoNN.create(new PairIDFK(e.getFkSerie(), e.getId()));
182182
}
183183
return true;
184184
}
Lines changed: 29 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,77 @@
1-
/*
2-
* Esta classe representa um objeto para uma entidade
3-
* que será armazenado em uma árvore B+.
4-
*
5-
* Neste caso em particular, este objeto é representado
6-
* por dois números inteiros para que possa conter
7-
* relacionamentos entre dois IDs de entidades quaisquer.
8-
*
9-
* Implementado pelo Prof. Marcos Kutova
10-
* v1.0 - 2021
11-
*/
121
package tp01.src.storage.indexes;
132

14-
import java.io.ByteArrayInputStream;
15-
import java.io.ByteArrayOutputStream;
16-
import java.io.DataInputStream;
17-
import java.io.DataOutputStream;
18-
import java.io.IOException;
3+
import java.io.*;
194

205
import tp01.src.storage.records.RegisterTreeB;
216

22-
/**
23-
* Classe que representa um par de IDs para ser armazenado em uma estrutura de
24-
* árvore B+.
25-
* Usado para representar relacionamentos entre registros de duas entidades.
26-
*/
277
public class PairIDFK implements RegisterTreeB<PairIDFK> {
288

29-
private int id; /** ID da entidade principal (chave primária). */
30-
private int fk; /** ID da entidade relacionada (chave estrangeira). */
31-
private short TAMANHO = 8; /** Tamanho fixo da estrutura serializada (em bytes). */
9+
private int fk; // Chave estrangeira — identificador da entidade relacionada
10+
private int id; // Chave primária — identificador da entidade principal
11+
private short TAMANHO = 8;
3212

33-
/**
34-
* Construtor padrão. Inicializa ambos os campos com -1.
35-
*/
3613
public PairIDFK() {
3714
this(-1, -1);
3815
}
3916

40-
/**
41-
* Construtor que inicializa apenas a chave estrangeira (FK), com ID -1.
42-
*
43-
* @param n1 Valor da chave estrangeira.
44-
*/
45-
public PairIDFK(int n1) {
46-
this(n1, -1);
17+
public PairIDFK(int fk) {
18+
this(fk, -1);
4719
}
4820

49-
/**
50-
* Construtor que inicializa os dois campos: ID e FK.
51-
*
52-
* @param n1 ID da entidade principal.
53-
* @param n2 ID da entidade relacionada (FK).
54-
*/
55-
public PairIDFK(int n1, int n2) {
56-
try {
57-
this.id = n1; // ID da tabela principal (N)
58-
this.fk = n2; // ID da tabela secundária (1)
59-
} catch (Exception ec) {
60-
ec.printStackTrace();
61-
}
62-
}
63-
64-
/**
65-
* Retorna o ID da entidade principal.
66-
*
67-
* @return ID.
68-
*/
69-
public int getId() {
70-
return id;
21+
public PairIDFK(int fk, int id) {
22+
this.fk = fk;
23+
this.id = id;
7124
}
7225

73-
/**
74-
* Retorna a chave estrangeira (FK).
75-
*
76-
* @return FK.
77-
*/
7826
public int getFk() {
7927
return fk;
8028
}
8129

82-
/**
83-
* Cria e retorna uma cópia deste objeto.
84-
*
85-
* @return Clone de PairIDFK.
86-
*/
30+
public int getId() {
31+
return id;
32+
}
33+
8734
@Override
8835
public PairIDFK clone() {
89-
return new PairIDFK(this.id, this.fk);
36+
return new PairIDFK(this.fk, this.id);
9037
}
9138

92-
/**
93-
* Retorna o tamanho da estrutura em bytes.
94-
*
95-
* @return Tamanho em bytes (8).
96-
*/
9739
public short size() {
9840
return this.TAMANHO;
9941
}
10042

101-
/**
102-
* Compara dois objetos {@code PairIDFK} com base em seus IDs e FKs.
103-
* A comparação dá prioridade ao ID, e em caso de empate, à FK.
104-
*
105-
* @param a Outro objeto PairIDFK.
106-
* @return Valor negativo, zero ou positivo conforme a ordenação.
107-
*/
43+
@Override
10844
public int compareTo(PairIDFK a) {
109-
if (this.fk == -1) {
110-
return this.id - a.id;
45+
System.out.println("FK (chave estrangeira): " + this.fk);
46+
System.out.println("ID (chave primária): " + this.id);
47+
48+
if (this.id == -1) {
49+
return this.fk - a.fk;
11150
}
112-
if (this.id != a.id) {
113-
return this.id - a.id;
51+
52+
if (this.fk != a.fk) {
53+
return this.fk - a.fk;
11454
}
115-
return this.fk == -1 ? 0 : this.fk - a.fk;
55+
56+
return this.id == -1 ? 0 : this.id - a.id;
11657
}
11758

118-
/**
119-
* Retorna uma representação em string do par ID;FK.
120-
*
121-
* @return String formatada com os dois valores.
122-
*/
12359
public String toString() {
124-
return String.format("%3d", this.id) + ";" + String.format("%-3d", this.fk);
60+
return String.format("%3d", this.fk) + ";" + String.format("%-3d", this.id);
12561
}
12662

127-
/**
128-
* Serializa o objeto em um array de bytes.
129-
*
130-
* @return Array de bytes com os dados serializados.
131-
* @throws IOException Se ocorrer erro na escrita.
132-
*/
13363
public byte[] toByteArray() throws IOException {
13464
ByteArrayOutputStream baos = new ByteArrayOutputStream();
13565
DataOutputStream dos = new DataOutputStream(baos);
136-
dos.writeInt(this.id);
13766
dos.writeInt(this.fk);
67+
dos.writeInt(this.id);
13868
return baos.toByteArray();
13969
}
14070

141-
/**
142-
* Desserializa os dados de um array de bytes para preencher o objeto.
143-
*
144-
* @param ba Array de bytes com os dados serializados.
145-
* @throws IOException Se ocorrer erro na leitura.
146-
*/
14771
public void fromByteArray(byte[] ba) throws IOException {
14872
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
14973
DataInputStream dis = new DataInputStream(bais);
150-
this.id = dis.readInt();
15174
this.fk = dis.readInt();
75+
this.id = dis.readInt();
15276
}
153-
154-
}
77+
}
0 Bytes
Binary file not shown.

tp02/files/episodios/episodios.db

35 Bytes
Binary file not shown.

tp02/src/data/ArchiveEpisode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public ArchiveEpisode() throws Exception {
4444
public int create(Episode e) throws Exception {
4545
int id = super.create(e);
4646
indiceIndiretoNome.create(new PairNameID(e.getName(), id));
47+
System.out.println(e.getId());
4748
relacao1N.create(new PairIDFK(e.getFkSerie(), e.getId()));
4849
return id;
4950
}
@@ -56,7 +57,8 @@ public int create(Episode e) throws Exception {
5657
* @throws Exception caso ocorra erro durante a leitura.
5758
*/
5859
public Episode[] readFkSerie(int fkSeries) throws Exception { // Faz a busca somente dentro de epsódios
59-
ArrayList<PairIDFK> pares = relacao1N.read(new PairIDFK(fkSeries));
60+
System.out.println(fkSeries);
61+
ArrayList<PairIDFK> pares = relacao1N.read(new PairIDFK(fkSeries, -1));
6062

6163
if (pares.size() > 0) {
6264

@@ -176,7 +178,7 @@ public boolean update(Episode novaEpisodio) throws Exception {
176178
if (super.update(novaEpisodio)) {
177179
if (!e.getName().equals(novaEpisodio.getName())) {
178180
indiceIndiretoNome.delete(new PairNameID(e.getName(), e.getId()));
179-
relacao1N.delete(new PairIDFK(e.getId(), e.getFkSerie()));
181+
relacao1N.delete(new PairIDFK(e.getFkSerie(), e.getId()));
180182
indiceIndiretoNome.create(new PairNameID(novaEpisodio.getName(), novaEpisodio.getId()));
181183
relacao1N.create(new PairIDFK(e.getFkSerie(), e.getId()));
182184
}

tp02/src/data/ArchiveRelationNN.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
public class ArchiveRelationNN {
1414

1515
/** Relação de ator para série. */
16-
private ArchiveTreeB<PairIDFK> actorSerie;
16+
private ArchiveTreeB<PairFKFK> actorSerie;
1717

1818
/** Relação de série para ator. */
19-
private ArchiveTreeB<PairIDFK> serieActor;
19+
private ArchiveTreeB<PairFKFK> serieActor;
2020

2121
/**
2222
* Construtor que inicializa os arquivos de controle de relações.
2323
*
2424
* @throws Exception caso ocorra falha na criação dos arquivos ou índices.
2525
*/
2626
public ArchiveRelationNN() throws Exception {
27-
actorSerie = new ArchiveTreeB<>(PairIDFK.class.getConstructor(), 5, "tp02/files/actor_serie.db");
28-
serieActor = new ArchiveTreeB<>(PairIDFK.class.getConstructor(), 5, "tp02/files/serie_actor.db");
27+
actorSerie = new ArchiveTreeB<>(PairFKFK.class.getConstructor(), 5, "tp02/files/actor_serie.db");
28+
serieActor = new ArchiveTreeB<>(PairFKFK.class.getConstructor(), 5, "tp02/files/serie_actor.db");
2929
}
3030

3131
/**
@@ -36,8 +36,8 @@ public ArchiveRelationNN() throws Exception {
3636
* @throws Exception caso ocorra erro durante a criação das relações.
3737
*/
3838
public void createRelation(int idActor, int idSerie) throws Exception {
39-
actorSerie.create(new PairIDFK(idActor, idSerie));
40-
serieActor.create(new PairIDFK(idSerie, idActor));
39+
actorSerie.create(new PairFKFK(idActor, idSerie));
40+
serieActor.create(new PairFKFK(idSerie, idActor));
4141
}
4242

4343
/**
@@ -47,8 +47,8 @@ public void createRelation(int idActor, int idSerie) throws Exception {
4747
* @return lista de relações ator -> série.
4848
* @throws Exception caso ocorra erro durante a leitura.
4949
*/
50-
public ArrayList<PairIDFK> readSeriesByActor(int idActor) throws Exception {
51-
return actorSerie.read(new PairIDFK(idActor));
50+
public ArrayList<PairFKFK> readSeriesByActor(int idActor) throws Exception {
51+
return actorSerie.read(new PairFKFK(idActor));
5252
}
5353

5454
/**
@@ -58,8 +58,8 @@ public ArrayList<PairIDFK> readSeriesByActor(int idActor) throws Exception {
5858
* @return lista de relações série -> ator.
5959
* @throws Exception caso ocorra erro durante a leitura.
6060
*/
61-
public ArrayList<PairIDFK> readActorsBySerie(int idSerie) throws Exception {
62-
return serieActor.read(new PairIDFK(idSerie));
61+
public ArrayList<PairFKFK> readActorsBySerie(int idSerie) throws Exception {
62+
return serieActor.read(new PairFKFK(idSerie));
6363
}
6464

6565
/**
@@ -71,8 +71,8 @@ public ArrayList<PairIDFK> readActorsBySerie(int idSerie) throws Exception {
7171
* @throws Exception caso ocorra erro durante a exclusão.
7272
*/
7373
public boolean deleteRelation(int idActor, int idSerie) throws Exception {
74-
boolean deletedActorSerie = actorSerie.delete(new PairIDFK(idActor, idSerie));
75-
boolean deletedSerieActor = serieActor.delete(new PairIDFK(idSerie, idActor));
74+
boolean deletedActorSerie = actorSerie.delete(new PairFKFK(idActor, idSerie));
75+
boolean deletedSerieActor = serieActor.delete(new PairFKFK(idSerie, idActor));
7676
return deletedActorSerie && deletedSerieActor;
7777
}
7878
}

0 commit comments

Comments
 (0)