nostrdb

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

commit e851c12abfed3a2387e95713654445206a19a0dc
parent a834a9bc88be0970f46136cbed45b15f56504127
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  7 Aug 2023 11:25:39 -0700

ndb: lmdb init

Initial lmdb initialization. Sets up a database in a test directory, to
be configurable.

Add some initial tests to make sure everything is ok so far.

Diffstat:
Mnostrdb.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++--
Mnostrdb.h | 4++--
Mtest.c | 11+++++++++++
3 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -5,6 +5,7 @@ #include "cursor.h" #include "random.h" #include "sha256.h" +#include "lmdb.h" #include "protected_queue.h" #include <stdlib.h> #include <limits.h> @@ -28,14 +29,62 @@ struct ndb_ingester { }; struct ndb { + MDB_env *env; struct ndb_ingester ingester; // lmdb environ handles, etc }; -int ndb_init(struct ndb **ndb) +// A clustered key with an id and a timestamp +struct ndb_id_ts { + unsigned char id[32]; + uint32_t created; +}; + +static void ndb_make_id_ts(unsigned char *id, uint32_t created, + struct ndb_id_ts *ts) +{ + memcpy(ts->id, id, 32); + ts->created = created; +} + +int ndb_init(struct ndb **ndb, size_t mapsize) { + struct ndb *db; + //MDB_dbi ind_id; // TODO: ind_pk, etc + int rc; + + db = *ndb = calloc(1, sizeof(struct ndb)); + if (*ndb == NULL) { + fprintf(stderr, "ndb_init: malloc failed\n"); + return 0; + } + + if ((rc = mdb_env_create(&db->env))) { + fprintf(stderr, "mdb_env_create failed, error %d\n", rc); + return 0; + } + + if ((rc = mdb_env_set_mapsize(db->env, mapsize))) { + fprintf(stderr, "mdb_env_set_mapsize failed, error %d\n", rc); + return 0; + } + + if ((rc = mdb_env_open(db->env, "./testdata/db", 0, 0664))) { + fprintf(stderr, "mdb_env_open failed, error %d\n", rc); + return 0; + } + // Initialize LMDB environment and spin up threads - return 0; + return 1; +} + +void ndb_destroy(struct ndb *ndb) +{ + if (ndb == NULL) + return; + + mdb_env_close(ndb->env); + free(ndb); } // Process a nostr event, ie: ["EVENT", "subid", {"content":"..."}...] diff --git a/nostrdb.h b/nostrdb.h @@ -129,9 +129,9 @@ int ndb_create_keypair(struct ndb_keypair *key); int ndb_decode_key(const char *secstr, struct ndb_keypair *keypair); // NDB -int ndb_init(struct ndb **); +int ndb_init(struct ndb **ndb, size_t mapsize); int ndb_process_event(struct ndb *, const char *json, int len); -int ndb_destroy(struct ndb *); +void ndb_destroy(struct ndb *); // BUILDER int ndb_parse_json_note(struct ndb_json_parser *, struct ndb_note **); diff --git a/test.c b/test.c @@ -9,6 +9,15 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +static void test_lmdb_put() +{ + struct ndb *ndb; + + // 256MB + assert(ndb_init(&ndb, 2 << 28)); + ndb_destroy(ndb); +} + static void test_basic_event() { unsigned char buf[512]; struct ndb_builder builder, *b = &builder; @@ -475,5 +484,7 @@ int main(int argc, const char *argv[]) { test_queue_thread_safety(); test_queue_boundary_conditions(); + test_lmdb_put(); + printf("All tests passed!\n"); // Print this if all tests pass. }