feat(lander): store entry type as single byte

lsm
Jef Roosens 2023-11-08 18:17:58 +01:00
parent 9c03a36aa2
commit 0efcdece48
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 57 additions and 43 deletions

View File

@ -41,7 +41,18 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
* @param entry entry to search for * @param entry entry to search for
* @param type type of attribute to return * @param type type of attribute to return
*/ */
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, lsm_entry_handle *handle,
uint8_t type);
/**
* Convenience wrapper around `lsm_entry_attr_get` that can be used if we know
* beforehand the attribute value is an 8-bit number.
*
* @param out where to store attribute data
* @param entry entry to search for
* @param type type of attribute to return
*/
lsm_error lsm_entry_attr_get_uint8_t(uint8_t *out, lsm_entry_handle *handle,
uint8_t type); uint8_t type);
/** /**
@ -62,9 +73,20 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type,
* @param type type of attribute to add * @param type type of attribute to add
* @param data data of attribute * @param data data of attribute
*/ */
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint8_t type, lsm_error lsm_entry_attr_insert_uint64_t(lsm_entry_handle *handle, uint8_t type,
uint64_t data); uint64_t data);
/**
* Convenience wrapper around `lsm_entry_attr_insert` that can be used if the
* data to be stored is an 8-bit number.
*
* @param entry entry to modify
* @param type type of attribute to add
* @param data data of attribute
*/
lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type,
uint8_t data);
/** /**
* Remove an atribute from the given entry, if present. * Remove an atribute from the given entry, if present.
* *

View File

@ -79,7 +79,7 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
return lsm_error_ok; return lsm_error_ok;
} }
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, lsm_entry_handle *handle,
uint8_t type) { uint8_t type) {
lsm_str *s; lsm_str *s;
@ -96,6 +96,17 @@ lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
return lsm_error_ok; return lsm_error_ok;
} }
lsm_error lsm_entry_attr_get_uint8_t(uint8_t *out, lsm_entry_handle *handle,
uint8_t type) {
lsm_str *s;
LSM_RES(lsm_entry_attr_get(&s, handle, type));
*out = lsm_str_char(s, 0);
return lsm_error_ok;
}
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
uint8_t type) { uint8_t type) {
if (!lsm_entry_attr_present(handle, type)) { if (!lsm_entry_attr_present(handle, type)) {
@ -169,7 +180,7 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type,
return lsm_error_ok; return lsm_error_ok;
} }
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint8_t type, lsm_error lsm_entry_attr_insert_uint64_t(lsm_entry_handle *handle, uint8_t type,
uint64_t data) { uint64_t data) {
lsm_str *s; lsm_str *s;
LSM_RES( LSM_RES(
@ -178,6 +189,15 @@ lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint8_t type,
return lsm_entry_attr_insert(handle, type, s); return lsm_entry_attr_insert(handle, type, s);
} }
lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type,
uint8_t data) {
lsm_str *s;
LSM_RES(
lsm_str_init_copy_n(&s, (char *)&data, sizeof(uint8_t) / sizeof(char)));
return lsm_entry_attr_insert(handle, type, s);
}
uint64_t lsm_entry_data_len(lsm_entry_handle *handle) { uint64_t lsm_entry_data_len(lsm_entry_handle *handle) {
return handle->wrapper->entry->data_len; return handle->wrapper->entry->data_len;
} }

View File

@ -26,35 +26,6 @@ bool lander_get_index(event_loop_conn *conn) {
return true; return true;
} }
bool lander_get_entry(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
lander_gctx *c_gctx = ctx->g->c;
const char *key = &ctx->req.path[ctx->req.regex_groups[1].rm_so];
int key_len = ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so;
Entry *entry;
TrieExitCode res = trie_search_len(c_gctx->trie, &entry, key, key_len);
if (res == NotFound) {
ctx->res.status = http_not_found;
} else if (entry->type == Redirect) {
ctx->res.status = http_moved_permanently;
http_res_add_header(&ctx->res, http_header_location, entry->string, false);
} else if (entry->type == Paste) {
char fname[strlen(c_gctx->data_dir) + 8 + key_len + 1];
sprintf(fname, "%s/pastes/%.*s", c_gctx->data_dir, key_len, key);
http_res_set_body_file(&ctx->res, fname);
// TODO don't call everything a text file
http_res_set_mime_type(&ctx->res, http_mime_txt);
}
conn->state = event_loop_conn_state_res;
return true;
}
bool lander_get_entry_lsm(event_loop_conn *conn) { bool lander_get_entry_lsm(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx; http_loop_ctx *ctx = conn->ctx;
lander_ctx *c_ctx = ctx->c; lander_ctx *c_ctx = ctx->c;
@ -79,7 +50,7 @@ bool lander_get_entry_lsm(event_loop_conn *conn) {
} }
lander_entry_type t; lander_entry_type t;
lsm_entry_attr_get_num((uint64_t *)&t, c_ctx->entry, lsm_entry_attr_get_uint8_t((uint8_t *)&t, c_ctx->entry,
lander_attr_type_entry_type); lander_attr_type_entry_type);
if (t == lander_entry_type_redirect) { if (t == lander_entry_type_redirect) {
@ -113,6 +84,7 @@ bool lander_get_entry_lsm(event_loop_conn *conn) {
c_ctx->entry = NULL; c_ctx->entry = NULL;
} else { } else {
ctx->res.body.expected_len = lsm_entry_data_len(c_ctx->entry); ctx->res.body.expected_len = lsm_entry_data_len(c_ctx->entry);
http_res_set_mime_type(&ctx->res, http_mime_txt);
} }
return true; return true;

View File

@ -132,7 +132,7 @@ bool lander_post_redirect_lsm(event_loop_conn *conn) {
return true; return true;
} }
lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type, lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
lander_entry_type_redirect); lander_entry_type_redirect);
return true; return true;
@ -171,7 +171,7 @@ bool lander_post_paste_lsm(event_loop_conn *conn) {
return true; return true;
} }
lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type, lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
lander_entry_type_paste); lander_entry_type_paste);
return true; return true;