diff --git a/CHANGELOG.md b/CHANGELOG.md index 388f188..11ae703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * LSM * Binary tree iterators * Trie iterators - * Store iterators ## [0.2.0](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.0) diff --git a/Makefile b/Makefile index 5d455f5..0ee4c84 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ run: $(BIN) valgrind: $(BIN) LANDER_API_KEY=test \ LANDER_DATA_DIR=data \ - valgrind --track-origins=yes --leak-check=full '$(BUILD_DIR)/$(BIN_FILENAME)' + valgrind --track-origins=yes '$(BUILD_DIR)/$(BIN_FILENAME)' .PHONY: test test: $(TARGETS_TEST) diff --git a/include/lander.h b/include/lander.h index 7fa3199..f670234 100644 --- a/include/lander.h +++ b/include/lander.h @@ -13,7 +13,6 @@ typedef struct lander_gctx { lsm_store *store; struct { ltm_template *paste; - ltm_template *index; } templates; } lander_gctx; @@ -33,7 +32,6 @@ typedef enum lander_entry_type : uint8_t { lander_entry_type_redirect = 0, lander_entry_type_paste = 1, lander_entry_type_file = 2, - lander_entry_type_index = 3, } lander_entry_type; void *lander_gctx_init(); @@ -44,7 +42,7 @@ void lander_ctx_reset(lander_ctx *ctx); void lander_ctx_free(lander_ctx *ctx); -lnm_http_step_err lander_get_root(lnm_http_conn *conn); +lnm_http_step_err lander_get_index(lnm_http_conn *conn); lnm_http_step_err lander_get_entry(lnm_http_conn *conn); @@ -60,8 +58,6 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn); lnm_http_step_err lander_post_file(lnm_http_conn *conn); -lnm_http_step_err lander_post_index(lnm_http_conn *conn); - /** * Store the requested header as an attribute, if it's present. */ diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index c16340b..c680c43 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -98,11 +98,6 @@ lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type, lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle, uint8_t type); -/** - * Retrieve a pointer to the entry's key. - */ -void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle); - /** * A store consisting of LSM entries. * @@ -233,40 +228,4 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf, */ uint64_t lsm_entry_data_len(lsm_entry_handle *handle); -/** - * Represents an in-flight iterator over an LSM store. - */ -typedef struct lsm_store_iterator lsm_store_iterator; - -/** - * Initialize an iterator to iterate over all entries with keys starting - * with the given prefix. - * - * @param out pointer to store iterator pointer in - * @param trie trie to iterate - * @param prefix prefix of the keys; a zero-length string means iterating over - * the entire trie; NULL is interpreted as a zero-length string - */ -lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store, - const lsm_str *prefix); - -/** - * Advance the iterator, returning a read handle to the next entry. The caller - * is responsible for closing this handle. - */ -lsm_error lsm_store_iter_next_read(lsm_entry_handle **out, - lsm_store_iterator *iter); - -/** - * Advance the iterator, returning a write handle to the next entry. The caller - * is responsible for closing this handle. - */ -/* lsm_error lsm_store_iter_next_write(lsm_entry_handle **out, - * lsm_store_iterator *iter); */ - -/** - * Free the given iterator. - */ -void lsm_store_iter_free(lsm_store_iterator *iter); - #endif diff --git a/lsm/include/lsm/str.h b/lsm/include/lsm/str.h index 147e4ed..b3cf965 100644 --- a/lsm/include/lsm/str.h +++ b/lsm/include/lsm/str.h @@ -178,15 +178,6 @@ lsm_error lsm_str_split(lsm_str *s, lsm_str *s2, uint64_t index); * @param s string to append s2 to * @param s2 string to append to s */ -lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2); - -/** - * Same as `lsm_str_append`, but it takes a C-style string instead. - * - * @param s string to append c_str to - * @param c_str char buffer to append - * @param len length of c_str - */ -lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len); +lsm_error lsm_str_append(lsm_str *s, lsm_str *s2); #endif diff --git a/lsm/src/_include/lsm/store_internal.h b/lsm/src/_include/lsm/store_internal.h index e853308..e446a0d 100644 --- a/lsm/src/_include/lsm/store_internal.h +++ b/lsm/src/_include/lsm/store_internal.h @@ -34,11 +34,6 @@ typedef struct lsm_entry { uint64_t idx_file_offset; } lsm_entry; -struct lsm_store_iterator { - lsm_trie_iterator *iter; - lsm_store *store; -}; - /** * Allocate and initialize a new lsm_entry object. * diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 101641b..63d90a7 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -316,7 +316,3 @@ lsm_error lsm_entry_data_open_read(lsm_entry_handle *handle) { return lsm_error_ok; } - -void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle) { - *out = handle->wrapper->entry->key; -} diff --git a/lsm/src/store/lsm_store_iter.c b/lsm/src/store/lsm_store_iter.c deleted file mode 100644 index 3879d5e..0000000 --- a/lsm/src/store/lsm_store_iter.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "lsm/store.h" -#include "lsm/store_internal.h" - -lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store, - const lsm_str *prefix) { - lsm_trie_iterator *trie_iter; - LSM_RES(lsm_trie_iter(&trie_iter, store->trie, prefix)); - - lsm_store_iterator *iter = calloc(1, sizeof(lsm_store_iterator)); - - if (iter == NULL) { - lsm_trie_iter_free(trie_iter); - - return lsm_error_failed_alloc; - } - - iter->iter = trie_iter; - iter->store = store; - - *out = iter; - - return lsm_error_ok; -} - -lsm_error lsm_store_iter_next_read(lsm_entry_handle **out, - lsm_store_iterator *iter) { - lsm_entry_wrapper *wrapper = NULL; - - // Traverse through the trie until a node is found with a filled data field - do { - // Exits function if iterator is done - LSM_RES(lsm_trie_iter_next((void **)&wrapper, iter->iter)); - - // TODO error handling? - pthread_rwlock_rdlock(&wrapper->lock); - - if (wrapper->entry == NULL) { - pthread_rwlock_unlock(&wrapper->lock); - - wrapper = NULL; - } - } while (wrapper == NULL); - - lsm_entry_handle *handle; - LSM_RES2(lsm_entry_handle_init(&handle), - pthread_rwlock_unlock(&wrapper->lock)); - - handle->wrapper = wrapper; - handle->store = iter->store; - *out = handle; - - return lsm_error_ok; -} - -void lsm_store_iter_free(lsm_store_iterator *iter) { - lsm_trie_iter_free(iter->iter); - free(iter); -} diff --git a/lsm/src/str/lsm_str.c b/lsm/src/str/lsm_str.c index e152fc1..217bad7 100644 --- a/lsm/src/str/lsm_str.c +++ b/lsm/src/str/lsm_str.c @@ -215,15 +215,15 @@ bool lsm_str_eq(const lsm_str *s1, const lsm_str *s2) { return memcmp(lsm_str_ptr(s1), lsm_str_ptr(s2), s1->len) == 0; } -lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len) { - if (len == 0) { +lsm_error lsm_str_append(lsm_str *s, lsm_str *s2) { + if (s2->len == 0) { return lsm_error_ok; } - uint64_t new_len = s->len + len; + uint64_t new_len = s->len + s2->len; if (new_len <= 8) { - memcpy(&s->data.val[s->len], c_str, len); + memcpy(&s->data.val[s->len], s2->data.val, s2->len); } else { char *buf; @@ -243,15 +243,11 @@ lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len) { } } - memcpy(&buf[s->len], c_str, len); + memcpy(&buf[s->len], lsm_str_ptr(s2), s2->len); s->data.ptr = buf; } - s->len += len; + s->len += s2->len; return lsm_error_ok; } - -lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2) { - return lsm_str_append_c(s, lsm_str_ptr(s2), s2->len); -} diff --git a/src/lander/lander.c b/src/lander/lander.c index 0225837..203d71e 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -33,7 +33,7 @@ void lander_ctx_reset(lander_ctx *ctx) { if (ctx->instance != NULL) { ltm_instance_free(ctx->instance); - + ctx->instance = NULL; } } diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index 1cbc772..d59d5e7 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -20,7 +20,7 @@ static const char index_page[] = " \n" "\n"; -lnm_http_step_err lander_get_root(lnm_http_conn *conn) { +lnm_http_step_err lander_get_index(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lnm_http_res_body_set_buf(&ctx->res, (char *)index_page, @@ -73,9 +73,7 @@ lnm_err lander_entry_data_streamer(uint64_t *written, char *buf, return lnm_err_ok; } -lnm_err lander_template_streamer(size_t *written, char *buf, - lnm_http_conn *conn, uint64_t offset, - uint64_t len) { +lnm_err lander_template_streamer(size_t *written, char *buf, lnm_http_conn *conn, uint64_t offset, uint64_t len) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; @@ -86,10 +84,9 @@ lnm_err lander_template_streamer(size_t *written, char *buf, return lnm_err_ok; } -ltm_err lander_data_to_template(size_t *written, char *buf, size_t len, - void *data) { +ltm_err lander_data_to_template(size_t *written, char *buf, size_t len, void *data) { lsm_entry_handle *entry = data; - + lsm_entry_data_read(written, buf, entry, len); return ltm_err_ok; @@ -101,11 +98,9 @@ lnm_http_step_err lander_get_paste(lnm_http_conn *conn) { lander_ctx *c_ctx = ctx->c; ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.paste); - /* ltm_instance_block_add_var(c_ctx->instance, ltm_instance_block_type_buf, - * lsm_str_ptr(c_ctx->entry)) */ - ltm_instance_block_add_var_fn(c_ctx->instance, "paste", - lander_data_to_template, c_ctx->entry, - lsm_entry_data_len(c_ctx->entry)); + /* ltm_instance_block_add_var(c_ctx->instance, ltm_instance_block_type_buf, lsm_str_ptr(c_ctx->entry)) */ + ltm_instance_block_add_var_fn(c_ctx->instance, "paste", lander_data_to_template, c_ctx->entry, lsm_entry_data_len(c_ctx->entry)); + lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer, ltm_instance_size(c_ctx->instance)); @@ -143,93 +138,6 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) { return lnm_http_step_err_done; } -lnm_http_step_err lander_get_index(lnm_http_conn *conn) { - lnm_http_loop_ctx *ctx = conn->ctx; - lander_gctx *c_gctx = ctx->g->c; - lander_ctx *c_ctx = ctx->c; - - ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.index); - - const lsm_str *key; - lsm_entry_key(&key, c_ctx->entry); - - ltm_instance_block_add_var(c_ctx->instance, "title_key", - ltm_instance_block_type_buf, - (void *)lsm_str_ptr(key), lsm_str_len(key)); - - lsm_str *prefix; - lsm_str_init_copy_n(&prefix, lsm_str_ptr(key), lsm_str_len(key)); - lsm_str_append_c(prefix, "_", 1); - - uint64_t prefix_len = lsm_str_len(prefix); - - lsm_store_iterator *iter; - lsm_store_iter(&iter, c_gctx->store, prefix); - - lsm_entry_handle *handle; - while (lsm_store_iter_next_read(&handle, iter) == lsm_error_ok) { - const lsm_str *entry_key; - lsm_entry_key(&entry_key, handle); - - lander_entry_type t; - lsm_entry_attr_get_uint8_t((uint8_t *)&t, handle, - lander_attr_type_entry_type); - - ltm_instance *nested; - - switch (t) { - case lander_entry_type_redirect: - ltm_instance_block_add_nested(&nested, c_ctx->instance, "redirect"); - - ltm_instance_block_add_var( - nested, "full_key", ltm_instance_block_type_buf, - (void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key)); - ltm_instance_block_add_var(nested, "short_key", - ltm_instance_block_type_buf, - (void *)(lsm_str_ptr(entry_key) + prefix_len), - lsm_str_len(entry_key) - prefix_len); - break; - case lander_entry_type_file: - case lander_entry_type_paste: - ltm_instance_block_add_nested(&nested, c_ctx->instance, - t == lander_entry_type_file ? "file" - : "paste"); - - ltm_instance_block_add_var( - nested, "full_key", ltm_instance_block_type_buf, - (void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key)); - - // Add the filename as the link name if known, or just the key otherwise - lsm_str *filename; - if (lsm_entry_attr_get(&filename, handle, lander_attr_type_file_name) == - lsm_error_ok) { - ltm_instance_block_add_var( - nested, "filename", ltm_instance_block_type_buf, - (void *)lsm_str_ptr(filename), lsm_str_len(filename)); - } else { - ltm_instance_block_add_var( - nested, "filename", ltm_instance_block_type_buf, - (void *)(lsm_str_ptr(entry_key) + prefix_len), - lsm_str_len(entry_key) - prefix_len); - } - break; - } - - // TODO this should be done after writing the template - lsm_entry_close(handle); - } - - lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer, - ltm_instance_size(c_ctx->instance)); - lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html", - false); - - lsm_store_iter_free(iter); - lsm_str_free(prefix); - - return lnm_http_step_err_done; -} - lnm_http_step_err lander_get_entry(lnm_http_conn *conn) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; @@ -271,9 +179,6 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) { case lander_entry_type_file: res = lander_get_file(conn); break; - case lander_entry_type_index: - res = lander_get_index(conn); - break; } return res; diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index d30eb69..1244cb7 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -124,21 +124,3 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) { return lnm_http_step_err_done; } - -lnm_http_step_err lander_post_index(lnm_http_conn *conn) { - lnm_http_loop_ctx *ctx = conn->ctx; - lander_ctx *c_ctx = ctx->c; - - if (!lander_insert_entry(ctx)) { - return lnm_http_step_err_res; - } - - lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type, - lander_entry_type_index); - /* lander_header_to_attr(ctx, "X-Lander-Content-Type", */ - /* lander_attr_type_content_type); */ - /* lander_header_to_attr(ctx, "X-Lander-Filename", - * lander_attr_type_file_name); */ - - return lnm_http_step_err_done; -} diff --git a/src/main.c b/src/main.c index c2dea68..5a6245f 100644 --- a/src/main.c +++ b/src/main.c @@ -10,40 +10,12 @@ #include "log.h" const char *lander_server = "lander/" LANDER_VERSION; -const char *paste_template = - "\n" - "\n" - "\n" - "\n" - "

