nostrdb: initial Ndb class
This commit is contained in:
47
nostrdb/Ndb.swift
Normal file
47
nostrdb/Ndb.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Ndb.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-08-25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Ndb {
|
||||
let ndb: ndb_t
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.ndb = ndb_t(ndb: ndb_p)
|
||||
}
|
||||
|
||||
func lookup_note(_ id: NoteId) -> NdbNote? {
|
||||
id.id.withUnsafeBytes { bs in
|
||||
guard let note_p = ndb_get_note_by_id(ndb.ndb, bs) else {
|
||||
return nil
|
||||
}
|
||||
return NdbNote(note: note_p, owned_size: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func process_events(_ str: String) -> Bool {
|
||||
return str.withCString { cstr in
|
||||
return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
ndb_destroy(ndb.ndb)
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,29 @@ final class NdbTests: XCTestCase {
|
||||
|
||||
}
|
||||
|
||||
func test_ndb_init() {
|
||||
|
||||
do {
|
||||
let ndb = Ndb()!
|
||||
let ok = ndb.process_events(test_wire_events)
|
||||
XCTAssertTrue(ok)
|
||||
}
|
||||
|
||||
do {
|
||||
let ndb = Ndb()!
|
||||
let id1 = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
|
||||
let note1 = ndb.lookup_note(id1)
|
||||
XCTAssertNotNil(note1)
|
||||
let id = NoteId(hex: "b2e03951843b191b5d9d1969f48db0156b83cc7dbd841f543f109362e24c4a9c")!
|
||||
let note = ndb.lookup_note(id)
|
||||
XCTAssertNotNil(note)
|
||||
guard let note else { return }
|
||||
XCTAssertEqual(note.pubkey, Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func test_ndb_note() throws {
|
||||
let note = NdbNote.owned_from_json(json: test_contact_list_json)
|
||||
XCTAssertNotNil(note)
|
||||
|
||||
Reference in New Issue
Block a user