From ff60f8a2dbe92f92d9e6f6acbd8b6c615dde60f7 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sat, 25 Nov 2023 15:24:24 -0800 Subject: [PATCH] nostrdb/index: write kind index when processing notes We have a kind index database now, so write to it when processing notes Signed-off-by: William Casarin --- nostrdb/nostrdb.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c index d23a30ed..96343734 100644 --- a/nostrdb/nostrdb.c +++ b/nostrdb/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 = ¬e_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); }