nostrdb

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

commit 808590bb4aaa0144c7e43ccb8fc7d244b232df38
parent be06e3d1a622e436050a66df1b200f7441242c07
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 25 Aug 2023 18:03:49 -0700

ndb: add db directory argument

Diffstat:
Mnostrdb.c | 8++++----
Mnostrdb.h | 6+++++-
Mtest.c | 8+++++---
3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -782,7 +782,7 @@ static int ndb_ingester_queue_event(struct ndb_ingester *ingester, return threadpool_dispatch(&ingester->tp, &msg); } -static int ndb_init_lmdb(struct ndb_lmdb *lmdb, size_t mapsize) +static int ndb_init_lmdb(const char *filename, struct ndb_lmdb *lmdb, size_t mapsize) { int rc; MDB_txn *txn; @@ -802,7 +802,7 @@ static int ndb_init_lmdb(struct ndb_lmdb *lmdb, size_t mapsize) return 0; } - if ((rc = mdb_env_open(lmdb->env, "./testdata/db", 0, 0664))) { + if ((rc = mdb_env_open(lmdb->env, filename, 0, 0664))) { fprintf(stderr, "mdb_env_open failed, error %d\n", rc); return 0; } @@ -857,7 +857,7 @@ static int ndb_init_lmdb(struct ndb_lmdb *lmdb, size_t mapsize) return 1; } -int ndb_init(struct ndb **pndb, size_t mapsize, int ingester_threads) +int ndb_init(struct ndb **pndb, const char *filename, size_t mapsize, int ingester_threads) { struct ndb *ndb; //MDB_dbi ind_id; // TODO: ind_pk, etc @@ -868,7 +868,7 @@ int ndb_init(struct ndb **pndb, size_t mapsize, int ingester_threads) return 0; } - if (!ndb_init_lmdb(&ndb->lmdb, mapsize)) + if (!ndb_init_lmdb(filename, &ndb->lmdb, mapsize)) return 0; if (!ndb_writer_init(&ndb->writer, &ndb->lmdb)) { diff --git a/nostrdb.h b/nostrdb.h @@ -18,6 +18,10 @@ struct ndb_json_parser; struct ndb; +struct ndb_t { + struct ndb *ndb; +}; + // To-client event types enum tce_type { NDB_TCE_EVENT = 0x1, @@ -148,7 +152,7 @@ int ndb_decode_key(const char *secstr, struct ndb_keypair *keypair); int ndb_note_verify(void *secp_ctx, unsigned char pubkey[32], unsigned char id[32], unsigned char signature[64]); // NDB -int ndb_init(struct ndb **ndb, size_t mapsize, int ingester_threads); +int ndb_init(struct ndb **ndb, const char *dbdir, size_t mapsize, int ingester_threads); int ndb_process_event(struct ndb *, const char *json, int len); int ndb_process_events(struct ndb *, const char *ldjson, size_t len); int ndb_get_profile(struct ndb *, unsigned char pubkey[32], void **out); diff --git a/test.c b/test.c @@ -11,6 +11,8 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +static const char *test_dir = "./testdata/db"; + static void test_load_profiles() { static const int alloc_size = 1024 * 1024; @@ -22,7 +24,7 @@ static void test_load_profiles() mapsize = 1024 * 1024 * 100; ingester_threads = 1; - assert(ndb_init(&ndb, mapsize, ingester_threads)); + assert(ndb_init(&ndb, test_dir, mapsize, ingester_threads)); read_file("testdata/profiles.json", (unsigned char*)json, alloc_size, &written); @@ -30,7 +32,7 @@ static void test_load_profiles() ndb_destroy(ndb); - assert(ndb_init(&ndb, mapsize, ingester_threads)); + assert(ndb_init(&ndb, test_dir, mapsize, ingester_threads)); unsigned char id[32] = { 0x22, 0x05, 0x0b, 0x6d, 0x97, 0xbb, 0x9d, 0xa0, 0x9e, 0x90, 0xed, 0x0c, 0x6d, 0xd9, 0x5e, 0xed, 0x1d, 0x42, 0x3e, 0x27, 0xd5, 0xcb, 0xa5, 0x94, @@ -51,7 +53,7 @@ static void test_fuzz_events() { struct ndb *ndb; const char *str = "[\"EVENT\"\"\"{\"content\"\"created_at\":0 \"id\"\"5086a8f76fe1da7fb56a25d1bebbafd70fca62e36a72c6263f900ff49b8f8604\"\"kind\":0 \"pubkey\":9c87f94bcbe2a837adc28d46c34eeaab8fc2e1cdf94fe19d4b99ae6a5e6acedc \"sig\"\"27374975879c94658412469cee6db73d538971d21a7b580726a407329a4cafc677fb56b946994cea59c3d9e118fef27e4e61de9d2c46ac0a65df14153 ea93cf5\"\"tags\"[[][\"\"]]}]"; - ndb_init(&ndb, 1024 * 1024, 1); + ndb_init(&ndb, test_dir, 1024 * 1024, 1); ndb_process_event(ndb, str, strlen(str)); ndb_destroy(ndb); }