Skip to content

Commit

Permalink
filters: remove ndb_filter_group from public API
Browse files Browse the repository at this point in the history
We can just use a list of filters instead when subscribing
  • Loading branch information
jb55 committed Jan 4, 2024
1 parent e749479 commit 5300879
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
25 changes: 18 additions & 7 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
13 changes: 1 addition & 12 deletions src/nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 3 additions & 11 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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)));

Expand Down

0 comments on commit 5300879

Please sign in to comment.