nostrdb

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

commit ec60476693bf9c4d6ef8cb7ac123c34dcbc904eb
parent 61d003518867c27d7a535cdcd2acaab36467f4a5
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  7 Aug 2023 10:04:42 -0700

ndb: add ndb_init and ndb_process_event placeholders

Diffstat:
Mnostrdb.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Mnostrdb.h | 7++++++-
2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -5,6 +5,7 @@ #include "cursor.h" #include "random.h" #include "sha256.h" +#include "protected_queue.h" #include <stdlib.h> #include <limits.h> @@ -22,6 +23,51 @@ struct ndb_json_parser { int num_tokens; }; +struct ndb_ingester { + struct prot_queue inbox; +}; + +struct ndb { + struct ndb_ingester ingester; + // lmdb environ handles, etc +}; + +int ndb_init(struct ndb **ndb) +{ + // Initialize LMDB environment and spin up threads + return 0; +} + +// Process a nostr event, ie: ["EVENT", "subid", {"content":"..."}...] +// +// This function returns as soon as possible, first copying the passed +// json and then queueing it up for processing. Worker threads then take +// the json and process it. +// +// Processing: +// +// 1. The event is parsed into ndb_notes and the signature is validated +// 2. A quick lookup is made on the database to see if we already have +// the note id, if we do we don't need to waste time on json parsing +// or note validation. +// 3. Once validation is done we pass it to the writer queue for writing +// to LMDB. +// +int ndb_process_event(struct ndb *ndb, const char *json, int json_len) +{ + // Since we need to return as soon as possible, and we're not + // making any assumptions about the lifetime of the string, we + // definitely need to copy the json here. In the future once we + // have our thread that manages a websocket connection, we can + // avoid the copy and just use the buffer we get from that + // thread. + char *json_copy = strdup(json); + if (json_copy == NULL) + return 0; + + return 0; +} + static inline int cursor_push_tag(struct cursor *cur, struct ndb_tag *tag) { return cursor_push_u16(cur, tag->count); diff --git a/nostrdb.h b/nostrdb.h @@ -8,6 +8,7 @@ #define NDB_PACKED_ID 0x2 struct ndb_json_parser; +struct ndb; // To-client event types enum tce_type { @@ -127,8 +128,12 @@ int ndb_sign_id(struct ndb_keypair *keypair, unsigned char id[32], unsigned char int ndb_create_keypair(struct ndb_keypair *key); int ndb_decode_key(const char *secstr, struct ndb_keypair *keypair); -// BUILDER +// NDB +int ndb_init(struct ndb **); +int ndb_process_event(struct ndb *, const char *json, int len); +int ndb_destroy(struct ndb *); +// BUILDER int ndb_parse_json_note(struct ndb_json_parser *, struct ndb_note **); int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce, unsigned char *buf, int bufsize); int ndb_note_from_json(const char *json, int len, struct ndb_note **, unsigned char *buf, int buflen);