feat(lsm): start of library

This commit is contained in:
Jef Roosens 2023-10-12 10:06:20 +02:00 committed by Jef Roosens
parent 98a1f5f671
commit fd42b446a6
8 changed files with 314 additions and 1 deletions

22
lsm/src/lsm_store.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdlib.h>
#include "lsm.h"
#include "lsm_store.h"
/**
* Initialize a new lsm_store struct.
*
* @param lsm_store pointer to where to store the newly allocated object's pointer
* @return success of the function
*/
lsm_error lsm_store_init(lsm_store **ptr) {
lsm_store *store = calloc(1, sizeof(lsm_store));
if (store == NULL) {
return lsm_error_failed_alloc;
}
*ptr = store;
return lsm_error_ok;
}

12
lsm/src/lsm_store.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef LSM_STORE_INTERNAL
#define LSM_STORE_INTERNAL
#include "lsm.h"
#include "lsm_store_node.h"
struct lsm_store {
lsm_store_node *root;
uint64_t size;
};
#endif

62
lsm/src/lsm_store_node.c Normal file
View file

@ -0,0 +1,62 @@
#include <stdlib.h>
#include "lsm_store_node.h"
#include "lsm.h"
lsm_error lsm_store_inode_init(lsm_store_inode **ptr, const char c) {
lsm_store_inode *node = calloc(1, sizeof(lsm_store_inode));
if (node == NULL) {
return lsm_error_failed_alloc;
}
node->key = c;
*ptr = node;
return lsm_error_ok;
}
lsm_error lsm_store_node_init(lsm_store_node **ptr) {
lsm_store_node *node = calloc(1, sizeof(lsm_store_node));
if (node == NULL) {
return lsm_error_failed_alloc;
}
*ptr = node;
return lsm_error_ok;
}
lsm_error lsm_store_node_search(lsm_store_node **out_ptr, lsm_store_node *node, const char c) {
if (node->size == 0) {
return lsm_error_not_found;
}
lsm_store_inode *parent = node->root;
lsm_store_inode *child;
lsm_store_node *out = NULL;
while (1) {
if (parent->key == c) {
out = parent->next;
break;
}
child = (c < parent->key) ? parent->left : parent->right;
if (child == NULL) {
break;
}
parent = child;
};
if (out == NULL) {
return lsm_error_not_found;
}
*out_ptr = out;
return lsm_error_ok;
}

42
lsm/src/lsm_store_node.h Normal file
View file

@ -0,0 +1,42 @@
#ifndef LSM_STORE_NODE_INTERNAL
#define LSM_STORE_NODE_INTERNAL
#include "lsm.h"
/**
* A node inside a store node's internal binary tree.
*/
typedef struct lsm_store_inode {
struct lsm_store_inode *left;
struct lsm_store_inode *right;
struct lsm_store_node *next;
char key;
} lsm_store_inode;
/**
* Initialize a new lsm_store_inode.
*/
lsm_error lsm_store_inode_init(lsm_store_inode **ptr, const char c);
/**
* A node inside the store's trie structure. Internally, each node manages a
* binary tree.
*/
typedef struct lsm_store_node {
lsm_entry *entry;
lsm_store_inode *root;
uint8_t size;
lsm_string skip;
} lsm_store_node;
/**
* Initialize a new lsm_store_node.
*/
lsm_error lsm_store_node_init(lsm_store_node **out);
/**
* Search for the next node following the given character, if present.
*/
lsm_error lsm_store_node_search(lsm_store_node **out, lsm_store_node *node, const char c);
#endif