ndb: switch profile queries to use transactions

this should ensure no crashing occurs when querying profiles
This commit is contained in:
William Casarin
2023-09-10 14:51:55 -07:00
parent 622a436589
commit fc9b9f2940
51 changed files with 435 additions and 252 deletions

View File

@@ -8,15 +8,18 @@
import Foundation
typealias Profile = NdbProfile
typealias ProfileKey = UInt64
//typealias ProfileRecord = NdbProfileRecord
class ProfileRecord {
let data: NdbProfileRecord
init(data: NdbProfileRecord) {
init(data: NdbProfileRecord, key: ProfileKey) {
self.data = data
self.profileKey = key
}
let profileKey: ProfileKey
var profile: Profile? { return data.profile }
var receivedAt: UInt64 { data.receivedAt }
var noteKey: UInt64 { data.noteKey }

View File

@@ -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))
let note = NdbNote(note: new_note, owned_size: Int(len), key: nil)
guard let subid = sized_cstr(cstr: tce.subid, len: tce.subid_len) else {
free(data)

View File

@@ -76,18 +76,25 @@ class Profiles {
profile_data(pubkey).zapper
}
func lookup_with_timestamp(_ pubkey: Pubkey) -> ProfileRecord? {
func lookup_with_timestamp(_ pubkey: Pubkey) -> NdbTxn<ProfileRecord?> {
return ndb.lookup_profile(pubkey)
}
func lookup(id: Pubkey) -> Profile? {
return ndb.lookup_profile(id)?.profile
func lookup_by_key(key: ProfileKey) -> NdbTxn<ProfileRecord?> {
return ndb.lookup_profile_by_key(key: key)
}
func lookup(id: Pubkey) -> NdbTxn<Profile?> {
return ndb.lookup_profile(id).map({ pr in pr?.profile })
}
func lookup_key_by_pubkey(_ pubkey: Pubkey) -> ProfileKey? {
return ndb.lookup_profile_key(pubkey)
}
func has_fresh_profile(id: Pubkey) -> Bool {
var profile: Profile?
guard let profile = lookup_with_timestamp(id) else { return false }
return Date.now.timeIntervalSince(Date(timeIntervalSince1970: Double(profile.receivedAt))) < Profiles.db_freshness_threshold
guard let recv = lookup_with_timestamp(id).unsafeUnownedValue?.receivedAt else { return false }
return Date.now.timeIntervalSince(Date(timeIntervalSince1970: Double(recv))) < Profiles.db_freshness_threshold
}
}