diff --git a/lsm/include/lsm.h b/lsm/include/lsm.h index 2430091..aa76826 100644 --- a/lsm/include/lsm.h +++ b/lsm/include/lsm.h @@ -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 diff --git a/lsm/src/bt/lsm_bt.h b/lsm/include/lsm/bt.h similarity index 63% rename from lsm/src/bt/lsm_bt.h rename to lsm/include/lsm/bt.h index 60219d1..a2826b0 100644 --- a/lsm/src/bt/lsm_bt.h +++ b/lsm/include/lsm/bt.h @@ -1,41 +1,12 @@ -#ifndef LSM_BT_INTERNAL -#define LSM_BT_INTERNAL - -#include +#ifndef LSM_BT +#define LSM_BT #include "lsm.h" -/** - * Node inside a binary tree - */ -typedef struct lsm_bt_node { - struct lsm_bt_node *left; - struct lsm_bt_node *right; - void *data; - char key; -} lsm_bt_node; - -/** - * Initialize a new binary tree node - * - * @param ptr where to store newly allocated pointer - * @param key key for the node - * @param data data to store - */ -lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data); - -/** - * Deallocate a single binary tree node - */ -void lsm_bt_node_free(lsm_bt_node *node); - /** * A binary tree implementation using char values as keys */ -typedef struct lsm_bt { - lsm_bt_node *root; - uint8_t size; -} lsm_bt; +typedef struct lsm_bt lsm_bt; /** * Initialize a new binary tree diff --git a/lsm/include/lsm/str.h b/lsm/include/lsm/str.h new file mode 100644 index 0000000..58930ec --- /dev/null +++ b/lsm/include/lsm/str.h @@ -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 diff --git a/lsm/include/lsm/trie.h b/lsm/include/lsm/trie.h new file mode 100644 index 0000000..c50d1b3 --- /dev/null +++ b/lsm/include/lsm/trie.h @@ -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 diff --git a/lsm/src/bt/lsm_bt.c b/lsm/src/bt/lsm_bt.c index aadaaaf..d6e8699 100644 --- a/lsm/src/bt/lsm_bt.c +++ b/lsm/src/bt/lsm_bt.c @@ -1,7 +1,6 @@ #include -#include "lsm.h" -#include "lsm_bt.h" +#include "lsm_bt_internal.h" lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data) { lsm_bt_node *node = calloc(1, sizeof(lsm_bt_node)); diff --git a/lsm/src/bt/lsm_bt_internal.h b/lsm/src/bt/lsm_bt_internal.h new file mode 100644 index 0000000..4b55771 --- /dev/null +++ b/lsm/src/bt/lsm_bt_internal.h @@ -0,0 +1,38 @@ +#ifndef LSM_BT_INTERNAL +#define LSM_BT_INTERNAL + +#include + +#include "lsm.h" +#include "lsm/bt.h" + +/** + * Node inside a binary tree + */ +typedef struct lsm_bt_node { + struct lsm_bt_node *left; + struct lsm_bt_node *right; + void *data; + char key; +} lsm_bt_node; + +/** + * Initialize a new binary tree node + * + * @param ptr where to store newly allocated pointer + * @param key key for the node + * @param data data to store + */ +lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data); + +/** + * Deallocate a single binary tree node + */ +void lsm_bt_node_free(lsm_bt_node *node); + +struct lsm_bt { + lsm_bt_node *root; + uint8_t size; +}; + +#endif diff --git a/lsm/src/lsm_store.c b/lsm/src/lsm_store.c index f3503bc..1a5a445 100644 --- a/lsm/src/lsm_store.c +++ b/lsm/src/lsm_store.c @@ -10,14 +10,14 @@ * pointer * @return success of the function */ -lsm_error lsm_store_init(lsm_store **ptr) { - lsm_store *store = calloc(1, sizeof(lsm_store)); +/* lsm_error lsm_store_init(lsm_store **ptr) { */ +/* lsm_store *store = calloc(1, sizeof(lsm_store)); */ - if (store == NULL) { - return lsm_error_failed_alloc; - } +/* if (store == NULL) { */ +/* return lsm_error_failed_alloc; */ +/* } */ - *ptr = store; +/* *ptr = store; */ - return lsm_error_ok; -} +/* return lsm_error_ok; */ +/* } */ diff --git a/lsm/src/lsm_store_node.h b/lsm/src/lsm_store_node.h index 548eb53..826b312 100644 --- a/lsm/src/lsm_store_node.h +++ b/lsm/src/lsm_store_node.h @@ -23,10 +23,10 @@ lsm_error lsm_store_inode_init(lsm_store_inode **ptr, const char c); * binary tree. */ typedef struct lsm_store_node { - lsm_entry *entry; + /* lsm_entry *entry; */ lsm_store_inode *root; uint8_t size; - lsm_string skip; + /* lsm_string skip; */ } lsm_store_node; /** diff --git a/lsm/src/string/lsm_str.c b/lsm/src/string/lsm_str.c new file mode 100644 index 0000000..81e8797 --- /dev/null +++ b/lsm/src/string/lsm_str.c @@ -0,0 +1,61 @@ +#include +#include +#include + +#include "lsm.h" +#include "lsm_str_internal.h" + +lsm_error lsm_str_init_zero(lsm_str **ptr) { + lsm_str *str = calloc(1, sizeof(lsm_str)); + + if (str == NULL) { + return lsm_error_failed_alloc; + } + + *ptr = str; + + return lsm_error_ok; +} + +void lsm_str_init_prealloc(lsm_str *str, char *s) { + str->len = strlen(s); + + if (str->len <= 8) { + memcpy(str->data.val, s, str->len); + free(s); + } else { + str->data.ptr = s; + } +} + +lsm_error lsm_str_init(lsm_str **ptr, char *s) { + lsm_str *str = calloc(1, sizeof(lsm_str)); + + if (str == NULL) { + return lsm_error_failed_alloc; + } + + lsm_str_init_prealloc(str, s); + + *ptr = str; + + return lsm_error_ok; +} + +void lsm_str_zero(lsm_str *str) { + if (str->len > 8) { + free(str->data.ptr); + } + + str->len = 0; +} + +void lsm_str_free(lsm_str *str) { + if (str->len > 8) { + free(str->data.ptr); + } + + free(str); +} + +uint64_t lsm_str_len(lsm_str *str) { return str->len; } diff --git a/lsm/src/string/lsm_str_internal.h b/lsm/src/string/lsm_str_internal.h new file mode 100644 index 0000000..909a0df --- /dev/null +++ b/lsm/src/string/lsm_str_internal.h @@ -0,0 +1,16 @@ +#ifndef LSM_STR_INTERNAL +#define LSM_STR_INTERNAL + +#include + +#include "lsm/str.h" + +struct lsm_str { + uint64_t len; + union { + void *ptr; + char val[8]; + } data; +}; + +#endif diff --git a/lsm/test/bt/bt.c b/lsm/test/bt/bt.c index 21b4ed2..f2d2781 100644 --- a/lsm/test/bt/bt.c +++ b/lsm/test/bt/bt.c @@ -1,6 +1,5 @@ #include "test.h" -#include "lsm.h" -#include "lsm_bt.h" +#include "lsm_bt_internal.h" #define BT_INIT() \ lsm_bt *bt; \