feat(lsm): started string implementation

lsm
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

View File

@ -1,41 +1,12 @@
#ifndef LSM_BT_INTERNAL
#define LSM_BT_INTERNAL
#include <stdint.h>
#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

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

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

View File

@ -1,7 +1,6 @@
#include <stdlib.h>
#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));

View File

@ -0,0 +1,38 @@
#ifndef LSM_BT_INTERNAL
#define LSM_BT_INTERNAL
#include <stdint.h>
#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

View File

@ -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; */
/* } */

View File

@ -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;
/**

View File

@ -0,0 +1,61 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#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; }

View File

@ -0,0 +1,16 @@
#ifndef LSM_STR_INTERNAL
#define LSM_STR_INTERNAL
#include <stdint.h>
#include "lsm/str.h"
struct lsm_str {
uint64_t len;
union {
void *ptr;
char val[8];
} data;
};
#endif

View File

@ -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; \