2023-10-25 10:57:45 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lsm.h"
|
2023-10-20 10:41:53 +02:00
|
|
|
#include "lsm/store.h"
|
2023-10-25 10:57:45 +02:00
|
|
|
#include "lsm/trie.h"
|
2023-10-20 10:41:53 +02:00
|
|
|
#include "lsm/store_internal.h"
|
|
|
|
|
2023-10-25 10:57:45 +02:00
|
|
|
lsm_error lsm_store_init(lsm_store **ptr) {
|
|
|
|
lsm_store *store = calloc(1, sizeof(lsm_store));
|
|
|
|
|
|
|
|
if (store == NULL) {
|
|
|
|
return lsm_error_failed_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsm_error res = lsm_trie_init(&store->trie);
|
|
|
|
|
|
|
|
if (res != lsm_error_ok) {
|
|
|
|
free(store);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = store;
|
|
|
|
|
|
|
|
return lsm_error_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsm_error lsm_store_open(lsm_store **ptr, char *db_path, char *data_path) {
|
|
|
|
lsm_store *store;
|
|
|
|
LSM_RES(lsm_store_init(&store));
|
|
|
|
|
|
|
|
// TODO implement all of reading the db file
|
|
|
|
|
|
|
|
store->db_path = db_path;
|
|
|
|
store->data_path = data_path;
|
|
|
|
|
|
|
|
*ptr = store;
|
|
|
|
|
|
|
|
return lsm_error_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsm_error lsm_store_search(lsm_entry **out, lsm_store *store, lsm_str *key) {
|
|
|
|
return lsm_trie_search((void **)out, store->trie, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
lsm_error lsm_store_insert(lsm_entry **out, lsm_store *store, lsm_str *key) {
|
|
|
|
lsm_entry *entry;
|
|
|
|
|
|
|
|
LSM_RES(lsm_entry_init(&entry));
|
|
|
|
LSM_RES(lsm_trie_insert(store->trie, key, entry));
|
|
|
|
|
|
|
|
entry->key = key;
|
|
|
|
*out = entry;
|
|
|
|
|
|
|
|
return lsm_error_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsm_error lsm_store_data_append(lsm_store *store, lsm_entry *entry, lsm_str *data) {
|
|
|
|
uint64_t new_len = entry->data.len + lsm_str_len(data);
|
|
|
|
|
|
|
|
// Data is in memory and still fits -> keep it in memory
|
|
|
|
if ((new_len <= LSM_STORE_DISK_THRESHOLD) && (!entry->data.on_disk)) {
|
|
|
|
char *buf = realloc(entry->data.value.ptr, new_len * sizeof(char));
|
|
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
return lsm_error_failed_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&buf[entry->data.len], lsm_str_ptr(data), lsm_str_len(data));
|
|
|
|
entry->data.value.ptr = buf;
|
|
|
|
entry->data.len = new_len;
|
|
|
|
}
|
|
|
|
// Data will end up on disk
|
|
|
|
else {
|
|
|
|
// Data is not yet on disk, so we create the file
|
|
|
|
if (!entry->data.on_disk) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 11:19:28 +02:00
|
|
|
|
|
|
|
return lsm_error_ok;
|
2023-10-25 10:57:45 +02:00
|
|
|
}
|