nostrdb

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

commit 407db76f0299506ede769891528005d899d458b3
parent e12e6d18a1a821b5a43bc3e52ab5545e1617c146
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 26 Aug 2023 17:40:43 -0700

ndb: add ndb_lookup_tsid helper

This is used for timestamp-clustered pubkey and id lookups.

Diffstat:
Mnostrdb.c | 29+++++++++++++++++++++--------
Mnostrdb.h | 2+-
2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -275,29 +275,42 @@ cleanup: return success; } -struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id) +static void *ndb_lookup_tsid(struct ndb *ndb, enum ndb_dbs ind, + enum ndb_dbs store, const unsigned char *pk) { MDB_val k, v; MDB_txn *txn; + void *res = NULL; if (mdb_txn_begin(ndb->lmdb.env, 0, 0, &txn)) { ndb_debug("ndb_get_note_by_id: mdb_txn_begin failed\n"); return NULL; } - if (!ndb_get_tsid(txn, &ndb->lmdb, NDB_DB_NOTE_ID, id, &k)) { - ndb_debug("ndb_get_note_by_id: ndb_get_tsid failed\n"); - return NULL; + if (!ndb_get_tsid(txn, &ndb->lmdb, ind, pk, &k)) { + ndb_debug("ndb_get_profile_by_pubkey: ndb_get_tsid failed\n"); + goto cleanup; } - if (mdb_get(txn, ndb->lmdb.dbs[NDB_DB_NOTE], &k, &v)) { - ndb_debug("ndb_get_note_by_id: mdb_get note failed\n"); - return NULL; + if (mdb_get(txn, ndb->lmdb.dbs[store], &k, &v)) { + ndb_debug("ndb_get_profile_by_pubkey: mdb_get note failed\n"); + goto cleanup; } + res = v.mv_data; +cleanup: mdb_txn_abort(txn); + return res; +} + +void *ndb_get_profile_by_pubkey(struct ndb *ndb, const unsigned char *pk) +{ + return ndb_lookup_tsid(ndb, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk); +} - return (struct ndb_note *)v.mv_data; +struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id) +{ + return ndb_lookup_tsid(ndb, NDB_DB_NOTE_ID, NDB_DB_NOTE, id); } 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,7 +155,7 @@ 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); -int ndb_get_profile(struct ndb *, unsigned char pubkey[32], void **out); +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_destroy(struct ndb *);