Skip to content
Snippets Groups Projects
Commit 43ba100c authored by Eduardo Souza's avatar Eduardo Souza
Browse files

[FIX] Lookuptable não está tão mais feita a cuspe

parent 504657f6
Branches
No related tags found
No related merge requests found
No preview for this file type
...@@ -189,7 +189,7 @@ void calculate_finger_table(Node* head, int max_id) { ...@@ -189,7 +189,7 @@ void calculate_finger_table(Node* head, int max_id) {
//Função para imprimir todas as tabelas finger //Função para imprimir todas as tabelas finger
void print_finger_table(Node** nodes_involved,int num_nodes, int timestamp) { void print_finger_table(Node** nodes_involved,int num_nodes, int timestamp) {
for(int i = 0; i <= num_nodes; i++) { for(int i = 0; i < num_nodes; i++) {
printf("%d T %d {", timestamp, nodes_involved[i]->id); printf("%d T %d {", timestamp, nodes_involved[i]->id);
for (int j = 0; j < nodes_involved[i]->tam_finger; j++) { for (int j = 0; j < nodes_involved[i]->tam_finger; j++) {
printf("%d", nodes_involved[i]->finger[j]); printf("%d", nodes_involved[i]->finger[j]);
...@@ -216,6 +216,7 @@ void print_finger_table_completa(Node* head, int timestamp) { ...@@ -216,6 +216,7 @@ void print_finger_table_completa(Node* head, int timestamp) {
// Função para realizar lookup de uma chave // Função para realizar lookup de uma chave
Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) { Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) {
printf("Lookup\n");
Node* current = head; Node* current = head;
Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*)); Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*));
*num_nodes = 0; *num_nodes = 0;
...@@ -225,25 +226,23 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) ...@@ -225,25 +226,23 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes)
} }
printf("%d L %d {", timestamp, key); printf("%d L %d {", timestamp, key);
Node* start = current; //Meu nó partida Node *start = current;
int id_aux = current->id; int id_aux = current->id;
nodes_involved[(*num_nodes)++] = current; nodes_involved[(*num_nodes)++] = current;
printf("%d,", current->id);
while(1){ while(1){
int found = 0; int found = 0;
printf("%d", current->id);
//Verificar se o nó atual já tem a chave //Verificar se o nó atual já tem a chave
for (int i = 0; i < current->key_count; i++) { for (int i = 0; i < current->key_count; i++) {
if (current->keys[i] == key) { if (current->keys[i] == key) {
printf("%d}\n", current->id); printf("}\n");
nodes_involved[(*num_nodes)++] = current;
return nodes_involved; return nodes_involved;
} }
} }
printf(",");
//Procura o maior nó conhecido pela finger_table que está acima do valor da chave //Procura o maior nó conhecido pela finger_table que está acima do valor da chave
for (int i = 0; i < current->tam_finger; i++) { for (int i = 0; i < current->tam_finger; i++) {
if (current->finger[i] > key) { if (current->finger[i] > key) {
printf("%d", current->finger[i]);
id_aux = current->finger[i]; id_aux = current->finger[i];
found = 1; found = 1;
break; break;
...@@ -258,28 +257,21 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) ...@@ -258,28 +257,21 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes)
max = current->finger[i]; max = current->finger[i];
} }
} }
id_aux = max;
} }
// Mover para o nó com ID id_aux // Mover para o nó com ID id_aux
while(current->id != id_aux){ while(current->id != id_aux){
current = current->next; current = current->next;
} }
nodes_involved[*num_nodes++] = current; //Adiciona o nó atual ao array de nós envolvidos nodes_involved[(*num_nodes)++] = current; //Adiciona o nó atual ao array de nós envolvidos
//Verificar se o nó atual já tem a chave //Previne loops infinitos
for (int i = 0; i < current->key_count; i++) { if(current->id == start->id){
if (current->keys[i] == key) {
nodes_involved[(*num_nodes)++] = current;
printf("}\n"); printf("}\n");
return nodes_involved; nodes_involved[(*num_nodes)++] = current;
} break;
} }
//Se o nó atual não tiver a chave, continuar a busca
printf(",");
start = current;
} }
printf("}\n");
nodes_involved[(*num_nodes)++] = current;
return nodes_involved; return nodes_involved;
} }
...@@ -336,6 +328,7 @@ int main() { ...@@ -336,6 +328,7 @@ int main() {
int num_nodes = 0; int num_nodes = 0;
nodes_involved = lookup(dht, timestamp, id, key, &num_nodes); nodes_involved = lookup(dht, timestamp, id, key, &num_nodes);
print_finger_table(nodes_involved, num_nodes, timestamp); print_finger_table(nodes_involved, num_nodes, timestamp);
free(nodes_involved);
} else if (op == 'P'){ } else if (op == 'P'){
imprime_chaves(dht); imprime_chaves(dht);
print_finger_table_completa(dht, timestamp); print_finger_table_completa(dht, timestamp);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment