nostrdb

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

commit ebe92071af181b1296ad4ad41b0e16ef9b1acf35
parent 650d8c9f602ba94d77b5a98435d9c4581644dcfb
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 25 Nov 2023 15:24:24 -0800

index: write kind index when processing notes

We have a kind index database now, so write to it when processing notes

Diffstat:
Mnostrdb.c | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -814,6 +814,13 @@ static inline void ndb_tsid_init(struct ndb_tsid *key, unsigned char *id, key->timestamp = timestamp; } +static inline void ndb_u64_tsid_init(struct ndb_tsid *key, uint64_t integer, + uint64_t timestamp) +{ + key->integer = integer; + key->timestamp = timestamp; +} + // useful for range-searching for the latest key with a clustered created_at timen static inline void ndb_tsid_high(struct ndb_tsid *key, const unsigned char *id) { @@ -1844,6 +1851,32 @@ static int ndb_write_note_id_index(struct ndb_txn *txn, struct ndb_note *note, return 1; } +static int ndb_write_note_kind_index(struct ndb_txn *txn, struct ndb_note *note, + uint64_t note_key) +{ + struct ndb_u64_tsid tsid; + int rc; + MDB_val key, val; + MDB_dbi kind_db; + + ndb_u64_tsid_init(&tsid, note->kind, note->created_at); + + key.mv_data = &tsid; + key.mv_size = sizeof(tsid); + val.mv_data = &note_key; + val.mv_size = sizeof(note_key); + + kind_db = txn->lmdb->dbs[NDB_DB_NOTE_KIND]; + + if ((rc = mdb_put(txn->mdb_txn, id_db, &key, &val, 0))) { + ndb_debug("write note kind index to db failed: %s\n", + mdb_strerror(rc)); + return 0; + } + + return 1; +} + static uint64_t ndb_write_note(struct ndb_txn *txn, struct ndb_writer_note *note) { @@ -1873,6 +1906,10 @@ static uint64_t ndb_write_note(struct ndb_txn *txn, if (!ndb_write_note_id_index(txn, note->note, note_key)) return 0; + // write note kind index + if (!ndb_write_note_kind_index(txn, note->note, note_key)) + return 0; + if (note->note->kind == 7) { ndb_write_reaction_stats(txn, note->note); }