commit bfd78c01ca467af09850b16b12cfa33ab72e9996
parent a2af030367559fb552d3f0c11c82300a8943e908
Author: William Casarin <jb55@jb55.com>
Date: Sat, 25 Nov 2023 15:15:23 -0800
nostrdb/refactor: move write id index to its own function
We will be writing more indices so I'm trying to clean this up a bit
before this function gets too messy
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
1 file changed, 29 insertions(+), 14 deletions(-)
diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c
@@ -1817,18 +1817,43 @@ static int ndb_write_reaction_stats(struct ndb_txn *txn, struct ndb_note *note)
}
+static int ndb_write_note_id_index(struct ndb_txn *txn, struct ndb_note *note,
+ uint64_t note_key)
+
+{
+ struct ndb_tsid tsid;
+ int rc;
+ MDB_val key, val;
+ MDB_dbi id_db;
+
+ ndb_tsid_init(&tsid, note->id, note->created_at);
+
+ key.mv_data = &tsid;
+ key.mv_size = sizeof(tsid);
+ val.mv_data = ¬e_key;
+ val.mv_size = sizeof(note_key);
+
+ id_db = txn->lmdb->dbs[NDB_DB_NOTE_ID];
+
+ if ((rc = mdb_put(txn->mdb_txn, id_db, &key, &val, 0))) {
+ ndb_debug("write note id 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)
{
int rc;
uint64_t note_key;
- struct ndb_tsid tsid;
- MDB_dbi note_db, id_db;
+ MDB_dbi note_db;
MDB_val key, val;
// get dbs
note_db = txn->lmdb->dbs[NDB_DB_NOTE];
- id_db = txn->lmdb->dbs[NDB_DB_NOTE_ID];
// get new key
note_key = ndb_get_last_key(txn->mdb_txn, note_db) + 1;
@@ -1845,18 +1870,8 @@ static uint64_t ndb_write_note(struct ndb_txn *txn,
}
// write id index key clustered with created_at
- ndb_tsid_init(&tsid, note->note->id, note->note->created_at);
-
- key.mv_data = &tsid;
- key.mv_size = sizeof(tsid);
- val.mv_data = ¬e_key;
- val.mv_size = sizeof(note_key);
-
- if ((rc = mdb_put(txn->mdb_txn, id_db, &key, &val, 0))) {
- ndb_debug("write note id index to db failed: %s\n",
- mdb_strerror(rc));
+ if (!ndb_write_note_id_index(txn, note->note, note_key))
return 0;
- }
if (note->note->kind == 7) {
ndb_write_reaction_stats(txn, note->note);