refactor(lsm): decouple attribute types

lsm
Jef Roosens 2023-11-08 12:25:47 +01:00
parent 535b92a6b6
commit b5fc3a3612
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 26 additions and 32 deletions

View File

@ -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,

View File

@ -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.

View File

@ -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;

View File

@ -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)));

View File

@ -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");

View File

@ -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;