nostrdb

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

commit 885643cea41b1504b00eedbd372041e848e2164c
parent dca97746561df34d3d9cb441237979910d6ee632
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 27 Aug 2023 12:44:41 -0700

ndb: switch to profile records in db

Diffstat:
MMakefile | 2+-
Mnostrdb.c | 47+++++++++++++++++++++++++++++++++++------------
Mnostrdb.h | 1-
Mtest.c | 11+++++++----
4 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -Wall -Wno-unused-function -Werror -O0 -g -Ideps/secp256k1/include -Ideps/lmdb -Ideps/flatcc/include HEADERS = sha256.h nostrdb.h cursor.h hex.h jsmn.h config.h sha256.h random.h memchr.h $(C_BINDINGS) FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c -SRCS = nostrdb.c sha256.c $(FLATCC_SRCS) +SRCS = nostrdb.c sha256.c bech32.c $(FLATCC_SRCS) LDS = $(SRCS) $(ARS) DEPS = $(SRCS) $(HEADERS) $(ARS) ARS = deps/lmdb/liblmdb.a deps/secp256k1/.libs/libsecp256k1.a diff --git a/nostrdb.c b/nostrdb.c @@ -15,6 +15,7 @@ #include <assert.h> #include "bindings/c/profile_json_parser.h" +#include "bindings/c/profile_builder.h" #include "secp256k1.h" #include "secp256k1_ecdh.h" #include "secp256k1_schnorrsig.h" @@ -343,29 +344,51 @@ static enum ndb_idres ndb_ingester_json_controller(void *data, const char *hexid return NDB_IDRES_STOP; } +static int ndbprofile_parse_json(flatcc_builder_t *B, + const char *buf, size_t bufsiz, int flags, NdbProfile_ref_t *profile) +{ + flatcc_json_parser_t parser, *ctx = &parser; + flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags); + + NdbProfile_parse_json_table(ctx, buf, buf + bufsiz, profile); + + if (ctx->error) + return 0; + + return 1; +} static int ndb_process_profile_note(struct ndb_note *note, void **profile, - size_t *profile_len) + size_t *profile_len) { - int res; - + NdbProfile_ref_t profile_table; flatcc_builder_t builder; - flatcc_json_parser_t json_parser; - flatcc_builder_init(&builder); - //printf("parsing profile '%.*s'\n", note->content_length, ndb_note_content(note)); - res = profile_parse_json(&builder, &json_parser, - ndb_note_content(note), - note->content_length, - flatcc_json_parser_f_skip_unknown); + NdbProfileRecord_start_as_root(&builder); - if (res != 0) { + //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)) + { ndb_debug("profile_parse_json failed %d '%.*s'\n", res, - note->content_length, ndb_note_content(note)); + note->content_length, ndb_note_content(note)); return 0; } + uint64_t received_at = time(NULL); + const char *lnurl = "fixme"; + + flatcc_builder_ref_t lnurl_off; + lnurl_off = flatcc_builder_create_string_str(&builder, lnurl); + + NdbProfileRecord_profile_add(&builder, profile_table); + NdbProfileRecord_received_at_add(&builder, received_at); + NdbProfileRecord_lnurl_add(&builder, lnurl_off); + + NdbProfileRecord_end_as_root(&builder); + *profile = flatcc_builder_finalize_aligned_buffer(&builder, profile_len); return 1; } diff --git a/nostrdb.h b/nostrdb.h @@ -120,7 +120,6 @@ struct ndb_note { uint32_t content_length; union ndb_packed_str content; uint32_t strings; - uint32_t reserved[4]; // expansion slots // nothing can come after tags since it contains variadic data struct ndb_tags tags; }; diff --git a/test.c b/test.c @@ -178,7 +178,7 @@ static void test_parse_contact_list() size = ndb_note_from_json((const char*)json, written, &note, buf, alloc_size); printf("ndb_note_from_json size %d\n", size); assert(size > 0); - assert(size == 34344); + assert(size == 34328); memcpy(id, note->id, 32); memset(note->id, 0, 32); @@ -273,14 +273,17 @@ 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 }; - void *profile = ndb_get_profile_by_pubkey(ndb, pk, &len); + void *profile_record = ndb_get_profile_by_pubkey(ndb, pk, &len); - assert(profile); + assert(profile_record); + NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record); + const char *lnurl = NdbProfileRecord_lnurl_get(profile_record); const char *name = NdbProfile_name_get(profile); assert(name); - printf("name '%s'\n", name); + assert(lnurl); assert(!strcmp(name, "jb55")); + assert(!strcmp(lnurl, "fixme")); //fwrite(profile, len, 1, stdout);