fix(lsm): store data len in db; fix bug

lsm
Jef Roosens 2023-11-08 16:04:21 +01:00
parent ef33825b7b
commit 9c03a36aa2
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 25 additions and 3 deletions

View File

@ -91,6 +91,14 @@ typedef struct lsm_store lsm_store;
*/ */
lsm_error lsm_store_init(lsm_store **ptr); lsm_error lsm_store_init(lsm_store **ptr);
/**
* Return how many elements are stored in the trie.
*
* @param store store to use
* @return how many elements are in the store
*/
uint64_t lsm_store_size(lsm_store *store);
/** /**
* Open the given database file and load it into a new store object. * Open the given database file and load it into a new store object.
* *

View File

@ -30,6 +30,8 @@ lsm_error lsm_store_init(lsm_store **ptr) {
return lsm_error_ok; return lsm_error_ok;
} }
uint64_t lsm_store_size(lsm_store *store) { return lsm_trie_size(store->trie); }
lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store, lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
lsm_str *key) { lsm_str *key) {
lsm_entry_wrapper *wrapper; lsm_entry_wrapper *wrapper;

View File

@ -105,6 +105,8 @@ static lsm_error lsm_entry_read_str(lsm_str **out, uint64_t *sum, FILE *f) {
return lsm_error_failed_alloc; return lsm_error_failed_alloc;
} }
buf[len] = '\0';
uint64_t read = 0; uint64_t read = 0;
while (read < len) { while (read < len) {
@ -158,6 +160,8 @@ lsm_error lsm_store_load_db(lsm_store *store) {
return lsm_error_failed_io; return lsm_error_failed_io;
} }
LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db_file,
sizeof(uint64_t), 1));
LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file)); LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file));
lsm_entry_close(handle); lsm_entry_close(handle);

View File

@ -37,10 +37,11 @@ lsm_error lsm_entry_write_db(uint64_t *size, FILE *db_file, lsm_entry *entry,
uint64_t pos) { uint64_t pos) {
LSM_RES(lsm_seek(db_file, pos)); LSM_RES(lsm_seek(db_file, pos));
// First we write how many attributes follow LSM_RES(lsm_entry_write_uint64_t(db_file, entry->data_len));
LSM_RES( LSM_RES(
lsm_entry_write_single(db_file, sizeof(uint8_t), &entry->attrs.count)); lsm_entry_write_single(db_file, sizeof(uint8_t), &entry->attrs.count));
*size = sizeof(uint8_t); *size = sizeof(uint64_t) + sizeof(uint8_t);
for (uint8_t i = 0; i < entry->attrs.count; i++) { for (uint8_t i = 0; i < entry->attrs.count; i++) {
// Write attribute type, length & value // Write attribute type, length & value

View File

@ -1,4 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "lander.h" #include "lander.h"
#include "log.h" #include "log.h"
@ -20,6 +22,7 @@
int main() { int main() {
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
srand(time(NULL));
ENV(api_key, "LANDER_API_KEY"); ENV(api_key, "LANDER_API_KEY");
ENV_OPT(port_str, "LANDER_PORT", "18080"); ENV_OPT(port_str, "LANDER_PORT", "18080");
@ -47,14 +50,18 @@ int main() {
lander_gctx *c_gctx = lander_gctx_init(); lander_gctx *c_gctx = lander_gctx_init();
c_gctx->data_dir = data_dir_s; c_gctx->data_dir = data_dir_s;
/* c_gctx->trie = trie; */
lsm_str *data_dir; lsm_str *data_dir;
lsm_str_init_copy(&data_dir, (char *)data_dir_s); lsm_str_init_copy(&data_dir, (char *)data_dir_s);
info("Initializing store from path '%s'", data_dir_s);
if (lsm_store_load(&c_gctx->store, data_dir) != lsm_error_ok) { if (lsm_store_load(&c_gctx->store, data_dir) != lsm_error_ok) {
critical(2, "Failed to load existing store."); critical(2, "Failed to load existing store.");
} }
info("Store loaded containing %lu entries.", lsm_store_size(c_gctx->store));
http_loop *hl = http_loop_init( http_loop *hl = http_loop_init(
lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]), c_gctx, lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]), c_gctx,
lander_ctx_init, (void (*)(void *))lander_ctx_reset, lander_ctx_init, (void (*)(void *))lander_ctx_reset,