mirror of
git://jb55.com/damus
synced 2024-09-18 19:23:49 +00:00
ndb: add safemode so we don't instantly crash on bad dbs
Fixes: https://github.com/damus-io/damus/issues/1741
This commit is contained in:
parent
4e447ddbed
commit
a07b78e47f
@ -602,7 +602,20 @@ struct ContentView: View {
|
|||||||
|
|
||||||
func connect() {
|
func connect() {
|
||||||
// nostrdb
|
// nostrdb
|
||||||
let ndb = try! Ndb()!
|
var mndb = Ndb()
|
||||||
|
if mndb == nil {
|
||||||
|
// try recovery
|
||||||
|
print("DB ISSUE! RECOVERING")
|
||||||
|
mndb = Ndb.safemode()
|
||||||
|
|
||||||
|
// out of space or something?? maybe we need a in-memory fallback
|
||||||
|
if mndb == nil {
|
||||||
|
notify(.logout)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let ndb = mndb else { return }
|
||||||
|
|
||||||
let pool = RelayPool(ndb: ndb)
|
let pool = RelayPool(ndb: ndb)
|
||||||
let model_cache = RelayModelCache()
|
let model_cache = RelayModelCache()
|
||||||
|
@ -64,7 +64,7 @@ var test_damus_state: DamusState = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
print("opening \(tempDir!)")
|
print("opening \(tempDir!)")
|
||||||
let ndb = try! Ndb(path: tempDir)!
|
let ndb = Ndb(path: tempDir)!
|
||||||
let our_pubkey = test_pubkey
|
let our_pubkey = test_pubkey
|
||||||
let pool = RelayPool(ndb: ndb)
|
let pool = RelayPool(ndb: ndb)
|
||||||
let settings = UserSettingsStore()
|
let settings = UserSettingsStore()
|
||||||
|
@ -10,7 +10,7 @@ import Security
|
|||||||
|
|
||||||
struct SaveKeysView: View {
|
struct SaveKeysView: View {
|
||||||
let account: CreateAccountModel
|
let account: CreateAccountModel
|
||||||
let pool: RelayPool = RelayPool(ndb: try! Ndb()!)
|
let pool: RelayPool = RelayPool(ndb: Ndb()!)
|
||||||
@State var pub_copied: Bool = false
|
@State var pub_copied: Bool = false
|
||||||
@State var priv_copied: Bool = false
|
@State var priv_copied: Bool = false
|
||||||
@State var loading: Bool = false
|
@State var loading: Bool = false
|
||||||
|
@ -27,7 +27,7 @@ final class ProfileViewTests: XCTestCase {
|
|||||||
let pk4 = Pubkey(hex: "cc590e46363d0fa66bb27081368d01f169b8ffc7c614629d4e9eef6c88b38670")!
|
let pk4 = Pubkey(hex: "cc590e46363d0fa66bb27081368d01f169b8ffc7c614629d4e9eef6c88b38670")!
|
||||||
let pk5 = Pubkey(hex: "f2aa579bb998627e04a8f553842a09446360c9d708c6141dd119c479f6ab9d29")!
|
let pk5 = Pubkey(hex: "f2aa579bb998627e04a8f553842a09446360c9d708c6141dd119c479f6ab9d29")!
|
||||||
|
|
||||||
let ndb = try! Ndb(path: Ndb.db_path)!
|
let ndb = Ndb(path: Ndb.db_path)!
|
||||||
let txn = NdbTxn(ndb: ndb)
|
let txn = NdbTxn(ndb: ndb)
|
||||||
|
|
||||||
let damus_name = "17ldvg64:nq5mhr77"
|
let damus_name = "17ldvg64:nq5mhr77"
|
||||||
|
@ -17,7 +17,24 @@ enum NdbSearchOrder {
|
|||||||
|
|
||||||
class Ndb {
|
class Ndb {
|
||||||
let ndb: ndb_t
|
let ndb: ndb_t
|
||||||
let owns_db_file: Bool // Determines whether this class should be allowed to create or move the db file.
|
|
||||||
|
static func safemode() -> Ndb? {
|
||||||
|
guard let path = db_path ?? old_db_path else { return nil }
|
||||||
|
|
||||||
|
// delete the database and start fresh
|
||||||
|
if Self.db_files_exist(path: path) {
|
||||||
|
let file_manager = FileManager.default
|
||||||
|
for db_file in db_files {
|
||||||
|
try? file_manager.removeItem(atPath: "\(path)/\(db_file)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let ndb = Ndb(path: path) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ndb
|
||||||
|
}
|
||||||
|
|
||||||
// NostrDB used to be stored on the app container's document directory
|
// NostrDB used to be stored on the app container's document directory
|
||||||
static private var old_db_path: String? {
|
static private var old_db_path: String? {
|
||||||
@ -42,7 +59,7 @@ class Ndb {
|
|||||||
Ndb(ndb: ndb_t(ndb: nil))
|
Ndb(ndb: ndb_t(ndb: nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
init?(path: String? = nil, owns_db_file: Bool = true) throws {
|
init?(path: String? = nil, owns_db_file: Bool = true) {
|
||||||
var ndb_p: OpaquePointer? = nil
|
var ndb_p: OpaquePointer? = nil
|
||||||
|
|
||||||
let ingest_threads: Int32 = 4
|
let ingest_threads: Int32 = 4
|
||||||
@ -66,7 +83,7 @@ class Ndb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard let path = path.map(remove_file_prefix) ?? Ndb.db_path else {
|
guard let path = path.map(remove_file_prefix) ?? Ndb.db_path else {
|
||||||
throw Errors.cannot_find_db_path
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let ok = path.withCString { testdir in
|
let ok = path.withCString { testdir in
|
||||||
@ -85,7 +102,6 @@ class Ndb {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
self.owns_db_file = owns_db_file
|
|
||||||
self.ndb = ndb_t(ndb: ndb_p)
|
self.ndb = ndb_t(ndb: ndb_p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +143,6 @@ class Ndb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init(ndb: ndb_t) {
|
init(ndb: ndb_t) {
|
||||||
self.owns_db_file = true
|
|
||||||
self.ndb = ndb
|
self.ndb = ndb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,13 +55,13 @@ final class NdbTests: XCTestCase {
|
|||||||
func test_ndb_init() {
|
func test_ndb_init() {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let ndb = try! Ndb(path: db_dir)!
|
let ndb = Ndb(path: db_dir)!
|
||||||
let ok = ndb.process_events(test_wire_events)
|
let ok = ndb.process_events(test_wire_events)
|
||||||
XCTAssertTrue(ok)
|
XCTAssertTrue(ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let ndb = try! Ndb(path: db_dir)!
|
let ndb = Ndb(path: db_dir)!
|
||||||
let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
|
let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
|
||||||
let txn = NdbTxn(ndb: ndb)
|
let txn = NdbTxn(ndb: ndb)
|
||||||
let note = ndb.lookup_note_with_txn(id: id, txn: txn)
|
let note = ndb.lookup_note_with_txn(id: id, txn: txn)
|
||||||
@ -83,13 +83,13 @@ final class NdbTests: XCTestCase {
|
|||||||
|
|
||||||
func test_ndb_seach() throws {
|
func test_ndb_seach() throws {
|
||||||
do {
|
do {
|
||||||
let ndb = try! Ndb(path: db_dir)!
|
let ndb = Ndb(path: db_dir)!
|
||||||
let ok = ndb.process_events(test_wire_events)
|
let ok = ndb.process_events(test_wire_events)
|
||||||
XCTAssertTrue(ok)
|
XCTAssertTrue(ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let ndb = try! Ndb(path: db_dir)!
|
let ndb = Ndb(path: db_dir)!
|
||||||
let note_ids = ndb.text_search(query: "barked")
|
let note_ids = ndb.text_search(query: "barked")
|
||||||
XCTAssertEqual(note_ids.count, 1)
|
XCTAssertEqual(note_ids.count, 1)
|
||||||
let expected_note_id = NoteId(hex: "b17a540710fe8495b16bfbaf31c6962c4ba8387f3284a7973ad523988095417e")!
|
let expected_note_id = NoteId(hex: "b17a540710fe8495b16bfbaf31c6962c4ba8387f3284a7973ad523988095417e")!
|
||||||
|
Loading…
Reference in New Issue
Block a user