nostrdb

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

commit e4f9c9269ca1fab7081a03ca0a187ad95dcd9d46
parent 5367d14d7e6566b684f3f0d3fa71cdc00ea50dff
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  3 Jan 2024 14:04:23 -0800

subs: subs and monitor cleanup

We need to free these resources when we're done with them.

Diffstat:
MTODO | 3---
Msrc/nostrdb.c | 34++++++++++++++++++++++++++++++----
Msrc/nostrdb.h | 2+-
Mtest.c | 6+++---
4 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/TODO b/TODO @@ -1,3 +0,0 @@ -ndb_monitor_destroy -cleanup subscriptions -cleanup filter groups in subscriptions diff --git a/src/nostrdb.c b/src/nostrdb.c @@ -177,7 +177,7 @@ struct ndb_ingester { struct ndb_subscription { uint64_t subid; - struct ndb_filter_group filter; + struct ndb_filter_group group; struct prot_queue inbox; }; @@ -520,7 +520,7 @@ void ndb_filter_reset(struct ndb_filter *filter) filter->current = NULL; } -void ndb_filter_free(struct ndb_filter *filter) +void ndb_filter_destroy(struct ndb_filter *filter) { if (filter->elem_buf.start) free(filter->elem_buf.start); @@ -2890,7 +2890,7 @@ static void ndb_notify_subscriptions(struct ndb_monitor *monitor, written = &wrote[k]; note = written->note->note; - if (ndb_filter_group_matches(&sub->filter, note)) { + if (ndb_filter_group_matches(&sub->group, note)) { ndb_debug("pushing note\n"); if (!prot_queue_push(&sub->inbox, &written->note_id)) { ndb_debug("couldn't push note to subscriber"); @@ -3362,6 +3362,31 @@ static void ndb_monitor_init(struct ndb_monitor *monitor) monitor->num_subscriptions = 0; } +void ndb_filter_group_destroy(struct ndb_filter_group *group) +{ + struct ndb_filter *filter; + int i; + for (i = 0; i < group->num_filters; i++) { + filter = group->filters[i]; + ndb_filter_destroy(filter); + } +} + +static void ndb_monitor_destroy(struct ndb_monitor *monitor) +{ + int i; + struct ndb_subscription *sub; + struct ndb_filter_group *group; + + for (i = 0; i < monitor->num_subscriptions; i++) { + sub = &monitor->subscriptions[i]; + group = &sub->group; + + ndb_filter_group_destroy(group); + prot_queue_destroy(&sub->inbox); + } +} + int ndb_init(struct ndb **pndb, const char *filename, const struct ndb_config *config) { struct ndb *ndb; @@ -3409,6 +3434,7 @@ void ndb_destroy(struct ndb *ndb) // ingester depends on writer and must be destroyed first ndb_ingester_destroy(&ndb->ingester); ndb_writer_destroy(&ndb->writer); + ndb_monitor_destroy(&ndb->monitor); mdb_env_close(ndb->lmdb.env); @@ -4944,7 +4970,7 @@ uint64_t ndb_subscribe(struct ndb *ndb, struct ndb_filter_group *group) subid = ++subids; sub->subid = subid; - memcpy(&sub->filter, group, sizeof(*group)); + memcpy(&sub->group, group, sizeof(*group)); // 500k ought to be enough for anyone buflen = sizeof(uint64_t) * 65536; diff --git a/src/nostrdb.h b/src/nostrdb.h @@ -471,7 +471,7 @@ 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 *); int ndb_filter_group_add(struct ndb_filter_group *group, struct ndb_filter *f); -void ndb_filter_free(struct ndb_filter *); +void ndb_filter_destroy(struct ndb_filter *); // SUBSCRIPTIONS uint64_t ndb_subscribe(struct ndb *, struct ndb_filter_group *); diff --git a/test.c b/test.c @@ -106,7 +106,7 @@ static void test_filters() assert(f->current == NULL); assert(ndb_filter_matches(f, note)); - ndb_filter_free(f); + ndb_filter_destroy(f); } static void test_invoice_encoding(const char *bolt11_str) @@ -1243,14 +1243,14 @@ static void test_subscriptions() assert(ndb_process_event(ndb, ev, strlen(ev))); assert(ndb_wait_for_notes(ndb, subid, &note_id, 1) == 1); + assert(note_id > 0); assert(ndb_begin_query(ndb, &txn)); assert((note = ndb_get_note_by_key(&txn, note_id, NULL))); assert(!strcmp(ndb_note_content(note), "test")); ndb_end_query(&txn); - - assert(note_id > 0); + ndb_destroy(ndb); } int main(int argc, const char *argv[]) {