feat(lsm): possibly implemented trie insert

This commit is contained in:
Jef Roosens 2023-10-13 21:10:31 +02:00
parent 0548efda97
commit 622d644f25
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 340 additions and 3 deletions

View file

@ -8,7 +8,7 @@
struct lsm_str {
uint64_t len;
union {
void *ptr;
char *ptr;
char val[8];
} data;
};

View file

@ -5,10 +5,32 @@
#include "lsm/str_internal.h"
#include "lsm/trie.h"
/**
* A node inside a trie structure
*/
typedef struct lsm_trie_node {
lsm_bt bt;
lsm_str skip;
char c;
void *data;
} lsm_trie_node;
/**
* Allocate and initialize a new trie node
*
* @param ptr pointer to store new node pointer
*/
lsm_error lsm_trie_node_init(lsm_trie_node **ptr);
/**
* Deallocate a trie node
*
* @param node node to deallocate
*/
void lsm_trie_node_free(lsm_trie_node *node);
struct lsm_trie {
lsm_trie_node *root;
uint64_t size;
};
#endif

View file

@ -130,3 +130,20 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key) {
return lsm_error_ok;
}
lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data) {
lsm_bt_node *node = bt->root;
while ((node != NULL) && (node->key != key)) {
node = key < node->key ? node->left : node->right;
}
if (node == NULL) {
return lsm_error_not_found;
}
*out = node->data;
node->data = data;
return lsm_error_ok;
}

View file

@ -5,6 +5,8 @@
#include "lsm.h"
#include "lsm/str_internal.h"
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
lsm_error lsm_str_init_zero(lsm_str **ptr) {
lsm_str *str = calloc(1, sizeof(lsm_str));
@ -59,3 +61,100 @@ void lsm_str_free(lsm_str *str) {
}
uint64_t lsm_str_len(lsm_str *str) { return str->len; }
const char *lsm_str_ptr(lsm_str *str) {
if (str->len <= 8) {
return str->data.val;
} else {
return str->data.ptr;
}
}
char lsm_str_char(lsm_str *str, uint64_t index) {
if (str->len <= 8) {
return str->data.val[index];
} else {
return str->data.ptr[index];
}
}
lsm_error lsm_str_substr(lsm_str *out, lsm_str *str, uint64_t start,
uint64_t end) {
// A substring that starts past the string's length will have length 0
uint64_t len = start < str->len ? end - start : 0;
const char *str_ptr = lsm_str_ptr(str);
if (len <= 8) {
lsm_str_zero(out);
memcpy(out->data.val, &str_ptr[start], len);
} else {
char *buf = malloc(len * sizeof(char));
if (buf == NULL) {
return lsm_error_failed_alloc;
}
memcpy(buf, &str_ptr[start], len);
lsm_str_zero(out);
out->data.ptr = buf;
}
out->len = len;
return lsm_error_ok;
}
uint64_t lsm_str_cmp(lsm_str *s1, uint64_t s1_offset, lsm_str *s2,
uint64_t s2_offset) {
uint64_t index = 0;
uint64_t max_len = MIN(s1->len - s1_offset, s2->len - s2_offset);
while ((index < max_len) && (lsm_str_char(s1, s1_offset + index) ==
lsm_str_char(s2, s2_offset + index))) {
index++;
}
return index;
}
lsm_error lsm_str_truncate(lsm_str *s, uint64_t new_len) {
if (new_len >= s->len) {
return lsm_error_ok;
}
if (new_len <= 8) {
char *s_buf = s->data.ptr;
memcpy(s->data.val, lsm_str_ptr(s), new_len);
if (s->len > 8) {
free(s_buf);
}
} else {
char *buf = malloc(new_len * sizeof(char));
if (buf == NULL) {
return lsm_error_failed_alloc;
}
memcpy(buf, s->data.ptr, new_len);
free(s->data.ptr);
s->data.ptr = buf;
}
s->len = new_len;
return lsm_error_ok;
}
lsm_error lsm_str_split(lsm_str *s, lsm_str *s2, uint64_t index) {
lsm_error res = lsm_str_substr(s2, s, index, s->len);
if (res != lsm_error_ok) {
return res;
}
return lsm_str_truncate(s, index);
}

View file

@ -1 +1,128 @@
#include <stdlib.h>
#include "lsm.h"
#include "lsm/trie_internal.h"
lsm_error lsm_trie_node_init(lsm_trie_node **ptr) {
lsm_trie_node *node = calloc(1, sizeof(lsm_trie_node));
if (node == NULL) {
return lsm_error_failed_alloc;
}
*ptr = node;
return lsm_error_ok;
}
lsm_error lsm_trie_init(lsm_trie **ptr) {
lsm_trie *trie = calloc(1, sizeof(lsm_trie));
if (trie == NULL) {
return lsm_error_failed_alloc;
}
lsm_trie_node *root;
lsm_error res = lsm_trie_node_init(&root);
if (res != lsm_error_ok) {
return res;
}
trie->root = root;
*ptr = trie;
return lsm_error_ok;
}
lsm_error lsm_trie_insert(lsm_trie *trie, lsm_str *key, void *data) {
// NULL is not allowed as a data value, as it's used to indicate a lack of
// data
if (data == NULL) {
return lsm_error_null_value;
}
uint64_t key_len = lsm_str_len(key);
// Empty string is represented by the root
if (key_len == 0) {
if (trie->root->data == NULL) {
trie->root->data = data;
return lsm_error_ok;
} else {
return lsm_error_already_present;
}
}
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);
// No child is present yet for this character, so we can insert the string
// here
if (res == lsm_error_not_found) {
lsm_trie_node *new_node;
res = lsm_trie_node_init(&new_node);
if (res != lsm_error_ok) {
return res;
}
new_node->data = data;
lsm_str_substr(&new_node->skip, key, index + 1, key_len);
return lsm_bt_insert(&node->bt, c, new_node);
}
index++;
// We compare the remaining part of the key with the node's skip. If cmp is
// less than the length of the skip, we know they differ and the edge should
// be split.
uint64_t cmp = lsm_str_cmp(key, index, &next_node->skip, 0);
if (cmp < lsm_str_len(&next_node->skip)) {
lsm_trie_node *split_node;
res = lsm_trie_node_init(&split_node);
if (res != lsm_error_ok) {
return res;
}
// split_node replaces the original node as the new child node
lsm_trie_node *bottom_node;
lsm_bt_replace((void **)&bottom_node, &node->bt, c, split_node);
// The old child node now becomes the child of split_node
lsm_bt_insert(&split_node->bt, lsm_str_char(key, index + cmp),
bottom_node);
// The new node splits the edge into two parts, so the new node will have
// the remaining part of the skip (minus the one character) as its skip
lsm_str_substr(&split_node->skip, &next_node->skip, cmp + 1,
lsm_str_len(&next_node->skip));
// The old node keeps the first part of the skip
lsm_str_truncate(&next_node->skip, cmp);
next_node = split_node;
}
node = next_node;
index += cmp;
}
if (node->data != NULL) {
return lsm_error_already_present;
}
node->data = data;
return lsm_error_ok;
}