refactor: started refactoring trie api (contains segfaults)
This commit is contained in:
parent
088322c18f
commit
50ebf86589
4 changed files with 109 additions and 115 deletions
122
trie/src/trie.c
122
trie/src/trie.c
|
|
@ -15,39 +15,25 @@ typedef struct ttrie {
|
|||
pthread_rwlock_t lock;
|
||||
} Trie;
|
||||
|
||||
TrieExitCode trie_add_no_lock(Trie *trie, const char *key, Entry *entry);
|
||||
|
||||
/**
|
||||
* Allocate and initialize an empty Trie
|
||||
*
|
||||
* @return pointer to the empty Trie
|
||||
*/
|
||||
Trie *trie_init() {
|
||||
TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) {
|
||||
// Allocate & initialize trie
|
||||
Trie *trie = calloc(1, sizeof(Trie));
|
||||
trie->root = tnode_init();
|
||||
trie->file_path = strdup(file_path);
|
||||
pthread_rwlock_init(&trie->lock, NULL);
|
||||
|
||||
return trie;
|
||||
}
|
||||
|
||||
/**
|
||||
* De-allocate a TernaryTree by freeing its entire underlying structure.
|
||||
*
|
||||
* @param trie trie to free
|
||||
*/
|
||||
void trie_free(Trie *trie) {
|
||||
tnode_free(trie->root);
|
||||
free(trie);
|
||||
}
|
||||
|
||||
bool trie_add_no_lock(Trie *trie, const char *key, Entry *entry);
|
||||
|
||||
int trie_populate(Trie *trie, const char *file_path) {
|
||||
trie->file_path = strdup(file_path);
|
||||
|
||||
// Populate trie with data from file
|
||||
FILE *fp = fopen(file_path, "r");
|
||||
|
||||
// TODO properly handle this
|
||||
if (fp == NULL) {
|
||||
return -1;
|
||||
return FileError;
|
||||
}
|
||||
|
||||
// We read in lines of at most 8192 characters (sounds like enough)
|
||||
|
|
@ -89,7 +75,19 @@ int trie_populate(Trie *trie, const char *file_path) {
|
|||
|
||||
fclose(fp);
|
||||
|
||||
return entries;
|
||||
*trie_ptr = trie;
|
||||
|
||||
return Ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* De-allocate a TernaryTree by freeing its entire underlying structure.
|
||||
*
|
||||
* @param trie trie to free
|
||||
*/
|
||||
void trie_free(Trie *trie) {
|
||||
tnode_free(trie->root);
|
||||
free(trie);
|
||||
}
|
||||
|
||||
typedef struct searchresult {
|
||||
|
|
@ -145,16 +143,16 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
|
|||
* @param string string to look up
|
||||
* @return true if the string is present in the trie, false otherwise
|
||||
*/
|
||||
Entry *trie_search(Trie *trie, const char *key) {
|
||||
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) {
|
||||
SearchResult res = trie_search_node(trie, key);
|
||||
|
||||
Entry *return_value = NULL;
|
||||
|
||||
if (res.child != NULL) {
|
||||
return_value = res.child->entry;
|
||||
if (res.child == NULL) {
|
||||
return NotFound;
|
||||
}
|
||||
|
||||
return return_value;
|
||||
*entry_ptr = res.child->entry;
|
||||
|
||||
return Ok;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -165,7 +163,7 @@ Entry *trie_search(Trie *trie, const char *key) {
|
|||
* @return true if the string wasn't present in the trie and thus added, false
|
||||
* otherwise
|
||||
*/
|
||||
bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
|
||||
TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
|
||||
size_t i = 0;
|
||||
uint8_t offset;
|
||||
TrieNode **node_ptr = &(trie->root);
|
||||
|
|
@ -207,7 +205,7 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
|
|||
child_node->entry = entry;
|
||||
|
||||
trie->size++;
|
||||
return true;
|
||||
return Ok;
|
||||
}
|
||||
|
||||
while (offset < (*child_node_ptr)->string_len) {
|
||||
|
|
@ -258,43 +256,44 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
|
|||
} while (string[i] != DELIMITER);
|
||||
|
||||
if ((*child_node_ptr)->represents) {
|
||||
return false;
|
||||
return AlreadyPresent;
|
||||
}
|
||||
|
||||
(*child_node_ptr)->represents = true;
|
||||
trie->size++;
|
||||
return true;
|
||||
return Ok;
|
||||
}
|
||||
|
||||
bool trie_add(Trie *trie, const char *key, Entry *entry) {
|
||||
if (trie->file_path != NULL) {
|
||||
// Easiest way to make sure we don't add duplicate entries
|
||||
// We use an internal function that doesn't require a read lock, as we're
|
||||
// already inside a write lock
|
||||
if (trie_search_node(trie, key).child != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(trie->file_path, "a");
|
||||
|
||||
if (fp == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fputs(key, fp);
|
||||
fputs(" ", fp);
|
||||
fputc(entry_type_to_char(entry->type), fp);
|
||||
fputs(" ", fp);
|
||||
fputs(entry->string, fp);
|
||||
fputs("\n", fp);
|
||||
TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) {
|
||||
// Easiest way to make sure we don't add duplicate entries
|
||||
// We use an internal function that doesn't require a read lock, as we're
|
||||
// already inside a write lock
|
||||
if (trie_search_node(trie, key).child != NULL) {
|
||||
return AlreadyPresent;
|
||||
}
|
||||
|
||||
// This function *should* always return true. Otherwise, the function would've
|
||||
FILE *fp = fopen(trie->file_path, "a");
|
||||
|
||||
if (fp == NULL) {
|
||||
return FileError;
|
||||
}
|
||||
|
||||
fputs(key, fp);
|
||||
fputs(" ", fp);
|
||||
fputc(entry_type_to_char(entry->type), fp);
|
||||
fputs(" ", fp);
|
||||
fputs(entry->string, fp);
|
||||
fputs("\n", fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
// This function *should* always return Ok. Otherwise, the function would've
|
||||
// exited because the string was found in the trie.
|
||||
return trie_add_no_lock(trie, key, entry);
|
||||
}
|
||||
|
||||
char *trie_add_random(Trie *trie, Entry *entry, bool secure) {
|
||||
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
|
||||
bool secure) {
|
||||
// Generate random key
|
||||
bool ok = false;
|
||||
int key_length = secure ? RANDOM_KEY_LENGTH_LONG : RANDOM_KEY_LENGTH_SHORT;
|
||||
|
|
@ -312,13 +311,11 @@ char *trie_add_random(Trie *trie, Entry *entry, bool secure) {
|
|||
ok = trie_search_node(trie, key).child == NULL;
|
||||
}
|
||||
|
||||
bool res = trie_add(trie, key, entry);
|
||||
char *return_value;
|
||||
TrieExitCode return_value = trie_add(trie, key, entry);
|
||||
|
||||
if (res) {
|
||||
return_value = key;
|
||||
if (return_value == Ok) {
|
||||
*key_ptr = key;
|
||||
} else {
|
||||
return_value = NULL;
|
||||
free(key);
|
||||
}
|
||||
|
||||
|
|
@ -398,7 +395,4 @@ int trie_rlock(Trie *trie) { return pthread_rwlock_rdlock(&trie->lock); }
|
|||
|
||||
int trie_wlock(Trie *trie) { return pthread_rwlock_wrlock(&trie->lock); }
|
||||
|
||||
int trie_unlock(Trie *trie) {
|
||||
printf("sup\n");
|
||||
return pthread_rwlock_unlock(&trie->lock);
|
||||
}
|
||||
int trie_unlock(Trie *trie) { return pthread_rwlock_unlock(&trie->lock); }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue