From b5fc3a3612520ddec4db9d62268df35ca80547ed Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Nov 2023 12:25:47 +0100 Subject: [PATCH] refactor(lsm): decouple attribute types --- include/lander.h | 6 ++++++ lsm/include/lsm/store.h | 26 +++++++------------------- lsm/src/_include/lsm/store_internal.h | 2 +- lsm/src/store/lsm_store_entry.c | 14 +++++++------- src/lander/lander_get.c | 4 ++-- src/lander/lander_post.c | 6 +++--- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/include/lander.h b/include/lander.h index 0ffca46..0697cce 100644 --- a/include/lander.h +++ b/include/lander.h @@ -18,6 +18,12 @@ typedef struct lander_ctx { uint64_t remaining_data; } lander_ctx; +typedef enum lander_attr_type : uint64_t { + lander_attr_type_entry_type = 1 << 0, + lander_attr_type_content_type = 1 << 1, + lander_attr_type_url = 1 << 2, +} lander_attr_type; + typedef enum lander_entry_type { lander_entry_type_redirect = 0, lander_entry_type_paste = 1, diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 16798f2..37fdb9d 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -9,18 +9,6 @@ #define LSM_STORE_DISK_THRESHOLD 1024 -/** - * The type of an entry attribute. - * - * Each type is represented as a single bit of an - * integer, so they can be easily combined into a bitmap. - */ -typedef enum lsm_attr_type : uint64_t { - lsm_attr_type_entry_type = 1 << 0, - lsm_attr_type_content_type = 1 << 1, - lsm_attr_type_url = 1 << 2, -} lsm_attr_type; - /** * A handle referencing an entry inside a store. Read/write operations from/to * the entry go through this handle. @@ -33,7 +21,7 @@ typedef struct lsm_entry_handle lsm_entry_handle; * @param entry entry to check * @param type type of attribute to check for */ -bool lsm_entry_attr_present(lsm_entry_handle *handle, lsm_attr_type type); +bool lsm_entry_attr_present(lsm_entry_handle *handle, uint64_t type); /** * Retrieve the contents of an attribute from an entry, if present @@ -43,7 +31,7 @@ bool lsm_entry_attr_present(lsm_entry_handle *handle, lsm_attr_type type); * @param type type of attribute to return */ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle, - lsm_attr_type type); + uint64_t type); /** * Convenience wrapper around `lsm_entry_attr_get` that can be used if we know @@ -54,7 +42,7 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle, * @param type type of attribute to return */ lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, - lsm_attr_type type); + uint64_t type); /** * Add a new attribute to the entry. @@ -63,7 +51,7 @@ lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, * @param type type of attribute to add * @param data data of attribute; ownership of pointer is taken over */ -lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type, +lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint64_t type, lsm_str *data); /** @@ -74,8 +62,8 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type, * @param type type of attribute to add * @param data data of attribute */ -lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, - lsm_attr_type type, uint64_t data); +lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint64_t type, + uint64_t data); /** * Remove an atribute from the given entry, if present. @@ -86,7 +74,7 @@ lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, * @param type type of attribute to remove */ lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, - lsm_attr_type type); + uint64_t type); /** * A store consisting of LSM entries. diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index b9546e9..f426d34 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -12,7 +12,7 @@ #define LSM_IDX_FILE_NAME "lsm.idx" typedef struct lsm_attr { - lsm_attr_type type; + uint64_t type; lsm_str *str; } lsm_attr; diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 2878996..ea150f0 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -56,12 +56,12 @@ void lsm_entry_close(lsm_entry_handle *handle) { free(handle); } -bool lsm_entry_attr_present(lsm_entry_handle *handle, lsm_attr_type type) { +bool lsm_entry_attr_present(lsm_entry_handle *handle, uint64_t type) { return (handle->wrapper->entry->attrs.bitmap & type) != 0; } lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle, - lsm_attr_type type) { + uint64_t type) { if (!lsm_entry_attr_present(handle, type)) { return lsm_error_not_found; } @@ -79,7 +79,7 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle, } lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, - lsm_attr_type type) { + uint64_t type) { lsm_str *s; LSM_RES(lsm_entry_attr_get(&s, handle, type)); @@ -96,7 +96,7 @@ lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, } lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, - lsm_attr_type type) { + uint64_t type) { if (!lsm_entry_attr_present(handle, type)) { return lsm_error_not_found; } @@ -143,7 +143,7 @@ lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, return lsm_error_ok; } -lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type, +lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint64_t type, lsm_str *data) { if (lsm_entry_attr_present(handle, type)) { return lsm_error_already_present; @@ -168,8 +168,8 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type, return lsm_error_ok; } -lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, - lsm_attr_type type, uint64_t data) { +lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint64_t type, + uint64_t data) { lsm_str *s; LSM_RES( lsm_str_init_copy_n(&s, (char *)&data, sizeof(uint64_t) / sizeof(char))); diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index 026f449..b063e29 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -80,14 +80,14 @@ bool lander_get_entry_lsm(event_loop_conn *conn) { lander_entry_type t; lsm_entry_attr_get_num((uint64_t *)&t, c_ctx->entry, - lsm_attr_type_entry_type); + lander_attr_type_entry_type); if (t == lander_entry_type_redirect) { // For redirects, the URL is stored as an in-memory attribute lsm_str *url_attr_val; // This shouldn't be able to happen - if (lsm_entry_attr_get(&url_attr_val, c_ctx->entry, lsm_attr_type_url) != + if (lsm_entry_attr_get(&url_attr_val, c_ctx->entry, lander_attr_type_url) != lsm_error_ok) { error("Entry of type redirect detected without URL attribute"); diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index b03cd53..05dc662 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -132,7 +132,7 @@ bool lander_post_redirect_lsm(event_loop_conn *conn) { return true; } - lsm_entry_attr_insert_num(c_ctx->entry, lsm_attr_type_entry_type, + lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type, lander_entry_type_redirect); return true; @@ -144,7 +144,7 @@ bool lander_post_redirect_body_to_attr(event_loop_conn *conn) { lsm_str *attr_value; lsm_str_init_copy_n(&attr_value, ctx->req.body.buf, ctx->req.body.len); - lsm_entry_attr_insert(c_ctx->entry, lsm_attr_type_url, attr_value); + lsm_entry_attr_insert(c_ctx->entry, lander_attr_type_url, attr_value); return true; } @@ -171,7 +171,7 @@ bool lander_post_paste_lsm(event_loop_conn *conn) { return true; } - lsm_entry_attr_insert_num(c_ctx->entry, lsm_attr_type_entry_type, + lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type, lander_entry_type_paste); return true;