diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 7de2946..bad5484 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -91,6 +91,14 @@ typedef struct lsm_store lsm_store; */ 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. * diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index c6df34e..3ac2232 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -30,6 +30,8 @@ lsm_error lsm_store_init(lsm_store **ptr) { 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_str *key) { lsm_entry_wrapper *wrapper; diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 2b11a30..fc4d748 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -105,6 +105,8 @@ static lsm_error lsm_entry_read_str(lsm_str **out, uint64_t *sum, FILE *f) { return lsm_error_failed_alloc; } + buf[len] = '\0'; + uint64_t read = 0; while (read < len) { @@ -158,6 +160,8 @@ lsm_error lsm_store_load_db(lsm_store *store) { 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_entry_close(handle); diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index 4bdd297..eb60c22 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -37,10 +37,11 @@ lsm_error lsm_entry_write_db(uint64_t *size, FILE *db_file, lsm_entry *entry, uint64_t 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_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++) { // Write attribute type, length & value diff --git a/src/main.c b/src/main.c index f854f3e..bf0dd9a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,6 @@ #include +#include +#include #include "lander.h" #include "log.h" @@ -20,6 +22,7 @@ int main() { setvbuf(stdout, NULL, _IONBF, 0); + srand(time(NULL)); ENV(api_key, "LANDER_API_KEY"); ENV_OPT(port_str, "LANDER_PORT", "18080"); @@ -47,14 +50,18 @@ int main() { lander_gctx *c_gctx = lander_gctx_init(); c_gctx->data_dir = data_dir_s; - /* c_gctx->trie = trie; */ lsm_str *data_dir; 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) { 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( lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]), c_gctx, lander_ctx_init, (void (*)(void *))lander_ctx_reset,