From 6e0ba3206d2c75800034adf1c3148c3476dfa73c Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 26 Jan 2024 10:50:58 -0800 Subject: [PATCH] debug: add some transaction debugging --- damus/ContentView.swift | 14 +++++++++----- damus/Models/DamusState.swift | 1 + nostrdb/Ndb.swift | 7 +++++-- nostrdb/NdbTxn.swift | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/damus/ContentView.swift b/damus/ContentView.swift index 6dde4649..f4dda13a 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -462,23 +462,27 @@ struct ContentView: View { damus_state.pool.disconnect() } .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { obj in - print("📙 DAMUS ACTIVE NOTIFY") - try? damus_state.ndb.reopen() + print("txn: 📙 DAMUS ACTIVE NOTIFY") + if damus_state.ndb.reopen() { + print("txn: NOSTRDB REOPENED") + } else { + print("txn: NOSTRDB FAILED TO REOPEN closed:\(damus_state.ndb.closed)") + } } .onChange(of: scenePhase) { (phase: ScenePhase) in guard let damus_state else { return } switch phase { case .background: - print("📙 DAMUS BACKGROUNDED") + print("txn: 📙 DAMUS BACKGROUNDED") Task { @MainActor in damus_state.ndb.close() } break case .inactive: - print("📙 DAMUS INACTIVE") + print("txn: 📙 DAMUS INACTIVE") break case .active: - print("📙 DAMUS ACTIVE") + print("txn: 📙 DAMUS ACTIVE") damus_state.pool.ping() @unknown default: break diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift index 3e5b4120..fceaad67 100644 --- a/damus/Models/DamusState.swift +++ b/damus/Models/DamusState.swift @@ -94,6 +94,7 @@ struct DamusState: HeadlessDamusState { } func close() { + print("txn: damus close") pool.close() ndb.close() } diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift index e1e968e4..07d9ccce 100644 --- a/nostrdb/Ndb.swift +++ b/nostrdb/Ndb.swift @@ -178,17 +178,20 @@ class Ndb { func close() { guard !self.closed else { return } self.closed = true + print("txn: CLOSING NOSTRDB") ndb_destroy(self.ndb.ndb) + print("txn: NOSTRDB CLOSED") } - func reopen() throws { + func reopen() -> Bool { guard self.closed, let db = Self.open(path: self.path, owns_db_file: self.owns_db) else { - throw DatabaseError.failed_open + return false } self.closed = false self.ndb = db + return true } func lookup_note_by_key_with_txn(_ key: NoteKey, txn: NdbTxn) -> NdbNote? { diff --git a/nostrdb/NdbTxn.swift b/nostrdb/NdbTxn.swift index 3660f30f..bce71768 100644 --- a/nostrdb/NdbTxn.swift +++ b/nostrdb/NdbTxn.swift @@ -65,7 +65,7 @@ class NdbTxn { #if TXNDEBUG txn_count -= 1; - print("closing transaction \(txn_count)") + print("txn: closing transaction \(txn_count)") #endif ndb_end_query(&self.txn) Thread.current.threadDictionary.removeObject(forKey: "ndb_txn")