commit 4e27cca12b2c39438c8e5cc23688897304c7acf5
parent 98e9ba25da17fb106863adade031b026e6636a7f
Author: William Casarin <jb55@jb55.com>
Date: Sun, 12 Jan 2025 17:38:50 -0800
nostrdb: filter: introduce ndb_filter_init_with
Just a static function for now for creating smaller filter sizes. We
will use this for filters that we know are small so that we don't have
to allocate so many pages at once. It's likely the OS will only allocate
a single page anyways, but its nice to be explicit.
Changelog-Added: Add ndb_filter_init_with
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/nostrdb/src/nostrdb.c b/nostrdb/src/nostrdb.c
@@ -676,15 +676,15 @@ ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index)
return els->elements[index];
}
-int ndb_filter_init(struct ndb_filter *filter)
+static int ndb_filter_init_with(struct ndb_filter *filter, int pages)
{
struct cursor cur;
int page_size, elem_pages, data_pages, buf_size;
page_size = 4096; // assuming this, not a big deal if we're wrong
- elem_pages = NDB_FILTER_PAGES / 4;
- data_pages = NDB_FILTER_PAGES - elem_pages;
- buf_size = page_size * NDB_FILTER_PAGES;
+ elem_pages = pages / 4;
+ data_pages = pages - elem_pages;
+ buf_size = page_size * pages;
unsigned char *buf = malloc(buf_size);
if (!buf)
@@ -710,6 +710,10 @@ int ndb_filter_init(struct ndb_filter *filter)
return 1;
}
+int ndb_filter_init(struct ndb_filter *filter) {
+ return ndb_filter_init_with(filter, NDB_FILTER_PAGES);
+}
+
void ndb_filter_destroy(struct ndb_filter *filter)
{
if (filter->elem_buf.start)