feat(lsm): started string implementation
This commit is contained in:
parent
96fc645034
commit
c327be80e9
11 changed files with 256 additions and 90 deletions
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -1,80 +0,0 @@
|
|||
#ifndef LSM_BT_INTERNAL
|
||||
#define LSM_BT_INTERNAL
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
|
||||
/**
|
||||
* 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
|
||||
38
lsm/src/bt/lsm_bt_internal.h
Normal file
38
lsm/src/bt/lsm_bt_internal.h
Normal 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
|
||||
|
|
@ -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; */
|
||||
/* } */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
|||
61
lsm/src/string/lsm_str.c
Normal file
61
lsm/src/string/lsm_str.c
Normal 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; }
|
||||
16
lsm/src/string/lsm_str_internal.h
Normal file
16
lsm/src/string/lsm_str_internal.h
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue