damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit caffa0398b469d189d37f87f15d80a296c45a508
parent 92bbc9766d821c410a344e412ebad5e677428cc3
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 26 Aug 2023 20:45:59 -0700

nostrdb: profile flatbuffers in nostrdb working!

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 1+
Mnostrdb/Ndb.swift | 23++++++++++++++++++-----
Mnostrdb/Test/NdbTests.swift | 16+++++++++++-----
Mnostrdb/bindings/swift/NdbProfile.swift | 2--
Mnostrdb/nostrdb.c | 38++++++++++++++++++++++++++++----------
Mnostrdb/nostrdb.h | 4++--
6 files changed, 60 insertions(+), 24 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -2476,6 +2476,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4C32B9342A9AD01A00DC3548 /* NdbProfile.swift in Sources */, 4C32B9332A99845B00DC3548 /* Ndb.swift in Sources */, 4C4793082A993E8900489948 /* refmap.c in Sources */, 4C4793072A993E6200489948 /* emitter.c in Sources */, diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift @@ -10,13 +10,15 @@ import Foundation class Ndb { let ndb: ndb_t + static var db_path: String { + (FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: ""))! + } + init?() { var ndb_p: OpaquePointer? = nil - let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: "") - - let ok = dir!.withCString { testdir in - return ndb_init(&ndb_p, testdir, 1024 * 1024 * 700, 4) != 0 + let ok = Ndb.db_path.withCString { testdir in + return ndb_init(&ndb_p, testdir, 1024 * 1024 * 1024 * 32, 4) != 0 } if !ok { @@ -28,13 +30,24 @@ class Ndb { func lookup_note(_ id: NoteId) -> NdbNote? { id.id.withUnsafeBytes { bs in - guard let note_p = ndb_get_note_by_id(ndb.ndb, bs) else { + guard let note_p = ndb_get_note_by_id(ndb.ndb, bs, nil) else { return nil } return NdbNote(note: note_p, owned_size: nil) } } + func lookup_profile(_ pubkey: Pubkey) -> NdbProfile? { + return pubkey.id.withUnsafeBytes { pk_bytes in + var size: Int = 0 + guard let profile_p = ndb_get_profile_by_pubkey(ndb.ndb, pk_bytes, &size) else { + return nil + } + + return NdbProfile(.init(memory: profile_p, count: size), o: 0) + } + } + func process_events(_ str: String) -> Bool { return str.withCString { cstr in return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0 diff --git a/nostrdb/Test/NdbTests.swift b/nostrdb/Test/NdbTests.swift @@ -12,6 +12,8 @@ final class NdbTests: XCTestCase { override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. + try FileManager.default.removeItem(atPath: Ndb.db_path + "/lock.mdb") + try FileManager.default.removeItem(atPath: Ndb.db_path + "/data.mdb") } override func tearDownWithError() throws { @@ -41,14 +43,18 @@ final class NdbTests: XCTestCase { do { let ndb = Ndb()! - let id1 = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")! - let note1 = ndb.lookup_note(id1) - XCTAssertNotNil(note1) - let id = NoteId(hex: "b2e03951843b191b5d9d1969f48db0156b83cc7dbd841f543f109362e24c4a9c")! + let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")! let note = ndb.lookup_note(id) XCTAssertNotNil(note) guard let note else { return } - XCTAssertEqual(note.pubkey, Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!) + let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")! + XCTAssertEqual(note.pubkey, pk) + + let profile = ndb.lookup_profile(pk) + XCTAssertNotNil(profile) + guard let profile else { return } + + XCTAssertEqual(profile.name, "jb55") } diff --git a/nostrdb/bindings/swift/NdbProfile.swift b/nostrdb/bindings/swift/NdbProfile.swift @@ -2,8 +2,6 @@ // swiftlint:disable all // swiftformat:disable all -import FlatBuffers - public struct NdbProfile: FlatBufferObject, Verifiable { static func validateVersion() { FlatBuffersVersion_23_5_26() } diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c @@ -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))) { diff --git a/nostrdb/nostrdb.h b/nostrdb/nostrdb.h @@ -155,8 +155,8 @@ int ndb_note_verify(void *secp_ctx, unsigned char pubkey[32], unsigned char id[3 int ndb_init(struct ndb **ndb, const char *dbdir, size_t mapsize, int ingester_threads); int ndb_process_event(struct ndb *, const char *json, int len); int ndb_process_events(struct ndb *, const char *ldjson, size_t len); -int ndb_get_profile(struct ndb *, unsigned char pubkey[32], void **out); -struct ndb_note *ndb_get_note_by_id(struct ndb *, const unsigned char *id); +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); void ndb_destroy(struct ndb *); // BUILDER