1
0
mirror of git://jb55.com/damus synced 2024-09-30 00:40:45 +00:00

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

@ -2476,6 +2476,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
4C32B9342A9AD01A00DC3548 /* NdbProfile.swift in Sources */,
4C32B9332A99845B00DC3548 /* Ndb.swift in Sources */, 4C32B9332A99845B00DC3548 /* Ndb.swift in Sources */,
4C4793082A993E8900489948 /* refmap.c in Sources */, 4C4793082A993E8900489948 /* refmap.c in Sources */,
4C4793072A993E6200489948 /* emitter.c in Sources */, 4C4793072A993E6200489948 /* emitter.c in Sources */,

View File

@ -10,13 +10,15 @@ import Foundation
class Ndb { class Ndb {
let ndb: ndb_t let ndb: ndb_t
static var db_path: String {
(FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: ""))!
}
init?() { init?() {
var ndb_p: OpaquePointer? = nil var ndb_p: OpaquePointer? = nil
let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: "") let ok = Ndb.db_path.withCString { testdir in
return ndb_init(&ndb_p, testdir, 1024 * 1024 * 1024 * 32, 4) != 0
let ok = dir!.withCString { testdir in
return ndb_init(&ndb_p, testdir, 1024 * 1024 * 700, 4) != 0
} }
if !ok { if !ok {
@ -28,13 +30,24 @@ class Ndb {
func lookup_note(_ id: NoteId) -> NdbNote? { func lookup_note(_ id: NoteId) -> NdbNote? {
id.id.withUnsafeBytes { bs in 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 nil
} }
return NdbNote(note: note_p, owned_size: 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 { func process_events(_ str: String) -> Bool {
return str.withCString { cstr in return str.withCString { cstr in
return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0 return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0

View File

@ -12,6 +12,8 @@ final class NdbTests: XCTestCase {
override func setUpWithError() throws { override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class. // 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 { override func tearDownWithError() throws {
@ -41,14 +43,18 @@ final class NdbTests: XCTestCase {
do { do {
let ndb = Ndb()! let ndb = Ndb()!
let id1 = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")! let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
let note1 = ndb.lookup_note(id1)
XCTAssertNotNil(note1)
let id = NoteId(hex: "b2e03951843b191b5d9d1969f48db0156b83cc7dbd841f543f109362e24c4a9c")!
let note = ndb.lookup_note(id) let note = ndb.lookup_note(id)
XCTAssertNotNil(note) XCTAssertNotNil(note)
guard let note else { return } 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")
} }

View File

@ -2,8 +2,6 @@
// swiftlint:disable all // swiftlint:disable all
// swiftformat:disable all // swiftformat:disable all
import FlatBuffers
public struct NdbProfile: FlatBufferObject, Verifiable { public struct NdbProfile: FlatBufferObject, Verifiable {
static func validateVersion() { FlatBuffersVersion_23_5_26() } static func validateVersion() { FlatBuffersVersion_23_5_26() }

View File

@ -275,29 +275,47 @@ cleanup:
return success; 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_val k, v;
MDB_txn *txn; MDB_txn *txn;
void *res = NULL;
if (len)
*len = 0;
if (mdb_txn_begin(ndb->lmdb.env, 0, 0, &txn)) { if (mdb_txn_begin(ndb->lmdb.env, 0, 0, &txn)) {
ndb_debug("ndb_get_note_by_id: mdb_txn_begin failed\n"); ndb_debug("ndb_get_note_by_id: mdb_txn_begin failed\n");
return NULL; return NULL;
} }
if (!ndb_get_tsid(txn, &ndb->lmdb, NDB_DB_NOTE_ID, id, &k)) { if (!ndb_get_tsid(txn, &ndb->lmdb, ind, pk, &k)) {
ndb_debug("ndb_get_note_by_id: ndb_get_tsid failed\n"); ndb_debug("ndb_get_profile_by_pubkey: ndb_get_tsid failed\n");
return NULL; goto cleanup;
} }
if (mdb_get(txn, ndb->lmdb.dbs[NDB_DB_NOTE], &k, &v)) { if (mdb_get(txn, ndb->lmdb.dbs[store], &k, &v)) {
ndb_debug("ndb_get_note_by_id: mdb_get note failed\n"); ndb_debug("ndb_get_profile_by_pubkey: mdb_get note failed\n");
return NULL; goto cleanup;
} }
res = v.mv_data;
if (len)
*len = v.mv_size;
cleanup:
mdb_txn_abort(txn); 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) 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 // write profile to profile store
key.mv_data = &profile_key; key.mv_data = &profile_key;
key.mv_size = sizeof(profile_key); key.mv_size = sizeof(profile_key);
val.mv_data = profile->profile_flatbuf; val.mv_data = profile->profile_flatbuf + 4;
val.mv_size = profile->profile_len; val.mv_size = profile->profile_len - 4;
//ndb_debug("profile_len %ld\n", profile->profile_len); //ndb_debug("profile_len %ld\n", profile->profile_len);
if ((rc = mdb_put(txn, profile_db, &key, &val, 0))) { if ((rc = mdb_put(txn, profile_db, &key, &val, 0))) {

View File

@ -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_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_event(struct ndb *, const char *json, int len);
int ndb_process_events(struct ndb *, const char *ldjson, size_t 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); 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); struct ndb_note *ndb_get_note_by_id(struct ndb *, const unsigned char *id, size_t *len);
void ndb_destroy(struct ndb *); void ndb_destroy(struct ndb *);
// BUILDER // BUILDER