2023-10-12 10:06:20 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lsm.h"
|
2023-10-13 11:56:50 +02:00
|
|
|
#include "lsm_store_node.h"
|
2023-10-12 10:06:20 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-13 11:56:50 +02:00
|
|
|
lsm_error lsm_store_node_search(lsm_store_node **out_ptr, lsm_store_node *node,
|
|
|
|
const char c) {
|
2023-10-12 10:06:20 +02:00
|
|
|
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;
|
|
|
|
}
|