From eb0ce16f78b1a554ef8cdb521c86fece4ad8a052 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 9 Nov 2023 22:05:20 +0100 Subject: [PATCH] feat(lsm): store pointer to store in entry handle --- lsm/include/lsm/store.h | 11 +++--- lsm/src/_include/lsm/store_internal.h | 1 + lsm/src/store/lsm_store.c | 50 +++++---------------------- lsm/src/store/lsm_store_disk_read.c | 2 +- lsm/src/store/lsm_store_disk_write.c | 4 ++- src/lander/lander_get.c | 6 ++-- src/lander/lander_post.c | 8 ++--- 7 files changed, 23 insertions(+), 59 deletions(-) diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index d49bbdf..c7d46d7 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -188,8 +188,7 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, * @param entry entry to append data to * @param data data to append */ -lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, - lsm_str *data); +lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data); /** * Same as `lsm_entry_data_append`, except that it takes a direct char array. @@ -199,8 +198,8 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, * @param data data to append * @param len length of data array */ -lsm_error lsm_entry_data_append_raw(lsm_store *store, lsm_entry_handle *handle, - char *data, uint64_t len); +lsm_error lsm_entry_data_append_raw(lsm_entry_handle *handle, char *data, + uint64_t len); /** * Read a number of bytes from the entry's data field. The position from which @@ -211,7 +210,7 @@ lsm_error lsm_entry_data_append_raw(lsm_store *store, lsm_entry_handle *handle, * @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_store *store, +lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_entry_handle *handle, uint64_t len); /** @@ -220,7 +219,7 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_store *store, * @param store store to persist entry in * @param handle handle to entry to persist */ -lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle); +lsm_error lsm_entry_sync(lsm_entry_handle *handle); /** * Return the length of the entry's data. diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index 6bd7b00..2a5856c 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -58,6 +58,7 @@ void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper); struct lsm_entry_handle { lsm_entry_wrapper *wrapper; + lsm_store *store; FILE *f; uint64_t pos; }; diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index 3ac2232..2da7c51 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -62,24 +62,8 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store, return res; } - /* // Open a new file descriptor if needed */ - /* if (entry->data_len > 0) { */ - /* char path[store->data_path->len + entry->key->len + 2]; */ - /* sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), */ - /* lsm_str_ptr(entry->key)); */ - - /* FILE *f = fopen(path, "rb"); */ - - /* if (f == NULL) { */ - /* free(handle); */ - - /* return lsm_error_failed_io; */ - /* } */ - - /* handle->f = f; */ - /* } */ - handle->wrapper = wrapper; + handle->store = store; *out = handle; return lsm_error_ok; @@ -116,24 +100,8 @@ lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store, return res; } - /* // Open a new file descriptor if needed */ - /* if (entry->data_len > 0) { */ - /* char path[store->data_path->len + entry->key->len + 2]; */ - /* sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), */ - /* lsm_str_ptr(entry->key)); */ - - /* FILE *f = fopen(path, "ab"); */ - - /* if (f == NULL) { */ - /* free(handle); */ - - /* return lsm_error_failed_io; */ - /* } */ - - /* handle->f = f; */ - /* } */ - handle->wrapper = wrapper; + handle->store = store; *out = handle; return lsm_error_ok; @@ -180,14 +148,14 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store, // No need to set the handle's file, as the entry doesn't have any data yet handle->wrapper = wrapper; + handle->store = store; *out = handle; return lsm_error_ok; } -lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, - lsm_str *data) { +lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) { if (lsm_str_len(data) == 0) { return lsm_error_ok; } @@ -199,8 +167,8 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, // Entries don't open their file unless needed if (handle->f == NULL) { - char path[store->data_path->len + entry->key->len + 2]; - sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), + char path[handle->store->data_path->len + entry->key->len + 2]; + sprintf(path, "%s/%s", lsm_str_ptr(handle->store->data_path), lsm_str_ptr(entry->key)); FILE *f = fopen(path, "ab"); @@ -225,7 +193,7 @@ 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_store *store, +lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_entry_handle *handle, uint64_t len) { lsm_entry *entry = handle->wrapper->entry; @@ -237,8 +205,8 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_store *store, // Entries don't open their file unless needed if (handle->f == NULL) { - char path[store->data_path->len + entry->key->len + 2]; - sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), + char path[handle->store->data_path->len + entry->key->len + 2]; + sprintf(path, "%s/%s", lsm_str_ptr(handle->store->data_path), lsm_str_ptr(entry->key)); FILE *f = fopen(path, "rb"); diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 8ce53c1..cba7a7b 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -132,7 +132,7 @@ static lsm_error lsm_entry_read_attrs(uint64_t *sum, lsm_entry_handle *handle, for (uint64_t i = 0; i < attr_count; i++) { LSM_RES(lsm_fread(&attr_type, sum, db_file, sizeof(uint8_t), 1)); LSM_RES(lsm_entry_read_str(&val, sum, db_file)); - lsm_entry_attr_insert(handle, attr_type, val); + LSM_RES(lsm_entry_attr_insert(handle, attr_type, val)); } 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 3c9293d..4a3de1b 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -75,7 +75,9 @@ lsm_error lsm_write_idx_entry(uint64_t *size, FILE *idx_file, lsm_entry *entry, return lsm_error_ok; } -lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) { +lsm_error lsm_entry_sync(lsm_entry_handle *handle) { + lsm_store *store = handle->store; + pthread_mutex_lock(&store->db_lock); uint64_t db_entry_index = store->db_file_size; diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index 5d1be5f..7c467b5 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -93,8 +93,6 @@ bool lander_get_entry_lsm(event_loop_conn *conn) { bool lander_stream_body_to_client(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - http_loop_gctx *gctx = ctx->g; - lander_gctx *c_gctx = gctx->c; if ((c_ctx->entry == NULL) || (ctx->res.body.expected_len == ctx->res.body.len)) { @@ -105,8 +103,8 @@ bool lander_stream_body_to_client(event_loop_conn *conn) { ctx->res.body.expected_len - ctx->res.body.len); uint64_t read = 0; - lsm_entry_data_read(&read, (char *)&conn->wbuf[conn->wbuf_size], - c_gctx->store, c_ctx->entry, to_write); + lsm_entry_data_read(&read, (char *)&conn->wbuf[conn->wbuf_size], c_ctx->entry, + to_write); ctx->res.body.len += read; conn->wbuf_size += read; diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index ee1a9cb..3f8f758 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -151,11 +151,9 @@ bool lander_post_redirect_body_to_attr(event_loop_conn *conn) { bool lander_entry_sync(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; - http_loop_gctx *gctx = ctx->g; - lander_gctx *c_gctx = gctx->c; lander_ctx *c_ctx = ctx->c; - if (lsm_entry_sync(c_gctx->store, c_ctx->entry) != lsm_error_ok) { + if (lsm_entry_sync(c_ctx->entry) != lsm_error_ok) { ctx->res.status = http_internal_server_error; } @@ -180,8 +178,6 @@ bool lander_post_paste_lsm(event_loop_conn *conn) { bool lander_stream_body_to_entry(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - http_loop_gctx *gctx = ctx->g; - lander_gctx *c_gctx = gctx->c; uint64_t to_append = MIN(conn->rbuf_size - conn->rbuf_read, @@ -189,7 +185,7 @@ bool lander_stream_body_to_entry(event_loop_conn *conn) { lsm_str *data; lsm_str_init_copy_n(&data, (char *)&conn->rbuf[conn->rbuf_read], to_append); - lsm_entry_data_append(c_gctx->store, c_ctx->entry, data); + lsm_entry_data_append(c_ctx->entry, data); conn->rbuf_read += to_append;