feat(lander): store entry type as single byte
parent
9c03a36aa2
commit
0efcdece48
|
@ -41,8 +41,19 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
|
|||
* @param entry entry to search for
|
||||
* @param type type of attribute to return
|
||||
*/
|
||||
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
|
||||
uint8_t type);
|
||||
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);
|
||||
|
||||
/**
|
||||
* Add a new attribute to the entry.
|
||||
|
@ -62,8 +73,19 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type,
|
|||
* @param type type of attribute to add
|
||||
* @param data data of attribute
|
||||
*/
|
||||
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint8_t type,
|
||||
uint64_t data);
|
||||
lsm_error lsm_entry_attr_insert_uint64_t(lsm_entry_handle *handle, uint8_t type,
|
||||
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.
|
||||
|
|
|
@ -79,8 +79,8 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
|
|||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
|
||||
uint8_t type) {
|
||||
lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, lsm_entry_handle *handle,
|
||||
uint8_t type) {
|
||||
lsm_str *s;
|
||||
|
||||
LSM_RES(lsm_entry_attr_get(&s, handle, type));
|
||||
|
@ -96,6 +96,17 @@ lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
|
|||
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,
|
||||
uint8_t type) {
|
||||
if (!lsm_entry_attr_present(handle, type)) {
|
||||
|
@ -169,8 +180,8 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type,
|
|||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, uint8_t type,
|
||||
uint64_t data) {
|
||||
lsm_error lsm_entry_attr_insert_uint64_t(lsm_entry_handle *handle, uint8_t type,
|
||||
uint64_t data) {
|
||||
lsm_str *s;
|
||||
LSM_RES(
|
||||
lsm_str_init_copy_n(&s, (char *)&data, sizeof(uint64_t) / sizeof(char)));
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
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) {
|
||||
return handle->wrapper->entry->data_len;
|
||||
}
|
||||
|
|
|
@ -26,35 +26,6 @@ bool lander_get_index(event_loop_conn *conn) {
|
|||
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) {
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
lander_ctx *c_ctx = ctx->c;
|
||||
|
@ -79,8 +50,8 @@ bool lander_get_entry_lsm(event_loop_conn *conn) {
|
|||
}
|
||||
|
||||
lander_entry_type t;
|
||||
lsm_entry_attr_get_num((uint64_t *)&t, c_ctx->entry,
|
||||
lander_attr_type_entry_type);
|
||||
lsm_entry_attr_get_uint8_t((uint8_t *)&t, c_ctx->entry,
|
||||
lander_attr_type_entry_type);
|
||||
|
||||
if (t == lander_entry_type_redirect) {
|
||||
// For redirects, the URL is stored as an in-memory attribute
|
||||
|
@ -113,6 +84,7 @@ bool lander_get_entry_lsm(event_loop_conn *conn) {
|
|||
c_ctx->entry = NULL;
|
||||
} else {
|
||||
ctx->res.body.expected_len = lsm_entry_data_len(c_ctx->entry);
|
||||
http_res_set_mime_type(&ctx->res, http_mime_txt);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -132,8 +132,8 @@ bool lander_post_redirect_lsm(event_loop_conn *conn) {
|
|||
return true;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lander_entry_type_redirect);
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lander_entry_type_redirect);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -171,8 +171,8 @@ bool lander_post_paste_lsm(event_loop_conn *conn) {
|
|||
return true;
|
||||
}
|
||||
|
||||
lsm_entry_attr_insert_num(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lander_entry_type_paste);
|
||||
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||
lander_entry_type_paste);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue