From 418de748f0523b394f00f3de7e6de45c8c6b8301 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 10 Nov 2023 16:10:20 +0100 Subject: [PATCH] feat(lsm): pave the way for removals and updates --- lsm/include/lsm/store.h | 2 -- lsm/src/_include/lsm/store_internal.h | 24 ++++++++++++++++++------ lsm/src/store/lsm_store.c | 4 ++-- lsm/src/store/lsm_store_disk_read.c | 4 +--- lsm/src/store/lsm_store_entry.c | 12 +++++++++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 9410746..31eb19b 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -7,8 +7,6 @@ #include "lsm.h" #include "lsm/str.h" -#define LSM_STORE_DISK_THRESHOLD 1024 - /** * A handle referencing an entry inside a store. Read/write operations from/to * the entry go through this handle. diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index ccafe99..bb35772 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -19,9 +19,8 @@ typedef struct lsm_attr { /** * An entry inside an LSM store. * - * Each entry consists of the key it's stored behind, zero or more attributes - * (metadata) and a data field. The data field can be stored on disk or - * in-memory, depending on the size. + * Each entry consists of the key it's stored behind, zero or more attributes + * (metadata) and a data file. */ typedef struct lsm_entry { lsm_str *key; @@ -56,6 +55,12 @@ typedef struct lsm_entry_wrapper { lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr); void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper); +typedef enum lsm_entry_handle_state : uint8_t { + lsm_entry_handle_state_new = 1 << 0, + lsm_entry_handle_state_updated = 1 << 1, + lsm_entry_handle_state_removed = 1 << 2, +} lsm_entry_handle_state; + struct lsm_entry_handle { lsm_entry_wrapper *wrapper; lsm_store *store; @@ -63,8 +68,8 @@ struct lsm_entry_handle { FILE *f; // Current position in the file pointer uint64_t pos; - // Whether the entry's metadata has changed - bool dirty; + // Required to determine in what way the database files need to be synced + uint64_t states; }; lsm_error lsm_entry_handle_init(lsm_entry_handle **out); @@ -100,10 +105,17 @@ lsm_error lsm_store_load_db(lsm_store *store); lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle); /** - * Remove an entry from the database + * Remove an entry from the database. * * @param handle handle to the removed entry */ lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle); +/** + * Update an existing entry already in the store. + * + * @param handle to updated entry + */ +lsm_error lsm_entry_disk_update(lsm_entry_handle *handle); + #endif diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index 022e68b..8afc56a 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -151,7 +151,7 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, handle->store = store; // Newly inserted entries are always dirty - handle->dirty = true; + handle->states |= lsm_entry_handle_state_new; *out = handle; @@ -192,7 +192,7 @@ lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) { } entry->data_len = new_len; - handle->dirty = true; + handle->states |= lsm_entry_handle_state_updated; return lsm_error_ok; } diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 5c71dee..eaaed32 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -173,9 +173,7 @@ lsm_error lsm_store_load_db(lsm_store *store) { handle->wrapper->entry->idx_file_offset = idx_file_offset; - // We explicitely set the dirty flag here to prevent writing to the datase - // when reading it in - handle->dirty = false; + handle->states = 0; lsm_entry_close(handle); store->db_file_size += db_dim[1]; diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 4623f36..8212ba6 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -53,8 +53,14 @@ void lsm_entry_close(lsm_entry_handle *handle) { } // TODO handle errors here - if (handle->dirty) { + if ((handle->states & lsm_entry_handle_state_new) && + !(handle->states & lsm_entry_handle_state_removed)) { 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); */ + } else if (handle->states & lsm_entry_handle_state_updated) { + /* lsm_entry_disk_update(handle); */ } pthread_rwlock_unlock(&handle->wrapper->lock); @@ -157,7 +163,7 @@ lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, entry->attrs.count--; entry->attrs.bitmap[type / 64] &= ~(((uint64_t)1) << (type % 64)); - handle->dirty = true; + handle->states |= lsm_entry_handle_state_updated; return lsm_error_ok; } @@ -184,7 +190,7 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type, entry->attrs.count++; entry->attrs.bitmap[type / 64] |= ((uint64_t)1) << (type % 64); - handle->dirty = true; + handle->states |= lsm_entry_handle_state_updated; return lsm_error_ok; }