nostrdb: profile flatbuffers in nostrdb working!

This commit is contained in:
William Casarin
2023-08-26 20:45:59 -07:00
parent 92bbc9766d
commit caffa0398b
6 changed files with 60 additions and 24 deletions

View File

@@ -275,29 +275,47 @@ 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,
size_t *len)
{
MDB_val k, v;
MDB_txn *txn;
void *res = NULL;
if (len)
*len = 0;
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;
if (len)
*len = v.mv_size;
cleanup:
mdb_txn_abort(txn);
return res;
}
return (struct ndb_note *)v.mv_data;
void *ndb_get_profile_by_pubkey(struct ndb *ndb, const unsigned char *pk, size_t *len)
{
return ndb_lookup_tsid(ndb, NDB_DB_PROFILE_PK, NDB_DB_PROFILE, pk, len);
}
struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id, size_t *len)
{
return ndb_lookup_tsid(ndb, NDB_DB_NOTE_ID, NDB_DB_NOTE, id, len);
}
static int ndb_has_note(MDB_txn *txn, struct ndb_lmdb *lmdb, const unsigned char *id)
@@ -486,8 +504,8 @@ static int ndb_write_profile(struct ndb_lmdb *lmdb, MDB_txn *txn,
// write profile to profile store
key.mv_data = &profile_key;
key.mv_size = sizeof(profile_key);
val.mv_data = profile->profile_flatbuf;
val.mv_size = profile->profile_len;
val.mv_data = profile->profile_flatbuf + 4;
val.mv_size = profile->profile_len - 4;
//ndb_debug("profile_len %ld\n", profile->profile_len);
if ((rc = mdb_put(txn, profile_db, &key, &val, 0))) {