From f19a8814f5796b974d6c9e898820247f4284437c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 29 Oct 2023 12:33:07 +0100 Subject: [PATCH] fix(lsm): write in-memory data to disk when switching to file --- lsm/include/lsm/store.h | 5 +++-- lsm/src/store/lsm_store.c | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 7e7e59a..1ddf6cc 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -108,8 +108,9 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store, lsm_str *key); /** - * Open a write handle to the given entry. This entry must be properly closed - * using `lsm_store_handle_close`. + * Open a write handle to the given entry. This handle should only be used for + * writing; read operations on this handle are unsupported. This entry must be + * properly closed using `lsm_store_handle_close`. * * @param out pointer to store handle pointer * @param store store to retrieve entry from diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index 27ebf68..42643b9 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -132,7 +132,7 @@ lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store, sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), lsm_str_ptr(entry->key)); - FILE *f = fopen(path, "wb"); + FILE *f = fopen(path, "ab"); if (f == NULL) { free(handle); @@ -184,6 +184,10 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, lsm_str *data) { + if (lsm_str_len(data) == 0) { + return lsm_error_ok; + } + lsm_entry *entry = handle->wrapper->entry; uint64_t new_len = entry->data.len + lsm_str_len(data); @@ -199,7 +203,6 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, memcpy(&buf[entry->data.len], data_s, lsm_str_len(data)); entry->data.value.ptr = buf; - entry->data.len = new_len; } // Data will end up on disk else { @@ -215,10 +218,19 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, return lsm_error_failed_io; } + size_t written = 0; + + // Write original in-memory data to file + while (written < entry->data.len) { + written += fwrite(&entry->data.value.ptr[written], sizeof(char), + entry->data.len - written, f); + } + + free(entry->data.value.ptr); + entry->data.value.ptr = NULL; + handle->f = f; entry->data.on_disk = true; - - // TODO free old buff, write original data to file } size_t written = 0; @@ -230,5 +242,7 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, } } + entry->data.len = new_len; + return lsm_error_ok; }