nostrdb

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

commit dca97746561df34d3d9cb441237979910d6ee632
parent 9df0972a4815253e28e80db498314694c0cb3087
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 27 Aug 2023 12:37:14 -0700

profile: use ProfileRecord to hold additional profile metadata

This will hold cached data such as processed lnurl, etc

Diffstat:
Mbindings/c/profile_builder.h | 37+++++++++++++++++++++++++++++++++++++
Mbindings/c/profile_json_parser.h | 90++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mbindings/c/profile_reader.h | 28++++++++++++++++++++++++++++
Mbindings/c/profile_verifier.h | 30++++++++++++++++++++++++++++++
Mbindings/swift/NdbProfile.swift | 48++++++++++++++++++++++++++++++++++++++++++++++++
Aschemas/note.fbs | 20++++++++++++++++++++
Mschemas/profile.fbs | 10+++++++++-
7 files changed, 261 insertions(+), 2 deletions(-)

diff --git a/bindings/c/profile_builder.h b/bindings/c/profile_builder.h @@ -22,6 +22,11 @@ typedef flatbuffers_ref_t NdbProfile_ref_t; static NdbProfile_ref_t NdbProfile_clone(flatbuffers_builder_t *B, NdbProfile_table_t t); __flatbuffers_build_table(flatbuffers_, NdbProfile, 12) +static const flatbuffers_voffset_t __NdbProfileRecord_required[] = { 0 }; +typedef flatbuffers_ref_t NdbProfileRecord_ref_t; +static NdbProfileRecord_ref_t NdbProfileRecord_clone(flatbuffers_builder_t *B, NdbProfileRecord_table_t t); +__flatbuffers_build_table(flatbuffers_, NdbProfileRecord, 3) + #define __NdbProfile_formal_args ,\ flatbuffers_string_ref_t v0, flatbuffers_string_ref_t v1, flatbuffers_string_ref_t v2, flatbuffers_string_ref_t v3,\ flatbuffers_string_ref_t v4, flatbuffers_string_ref_t v5, flatbuffers_bool_t v6, flatbuffers_string_ref_t v7,\ @@ -33,6 +38,11 @@ __flatbuffers_build_table(flatbuffers_, NdbProfile, 12) static inline NdbProfile_ref_t NdbProfile_create(flatbuffers_builder_t *B __NdbProfile_formal_args); __flatbuffers_build_table_prolog(flatbuffers_, NdbProfile, NdbProfile_file_identifier, NdbProfile_type_identifier) +#define __NdbProfileRecord_formal_args , NdbProfile_ref_t v0, uint64_t v1, flatbuffers_string_ref_t v2 +#define __NdbProfileRecord_call_args , v0, v1, v2 +static inline NdbProfileRecord_ref_t NdbProfileRecord_create(flatbuffers_builder_t *B __NdbProfileRecord_formal_args); +__flatbuffers_build_table_prolog(flatbuffers_, NdbProfileRecord, NdbProfileRecord_file_identifier, NdbProfileRecord_type_identifier) + __flatbuffers_build_string_field(0, flatbuffers_, NdbProfile_name, NdbProfile) __flatbuffers_build_string_field(1, flatbuffers_, NdbProfile_website, NdbProfile) __flatbuffers_build_string_field(2, flatbuffers_, NdbProfile_about, NdbProfile) @@ -87,5 +97,32 @@ static NdbProfile_ref_t NdbProfile_clone(flatbuffers_builder_t *B, NdbProfile_ta __flatbuffers_memoize_end(B, t, NdbProfile_end(B)); } +__flatbuffers_build_table_field(0, flatbuffers_, NdbProfileRecord_profile, NdbProfile, NdbProfileRecord) +__flatbuffers_build_scalar_field(1, flatbuffers_, NdbProfileRecord_received_at, flatbuffers_uint64, uint64_t, 8, 8, UINT64_C(0), NdbProfileRecord) +__flatbuffers_build_string_field(2, flatbuffers_, NdbProfileRecord_lnurl, NdbProfileRecord) + +static inline NdbProfileRecord_ref_t NdbProfileRecord_create(flatbuffers_builder_t *B __NdbProfileRecord_formal_args) +{ + if (NdbProfileRecord_start(B) + || NdbProfileRecord_received_at_add(B, v1) + || NdbProfileRecord_profile_add(B, v0) + || NdbProfileRecord_lnurl_add(B, v2)) { + return 0; + } + return NdbProfileRecord_end(B); +} + +static NdbProfileRecord_ref_t NdbProfileRecord_clone(flatbuffers_builder_t *B, NdbProfileRecord_table_t t) +{ + __flatbuffers_memoize_begin(B, t); + if (NdbProfileRecord_start(B) + || NdbProfileRecord_received_at_pick(B, t) + || NdbProfileRecord_profile_pick(B, t) + || NdbProfileRecord_lnurl_pick(B, t)) { + return 0; + } + __flatbuffers_memoize_end(B, t, NdbProfileRecord_end(B)); +} + #include "flatcc/flatcc_epilogue.h" #endif /* PROFILE_BUILDER_H */ diff --git a/bindings/c/profile_json_parser.h b/bindings/c/profile_json_parser.h @@ -19,6 +19,7 @@ static int profile_parse_json(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags); static const char *NdbProfile_parse_json_table(flatcc_json_parser_t *ctx, const char *buf, const char *end, flatcc_builder_ref_t *result); +static const char *NdbProfileRecord_parse_json_table(flatcc_json_parser_t *ctx, const char *buf, const char *end, flatcc_builder_ref_t *result); static const char *profile_local_json_parser_enum(flatcc_json_parser_t *ctx, const char *buf, const char *end, int *value_type, uint64_t *value, int *aggregate); static const char *profile_global_json_parser_enum(flatcc_json_parser_t *ctx, const char *buf, const char *end, @@ -263,6 +264,93 @@ static inline int NdbProfile_parse_json_as_root(flatcc_builder_t *B, flatcc_json return flatcc_json_parser_table_as_root(B, ctx, buf, bufsiz, flags, fid, NdbProfile_parse_json_table); } +static const char *NdbProfileRecord_parse_json_table(flatcc_json_parser_t *ctx, const char *buf, const char *end, flatcc_builder_ref_t *result) +{ + int more; + void *pval; + flatcc_builder_ref_t ref, *pref; + const char *mark; + uint64_t w; + + *result = 0; + if (flatcc_builder_start_table(ctx->ctx, 3)) goto failed; + buf = flatcc_json_parser_object_start(ctx, buf, end, &more); + while (more) { + buf = flatcc_json_parser_symbol_start(ctx, buf, end); + w = flatcc_json_parser_symbol_part(buf, end); + if (w < 0x70726f66696c6500) { /* branch "profile" */ + if ((w & 0xffffffffff000000) == 0x6c6e75726c000000) { /* "lnurl" */ + buf = flatcc_json_parser_match_symbol(ctx, (mark = buf), end, 5); + if (mark != buf) { + buf = flatcc_json_parser_build_string(ctx, buf, end, &ref); + if (!ref || !(pref = flatcc_builder_table_add_offset(ctx->ctx, 2))) goto failed; + *pref = ref; + } else { + buf = flatcc_json_parser_unmatched_symbol(ctx, buf, end); + } + } else { /* "lnurl" */ + buf = flatcc_json_parser_unmatched_symbol(ctx, buf, end); + } /* "lnurl" */ + } else { /* branch "profile" */ + if ((w & 0xffffffffffffff00) == 0x70726f66696c6500) { /* "profile" */ + buf = flatcc_json_parser_match_symbol(ctx, (mark = buf), end, 7); + if (mark != buf) { + buf = NdbProfile_parse_json_table(ctx, buf, end, &ref); + if (!ref || !(pref = flatcc_builder_table_add_offset(ctx->ctx, 0))) goto failed; + *pref = ref; + } else { + goto pfguard1; + } + } else { /* "profile" */ + goto pfguard1; + } /* "profile" */ + goto endpfguard1; +pfguard1: + if (w == 0x7265636569766564) { /* descend "received" */ + buf += 8; + w = flatcc_json_parser_symbol_part(buf, end); + if ((w & 0xffffff0000000000) == 0x5f61740000000000) { /* "_at" */ + buf = flatcc_json_parser_match_symbol(ctx, (mark = buf), end, 3); + if (mark != buf) { + uint64_t val = 0; + static flatcc_json_parser_integral_symbol_f *symbolic_parsers[] = { + profile_local_json_parser_enum, + profile_global_json_parser_enum, 0 }; + buf = flatcc_json_parser_uint64(ctx, (mark = buf), end, &val); + if (mark == buf) { + buf = flatcc_json_parser_symbolic_uint64(ctx, (mark = buf), end, symbolic_parsers, &val); + if (buf == mark || buf == end) goto failed; + } + if (val != UINT64_C(0) || (ctx->flags & flatcc_json_parser_f_force_add)) { + if (!(pval = flatcc_builder_table_add(ctx->ctx, 1, 8, 8))) goto failed; + flatbuffers_uint64_write_to_pe(pval, val); + } + } else { + buf = flatcc_json_parser_unmatched_symbol(ctx, buf, end); + } + } else { /* "_at" */ + buf = flatcc_json_parser_unmatched_symbol(ctx, buf, end); + } /* "_at" */ + } else { /* descend "received" */ + buf = flatcc_json_parser_unmatched_symbol(ctx, buf, end); + } /* descend "received" */ +endpfguard1: + (void)0; + } /* branch "profile" */ + buf = flatcc_json_parser_object_end(ctx, buf, end, &more); + } + if (ctx->error) goto failed; + if (!(*result = flatcc_builder_end_table(ctx->ctx))) goto failed; + return buf; +failed: + return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_runtime); +} + +static inline int NdbProfileRecord_parse_json_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags, const char *fid) +{ + return flatcc_json_parser_table_as_root(B, ctx, buf, bufsiz, flags, fid, NdbProfileRecord_parse_json_table); +} + static const char *profile_local_json_parser_enum(flatcc_json_parser_t *ctx, const char *buf, const char *end, int *value_type, uint64_t *value, int *aggregate) { @@ -286,7 +374,7 @@ static int profile_parse_json(flatcc_builder_t *B, flatcc_json_parser_t *ctx, ctx = ctx ? ctx : &parser; flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags); if (flatcc_builder_start_buffer(B, 0, 0, 0)) return -1; - NdbProfile_parse_json_table(ctx, buf, buf + bufsiz, &root); + NdbProfileRecord_parse_json_table(ctx, buf, buf + bufsiz, &root); if (ctx->error) { return ctx->error; } diff --git a/bindings/c/profile_reader.h b/bindings/c/profile_reader.h @@ -23,6 +23,10 @@ typedef const struct NdbProfile_table *NdbProfile_table_t; typedef struct NdbProfile_table *NdbProfile_mutable_table_t; typedef const flatbuffers_uoffset_t *NdbProfile_vec_t; typedef flatbuffers_uoffset_t *NdbProfile_mutable_vec_t; +typedef const struct NdbProfileRecord_table *NdbProfileRecord_table_t; +typedef struct NdbProfileRecord_table *NdbProfileRecord_mutable_table_t; +typedef const flatbuffers_uoffset_t *NdbProfileRecord_vec_t; +typedef flatbuffers_uoffset_t *NdbProfileRecord_mutable_vec_t; #ifndef NdbProfile_file_identifier #define NdbProfile_file_identifier 0 #endif @@ -35,6 +39,18 @@ typedef flatbuffers_uoffset_t *NdbProfile_mutable_vec_t; #ifndef NdbProfile_file_extension #define NdbProfile_file_extension "bin" #endif +#ifndef NdbProfileRecord_file_identifier +#define NdbProfileRecord_file_identifier 0 +#endif +/* deprecated, use NdbProfileRecord_file_identifier */ +#ifndef NdbProfileRecord_identifier +#define NdbProfileRecord_identifier 0 +#endif +#define NdbProfileRecord_type_hash ((flatbuffers_thash_t)0xa1a8569d) +#define NdbProfileRecord_type_identifier "\x9d\x56\xa8\xa1" +#ifndef NdbProfileRecord_file_extension +#define NdbProfileRecord_file_extension "bin" +#endif @@ -59,6 +75,18 @@ __flatbuffers_define_scalar_field(9, NdbProfile, damus_donation, flatbuffers_int __flatbuffers_define_scalar_field(10, NdbProfile, damus_donation_v2, flatbuffers_int32, int32_t, INT32_C(0)) __flatbuffers_define_string_field(11, NdbProfile, lud06, 0) +struct NdbProfileRecord_table { uint8_t unused__; }; + +static inline size_t NdbProfileRecord_vec_len(NdbProfileRecord_vec_t vec) +__flatbuffers_vec_len(vec) +static inline NdbProfileRecord_table_t NdbProfileRecord_vec_at(NdbProfileRecord_vec_t vec, size_t i) +__flatbuffers_offset_vec_at(NdbProfileRecord_table_t, vec, i, 0) +__flatbuffers_table_as_root(NdbProfileRecord) + +__flatbuffers_define_table_field(0, NdbProfileRecord, profile, NdbProfile_table_t, 0) +__flatbuffers_define_scalar_field(1, NdbProfileRecord, received_at, flatbuffers_uint64, uint64_t, UINT64_C(0)) +__flatbuffers_define_string_field(2, NdbProfileRecord, lnurl, 0) + #include "flatcc/flatcc_epilogue.h" #endif /* PROFILE_READER_H */ diff --git a/bindings/c/profile_verifier.h b/bindings/c/profile_verifier.h @@ -10,6 +10,7 @@ #include "flatcc/flatcc_prologue.h" static int NdbProfile_verify_table(flatcc_table_verifier_descriptor_t *td); +static int NdbProfileRecord_verify_table(flatcc_table_verifier_descriptor_t *td); static int NdbProfile_verify_table(flatcc_table_verifier_descriptor_t *td) { @@ -49,5 +50,34 @@ static inline int NdbProfile_verify_as_root_with_type_hash(const void *buf, size return flatcc_verify_table_as_typed_root(buf, bufsiz, thash, &NdbProfile_verify_table); } +static int NdbProfileRecord_verify_table(flatcc_table_verifier_descriptor_t *td) +{ + int ret; + if ((ret = flatcc_verify_table_field(td, 0, 0, &NdbProfile_verify_table) /* profile */)) return ret; + if ((ret = flatcc_verify_field(td, 1, 8, 8) /* received_at */)) return ret; + if ((ret = flatcc_verify_string_field(td, 2, 0) /* lnurl */)) return ret; + return flatcc_verify_ok; +} + +static inline int NdbProfileRecord_verify_as_root(const void *buf, size_t bufsiz) +{ + return flatcc_verify_table_as_root(buf, bufsiz, NdbProfileRecord_identifier, &NdbProfileRecord_verify_table); +} + +static inline int NdbProfileRecord_verify_as_typed_root(const void *buf, size_t bufsiz) +{ + return flatcc_verify_table_as_root(buf, bufsiz, NdbProfileRecord_type_identifier, &NdbProfileRecord_verify_table); +} + +static inline int NdbProfileRecord_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +{ + return flatcc_verify_table_as_root(buf, bufsiz, fid, &NdbProfileRecord_verify_table); +} + +static inline int NdbProfileRecord_verify_as_root_with_type_hash(const void *buf, size_t bufsiz, flatbuffers_thash_t thash) +{ + return flatcc_verify_table_as_typed_root(buf, bufsiz, thash, &NdbProfileRecord_verify_table); +} + #include "flatcc/flatcc_epilogue.h" #endif /* PROFILE_VERIFIER_H */ diff --git a/bindings/swift/NdbProfile.swift b/bindings/swift/NdbProfile.swift @@ -115,3 +115,51 @@ public struct NdbProfile: FlatBufferObject, Verifiable { } } +public struct NdbProfileRecord: FlatBufferObject, Verifiable { + + static func validateVersion() { FlatBuffersVersion_23_5_26() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Table + + private init(_ t: Table) { _accessor = t } + public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) } + + private enum VTOFFSET: VOffset { + case profile = 4 + case receivedAt = 6 + case lnurl = 8 + var v: Int32 { Int32(self.rawValue) } + var p: VOffset { self.rawValue } + } + + public var profile: NdbProfile? { let o = _accessor.offset(VTOFFSET.profile.v); return o == 0 ? nil : NdbProfile(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var receivedAt: UInt64 { let o = _accessor.offset(VTOFFSET.receivedAt.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt64.self, at: o) } + public var lnurl: String? { let o = _accessor.offset(VTOFFSET.lnurl.v); return o == 0 ? nil : _accessor.string(at: o) } + public var lnurlSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.lnurl.v) } + public static func startNdbProfileRecord(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 3) } + public static func add(profile: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: profile, at: VTOFFSET.profile.p) } + public static func add(receivedAt: UInt64, _ fbb: inout FlatBufferBuilder) { fbb.add(element: receivedAt, def: 0, at: VTOFFSET.receivedAt.p) } + public static func add(lnurl: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: lnurl, at: VTOFFSET.lnurl.p) } + public static func endNdbProfileRecord(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } + public static func createNdbProfileRecord( + _ fbb: inout FlatBufferBuilder, + profileOffset profile: Offset = Offset(), + receivedAt: UInt64 = 0, + lnurlOffset lnurl: Offset = Offset() + ) -> Offset { + let __start = NdbProfileRecord.startNdbProfileRecord(&fbb) + NdbProfileRecord.add(profile: profile, &fbb) + NdbProfileRecord.add(receivedAt: receivedAt, &fbb) + NdbProfileRecord.add(lnurl: lnurl, &fbb) + return NdbProfileRecord.endNdbProfileRecord(&fbb, start: __start) + } + + public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { + var _v = try verifier.visitTable(at: position) + try _v.visit(field: VTOFFSET.profile.p, fieldName: "profile", required: false, type: ForwardOffset<NdbProfile>.self) + try _v.visit(field: VTOFFSET.receivedAt.p, fieldName: "receivedAt", required: false, type: UInt64.self) + try _v.visit(field: VTOFFSET.lnurl.p, fieldName: "lnurl", required: false, type: ForwardOffset<String>.self) + _v.finish() + } +} + diff --git a/schemas/note.fbs b/schemas/note.fbs @@ -0,0 +1,20 @@ + + +/* +union NdbBlock { + +} + +table NdbBlocks { + words:int; + blocks:[NdbBlock]; +} +*/ + +table NdbNoteRecord { + note:[ubyte]; + received_at:ulong; + + // artifacts + //blocks:[Block]; +} diff --git a/schemas/profile.fbs b/schemas/profile.fbs @@ -14,5 +14,13 @@ table NdbProfile { lud06:string; } -root_type NdbProfile; +table NdbProfileRecord { + profile:NdbProfile; + received_at:ulong; + + // artifacts + lnurl:string; +} + +root_type NdbProfileRecord;