commit 8b03ed61754e94bf681f5c6b0b6c6c9863999367
parent 6cd7b945ca7e91d328152ce4c4da376dbe807880
Author: William Casarin <jb55@jb55.com>
Date: Wed, 3 Jan 2024 17:57:37 -0800
nostrdb/filters: remove ndb_filter_group from public API
We can just use a list of filters instead when subscribing
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/nostrdb/src/nostrdb.c b/nostrdb/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;
@@ -5017,7 +5024,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;
@@ -5036,8 +5043,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/nostrdb/src/nostrdb.h b/nostrdb/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);