feat(lsm): track entry idx file offset

lsm
Jef Roosens 2023-11-09 21:32:39 +01:00
parent 715e1f9a58
commit 2f58d1ee48
Signed by: Jef Roosens
GPG Key ID: 02D4C0997E74717B
4 changed files with 17 additions and 17 deletions

View File

@ -31,6 +31,7 @@ typedef struct lsm_entry {
lsm_attr *items; lsm_attr *items;
} attrs; } attrs;
uint64_t data_len; uint64_t data_len;
uint64_t idx_file_offset;
} lsm_entry; } lsm_entry;
/** /**

View File

@ -150,6 +150,8 @@ lsm_error lsm_store_load_db(lsm_store *store) {
store->idx_file, sizeof(uint64_t), 1)); store->idx_file, sizeof(uint64_t), 1));
for (uint64_t i = 0; i < store->idx_file_block_count; i++) { 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_entry_read_str(&key, &store->idx_file_size, store->idx_file));
LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file, LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file,
sizeof(uint64_t), 2)); 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, LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db_file,
sizeof(uint64_t), 1)); 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));
handle->wrapper->entry->idx_file_offset = idx_file_offset;
lsm_entry_close(handle); lsm_entry_close(handle);
store->db_file_size += db_dim[1]; store->db_file_size += db_dim[1];

View File

@ -41,7 +41,7 @@ static lsm_error lsm_fseek(FILE *f, uint64_t pos) {
return lsm_error_ok; 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) { uint64_t pos) {
*size = 0; *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; 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) { uint64_t offset, uint64_t len, uint64_t pos) {
*size = 0; *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) { lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
pthread_mutex_lock(&store->db_lock); pthread_mutex_lock(&store->db_lock);
uint64_t db_entry_index = store->db_file_size;
uint64_t db_entry_size; uint64_t db_entry_size;
lsm_error res = 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); store->db_file_size);
fflush(store->db_file); 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; return res;
} }
uint64_t db_entry_index = store->db_file_size;
pthread_mutex_unlock(&store->db_lock);
// Append entry to index file // Append entry to index file
pthread_mutex_lock(&store->idx_lock); pthread_mutex_lock(&store->idx_lock);
uint64_t idx_entry_index = store->idx_file_size;
uint64_t idx_entry_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, handle->wrapper->entry, db_entry_index,
db_entry_size, store->idx_file_size); 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_size += idx_entry_size;
store->idx_file_block_count = new_block_count; store->idx_file_block_count = new_block_count;
store->db_file_size += db_entry_size; store->db_file_size += db_entry_size;
handle->wrapper->entry->idx_file_offset = idx_entry_index;
} }
} }

View File

@ -252,19 +252,12 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key) {
return lsm_error_not_found; return lsm_error_not_found;
} }
// Child is the node we wish to delete
if (data != NULL) { if (data != NULL) {
*data = child->data; *data = child->data;
} }
child->data = NULL; 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--; trie->size--;
return lsm_error_ok; return lsm_error_ok;