{{ header }}

{{ paste }}
"; - -const char *index_template = - "\n" - "\n" - "

Index /{{ title_key }}

\n" - "

Redirects

\n" - "\n" - "

Files

" - "\n" - "

Pastes

\n" - "\n" - "\n"; +const char *paste_template = + "\n" + "\n" + "\n" + "\n" + "

{{ header }}

{{ paste }}
"; lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_loop *hl; @@ -55,7 +27,7 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_loop_set_api_key(hl, api_key); lnm_http_loop_set_server(hl, lander_server); - lnm_http_step_init(&step, lander_get_root); + lnm_http_step_init(&step, lander_get_index); lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step); lnm_http_loop_route_add(hl, route); @@ -85,14 +57,6 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_step_append(&step, step, lander_stream_body_to_entry); lnm_http_loop_route_add(hl, route); - lnm_http_step_init(&step, lnm_http_loop_step_auth); - lnm_http_route_init_regex(&route, lnm_http_method_post, "^/i(l?)/([^/]+)$", 2, - step); - lnm_http_step_append(&step, step, lander_post_index); - /* lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf); */ - /* lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr); */ - lnm_http_loop_route_add(hl, route); - lnm_http_step_init(&step, lnm_http_loop_step_auth); lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1, step); @@ -136,7 +100,6 @@ int main() { c_gctx->data_dir = data_dir_s; ltm_template_compile(&c_gctx->templates.paste, paste_template); - ltm_template_compile(&c_gctx->templates.index, index_template); lsm_str *data_dir; lsm_str_init_copy(&data_dir, (char *)data_dir_s);