void registry_free(void) { if(!registry.enabled) return; // we need to destroy the dictionaries ourselves // since the dictionaries use memory we allocated while(registry.persons->values_index.root) { REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value; registry_person_del(p); } while(registry.machines->values_index.root) { REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value; // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages); while(m->machine_urls->values_index.root) { REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value; // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", mu->url->url, mu->first_t, mu->last_t, mu->usages, mu->flags); //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url); //dictionary_destroy(mu->persons); debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid); dictionary_del(m->machine_urls, mu->url->url); debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url); registry_url_unlink(mu->url); debug(D_REGISTRY, "Registry: freeing machine url"); freez(mu); } debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid); dictionary_del(registry.machines, m->guid); debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid); dictionary_destroy(m->machine_urls); debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid); freez(m); } // and free the memory of remaining dictionary structures debug(D_REGISTRY, "Registry: destroying persons dictionary"); dictionary_destroy(registry.persons); debug(D_REGISTRY, "Registry: destroying machines dictionary"); dictionary_destroy(registry.machines); }
/** * * key の値が存在していれば取得 * * @param self * @param key * @return */ hash_item *dictionary_get(hash *self, char *key) { dictionary *it = dictionary_new(self); hash_item *pair; while ((pair = dictionary_next(it))) { if (strcmp(key, pair->key) == 0) { dictionary_destroy(it); return pair; } } dictionary_destroy(it); return NULL; }
int main (int argc, char **argv){ if((sem_init(&sem_kernel,1,0))==-1){ perror("No se puede crear el semaforo"); } PATH=argv[1]; inicializarConfiguracion(); printf("\n\n EL PID DE ESTA CPU ES: %d\n\n",(int)getpid()); pthread_create(&conexion_umv, NULL, (void*) &core_conexion_umv, NULL); pthread_create(&conexion_kernel, NULL, (void*) &core_conexion_kernel, NULL); pthread_join(conexion_umv, NULL); pthread_join(conexion_kernel,NULL); if(socket_cerrarConexion(sockUMV)==-1){ log_escribir(archLog,"Cerrar Conexion",ERROR,"No se pudo conectar a la UMV"); } free(config->path); dictionary_destroy(config->properties); free(config); return 0; }
void replication_task_balance_files_in_dir(ReplicationTask_t *task) { EXLog(REPL, DBG, "Task: Balance files in directory [%s]", task->path); /* Sanity */ if (!task->path) return; /* Strategy: * Iterate through directory * For each file, check conflicts * If no conflicts exist, add task to balance it to front of queue * If conflicts exists, put task to balance directory back on the front of the task list * If no conflicts existed, iterate through directory again * Put each subdir up for balancing */ /* Get all the DIR entries for the active volumes */ DIR **entries = volume_active_dir_entries(task->path); DIR **entries_start = entries; ReplicationTask_t child_task; replication_task_init(&child_task); child_task.opcode = REP_OP_BALANCE_FILE; child_task.volume_basepath[0] = 0; int saw_conflict = 0; if (entries) { /* create a hash table that will help detect duplicate entries */ Dictionary_t *dic = dictionary_create_with_size(512); int64_t tmp; while (*entries) { /* For each DIR, iterate through and grab entries */ DIR *entry = *entries; struct dirent *dent = readdir(entry); while (dent) { if (dent->d_type != DT_DIR && strcmp("..", dent->d_name) && strcmp(".", dent->d_name)) { /* For each entry, add it to the list if it doesn't exist in the hash table */ if (!dictionary_get_int(dic, dent->d_name, &tmp)) { dictionary_set_int(dic, dent->d_name, tmp); snprintf(child_task.path, PATH_MAX-1, "%s/%s", task->path, dent->d_name); mode_t mode = 0; int mode_conflict = 0; /* Perform diagnostics on the path */ volume_diagnose_raid_file_posession(child_task.path, NULL, NULL, &mode, &mode_conflict, NULL, NULL, NULL, NULL); if (!mode_conflict) { /* No conflict, add task for it */ replication_queue_task(&child_task, OP_PRI_SYNC_FILE, 1); } else { /* Conflict? Resolve it */ EXLog(REPL, DBG, " > Conflict detected on file [%s]", child_task.path); volume_resolve_conflicting_modes(child_task.path); saw_conflict = 1; } } } dent = readdir(entry); } closedir(entry); /* Don't forget to close the DIR after use! */ entries++; } /* Free memory */ dictionary_destroy(dic); free(entries_start); } /* Now, did we see a conflict? If so, re-queue the current tast */ if (saw_conflict) { EXLog(REPL, DBG, " > Conflicts detected; restarting sync"); replication_queue_task(task, OP_PRI_SYNC_FILE, 1); return; } /* Otherwise, go through the directory and queue up balancing for all subdirectories */ entries = volume_active_dir_entries(task->path); entries_start = entries; replication_task_init(&child_task); child_task.opcode = REP_OP_BALANCE_FILES_IN_DIR; child_task.volume_basepath[0] = 0; if (entries) { /* create a hash table that will help detect duplicate entries */ Dictionary_t *dic = dictionary_create_with_size(512); int64_t tmp; while (*entries) { /* For each DIR, iterate through and grab entries */ DIR *entry = *entries; struct dirent *dent = readdir(entry); while (dent) { if (dent->d_type == DT_DIR && strcmp("..", dent->d_name) && strcmp(".", dent->d_name)) { /* For each entry, add it to the list if it doesn't exist in the hash table */ if (!dictionary_get_int(dic, dent->d_name, &tmp)) { dictionary_set_int(dic, dent->d_name, tmp); snprintf(child_task.path, PATH_MAX-1, "%s/%s", task->path, dent->d_name); replication_queue_task(&child_task, OP_PRI_SYNC_FILE, 1); } } dent = readdir(entry); } closedir(entry); /* Don't forget to close the DIR after use! */ entries++; } /* Free memory */ dictionary_destroy(dic); free(entries_start); } }
void replication_task_mirror_directory(ReplicationTask_t *task) { int absences = 0; mode_t mode = 0; int mode_conflict = 0; EXLog(REPL, DBG, "Task: mirror directory [%s]", task->path); /* Sanity */ if (!task->path) return; /* Perform diagnostics on the path */ volume_diagnose_raid_file_posession(task->path, NULL, &absences, &mode, &mode_conflict, NULL, NULL, NULL, NULL); /* If this isn't a directory, just exit */ if (!mode_conflict && (mode & S_IFMT) != S_IFDIR) { EXLog(REPL, DBG, " > Path is not a directory"); return; } /* Conflict? Resolve */ if (mode_conflict) { EXLog(REPL, DBG, " > Detected mode conflicts"); volume_resolve_conflicting_modes(task->path); /* Perform diagnostics on the path again */ volume_diagnose_raid_file_posession(task->path, NULL, &absences, &mode, &mode_conflict, NULL, NULL, NULL, NULL); /* If this isn't a directory, just exit */ if (!mode_conflict && (mode & S_IFMT) != S_IFDIR) { EXLog(REPL, DBG, " > Path is not a directory [after conflict resolution]"); return; } } /* Absences? Make directories */ if (absences) { EXLog(REPL, DBG, " > Absences found; mirroring"); volume_mkdir_path_on_active_volumes(task->path, (mode & 0xFFFF) | S_IFDIR); } /* Now we should queue up child directories */ /* Get all the DIR entries for the active volumes */ DIR **entries = volume_active_dir_entries(task->path); DIR **entries_start = entries; ReplicationTask_t child_task; replication_task_init(&child_task); child_task.opcode = REP_OP_MIRROR_DIRECTORY; child_task.volume_basepath[0] = 0; if (entries) { /* create a hash table that will help detect duplicate entries */ Dictionary_t *dic = dictionary_create_with_size(512); int64_t tmp; while (*entries) { /* For each DIR, iterate through and grab entries */ DIR *entry = *entries; struct dirent *dent = readdir(entry); while (dent) { if (dent->d_type == DT_DIR && strcmp("..", dent->d_name) && strcmp(".", dent->d_name)) { /* For each entry, add it to the list if it doesn't exist in the hash table */ if (!dictionary_get_int(dic, dent->d_name, &tmp)) { dictionary_set_int(dic, dent->d_name, tmp); snprintf(child_task.path, PATH_MAX-1, "%s/%s", task->path, dent->d_name); replication_queue_task(&child_task, OP_PRI_SYNC_DIR, 1); } } dent = readdir(entry); } closedir(entry); /* Don't forget to close the DIR after use! */ entries++; } /* Free memory */ dictionary_destroy(dic); free(entries_start); } }
int main(int argc, char **argv) { if(argc || argv) {;} // DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS); DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS); if(!dict) fatal("Cannot create dictionary."); struct rusage start, end; unsigned long long dt; char buf[100 + 1]; struct myvalue value, *v; int i, max = 100000, max2; // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Inserting %d entries in the dictionary\n", max); for(i = 0; i < max; i++) { value.i = i; snprintf(buf, 100, "%d", i); dictionary_set(dict, buf, &value, sizeof(struct myvalue)); } getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt); fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Retrieving %d entries from the dictionary\n", max); for(i = 0; i < max; i++) { value.i = i; snprintf(buf, 100, "%d", i); v = dictionary_get(dict, buf); if(!v) fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i); else if(v->i != i) fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i); } getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt); fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Resetting %d entries in the dictionary\n", max); for(i = 0; i < max; i++) { value.i = i; snprintf(buf, 100, "%d", i); dictionary_set(dict, buf, &value, sizeof(struct myvalue)); } getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt); fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max); max2 = max * 2; for(i = max; i < max2; i++) { value.i = i; snprintf(buf, 100, "%d", i); v = dictionary_get(dict, buf); if(v) fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i); } getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt); fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Deleting %d entries from the dictionary\n", max); for(i = 0; i < max; i++) { value.i = i; snprintf(buf, 100, "%d", i); dictionary_del(dict, buf); } getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt); fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); // ------------------------------------------------------------------------ getrusage(RUSAGE_SELF, &start); dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; fprintf(stderr, "Destroying dictionary\n"); dictionary_destroy(dict); getrusage(RUSAGE_SELF, &end); dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt); return 0; }
int main() { /* * Initialize the dictionary data structure. */ dictionary_t dictionary; dictionary_init(&dictionary); /* * Preform some basic actions */ int result; const char *s; /* _add() */ result = dictionary_add(&dictionary, "key", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key2", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key3", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } //dictionary_print(&dictionary); result = dictionary_add(&dictionary, "key", "value2"); if (result == 0) { printf("_add() was successful, and it should've failed.\n"); } else { printf("_add(): OKAY!\n"); } //dictionary_print(&dictionary); /* _remove() */ dictionary_remove(&dictionary, "key3"); /* _get() */ s = dictionary_get(&dictionary, "non-existant"); if (s != NULL) { printf("_get() was successful, and it should've failed.\n"); } else { printf("_get(): OKAY!\n"); } s = dictionary_get(&dictionary, "key"); if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } /* _parse() */ char *s1 = malloc(100); strcpy(s1, "key3: value"); result = dictionary_parse(&dictionary, s1); if (result != 0) { printf("_parse() failed, and it should have been successful.\n"); } else { printf("_parse(): OKAY!\n"); } //if(dictionary_remove(&dictionary, "key3")!=0) printf("unsuccesfull\n");//added char *s2 = malloc(100); strcpy(s2, "bad key-value"); result = dictionary_parse(&dictionary, s2); if (result == 0) { printf("_parse() was successful, and it should've failed.\n"); } else { printf("_parse(): OKAY!\n"); } /* _get() */ s = dictionary_get(&dictionary, "key3"); if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } /* * Free up the memory used by the dictionary and close the file. */ dictionary_destroy(&dictionary); free(s1); free(s2); return 0; }
int main(int argc, char *argv[]) { char *prgm = *argv; argv++; argc--; dictionary_t dictionary; if(argc < 1) { printf("Usage: %s [options]\n",prgm); printf("Use any combination of the following:\n"); printf(" -i: Initialize the dictionary\n"); printf(" -a [key] [value]: Add a key/value to the dictionary\n"); printf(" -p [string]: Parse a string into the dictionary\n"); printf(" -g [key]: Get a value by its key from the dictionary\n"); printf(" -r [key]: Remove a key from the dictionary\n"); printf(" -d: Destroy the dictionary\n"); } char *key, *value, *string; while(argc > 0) { if(strcmp(*argv,"-i") == 0) { //Initialize printf("Running init()\n"); dictionary_init(&dictionary); printf(" completed\n"); } else if(strcmp(*argv,"-a") == 0) { if(argc < 3) { printf("Not enough parameters to -a\n"); return 2; } key = mstrcpy(argv[1]); value = mstrcpy(argv[2]); printf("Running add(\"%s\",\"%s\")\n",argv[1],argv[2]); d_retint(dictionary_add(&dictionary, key, value)); d_compare("key",argv[1],key); d_compare("value",argv[2],value); argv += 2; argc -= 2; } else if(strcmp(*argv,"-p") == 0) { if(argc < 2) { printf("Not enough parameters to -p\n"); return 2; } string = mstrcpy(argv[1]); printf("Running parse(\"%s\")\n",argv[1]); d_retint(dictionary_parse(&dictionary, string)); d_compare("string",argv[1],string); argv += 1; argc -= 1; } else if(strcmp(*argv,"-g") == 0) { if(argc < 2) { printf("Not enough parameters to -g\n"); return 2; } key = mstrcpy(argv[1]); printf("Running get(\"%s\")\n",argv[1]); d_retstr(dictionary_get(&dictionary, key)); d_compare("key",argv[1],key); argv += 1; argc -= 1; } else if(strcmp(*argv,"-r") == 0) { if(argc < 2) { printf("Not enough parameters to -r\n"); return 2; } key = mstrcpy(argv[1]); printf("Running remove(\"%s\")\n",argv[1]); d_retint(dictionary_remove(&dictionary, key)); d_compare("remove",argv[1],key); argv += 1; argc -= 1; } else if(strcmp(*argv,"-d") == 0) { printf("Running destroy()\n"); dictionary_destroy(&dictionary); printf(" completed\n"); } else { printf("Did not understand parameter: %s\n",*argv); return 3; } argv++; argc--; } }
int main() { /* * Initialize the dictionary data structure. */ dictionary_t dictionary; dictionary_init(&dictionary); /* * Preform some basic actions */ int result; const char *s; /* _add() */ result = dictionary_add(&dictionary, "key", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key2", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key3", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key", "value2"); if (result == 0) { printf("_add() was successful, and it should've failed.\n"); } else { printf("_add(): OKAY!\n"); } /* _remove() */ dictionary_remove(&dictionary, "key3"); /* _get() */ s = dictionary_get(&dictionary, "non-existant"); if (s != NULL) { printf("_get() was successful, and it should've failed.\n"); } else { printf("_get(): OKAY!\n"); } s = dictionary_get(&dictionary, "key"); if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } /* _parse() */ char *s1 = malloc(100); strcpy(s1, "key3: value"); result = dictionary_parse(&dictionary, s1); if (result != 0) { printf("_parse() failed, and it should have been successful.\n"); } else { printf("_parse(): OKAY!\n"); } char *s2 = malloc(100); strcpy(s2, "bad key-value"); result = dictionary_parse(&dictionary, s2); if (result == 0) { printf("_parse() was successful, and it should've failed.\n"); } else { printf("_parse(): OKAY!\n"); } /* _get() */ s = dictionary_get(&dictionary, "key3"); if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } result = dictionary_parse(&dictionary, "hola, :senor"); if(result == 0) printf("Bad!\r\n"); else printf("Success\r\n"); strcpy(s1, "w00t: d00ddd"); result = dictionary_parse(&dictionary, s1); //char * z = "w00t: d00ddd"; s = dictionary_get(&dictionary, "w00t"); if(s == 0 || strcmp(s, "d00ddd") != 0) printf("Bad: %s\r\n", s); else printf("Success\r\n"); char * s3 = (char*)malloc(100); strcpy(s3, " : sdasda"); result = dictionary_parse(&dictionary, s3); s = dictionary_get(&dictionary, ""); if(s != 0) printf("Bad: %s\r\n", s); else printf("Success\r\n"); char* s4 = (char*)malloc(100); strcpy(s1, "ad: (&DAS^\\x"); if(dictionary_parse(&dictionary, s1) != 0) printf("Error\r\n"); s = dictionary_get(&dictionary, "ad"); strcpy(s1, "w00t1: test"); dictionary_parse(&dictionary, s1); strcpy(s4, "w00t1"); s = dictionary_get(&dictionary, s4); if(s == 0 || strcmp(s, "test") != 0) printf("Error3\r\n"); else printf("Success\r\n"); strcpy(s1, "w00t1: test2324334"); if((result = dictionary_parse(&dictionary, s1)) != KEY_EXISTS) printf("Error: %d\r\n", result); else printf("Success\r\n"); /**** BEGIN "Robust" Test Cases ****/ // Test 1 printf("\r\n\r\n=== BEGIN ROBUST ===\r\n\r\n"); char x[] = "Test Me: 57 ad9 X"; result = dictionary_parse(&dictionary, x); if(result != 0) printf("Error: Test 1, %d\r\n", result); else printf("Test 1 Passed!\r\n"); // Test 2 s = dictionary_get(&dictionary, "Test Me"); if(s != 0 && strcmp(s, "57 ad9 X") == 0) printf("Test 2 Passed!\r\n"); else printf("Error: Test2\r\n"); // Test 3 result = dictionary_add(&dictionary, "Test me", "test_value"); s = dictionary_get(&dictionary, "tESt mE"); if(result != KEY_EXISTS || strcmp(s, "57 ad9 X") != 0) printf("Error: Test 3, %d, %s\r\n", result, s); else printf("Test 3 Passed!\r\n"); // Test 4 char y[] = "TeSt Me: d00d another one <3"; result = dictionary_parse(&dictionary, y); if(result != KEY_EXISTS) printf("Error: Test 4\r\n"); else printf("Test 4 Passed!\r\n"); // Test 5 dictionary_remove(&dictionary, "TeSt ME"); s = dictionary_get(&dictionary, "test me"); if(s != 0) printf("Error: Test 5\r\n"); else printf("Test 5 Passed!\r\n"); /* * Free up the memory used by the dictionary and close the file. */ dictionary_destroy(&dictionary); free(s1); free(s2); free(s3); free(s4); return 0; }
int main() { /** * Set the start time */ clock_t start, end; double time_spent; start = clock(); /* * Initialize the dictionary data structure. */ dictionary_t dictionary; dictionary_init(&dictionary); int i=0; char** key_values = (char**)malloc(MAX_ENTRIES*sizeof(char*)); for(i=0; i<MAX_ENTRIES; i++){ key_values[i] = (char*)malloc(50*sizeof(char)); sprintf(key_values[i], "%d: %d", i, i); if(dictionary_parse(&dictionary, key_values[i])!=0) { printf("NOT_OK...THE ITEM SHOULD NOT ALREADY EXIST IN DICTIONARY\n"); return 1; } } for(i=0; i<MAX_ENTRIES; i++){ char tmp[30]; sprintf(tmp, "%d", i); if(strcmp(tmp, dictionary_get(&dictionary, tmp))!=0) { printf("NOT_OK...CANNOT GET EXISTING ITEM IN DICTIONARY\n"); return 1; } } for(i=0; i<MAX_ENTRIES; i++){ if(dictionary_parse(&dictionary, key_values[i])==0) { printf("NOT_OK...THE ITEM SHOULD ALREADY EXIST IN DICTIONARY\n"); return 1; } } for(i=0; i<MAX_ENTRIES; i++){ char tmp[30]; sprintf(tmp, "%d", i); if(dictionary_remove(&dictionary, tmp)!=0){ printf("NOT_OK...THE ITEM TO REMOVE SHOULD IN DICTIONARY\n"); return 1; } } /* * Free up the memory used by the dictionary and close the file. */ dictionary_destroy(&dictionary); for(i=0; i<MAX_ENTRIES; i++) free(key_values[i]); free(key_values); /** * Set the end time */ end = clock(); time_spent = (double)(end - start) / CLOCKS_PER_SEC; printf("The running time for your dictionary is: %f seconds\n", time_spent); return 0; }
int main() { /* * Initialize the dictionary data structure. */ dictionary_t dictionary; dictionary_init(&dictionary); /* * Preform some basic actions */ int result; const char *s; /* _add() */ result = dictionary_add(&dictionary, "key", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key2", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key3", "value"); if (result != 0) { printf("_add() failed, and it should have been successful.\n"); } else { printf("_add(): OKAY!\n"); } result = dictionary_add(&dictionary, "key", "value2"); if (result == 0) { printf("_add() was successful, and it should've failed.\n"); } else { printf("_add(): OKAY!\n"); } /* _remove() */ dictionary_remove(&dictionary, "key3"); /* _get() */ s = dictionary_get(&dictionary, "non-existant"); if (s != NULL) { printf("_get() was successful, and it should've failed.\n"); } else { printf("_get(): OKAY!\n"); } s = dictionary_get(&dictionary, "key"); if (s == NULL){printf("NULL");} if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } /* _parse() */ char *s1 = malloc(100); strcpy(s1, "key3: value"); result = dictionary_parse(&dictionary, s1); if (result != 0) { printf("_parse() failed, and it should have been successful.\n"); } else { printf("_parse(): OKAY!\n"); } char *s2 = malloc(100); strcpy(s2, "a:: b"); result = dictionary_parse(&dictionary, s2); if (result == 0) { printf("_parse() was successful, and it should've failed.\n"); } else { printf("_parse(): OKAY!\n"); } /*OWN TEST * - ": www.cs.uiuc.edu", since the KEY is zero-length * - "MyKey, MyValue", since no colon, space exists * - "a:b", since no colon, space exists. * - "a:: b", since the first colon isn't followed by a space. * - ": key: value" * - " : " * - " : " */ char *s3 = malloc(100); strcpy(s3, " : "); result = dictionary_parse(&dictionary, s3); printf("The result is %d\n",result); /* _get() */ s = dictionary_get(&dictionary, "key3"); /* if (s != NULL){printf("ITS NOT NULL");} if (s = NULL){printf("ITS NULL");} if (strcmp(s,"value") == 0){printf("Its 0");} if (strcmp(s,"value") != 0){printf("Its NOT 0");} */ if (s == NULL || strcmp(s, "value") != 0) { printf("_get() failed or was not the expected result.\n"); } else { printf("_get(): OKAY!\n"); } //printf("Im Here\n"); /* * Free up the memory used by the dictionary and close the file. */ dictionary_destroy(&dictionary); free(s1); free(s2); return 0; }
/********************************************************************************************* F_SECUNDARIA: Creando un BSON del archivo, para insertalo en la DB TESTEADO =) ****************************/ bson_t * crearDocumentoParaLaDB(char * nombreDelArchivo, long tamanioDelArchivo, int dirPadreDelArchivo, t_dictionary * diccionarioDeBloques) //Va eliminando el diccionario que ingresa por parametro :D { bson_t child; bson_t child2; bson_t child3; bson_t child4; bson_oid_t oid; int cantidadTotalDeBloques; int identificadorDelBloque = 0; t_dictionary* copiasDelBloqueX; t_copiaX* copiaX; char * iDelBloque; //FIN DECLARACION DE VARIABLES bson_t * documento = bson_new(); // ::: Estructura BASICA del documento ::: bson_oid_init (&oid, NULL); BSON_APPEND_OID (documento, "_id", &oid); BSON_APPEND_UTF8 (documento, "nombreDelArchivo", nombreDelArchivo); BSON_APPEND_INT32 (documento, "tamanio", tamanioDelArchivo); BSON_APPEND_INT32 (documento, "dirPadre", dirPadreDelArchivo); BSON_APPEND_INT32 (documento, "estado", 1); cantidadTotalDeBloques = dictionary_size(diccionarioDeBloques); BSON_APPEND_INT32 (documento, "cantidadDeBloques", cantidadTotalDeBloques); // ::: Estructura de BLOQUES del documento ::: //Guardando los N bloques con copias 3 bson_append_document_begin (documento, "Bloques", 7, &child); while(identificadorDelBloque < cantidadTotalDeBloques) { iDelBloque = string_itoa(identificadorDelBloque); bson_append_document_begin (&child, iDelBloque, 1, &child2); copiasDelBloqueX = (t_dictionary*) dictionary_get( diccionarioDeBloques, iDelBloque ); //Guardando la cantidad de copias del bloque X BSON_APPEND_INT32 (&child2, "cantidadDeCopias", 3); //Guardando las 3 copias para el bloque X bson_append_array_begin (&child2, "copias", 6, &child4); copiaX = (t_copiaX*) dictionary_get( copiasDelBloqueX, "1" ); int Guachin= dictionary_size(copiasDelBloqueX); bson_append_document_begin (&child4, "0", 1, &child3); bson_append_int32(&child3,"IP", 2, (*copiaX).IP); bson_append_int32(&child3, "puerto", 6, (*copiaX).puerto); bson_append_int32(&child3, "numBloque", 9, (*copiaX).numBloque); bson_append_document_end (&child4, &child3); copiaX = (t_copiaX*) dictionary_get( copiasDelBloqueX, "2" ); bson_append_document_begin (&child4, "1", 1, &child3); bson_append_int32(&child3,"IP", 2, (*copiaX).IP); bson_append_int32(&child3, "puerto", 6, (*copiaX).puerto); bson_append_int32(&child3, "numBloque", 9, (*copiaX).numBloque); bson_append_document_end (&child4, &child3); copiaX = (t_copiaX*) dictionary_get( copiasDelBloqueX, "3" ); bson_append_document_begin (&child4, "2", 1, &child3); bson_append_int32(&child3,"IP", 2, (*copiaX).IP); bson_append_int32(&child3, "puerto", 6, (*copiaX).puerto); bson_append_int32(&child3, "numBloque", 9, (*copiaX).numBloque); bson_append_document_end (&child4, &child3); bson_append_array_end (&child2, &child4); dictionary_clean(copiasDelBloqueX); dictionary_destroy(copiasDelBloqueX); //Anido las 3 copias al bloque X bson_append_document_end (&child, &child2); free(iDelBloque); identificadorDelBloque++; }; //::Estructura BASICA + estructura BLOQUES:: bson_append_document_end (documento, &child); //Elimino el diccionarioDeBloques dictionary_clean(diccionarioDeBloques); dictionary_destroy(diccionarioDeBloques); return documento; }