nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 9a3b613bd44e9d6d7c0ab592934b2dd6ba904e05
parent 3d24351062a7ffa03c07a04adbd84c864839aae1
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 13 Jan 2025 11:25:10 -0800

filter: fix ndb_filter_init_with and make public

This fixes an allocation issue with ndb_filter_init_with for small
page sizes. instead of allocating the buffer around pages, we allocate
based on total buffer size.

Fixes: f7aac3215575 ("filter: introduce ndb_filter_init_with")

Diffstat:
Msrc/nostrdb.c | 13+++++++------
Msrc/nostrdb.h | 6++++++
2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/nostrdb.c b/src/nostrdb.c @@ -685,15 +685,16 @@ ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index) return els->elements[index]; } -static int ndb_filter_init_with(struct ndb_filter *filter, int pages) +int ndb_filter_init_with(struct ndb_filter *filter, int pages) { struct cursor cur; - int page_size, elem_pages, data_pages, buf_size; + int page_size, elem_size, data_size, buf_size; page_size = 4096; // assuming this, not a big deal if we're wrong - elem_pages = pages / 4; - data_pages = pages - elem_pages; + buf_size = page_size * pages; + elem_size = buf_size / 4; + data_size = buf_size - elem_size; unsigned char *buf = malloc(buf_size); if (!buf) @@ -702,8 +703,8 @@ static int ndb_filter_init_with(struct ndb_filter *filter, int pages) // init memory arena for the cursor make_cursor(buf, buf + buf_size, &cur); - cursor_slice(&cur, &filter->elem_buf, page_size * elem_pages); - cursor_slice(&cur, &filter->data_buf, page_size * data_pages); + cursor_slice(&cur, &filter->elem_buf, elem_size); + cursor_slice(&cur, &filter->data_buf, data_size); // make sure we are fully allocated assert(cur.p == cur.end); diff --git a/src/nostrdb.h b/src/nostrdb.h @@ -501,6 +501,12 @@ int ndb_builder_push_tag_str(struct ndb_builder *builder, const char *str, int l // FILTERS int ndb_filter_init(struct ndb_filter *); + +/// Allocate a filter with a fixed sized buffer (where pages is number of 4096-byte sized blocks) +/// You can set pages to 1 if you know you are constructing small filters +// TODO: replace this with passed-in buffers +int ndb_filter_init_with(struct ndb_filter *filter, int pages); + int ndb_filter_add_id_element(struct ndb_filter *, const unsigned char *id); int ndb_filter_add_int_element(struct ndb_filter *, uint64_t integer); int ndb_filter_add_str_element(struct ndb_filter *, const char *str);