nostrdb

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

commit a80b7c5d9ccf2540b07265ca36c2b810682ef1f6
parent ac7abb0cdcaeeecb9f2b2afcf995eef1fa567519
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  6 Sep 2023 11:21:21 -0700

return primkey keys on profile and note index queries

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

diff --git a/nostrdb.c b/nostrdb.c @@ -314,7 +314,7 @@ static void *ndb_lookup_by_key(struct ndb_txn *txn, uint64_t key, static void *ndb_lookup_tsid(struct ndb_txn *txn, enum ndb_dbs ind, enum ndb_dbs store, const unsigned char *pk, - size_t *len) + size_t *len, uint64_t *primkey) { MDB_val k, v; void *res = NULL; @@ -326,6 +326,9 @@ static void *ndb_lookup_tsid(struct ndb_txn *txn, enum ndb_dbs ind, return 0; } + if (primkey) + *primkey = *(uint64_t*)k.mv_data; + if (mdb_get(txn->mdb_txn, txn->ndb->lmdb.dbs[store], &k, &v)) { ndb_debug("ndb_get_profile_by_pubkey: mdb_get note failed\n"); return 0; @@ -338,14 +341,14 @@ static void *ndb_lookup_tsid(struct ndb_txn *txn, enum ndb_dbs ind, return res; } -void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pk, size_t *len) +void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pk, size_t *len, uint64_t *key) { - return ndb_lookup_tsid(txn, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk, len); + return ndb_lookup_tsid(txn, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk, len, key); } -struct ndb_note *ndb_get_note_by_id(struct ndb_txn *txn, const unsigned char *id, size_t *len) +struct ndb_note *ndb_get_note_by_id(struct ndb_txn *txn, const unsigned char *id, size_t *len, uint64_t *key) { - return ndb_lookup_tsid(txn, NDB_DB_NOTE_ID, NDB_DB_NOTE, id, len); + return ndb_lookup_tsid(txn, NDB_DB_NOTE_ID, NDB_DB_NOTE, id, len, key); } struct ndb_note *ndb_get_note_by_key(struct ndb_txn *txn, uint64_t key, size_t *len) @@ -353,6 +356,11 @@ struct ndb_note *ndb_get_note_by_key(struct ndb_txn *txn, uint64_t key, size_t * return ndb_lookup_by_key(txn, key, NDB_DB_NOTE, len); } +struct ndb_note *ndb_get_profile_by_key(struct ndb_txn *txn, uint64_t key, size_t *len) +{ + return ndb_lookup_by_key(txn, key, NDB_DB_PROFILE, len); +} + static int ndb_has_note(MDB_txn *txn, struct ndb_lmdb *lmdb, const unsigned char *id) { MDB_val val; diff --git a/nostrdb.h b/nostrdb.h @@ -163,8 +163,8 @@ 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_begin_query(struct ndb *, struct ndb_txn *); void ndb_end_query(struct ndb_txn *); -void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pubkey, size_t *len); -struct ndb_note *ndb_get_note_by_id(struct ndb_txn *txn, const unsigned char *id, size_t *len); +void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pubkey, size_t *len, uint64_t *primkey); +struct ndb_note *ndb_get_note_by_id(struct ndb_txn *txn, const unsigned char *id, size_t *len, uint64_t *primkey); struct ndb_note *ndb_get_note_by_key(struct ndb_txn *txn, uint64_t key, size_t *len); void ndb_destroy(struct ndb *); diff --git a/test.c b/test.c @@ -43,7 +43,7 @@ static void test_load_profiles() struct ndb_txn txn; assert(ndb_begin_query(ndb, &txn)); - struct ndb_note *note = ndb_get_note_by_id(&txn, id, NULL); + struct ndb_note *note = ndb_get_note_by_id(&txn, id, NULL, NULL); assert(note != NULL); assert(!strcmp(ndb_note_content(note), expected_content)); ndb_end_query(&txn); @@ -275,7 +275,7 @@ static void test_replacement() 0x9c, 0xde, 0x04, 0x07, 0x69, 0x7b, 0xb5, 0x7d, 0x97, 0x93, 0x9a, 0xd1, 0xd1, 0x49, 0xa7, 0x00, 0xc4, 0x3b, 0x59, 0x74 }; - void *root = ndb_get_profile_by_pubkey(&txn, pubkey, &len); + void *root = ndb_get_profile_by_pubkey(&txn, pubkey, &len, NULL); assert(root); int res = NdbProfileRecord_verify_as_root(root, len); @@ -317,7 +317,7 @@ static void test_fetch_last_noteid() struct ndb_txn txn; assert(ndb_begin_query(ndb, &txn)); - struct ndb_note *note = ndb_get_note_by_id(&txn, id, &len); + struct ndb_note *note = ndb_get_note_by_id(&txn, id, &len, NULL); assert(note != NULL); assert(note->created_at == 1650054135); @@ -327,7 +327,7 @@ static void test_fetch_last_noteid() 0xd1, 0x2c, 0x17, 0xbd, 0xe3, 0x09, 0x4a, 0xd3, 0x2f, 0x4a, 0xb8, 0x62, 0xa6, 0xcc, 0x6f, 0x5c, 0x28, 0x9c, 0xfe, 0x7d, 0x58, 0x02, 0x27, 0x0b, 0xdf, 0x34, 0x90, 0x4d, 0xf5, 0x85, 0xf3, 0x49 }; - void *root = ndb_get_profile_by_pubkey(&txn, pk, &len); + void *root = ndb_get_profile_by_pubkey(&txn, pk, &len, NULL); assert(root); int res = NdbProfileRecord_verify_as_root(root, len);