mirror of
git://jb55.com/damus
synced 2024-09-30 00:40:45 +00:00
ndb/note: always track note size, add to_owned
This commit is contained in:
parent
59cde41764
commit
9c3b052de2
@ -84,7 +84,7 @@ enum NostrResponse {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
let new_note = note_data.assumingMemoryBound(to: ndb_note.self)
|
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 {
|
guard let subid = sized_cstr(cstr: tce.subid, len: tce.subid_len) else {
|
||||||
free(data)
|
free(data)
|
||||||
|
@ -132,10 +132,11 @@ class Ndb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func lookup_note_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbNote? {
|
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 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] {
|
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? {
|
private func lookup_note_with_txn_inner<Y>(id: NoteId, txn: NdbTxn<Y>) -> NdbNote? {
|
||||||
return id.id.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) -> NdbNote? in
|
return id.id.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) -> NdbNote? in
|
||||||
var key: UInt64 = 0
|
var key: UInt64 = 0
|
||||||
|
var size: Int = 0
|
||||||
guard let baseAddress = ptr.baseAddress,
|
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 nil
|
||||||
}
|
}
|
||||||
return NdbNote(note: note_p, owned_size: nil, key: key)
|
return NdbNote(note: note_p, size: size, owned: false, key: key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +49,10 @@ class NdbNote: Encodable, Equatable, Hashable {
|
|||||||
// cached stuff (TODO: remove these)
|
// cached stuff (TODO: remove these)
|
||||||
var decrypted_content: String? = nil
|
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.note = note
|
||||||
self.owned = owned_size != nil
|
self.owned = owned
|
||||||
self.count = owned_size ?? 0
|
self.count = size
|
||||||
self.key = key
|
self.key = key
|
||||||
|
|
||||||
#if DEBUG_NOTE_SIZE
|
#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 {
|
var content: String {
|
||||||
String(cString: content_raw, encoding: .utf8) ?? ""
|
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 }
|
guard let note_data = realloc(data, Int(len)) else { return nil }
|
||||||
let new_note = note_data.assumingMemoryBound(to: ndb_note.self)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ struct TagsSequence: Encodable, Sequence {
|
|||||||
}
|
}
|
||||||
precondition(false, "sequence subscript oob")
|
precondition(false, "sequence subscript oob")
|
||||||
// it seems like the compiler needs this or it gets bitchy
|
// 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 {
|
func makeIterator() -> TagsIterator {
|
||||||
|
Loading…
Reference in New Issue
Block a user