From 2f58d1ee489d25730272b940c680f5a63c9343bd Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 9 Nov 2023 21:32:39 +0100 Subject: [PATCH] feat(lsm): track entry idx file offset --- lsm/src/_include/lsm/store_internal.h | 1 + lsm/src/store/lsm_store_disk_read.c | 4 ++++ lsm/src/store/lsm_store_disk_write.c | 22 ++++++++++++---------- lsm/src/trie/lsm_trie.c | 7 ------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index e4bbdba..6bd7b00 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -31,6 +31,7 @@ typedef struct lsm_entry { lsm_attr *items; } attrs; uint64_t data_len; + uint64_t idx_file_offset; } lsm_entry; /** diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index fc4d748..17b91d7 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -150,6 +150,8 @@ lsm_error lsm_store_load_db(lsm_store *store) { store->idx_file, sizeof(uint64_t), 1)); for (uint64_t i = 0; i < store->idx_file_block_count; i++) { + uint64_t idx_file_offset = store->idx_file_size; + LSM_RES(lsm_entry_read_str(&key, &store->idx_file_size, store->idx_file)); LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file, sizeof(uint64_t), 2)); @@ -163,6 +165,8 @@ lsm_error lsm_store_load_db(lsm_store *store) { 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)); + + handle->wrapper->entry->idx_file_offset = idx_file_offset; lsm_entry_close(handle); store->db_file_size += db_dim[1]; diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index b79a78b..3482f53 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -41,7 +41,7 @@ static lsm_error lsm_fseek(FILE *f, uint64_t pos) { return lsm_error_ok; } -lsm_error lsm_entry_write_db(uint64_t *size, FILE *db_file, lsm_entry *entry, +lsm_error lsm_write_db_entry(uint64_t *size, FILE *db_file, lsm_entry *entry, uint64_t pos) { *size = 0; @@ -59,7 +59,7 @@ lsm_error lsm_entry_write_db(uint64_t *size, FILE *db_file, lsm_entry *entry, return lsm_error_ok; } -lsm_error lsm_entry_write_idx(uint64_t *size, FILE *idx_file, lsm_entry *entry, +lsm_error lsm_write_idx_entry(uint64_t *size, FILE *idx_file, lsm_entry *entry, uint64_t offset, uint64_t len, uint64_t pos) { *size = 0; @@ -75,27 +75,27 @@ lsm_error lsm_entry_write_idx(uint64_t *size, FILE *idx_file, lsm_entry *entry, lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) { pthread_mutex_lock(&store->db_lock); + uint64_t db_entry_index = store->db_file_size; + uint64_t db_entry_size; lsm_error res = - lsm_entry_write_db(&db_entry_size, store->db_file, handle->wrapper->entry, + lsm_write_db_entry(&db_entry_size, store->db_file, handle->wrapper->entry, store->db_file_size); fflush(store->db_file); - if (res != lsm_error_ok) { - pthread_mutex_unlock(&store->db_lock); + pthread_mutex_unlock(&store->db_lock); + if (res != lsm_error_ok) { return res; } - uint64_t db_entry_index = store->db_file_size; - - pthread_mutex_unlock(&store->db_lock); - // Append entry to index file pthread_mutex_lock(&store->idx_lock); + uint64_t idx_entry_index = store->idx_file_size; + uint64_t idx_entry_size; - res = lsm_entry_write_idx(&idx_entry_size, store->idx_file, + res = lsm_write_idx_entry(&idx_entry_size, store->idx_file, handle->wrapper->entry, db_entry_index, db_entry_size, store->idx_file_size); @@ -115,6 +115,8 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) { store->idx_file_size += idx_entry_size; store->idx_file_block_count = new_block_count; store->db_file_size += db_entry_size; + + handle->wrapper->entry->idx_file_offset = idx_entry_index; } } diff --git a/lsm/src/trie/lsm_trie.c b/lsm/src/trie/lsm_trie.c index 8744b4e..0e5b548 100644 --- a/lsm/src/trie/lsm_trie.c +++ b/lsm/src/trie/lsm_trie.c @@ -252,19 +252,12 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key) { return lsm_error_not_found; } - // Child is the node we wish to delete if (data != NULL) { *data = child->data; } child->data = NULL; - // We only remove child if it has no children of its own - if (lsm_bt_size(&child->bt) == 0) { - lsm_bt_remove(NULL, &parent->bt, c); - lsm_trie_node_free(child); - } - trie->size--; return lsm_error_ok;