nostrdb

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

commit 322375397f4103f2b9838f075864f9491bd20fe9
parent 8629d355cb52bfc1dc6d053a869a2a860c3b37aa
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 28 Sep 2023 04:43:04 +0200

db: add new database for tracking last profile fetches

this makes sense for data that needs to be mutated frequently

Diffstat:
Mnostrdb.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Mnostrdb.h | 1+
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -70,6 +70,7 @@ enum ndb_dbs { NDB_DB_PROFILE_PK, NDB_DB_NDB_META, NDB_DB_PROFILE_SEARCH, + NDB_DB_PROFILE_LAST_FETCH, NDB_DBS, }; @@ -410,6 +411,7 @@ enum ndb_writer_msgtype { NDB_WRITER_NOTE, // write a note to the db NDB_WRITER_PROFILE, // write a profile to the db NDB_WRITER_DBMETA, // write ndb metadata + NDB_WRITER_PROFILE_LAST_FETCH, // when profiles were last fetched }; struct ndb_ingester_event { @@ -440,12 +442,18 @@ struct ndb_writer_ndb_meta { uint64_t version; }; +struct ndb_writer_last_fetch { + uint64_t profile_key; + uint64_t fetched_at; +}; + struct ndb_writer_msg { enum ndb_writer_msgtype type; union { struct ndb_writer_note note; struct ndb_writer_profile profile; struct ndb_writer_ndb_meta ndb_meta; + struct ndb_writer_last_fetch last_fetch; }; }; @@ -496,6 +504,37 @@ static int ndb_writer_queue_note(struct ndb_writer *writer, return prot_queue_push(&writer->inbox, &msg); } +static void ndb_writer_last_profile_fetch(struct ndb_lmdb *lmdb, MDB_txn *txn, + struct ndb_writer_last_fetch *w) +{ + int rc; + MDB_val key, val; + + key.mv_data = &w->profile_key; + key.mv_size = sizeof(w->profile_key); + val.mv_data = &w->fetched_at; + val.mv_size = sizeof(w->fetched_at); + + if ((rc = mdb_put(txn, lmdb->dbs[NDB_DB_PROFILE_LAST_FETCH], &key, &val, 0))) { + ndb_debug("write version to ndb_meta failed: %s\n", + mdb_strerror(rc)); + return; + } + + //fprintf(stderr, "writing version %" PRIu64 "\n", version); +} + +int ndb_write_last_profile_fetch(struct ndb *ndb, uint64_t profile_key, + uint64_t fetched_at) +{ + struct ndb_writer_msg msg; + msg.type = NDB_WRITER_PROFILE_LAST_FETCH; + msg.last_fetch.profile_key = profile_key; + msg.last_fetch.fetched_at = fetched_at; + + return ndb_writer_queue_msg(&ndb->writer, &msg); +} + // get some value based on a clustered id key int ndb_get_tsid(MDB_txn *txn, struct ndb_lmdb *lmdb, enum ndb_dbs db, const unsigned char *id, MDB_val *val) @@ -1152,6 +1191,7 @@ static void *ndb_writer_thread(void *data) case NDB_WRITER_NOTE: any_note = 1; break; case NDB_WRITER_PROFILE: any_note = 1; break; case NDB_WRITER_DBMETA: any_note = 1; break; + case NDB_WRITER_PROFILE_LAST_FETCH: any_note = 1; break; case NDB_WRITER_QUIT: break; } } @@ -1188,6 +1228,9 @@ static void *ndb_writer_thread(void *data) case NDB_WRITER_DBMETA: ndb_write_version(writer->lmdb, txn, msg->ndb_meta.version); break; + case NDB_WRITER_PROFILE_LAST_FETCH: + ndb_writer_last_profile_fetch(writer->lmdb, txn, &msg->last_fetch); + break; } } @@ -1430,6 +1473,12 @@ static int ndb_init_lmdb(const char *filename, struct ndb_lmdb *lmdb, size_t map return 0; } + // profile last fetches + if ((rc = mdb_dbi_open(txn, "profile_last_fetch", MDB_CREATE | MDB_INTEGERKEY, &lmdb->dbs[NDB_DB_PROFILE_LAST_FETCH]))) { + fprintf(stderr, "mdb_dbi_open profile last fetch, error %d\n", rc); + return 0; + } + // id+ts index flags unsigned int tsid_flags = MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED; diff --git a/nostrdb.h b/nostrdb.h @@ -197,6 +197,7 @@ int ndb_search_profile(struct ndb_txn *txn, struct ndb_search *search, const cha int ndb_search_profile_next(struct ndb_search *search); void ndb_search_profile_end(struct ndb_search *search); void ndb_end_query(struct ndb_txn *); +int ndb_write_last_profile_fetch(struct ndb *ndb, uint64_t profile_key, uint64_t fetched_at); void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pubkey, size_t *len, uint64_t *primkey); void *ndb_get_profile_by_key(struct ndb_txn *txn, uint64_t key, size_t *len); uint64_t ndb_get_notekey_by_id(struct ndb_txn *txn, const unsigned char *id);