From fc4187e6ce8327c81404a52d14d3bdb6350199b2 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 29 Oct 2023 14:41:40 +0100 Subject: [PATCH] feat(lsm): add entry data reading --- lsm/example/test.c | 43 ++++++++++++++++++++++++++- lsm/include/lsm/store.h | 12 ++++++++ lsm/src/_include/lsm/store_internal.h | 1 + lsm/src/store/lsm_store.c | 31 +++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/lsm/example/test.c b/lsm/example/test.c index 13b8738..2a7e3d3 100644 --- a/lsm/example/test.c +++ b/lsm/example/test.c @@ -1,5 +1,46 @@ +#include #include #include "lsm.h" +#include "lsm/store.h" +#include "lsm/str.h" -int main() { printf("yuh"); } +int main() { + lsm_str *db_path, *data_dir; + lsm_str_init_copy(&db_path, "data/data.db"); + lsm_str_init_copy(&data_dir, "data"); + + lsm_store *store; + lsm_store_load(&store, db_path, data_dir); + + lsm_str *key; + lsm_str_init_copy(&key, "key"); + + lsm_entry_handle *handle; + assert(lsm_store_insert(&handle, store, key) == lsm_error_ok); + + lsm_str *data; + lsm_str_init_copy(&data, "hello"); + + for (int i = 0; i < 50; i++) { + lsm_entry_data_append(store, handle, data); + } + + lsm_entry_close(handle); + + assert(lsm_store_open_read(&handle, store, key) == lsm_error_ok); + + char buf[24]; + uint64_t read; + uint64_t total = 0; + + lsm_entry_data_read(&read, buf, handle, 24); + total += read; + + while (read > 0) { + printf("%.*s", read, buf); + lsm_entry_data_read(&read, buf, handle, 24); + total += read; + } + printf("\n%lu", total); +} diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 1ddf6cc..d7e2e83 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -151,4 +151,16 @@ 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); +/** + * Read a number of bytes from the entry's data field. The position from which + * data is read is dependent on previous read calls. + * + * @param out where to write how many bytes were read + * @param buf buffer to store read data in + * @param handle entry handle to read from + * @param len how many bytes to read at most + */ +lsm_error lsm_entry_data_read(uint64_t *out, char *buf, + lsm_entry_handle *handle, uint64_t len); + #endif diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index 27b6e5b..c8bad4c 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -62,6 +62,7 @@ void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper); struct lsm_entry_handle { lsm_entry_wrapper *wrapper; FILE *f; + uint64_t pos; }; lsm_error lsm_entry_handle_init(lsm_entry_handle **out); diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index cd6a45b..26e6af7 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -259,3 +259,34 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, return lsm_error_ok; } + +lsm_error lsm_entry_data_read(uint64_t *out, char *buf, + lsm_entry_handle *handle, uint64_t len) { + lsm_entry *entry = handle->wrapper->entry; + + if (entry->data.len == 0) { + *out = 0; + + return lsm_error_ok; + } + + uint64_t read; + + if (entry->data.on_disk) { + read = fread(buf, sizeof(char), len, handle->f); + + if ((read == 0) && (ferror(handle->f) != 0)) { + return lsm_error_failed_io; + } + } else { + read = (entry->data.len - handle->pos) < len + ? (entry->data.len - handle->pos) + : len; + memcpy(buf, &entry->data.value.ptr[handle->pos], read * sizeof(char)); + } + + handle->pos += read; + *out = read; + + return lsm_error_ok; +}