refactor: start using void* with trie

This commit is contained in:
Jef Roosens 2022-12-07 23:20:39 +01:00
parent 01eb5ece55
commit c99bc83015
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 83 additions and 57 deletions

View file

@ -10,12 +10,12 @@
typedef struct ttrie {
TrieNode *root;
size_t size;
uint64_t size;
char *file_path;
pthread_rwlock_t lock;
} Trie;
TrieExitCode trie_add_no_lock(Trie *trie, const char *key, Entry *entry);
TrieExitCode trie_add_no_lock(Trie *trie, const char *key, void *data);
/**
* Allocate and initialize an empty Trie
@ -138,7 +138,7 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
// At this point, we've either arrived at an empty child, or traversed through
// the entire string. Therefore, all we have to do is check whether we're at
// the end of the string and if node represents a string.
if (key[i] == DELIMITER && (*child_ptr)->represents) {
if (key[i] == DELIMITER && (*child_ptr)->data_size > 0) {
out.parent = *node_ptr;
out.child = *child_ptr;
}
@ -153,14 +153,14 @@ 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
*/
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) {
TrieExitCode trie_search(Trie *trie, void **data_ptr, const char *key) {
SearchResult res = trie_search_node(trie, key);
if (res.child == NULL) {
return NotFound;
}
*entry_ptr = res.child->entry;
*data_ptr = res.child->data;
return Ok;
}
@ -173,7 +173,7 @@ TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) {
* @return true if the string wasn't present in the trie and thus added, false
* otherwise
*/
TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
TrieExitCode trie_add_no_lock(Trie *trie, const char *string, void *data) {
size_t i = 0;
uint8_t offset;
TrieNode **node_ptr = &(trie->root);
@ -211,8 +211,8 @@ TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
continue;
}
child_node->represents = true;
child_node->entry = entry;
child_node->data_size = sizeof(data);
child_node->data = data;
trie->size++;
return Ok;
@ -265,17 +265,17 @@ TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
i += offset;
} while (string[i] != DELIMITER);
if ((*child_node_ptr)->represents) {
if ((*child_node_ptr)->data_size > 0) {
return AlreadyPresent;
}
(*child_node_ptr)->represents = true;
(*child_node_ptr)->entry = entry;
(*child_node_ptr)->data_size = sizeof(data);
(*child_node_ptr)->data = data;
trie->size++;
return Ok;
}
TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) {
TrieExitCode trie_add(Trie *trie, const char *key, void *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
@ -290,12 +290,12 @@ TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) {
return FileError;
}
fputs(key, fp);
fputs(" ", fp);
fputc(entry_type_to_char(entry->type), fp);
fputs(" ", fp);
fputs(entry->string, fp);
fputs("\n", fp);
/* fputs(key, fp); */
/* fputs(" ", fp); */
/* fputc(entry_type_to_char(entry->type), fp); */
/* fputs(" ", fp); */
/* fputs(entry->string, fp); */
/* fputs("\n", fp); */
fclose(fp);
}
@ -305,7 +305,7 @@ TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) {
return trie_add_no_lock(trie, key, entry);
}
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, void *data,
bool secure) {
// Generate random key
bool ok = false;
@ -324,7 +324,7 @@ TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
ok = trie_search_node(trie, key).child == NULL;
}
TrieExitCode return_value = trie_add(trie, key, entry);
TrieExitCode return_value = trie_add(trie, key, data);
if (return_value == Ok) {
*key_ptr = key;
@ -402,7 +402,7 @@ TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
* @param trie trie to return size for
* @return size of the trie
*/
size_t trie_size(Trie *trie) { return trie->size; }
uint64_t trie_size(Trie *trie) { return trie->size; }
int trie_rlock(Trie *trie) { return pthread_rwlock_rdlock(&trie->lock); }

View file

@ -35,3 +35,11 @@ Entry *entry_new(EntryType type, const char *string) {
return entry;
}
void entry_free(Entry *entry) {
if (entry->string != NULL) {
free(entry->string);
}
free(entry);
}

View file

@ -27,7 +27,8 @@ typedef struct tinode {
* and its string pointer is initialized.
*/
typedef struct tnode {
Entry *entry;
void *data;
uint64_t data_size;
TrieInnerNode *tree;
uint8_t tree_size;
@ -36,8 +37,6 @@ typedef struct tnode {
// nodes
char string[TRIE_MAX_SKIP_SIZE];
uint8_t string_len;
bool represents;
} TrieNode;
// Required for recursively freeing tree structure
@ -67,7 +66,7 @@ TrieNode *tnode_init() {
node->tree_size = 0;
node->string_len = 0;
node->represents = false;
node->data_size = 0;
return node;
}
@ -105,6 +104,10 @@ void tnode_free(TrieNode *node) {
tinode_free_cascade(node->tree);
}
if (node->data_size > 0) {
free(node->data);
}
// TODO properly free entry
/* if (node->payload != NULL) { */
/* free(node->payload); */