commit 0ff4d2624c145b5951bd0c02163adadb7f2b391d
parent 05acd3b8170e7a16dd50a97f4780a6566d15b98e
Author: William Casarin <jb55@jb55.com>
Date: Sun, 27 Aug 2023 18:57:24 -0700
ndb: add support for fetching notes by primary key
Diffstat:
3 files changed, 45 insertions(+), 4 deletions(-)
diff --git a/nostrdb.c b/nostrdb.c
@@ -279,6 +279,32 @@ cleanup:
return success;
}
+static void *ndb_lookup_by_key(struct ndb *ndb, uint64_t key,
+ enum ndb_dbs store, size_t *len)
+{
+ MDB_val k, v;
+ MDB_txn *txn;
+
+ k.mv_data = &key;
+ k.mv_size = sizeof(key);
+
+ 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 (mdb_get(txn, ndb->lmdb.dbs[store], &k, &v)) {
+ ndb_debug("ndb_get_profile_by_pubkey: mdb_get note failed\n");
+ mdb_txn_abort(txn);
+ return NULL;
+ }
+
+ if (len)
+ *len = v.mv_size;
+
+ return v.mv_data;
+}
+
static void *ndb_lookup_tsid(struct ndb *ndb, enum ndb_dbs ind,
enum ndb_dbs store, const unsigned char *pk,
size_t *len)
@@ -322,6 +348,11 @@ struct ndb_note *ndb_get_note_by_id(struct ndb *ndb, const unsigned char *id, si
return ndb_lookup_tsid(ndb, NDB_DB_NOTE_ID, NDB_DB_NOTE, id, len);
}
+struct ndb_note *ndb_get_note_by_key(struct ndb *ndb, uint64_t key, size_t *len)
+{
+ return ndb_lookup_by_key(ndb, key, NDB_DB_NOTE, len);
+}
+
static int ndb_has_note(MDB_txn *txn, struct ndb_lmdb *lmdb, const unsigned char *id)
{
MDB_val val;
@@ -381,6 +412,8 @@ void ndb_profile_record_builder_free(struct ndb_profile_record_builder *b)
static int ndb_process_profile_note(struct ndb_note *note,
struct ndb_profile_record_builder *profile)
{
+ int res;
+
NdbProfile_ref_t profile_table;
flatcc_builder_t *builder;
@@ -391,9 +424,10 @@ static int ndb_process_profile_note(struct ndb_note *note,
NdbProfileRecord_start_as_root(builder);
//printf("parsing profile '%.*s'\n", note->content_length, ndb_note_content(note));
- if (!ndbprofile_parse_json(builder, ndb_note_content(note),
- note->content_length,
- flatcc_json_parser_f_skip_unknown, &profile_table))
+ if (!(res = ndbprofile_parse_json(builder, ndb_note_content(note),
+ note->content_length,
+ flatcc_json_parser_f_skip_unknown,
+ &profile_table)))
{
ndb_debug("profile_parse_json failed %d '%.*s'\n", res,
note->content_length, ndb_note_content(note));
diff --git a/nostrdb.h b/nostrdb.h
@@ -156,6 +156,7 @@ int ndb_process_event(struct ndb *, const char *json, int len);
int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
void *ndb_get_profile_by_pubkey(struct ndb *, const unsigned char *pubkey, size_t *len);
struct ndb_note *ndb_get_note_by_id(struct ndb *, const unsigned char *id, size_t *len);
+struct ndb_note *ndb_get_note_by_key(struct ndb *, uint64_t key, size_t *len);
void ndb_destroy(struct ndb *);
// BUILDER
diff --git a/test.c b/test.c
@@ -273,6 +273,10 @@ static void test_fetch_last_noteid()
unsigned char pk[32] = { 0x32, 0xe1, 0x82, 0x76, 0x35, 0x45, 0x0e, 0xbb, 0x3c, 0x5a, 0x7d, 0x12, 0xc1, 0xf8, 0xe7, 0xb2, 0xb5, 0x14, 0x43, 0x9a, 0xc1, 0x0a, 0x67, 0xee, 0xf3, 0xd9, 0xfd, 0x9c, 0x5c, 0x68, 0xe2, 0x45 };
+ unsigned char profile_note_id[32] = {
+ 0xd1, 0x2c, 0x17, 0xbd, 0xe3, 0x09, 0x4a, 0xd3, 0x2f, 0x4a, 0xb8, 0x62, 0xa6, 0xcc, 0x6f, 0x5c, 0x28, 0x9c, 0xfe, 0x7d, 0x58, 0x02, 0x27, 0x0b, 0xdf, 0x34, 0x90, 0x4d, 0xf5, 0x85, 0xf3, 0x49
+ };
+
void *profile_record = ndb_get_profile_by_pubkey(ndb, pk, &len);
assert(profile_record);
@@ -287,7 +291,9 @@ static void test_fetch_last_noteid()
assert(!strcmp(lnurl, "fixme"));
printf("note_key %" PRIu64 "\n", key);
- assert(key == 2);
+
+ struct ndb_note *n = ndb_get_note_by_key(ndb, key, NULL);
+ assert(memcmp(profile_note_id, n->id, 32) == 0);
//fwrite(profile, len, 1, stdout);