From 9b223d04a0226ae4234d39d0778ccee4d2dcf688 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 9 Nov 2023 22:40:06 +0100 Subject: [PATCH] feat(lsm): sync database when closing handle --- include/lander.h | 2 -- lsm/include/lsm/store.h | 8 -------- lsm/src/_include/lsm/store_internal.h | 21 +++++++++++++++++++++ lsm/src/store/lsm_store_disk_read.c | 2 +- lsm/src/store/lsm_store_disk_write.c | 2 +- lsm/src/store/lsm_store_entry.c | 10 ++++++++-- src/lander/lander.c | 5 ++--- src/lander/lander_post.c | 11 ----------- 8 files changed, 33 insertions(+), 28 deletions(-) diff --git a/include/lander.h b/include/lander.h index 3c812c1..88bfab9 100644 --- a/include/lander.h +++ b/include/lander.h @@ -57,6 +57,4 @@ bool lander_get_entry_lsm(event_loop_conn *conn); bool lander_post_redirect_body_to_attr(event_loop_conn *conn); -bool lander_entry_sync(event_loop_conn *conn); - #endif diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index c7d46d7..9410746 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -213,14 +213,6 @@ lsm_error lsm_entry_data_append_raw(lsm_entry_handle *handle, char *data, lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_entry_handle *handle, uint64_t len); -/** - * Persist the entry's data to disk. - * - * @param store store to persist entry in - * @param handle handle to entry to persist - */ -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 2a5856c..cebb41b 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -88,4 +88,25 @@ struct lsm_store { */ lsm_error lsm_store_load_db(lsm_store *store); +/** + * Close & free the handle without updating the database + * + * @param handle handle to close + */ +void lsm_entry_close_no_disk(lsm_entry_handle *handle); + +/** + * Write a new insert to the database. + * + * @param handle handle to added entry + */ +lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle); + +/** + * Remove an entry from the database + * + * @param handle handle to the removed entry + */ +lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle); + #endif diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index cba7a7b..040708a 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -172,7 +172,7 @@ lsm_error lsm_store_load_db(lsm_store *store) { LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file)); handle->wrapper->entry->idx_file_offset = idx_file_offset; - lsm_entry_close(handle); + lsm_entry_close_no_disk(handle); store->db_file_size += db_dim[1]; } diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index 4a3de1b..ffe182f 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -75,7 +75,7 @@ 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_entry_handle *handle) { +lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) { lsm_store *store = handle->store; pthread_mutex_lock(&store->db_lock); diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 58eba29..45ead55 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -47,13 +47,19 @@ lsm_error lsm_entry_handle_init(lsm_entry_handle **out) { return lsm_error_ok; } +void lsm_entry_close_no_disk(lsm_entry_handle *handle) { + pthread_rwlock_unlock(&handle->wrapper->lock); + free(handle); +} + void lsm_entry_close(lsm_entry_handle *handle) { if (handle->f != NULL) { fclose(handle->f); } - pthread_rwlock_unlock(&handle->wrapper->lock); - free(handle); + // TODO handle errors here + lsm_entry_disk_insert(handle); + lsm_entry_close_no_disk(handle); } bool lsm_entry_attr_present(lsm_entry_handle *handle, uint8_t type) { diff --git a/src/lander/lander.c b/src/lander/lander.c index 8045f28..57f5c5e 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -25,7 +25,7 @@ http_route lander_routes[] = { .path = "^/s(l?)/([^/]*)$", .steps = {http_loop_step_auth, lander_post_redirect_lsm, http_loop_step_body_to_buf, lander_post_redirect_body_to_attr, - lander_entry_sync, NULL}, + NULL}, .steps_res = {http_loop_step_write_header, http_loop_step_write_body, NULL}, }, @@ -33,8 +33,7 @@ http_route lander_routes[] = { .method = http_post, .path = "^/p(l?)/([^/]*)$", .steps = {http_loop_step_auth, http_loop_step_parse_content_length, - lander_post_paste_lsm, lander_stream_body_to_entry, - lander_entry_sync, NULL}, + lander_post_paste_lsm, lander_stream_body_to_entry, NULL}, .steps_res = {http_loop_step_write_header, http_loop_step_write_body, NULL}}, }; diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index 3f8f758..429ea39 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -149,17 +149,6 @@ bool lander_post_redirect_body_to_attr(event_loop_conn *conn) { return true; } -bool lander_entry_sync(event_loop_conn *conn) { - http_loop_ctx *ctx = conn->ctx; - lander_ctx *c_ctx = ctx->c; - - if (lsm_entry_sync(c_ctx->entry) != lsm_error_ok) { - ctx->res.status = http_internal_server_error; - } - - return true; -} - bool lander_post_paste_lsm(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c;