From b40389bbe2d5eda10e751ea9ab557316ea9738d0 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 12 Nov 2023 12:48:36 +0100 Subject: [PATCH] feat(lsm): implement basic remove --- lsm/example/test.c | 17 ++++++++++------- lsm/include/lsm/store.h | 7 +++++++ lsm/src/_include/lsm/store_internal.h | 7 +++++++ lsm/src/store/lsm_store.c | 4 ++++ lsm/src/store/lsm_store_disk_write.c | 25 +++++++++++++++++++++++++ lsm/src/store/lsm_store_entry.c | 13 ++++++++++++- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/lsm/example/test.c b/lsm/example/test.c index bd78f2e..660cd04 100644 --- a/lsm/example/test.c +++ b/lsm/example/test.c @@ -20,18 +20,13 @@ int main() { lsm_str *attr; lsm_str_init_copy(&attr, "some attribute value"); - lsm_entry_attr_insert(handle, lsm_attr_type_content_type, attr); + lsm_entry_attr_insert(handle, 1, attr); lsm_str *data; lsm_str_init_copy(&data, "hello"); for (int i = 0; i < 50; i++) { - lsm_entry_data_append(store, handle, data); - } - - if (lsm_entry_sync(store, handle) != lsm_error_ok) { - printf("godver"); - return 1; + lsm_entry_data_append(handle, data); } lsm_entry_close(handle); @@ -50,4 +45,12 @@ int main() { total += read; } printf("\n%lu", total); + + lsm_entry_close(handle); + + assert(lsm_store_open_write(&handle, store, key) == lsm_error_ok); + lsm_entry_remove(handle); + lsm_entry_close(handle); + + assert(lsm_store_open_read(&handle, store, key) == lsm_error_not_found); } diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 31eb19b..45eda7e 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -176,6 +176,13 @@ void lsm_entry_close(lsm_entry_handle *handle); lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, lsm_str *key); +/** + * Mark the entry as removed. + * + * @param handle handle to entry to remove + */ +void lsm_entry_remove(lsm_entry_handle *handle); + /** * Append new data to the given entry, which is expected to be in the store. * diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index fb50838..afbd4f3 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -40,6 +40,13 @@ typedef struct lsm_entry { */ lsm_error lsm_entry_init(lsm_entry **ptr); +/** + * Deallocate an existing entry + * + * @param entry pointer to entry + */ +void lsm_entry_free(lsm_entry *entry); + /** * Deallocate an existing lsm_entry object. * diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index 2185418..57eacb7 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -158,6 +158,10 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, return lsm_error_ok; } +void lsm_entry_remove(lsm_entry_handle *handle) { + handle->states |= lsm_entry_handle_state_removed; +} + lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) { if (lsm_str_len(data) == 0) { return lsm_error_ok; diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index acb0015..b5081af 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -124,3 +124,28 @@ lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) { return res; } + +// Marking an entry as removed in the idx file is simply setting the length of +// its entry to zero +lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle) { + lsm_store *store = handle->store; + lsm_entry *entry = handle->wrapper->entry; + + pthread_mutex_lock(&store->idx.lock); + + lsm_error res = + lsm_fseek(store->idx.f, entry->idx_file_offset + sizeof(uint64_t)); + + if (res != lsm_error_ok) { + pthread_mutex_unlock(&store->idx.lock); + + return res; + } + + uint64_t val = 0; + res = lsm_fwrite(NULL, store->idx.f, sizeof(uint64_t), 1, &val); + + pthread_mutex_unlock(&store->idx.lock); + + return res; +} diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 8212ba6..fd55624 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -19,6 +19,14 @@ lsm_error lsm_entry_init(lsm_entry **ptr) { return lsm_error_ok; } +void lsm_entry_free(lsm_entry *entry) { + if (entry->attrs.count > 0) { + free(entry->attrs.items); + } + + free(entry); +} + lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr) { lsm_entry_wrapper *wrap = calloc(1, sizeof(lsm_entry_wrapper)); @@ -58,7 +66,10 @@ void lsm_entry_close(lsm_entry_handle *handle) { lsm_entry_disk_insert(handle); } else if ((handle->states & lsm_entry_handle_state_removed) && !(handle->states & lsm_entry_handle_state_new)) { - /* lsm_entry_disk_remove(handle); */ + lsm_entry_disk_remove(handle); + + lsm_entry_free(handle->wrapper->entry); + handle->wrapper->entry = NULL; } else if (handle->states & lsm_entry_handle_state_updated) { /* lsm_entry_disk_update(handle); */ }