nostrdb

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

commit 53008790f983ee53243498f1dc75aaf63f6f8440
parent e749479ef157fb5b8766959f5870741772d69fbc
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  3 Jan 2024 17:57:37 -0800

filters: remove ndb_filter_group from public API

We can just use a list of filters instead when subscribing

Diffstat:
Msrc/nostrdb.c | 25++++++++++++++++++-------
Msrc/nostrdb.h | 13+------------
Mtest.c | 14+++-----------
3 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/src/nostrdb.c b/src/nostrdb.c @@ -35,6 +35,8 @@ static const int THREAD_QUEUE_BATCH = 4096; // maximum number of active subscriptions #define MAX_SUBSCRIPTIONS 32 +#define MAX_SCAN_CURSORS 12 +#define MAX_FILTERS 16 // the maximum size of inbox queues static const int DEFAULT_QUEUE_SIZE = 1000000; @@ -175,6 +177,11 @@ struct ndb_ingester { ndb_ingest_filter_fn filter; }; +struct ndb_filter_group { + struct ndb_filter *filters[MAX_FILTERS]; + int num_filters; +}; + struct ndb_subscription { uint64_t subid; struct ndb_filter_group group; @@ -213,7 +220,7 @@ struct ndb_scan_cursor { // same idea as DBScan in strfry struct ndb_dbscan { - struct ndb_scan_cursor cursors[12]; + struct ndb_scan_cursor cursors[MAX_SCAN_CURSORS]; int num_cursors; }; @@ -916,15 +923,15 @@ void ndb_filter_end_field(struct ndb_filter *filter) } -void ndb_filter_group_init(struct ndb_filter_group *group) +static void ndb_filter_group_init(struct ndb_filter_group *group) { group->num_filters = 0; } -int ndb_filter_group_add(struct ndb_filter_group *group, +static int ndb_filter_group_add(struct ndb_filter_group *group, struct ndb_filter *filter) { - if (group->num_filters + 1 > NDB_MAX_FILTERS) + if (group->num_filters + 1 > MAX_FILTERS) return 0; group->filters[group->num_filters++] = filter; @@ -5018,7 +5025,7 @@ int ndb_wait_for_notes(struct ndb *ndb, uint64_t subid, uint64_t *note_ids, return prot_queue_pop_all(&sub->inbox, note_ids, note_id_capacity); } -uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter_group *group) +uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter *filters, int num_filters) { static uint64_t subids = 0; struct ndb_subscription *sub; @@ -5037,8 +5044,12 @@ uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter_group *group) subid = ++subids; sub->subid = subid; - memcpy(&sub->group, group, sizeof(*group)); - + ndb_filter_group_init(&sub->group); + for (index = 0; index < num_filters; index++) { + if (!ndb_filter_group_add(&sub->group, &filters[index])) + return 0; + } + // 500k ought to be enough for anyone buflen = sizeof(uint64_t) * 65536; buf = malloc(buflen); diff --git a/src/nostrdb.h b/src/nostrdb.h @@ -4,9 +4,6 @@ #include <inttypes.h> #include "cursor.h" -// how many filters are allowed in a filter group -#define NDB_MAX_FILTERS 16 - // maximum number of filters allowed in a filter group #define NDB_PACKED_STR 0x1 #define NDB_PACKED_ID 0x2 @@ -30,7 +27,6 @@ struct ndb_blocks; struct ndb_block; struct ndb_note; struct ndb_tag; -struct ndb_filter_group; struct ndb_tags; struct ndb_lmdb; union ndb_packed_str; @@ -241,11 +237,6 @@ struct ndb_filter { struct ndb_filter_elements *elements[NDB_NUM_FILTERS]; }; -struct ndb_filter_group { - struct ndb_filter *filters[NDB_MAX_FILTERS]; - int num_filters; -}; - struct ndb_config { int flags; int ingester_threads; @@ -470,12 +461,10 @@ int ndb_filter_start_generic_field(struct ndb_filter *, char tag); int ndb_filter_matches(struct ndb_filter *, struct ndb_note *); void ndb_filter_reset(struct ndb_filter *); void ndb_filter_end_field(struct ndb_filter *); -void ndb_filter_group_init(struct ndb_filter_group *group); -int ndb_filter_group_add(struct ndb_filter_group *group, struct ndb_filter *f); void ndb_filter_destroy(struct ndb_filter *); // SUBSCRIPTIONS -uint64_t ndb_subscribe(struct ndb *, struct ndb_filter_group *); +uint64_t ndb_subscribe(struct ndb *, struct ndb_filter *, int num_filters); int ndb_wait_for_notes(struct ndb *, uint64_t subid, uint64_t *note_ids, int note_id_capacity); int ndb_unsubscribe(int subid); diff --git a/test.c b/test.c @@ -220,15 +220,13 @@ static void test_reaction_counter() struct ndb_config config; ndb_default_config(&config); static const int num_reactions = 3; - struct ndb_filter_group group; uint64_t note_ids[num_reactions], subid; assert(ndb_init(&ndb, test_dir, &config)); read_file("testdata/reactions.json", (unsigned char*)json, alloc_size, &written); - group.num_filters = 0; - assert((subid = ndb_subscribe(ndb, &group))); + assert((subid = ndb_subscribe(ndb, NULL, 0))); assert(ndb_process_client_events(ndb, json, written)); @@ -299,7 +297,6 @@ static void test_profile_updates() size_t len; struct ndb *ndb; struct ndb_config config; - struct ndb_filter_group group; struct ndb_txn txn; uint64_t key, subid; uint64_t note_ids[num_notes]; @@ -310,8 +307,7 @@ static void test_profile_updates() ndb_default_config(&config); assert(ndb_init(&ndb, test_dir, &config)); - ndb_filter_group_init(&group); - subid = ndb_subscribe(ndb, &group); + subid = ndb_subscribe(ndb, NULL, 0); ndb_debug("testing profile updates\n"); read_file("testdata/profile-updates.json", (unsigned char*)json, alloc_size, &written); @@ -1234,7 +1230,6 @@ static void test_subscriptions() struct ndb_filter filter, *f = &filter; uint64_t subid; uint64_t note_id = 0; - struct ndb_filter_group group; struct ndb_txn txn; struct ndb_note *note; ndb_default_config(&config); @@ -1248,10 +1243,7 @@ static void test_subscriptions() assert(ndb_filter_add_int_element(f, 1337)); ndb_filter_end_field(f); - ndb_filter_group_init(&group); - ndb_filter_group_add(&group, f); - - assert((subid = ndb_subscribe(ndb, &group))); + assert((subid = ndb_subscribe(ndb, NULL, 0))); assert(ndb_process_event(ndb, ev, strlen(ev)));