damus

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

commit 26bd50c948a5b2efb48e767fc529fd75a68d6a60
parent 6e0af0ba103f9eef32e1f47a0a0667e89ba864ce
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Fri, 22 Dec 2023 21:25:02 +0000

Revert "nostrdb: close database when backgrounded"

This reverts commit da2bdad18d1ddc3f2bde40dcb16aca6677fd681c.

This commit was reverted because it was causing crashes (Occasional `EXC_BAD_ACCESS` errors when accessing database transactions), and because the push notification extension uses Ndb in a read-only manner, which means we no longer need these changes

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>

Diffstat:
Mdamus/ContentView.swift | 11++---------
Mnostrdb/Ndb.swift | 55+++++--------------------------------------------------
Mnostrdb/NdbTxn.swift | 9+--------
3 files changed, 8 insertions(+), 67 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -448,25 +448,18 @@ struct ContentView: View { break } } - .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { obj in - print("📙 DAMUS ACTIVE NOTIFY") - try? damus_state.ndb.reopen() - } .onChange(of: scenePhase) { (phase: ScenePhase) in - guard let damus_state else { return } switch phase { case .background: print("📙 DAMUS BACKGROUNDED") - Task { @MainActor in - damus_state.ndb.close() - } break case .inactive: print("📙 DAMUS INACTIVE") break case .active: print("📙 DAMUS ACTIVE") - damus_state.pool.ping() + guard let ds = damus_state else { return } + ds.pool.ping() @unknown default: break } diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift @@ -15,23 +15,8 @@ enum NdbSearchOrder { case newest_first } - -enum DatabaseError: Error { - case failed_open - - var errorDescription: String? { - switch self { - case .failed_open: - return "Failed to open database" - } - } -} - class Ndb { - var ndb: ndb_t - let path: String? - let owns_db: Bool - var closed: Bool + let ndb: ndb_t static func safemode() -> Ndb? { guard let path = db_path ?? old_db_path else { return nil } @@ -73,8 +58,8 @@ class Ndb { static var empty: Ndb { Ndb(ndb: ndb_t(ndb: nil)) } - - static func open(path: String? = nil, owns_db_file: Bool = true) -> ndb_t? { + + init?(path: String? = nil, owns_db_file: Bool = true) { var ndb_p: OpaquePointer? = nil let ingest_threads: Int32 = 4 @@ -117,20 +102,9 @@ class Ndb { return nil } - return ndb_t(ndb: ndb_p) + self.ndb = ndb_t(ndb: ndb_p) } - init?(path: String? = nil, owns_db_file: Bool = true) { - guard let db = Self.open(path: path, owns_db_file: owns_db_file) else { - return nil - } - - self.path = path - self.owns_db = owns_db_file - self.ndb = db - self.closed = false - } - private static func migrate_db_location_if_needed() throws { guard let old_db_path, let db_path else { throw Errors.cannot_find_db_path @@ -170,23 +144,6 @@ class Ndb { init(ndb: ndb_t) { self.ndb = ndb - self.path = nil - self.owns_db = true - self.closed = false - } - - func close() { - self.closed = true - ndb_destroy(self.ndb.ndb) - } - - func reopen() throws { - guard self.closed, - let db = Self.open(path: self.path, owns_db_file: self.owns_db) else { - throw DatabaseError.failed_open - } - - self.ndb = db } func lookup_note_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbNote? { @@ -387,14 +344,12 @@ class Ndb { } func process_event(_ str: String) -> Bool { - guard !closed else { return false } return str.withCString { cstr in return ndb_process_event(ndb.ndb, cstr, Int32(str.utf8.count)) != 0 } } func process_events(_ str: String) -> Bool { - guard !closed else { return false } return str.withCString { cstr in return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0 } @@ -432,7 +387,7 @@ class Ndb { } deinit { - self.close() + ndb_destroy(ndb.ndb) } } diff --git a/nostrdb/NdbTxn.swift b/nostrdb/NdbTxn.swift @@ -29,14 +29,7 @@ class NdbTxn<T> { self.inherited = true } else { self.txn = ndb_txn() - let ok = ndb_begin_query(ndb.ndb.ndb, &self.txn) != 0 - if !ok { - self.moved = false - self.txn = ndb_txn() - self.inherited = true - self.val = with(self) - return - } + let _ = ndb_begin_query(ndb.ndb.ndb, &self.txn) Thread.current.threadDictionary["ndb_txn"] = self.txn self.inherited = false }