damus

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

commit 9c3b052de2e5047474838b213d911a3ddf38979b
parent 59cde417642c48d620c51f1b38a6e8545cef5f80
Author: William Casarin <jb55@jb55.com>
Date:   Sun,  3 Dec 2023 22:12:31 -0800

ndb/note: always track note size, add to_owned

Diffstat:
Mdamus/Nostr/NostrResponse.swift | 2+-
Mnostrdb/Ndb.swift | 10++++++----
Mnostrdb/NdbNote.swift | 20++++++++++++++++----
Mnostrdb/NdbTagsIterator.swift | 2+-
4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/damus/Nostr/NostrResponse.swift b/damus/Nostr/NostrResponse.swift @@ -84,7 +84,7 @@ enum NostrResponse { return nil } let new_note = note_data.assumingMemoryBound(to: ndb_note.self) - let note = NdbNote(note: new_note, owned_size: Int(len), key: nil) + let note = NdbNote(note: new_note, size: Int(len), owned: true, key: nil) guard let subid = sized_cstr(cstr: tce.subid, len: tce.subid_len) else { free(data) diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift @@ -132,10 +132,11 @@ class Ndb { } func lookup_note_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbNote? { - guard let note_p = ndb_get_note_by_key(&txn.txn, key, nil) else { + var size: Int = 0 + guard let note_p = ndb_get_note_by_key(&txn.txn, key, &size) else { return nil } - return NdbNote(note: note_p, owned_size: nil, key: key) + return NdbNote(note: note_p, size: size, owned: false, key: key) } func text_search(query: String, limit: Int = 32, order: NdbSearchOrder = .newest_first) -> [NoteKey] { @@ -207,11 +208,12 @@ class Ndb { private func lookup_note_with_txn_inner<Y>(id: NoteId, txn: NdbTxn<Y>) -> NdbNote? { return id.id.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) -> NdbNote? in var key: UInt64 = 0 + var size: Int = 0 guard let baseAddress = ptr.baseAddress, - let note_p = ndb_get_note_by_id(&txn.txn, baseAddress, nil, &key) else { + let note_p = ndb_get_note_by_id(&txn.txn, baseAddress, &size, &key) else { return nil } - return NdbNote(note: note_p, owned_size: nil, key: key) + return NdbNote(note: note_p, size: size, owned: false, key: key) } } diff --git a/nostrdb/NdbNote.swift b/nostrdb/NdbNote.swift @@ -49,10 +49,10 @@ class NdbNote: Encodable, Equatable, Hashable { // cached stuff (TODO: remove these) var decrypted_content: String? = nil - init(note: UnsafeMutablePointer<ndb_note>, owned_size: Int?, key: NoteKey?) { + init(note: UnsafeMutablePointer<ndb_note>, size: Int, owned: Bool, key: NoteKey?) { self.note = note - self.owned = owned_size != nil - self.count = owned_size ?? 0 + self.owned = owned + self.count = size self.key = key #if DEBUG_NOTE_SIZE @@ -66,6 +66,18 @@ class NdbNote: Encodable, Equatable, Hashable { } + func to_owned() -> NdbNote { + if self.owned { + return self + } + + let buf = malloc(self.count)! + memcpy(buf, &self.note.pointee, self.count) + let new_note = buf.assumingMemoryBound(to: ndb_note.self) + + return NdbNote(note: new_note, size: self.count, owned: true, key: self.key) + } + var content: String { String(cString: content_raw, encoding: .utf8) ?? "" } @@ -248,7 +260,7 @@ class NdbNote: Encodable, Equatable, Hashable { guard let note_data = realloc(data, Int(len)) else { return nil } let new_note = note_data.assumingMemoryBound(to: ndb_note.self) - return NdbNote(note: new_note, owned_size: Int(len), key: nil) + return NdbNote(note: new_note, size: Int(len), owned: true, key: nil) } } diff --git a/nostrdb/NdbTagsIterator.swift b/nostrdb/NdbTagsIterator.swift @@ -70,7 +70,7 @@ struct TagsSequence: Encodable, Sequence { } precondition(false, "sequence subscript oob") // it seems like the compiler needs this or it gets bitchy - return .init(note: .init(note: .allocate(capacity: 1), owned_size: nil, key: nil), tag: .allocate(capacity: 1)) + return .init(note: .init(note: .allocate(capacity: 1), size: 0, owned: true, key: nil), tag: .allocate(capacity: 1)) } func makeIterator() -> TagsIterator {