nostrdb

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

commit 8189014adc4495c48992e2213ede97390b9f6ebd
parent 407db76f0299506ede769891528005d899d458b3
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 26 Aug 2023 18:09:04 -0700

tsid: return length

flatbuffers needs this

Diffstat:
Mnostrdb.c | 15++++++++++-----
Mnostrdb.h | 4++--
Mtest.c | 2+-
3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -276,11 +276,14 @@ cleanup: } static void *ndb_lookup_tsid(struct ndb *ndb, enum ndb_dbs ind, - enum ndb_dbs store, const unsigned char *pk) + enum ndb_dbs store, const unsigned char *pk, + size_t *len) { MDB_val k, v; MDB_txn *txn; void *res = NULL; + if (len) + *len = 0; if (mdb_txn_begin(ndb->lmdb.env, 0, 0, &txn)) { ndb_debug("ndb_get_note_by_id: mdb_txn_begin failed\n"); @@ -298,19 +301,21 @@ static void *ndb_lookup_tsid(struct ndb *ndb, enum ndb_dbs ind, } res = v.mv_data; + if (len) + *len = v.mv_size; cleanup: mdb_txn_abort(txn); return res; } -void *ndb_get_profile_by_pubkey(struct ndb *ndb, const unsigned char *pk) +void *ndb_get_profile_by_pubkey(struct ndb *ndb, const unsigned char *pk, size_t *len) { - return ndb_lookup_tsid(ndb, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk); + return ndb_lookup_tsid(ndb, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk, len); } -struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id) +struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id, size_t *len) { - return ndb_lookup_tsid(ndb, NDB_DB_NOTE_ID, NDB_DB_NOTE, id); + return ndb_lookup_tsid(ndb, NDB_DB_NOTE_ID, NDB_DB_NOTE, id, len); } static int ndb_has_note(MDB_txn *txn, struct ndb_lmdb *lmdb, const unsigned char *id) diff --git a/nostrdb.h b/nostrdb.h @@ -155,8 +155,8 @@ int ndb_note_verify(void *secp_ctx, unsigned char pubkey[32], unsigned char id[3 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); -void *ndb_get_profile_by_pubkey(struct ndb *, const unsigned char *pubkey); -struct ndb_note *ndb_get_note_by_id(struct ndb *, const unsigned char *id); +void *ndb_get_profile_by_pubkey(struct ndb *, const unsigned char *pubkey, size_t *len); +struct ndb_note *ndb_get_note_by_id(struct ndb *, const unsigned char *id, size_t *len); void ndb_destroy(struct ndb *); // BUILDER diff --git a/test.c b/test.c @@ -39,7 +39,7 @@ static void test_load_profiles() 0xd2, 0xb4, 0xd1, 0x3a, 0x55, 0x43, 0x09, 0x07 }; const char *expected_content = "{\"website\":\"selenejin.com\",\"lud06\":\"\",\"nip05\":\"selenejin@BitcoinNostr.com\",\"picture\":\"https://nostr.build/i/3549697beda0fe1f4ae621f359c639373d92b7c8d5c62582b656c5843138c9ed.jpg\",\"display_name\":\"Selene Jin\",\"about\":\"INTJ | Founding Designer @Blockstream\",\"name\":\"SeleneJin\"}"; - struct ndb_note *note = ndb_get_note_by_id(ndb, id); + struct ndb_note *note = ndb_get_note_by_id(ndb, id, NULL); assert(note != NULL); assert(!strcmp(ndb_note_content(note), expected_content));