feat(lsm): started string implementation

This commit is contained in:
Jef Roosens 2023-10-13 12:45:48 +02:00
parent 96fc645034
commit c327be80e9
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
11 changed files with 256 additions and 90 deletions

View file

@ -12,55 +12,52 @@ typedef enum lsm_error {
lsm_error_already_present = 3
} lsm_error;
/**
* Represents a string (or really any kind of data) with a known length. Data
* with length 8 or less is stored inside the pointer, and does not allocate
* additional memory.
*/
typedef struct lsm_string {
uint64_t len;
union {
void *ptr;
char val[8];
} str;
} lsm_string;
/*typedef struct lsm_string { */
/* uint64_t len; */
/* union { */
/* void *ptr; */
/* char val[8]; */
/* } str; */
/*} lsm_string; */
/**
* The type of an attribute. Each type is represented as a single bit of a
* 32-bit integer, so they can be easily combined into a bitmap.
*/
typedef enum lsm_attr_type { lsm_attr_type_entry_type = 1 << 0 } lsm_attr_type;
/*/1** */
/* * The type of an attribute. Each type is represented as a single bit of a */
/* * 32-bit integer, so they can be easily combined into a bitmap. */
/* *1/ */
/*typedef enum lsm_attr_type { lsm_attr_type_entry_type = 1 << 0 }
* lsm_attr_type; */
/**
* A single attribute associated with an entry
*/
typedef struct lsm_attr {
lsm_attr_type type;
lsm_string str;
} lsm_attr;
/*/1** */
/* * A single attribute associated with an entry */
/* *1/ */
/*typedef struct lsm_attr { */
/* lsm_attr_type type; */
/* lsm_string str; */
/*} lsm_attr; */
/**
* Represents a collection of attributes for an entry. A collection can only
* contain one of each attribute.
/*/1** */
/* * Represents a collection of attributes for an entry. A collection can only
*/
typedef struct lsm_attr_list {
uint64_t count;
lsm_attr *items;
uint32_t bitmap;
} lsm_attr_list;
/* * contain one of each attribute. */
/* *1/ */
/*typedef struct lsm_attr_list { */
/* uint64_t count; */
/* lsm_attr *items; */
/* uint32_t bitmap; */
/*} lsm_attr_list; */
/**
* An entry inside an LSM store
*/
typedef struct lsm_entry {
lsm_string key;
lsm_attr_list attrs;
lsm_string data;
} lsm_entry;
/*/1** */
/* * An entry inside an LSM store */
/* *1/ */
/*typedef struct lsm_entry { */
/* lsm_string key; */
/* lsm_attr_list attrs; */
/* lsm_string data; */
/*} lsm_entry; */
/**
* A store of entries, which manages its data both in-memory and on disk.
*/
typedef struct lsm_store lsm_store;
/*/1** */
/* * A store of entries, which manages its data both in-memory and on disk. */
/* *1/ */
/*typedef struct lsm_store lsm_store; */
#endif

51
lsm/include/lsm/bt.h Normal file
View file

@ -0,0 +1,51 @@
#ifndef LSM_BT
#define LSM_BT
#include "lsm.h"
/**
* A binary tree implementation using char values as keys
*/
typedef struct lsm_bt lsm_bt;
/**
* Initialize a new binary tree
*
* @param ptr where to store newly allocated pointer
*/
lsm_error lsm_bt_init(lsm_bt **ptr);
/**
* Deallocate an entire binary tree, including all its nodes
*/
void lsm_bt_free(lsm_bt *bt);
/**
* Search for the data stored behind the given key.
*
* @param out pointer to store data pointer in
* @param bt binary tree to search
* @param key key to search
*/
lsm_error lsm_bt_search(void **out, lsm_bt *bt, char key);
/**
* Insert a new data value into the tree with the given key.
*
* @param bt binary tree to insert into
* @param key key to insert
* @param data data to store
*/
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data);
/**
* Remove the given key from the binary tree. Ownership of the data pointer is
* returned to the caller.
*
* @param out address to write data pointer to
* @param bt binary tree to remove from
* @param key key to remove
*/
lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key);
#endif

60
lsm/include/lsm/str.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef LSM_STR
#define LSM_STR
#include "lsm.h"
/**
* Represents a string (or really any kind of data) with a known length. Data
* with length 8 or less is stored inside the pointer, and does not allocate
* additional memory.
*/
typedef struct lsm_str lsm_str;
/**
* Allocate a new string struct of length 0.
*
* @param ptr pointer to store newly allocated pointer in
*/
lsm_error lsm_str_init_zero(lsm_str **ptr);
/**
* Update an existing lsm_str so it now represents the new provided string. The
* string pointer of the original object is free'd if needed.
*
* @param str lsm_str object to modify
* @param s string to convert into lsm string; ownership is taken over
*/
void lsm_str_init_prealloc(lsm_str *str, char *s);
/**
* Allocate and initialize a new lsm_str object
*
* @param ptr pointer to store newly allocated pointer
* @param s string to convert into lsm string; ownership is taken over
*/
lsm_error lsm_str_init(lsm_str **ptr, char *s);
/**
* Deallocate the existing internal string if needed and replace the lsm_str
* with a string of length 0, wiping its contents.
*
* @param str string to wipe
*/
void lsm_str_zero(lsm_str *str);
/**
* Deallocate the string and its internal char buffer if needed. Only call this
* on heap-allocated strings.
*
* @param str string to dealloate
*/
void lsm_str_free(lsm_str *str);
/**
* Return the length of the string.
*
* @param str string to return length for.
*/
uint64_t lsm_str_len(lsm_str *str);
#endif

25
lsm/include/lsm/trie.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef LSM_TRIE
#define LSM_TRIE
#include "lsm.h"
/**
* A struct representing a trie
*/
typedef struct lsm_trie lsm_trie;
/**
* Initialize a new trie.
*
* @param ptr where to store the newly allocated pointer
*/
lsm_error lsm_trie_init(lsm_trie **ptr);
/**
* Deallocate an entire trie, including all its nodes
*
* @param trie trie to free
*/
void lsm_trie_free(lsm_trie *trie);
#endif