feat(lsm): added trie search
This commit is contained in:
parent
622d644f25
commit
87000e8f73
5 changed files with 124 additions and 6 deletions
|
|
@ -44,6 +44,33 @@ lsm_error lsm_str_init(lsm_str **ptr, char *s) {
|
|||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_str_init_copy(lsm_str **ptr, char *s) {
|
||||
lsm_str *str = calloc(1, sizeof(lsm_str));
|
||||
|
||||
if (str == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
str->len = strlen(s);
|
||||
|
||||
if (str->len <= 8) {
|
||||
memcpy(str->data.val, s, str->len);
|
||||
} else {
|
||||
char *buf = malloc(str->len * sizeof(char));
|
||||
|
||||
if (buf == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
memcpy(buf, s, str->len);
|
||||
str->data.ptr = buf;
|
||||
}
|
||||
|
||||
*ptr = str;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
void lsm_str_zero(lsm_str *str) {
|
||||
if (str->len > 8) {
|
||||
free(str->data.ptr);
|
||||
|
|
|
|||
|
|
@ -118,6 +118,8 @@ lsm_error lsm_trie_insert(lsm_trie *trie, lsm_str *key, void *data) {
|
|||
index += cmp;
|
||||
}
|
||||
|
||||
// This catches the edge case where the exact node for the string is already
|
||||
// present in the trie
|
||||
if (node->data != NULL) {
|
||||
return lsm_error_already_present;
|
||||
}
|
||||
|
|
@ -126,3 +128,51 @@ lsm_error lsm_trie_insert(lsm_trie *trie, lsm_str *key, void *data) {
|
|||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_trie_search(void **data, lsm_trie *trie, lsm_str *key) {
|
||||
uint64_t key_len = lsm_str_len(key);
|
||||
|
||||
if (key_len == 0) {
|
||||
if (trie->root->data != NULL) {
|
||||
*data = trie->root->data;
|
||||
|
||||
return lsm_error_ok;
|
||||
} else {
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t index = 0;
|
||||
lsm_trie_node *node = trie->root;
|
||||
lsm_trie_node *next_node;
|
||||
lsm_error res;
|
||||
|
||||
while (index < key_len) {
|
||||
char c = lsm_str_char(key, index);
|
||||
res = lsm_bt_search((void **)&next_node, &node->bt, c);
|
||||
|
||||
if (res != lsm_error_ok) {
|
||||
return res;
|
||||
}
|
||||
|
||||
index++;
|
||||
|
||||
uint64_t cmp = lsm_str_cmp(key, index, &next_node->skip, 0);
|
||||
|
||||
// If we end in the middle of an edge, we definitely haven't found the node
|
||||
if (cmp != lsm_str_len(&next_node->skip)) {
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
|
||||
node = next_node;
|
||||
index += cmp;
|
||||
}
|
||||
|
||||
if (node->data == NULL) {
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
|
||||
*data = node->data;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue