-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBrowserDB.swift
552 lines (468 loc) · 21.7 KB
/
BrowserDB.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import XCGLogger
import Deferred
import Shared
public let NotificationDatabaseWasRecreated = Notification.Name("NotificationDatabaseWasRecreated")
private let log = Logger.syncLogger
public typealias Args = [Any?]
protocol Changeable {
func run(_ sql: String, withArgs args: Args?) -> Success
func run(_ commands: [String]) -> Success
func run(_ commands: [(sql: String, args: Args?)]) -> Success
}
protocol Queryable {
func runQuery<T>(_ sql: String, args: Args?, factory: @escaping (SDRow) -> T) -> Deferred<Maybe<Cursor<T>>>
}
public enum DatabaseOpResult {
case success
case failure
case closed
}
class AttachedDB {
public let filename: String
public let schemaName: String
init(filename: String, schemaName: String) {
self.filename = filename
self.schemaName = schemaName
}
func attach(to browserDB: BrowserDB) -> NSError? {
let file = URL(fileURLWithPath: (try! browserDB.files.getAndEnsureDirectory())).appendingPathComponent(filename).path
let command = "ATTACH DATABASE '\(file)' AS '\(schemaName)'"
return browserDB.db.withConnection(SwiftData.Flags.readWriteCreate, synchronous: true) { connection in
return connection.executeChange(command, withArgs: [])
}
}
func detach(from browserDB: BrowserDB) -> NSError? {
let command = "DETACH DATABASE '\(schemaName)'"
return browserDB.db.withConnection(SwiftData.Flags.readWriteCreate, synchronous: true) { connection in
return connection.executeChange(command, withArgs: [])
}
}
}
// Version 1 - Basic history table.
// Version 2 - Added a visits table, refactored the history table to be a GenericTable.
// Version 3 - Added a favicons table.
// Version 4 - Added a readinglist table.
// Version 5 - Added the clients and the tabs tables.
// Version 6 - Visit timestamps are now microseconds.
// Version 7 - Eliminate most tables. See BrowserTable instead.
open class BrowserDB {
fileprivate let db: SwiftData
// XXX: Increasing this should blow away old history, since we currently don't support any upgrades.
fileprivate let Version: Int = 7
fileprivate let files: FileAccessor
fileprivate let filename: String
fileprivate let secretKey: String?
fileprivate let schemaTable: SchemaTable
fileprivate var attachedDBs: [AttachedDB]
fileprivate var initialized = [String]()
// SQLITE_MAX_VARIABLE_NUMBER = 999 by default. This controls how many ?s can
// appear in a query string.
open static let MaxVariableNumber = 999
public init(filename: String, secretKey: String? = nil, files: FileAccessor, leaveClosed: Bool = false) {
log.debug("Initializing BrowserDB: \(filename).")
self.files = files
self.filename = filename
self.schemaTable = SchemaTable()
self.secretKey = secretKey
self.attachedDBs = []
let file = URL(fileURLWithPath: (try! files.getAndEnsureDirectory())).appendingPathComponent(filename).path
self.db = SwiftData(filename: file, key: secretKey, prevKey: nil)
if AppConstants.BuildChannel == .developer && secretKey != nil {
log.debug("Creating db: \(file) with secret = \(secretKey!)")
}
if !leaveClosed {
self.prepareSchema()
}
}
// Do the same work that we normally do at the end of init.
public func prepareSchema() {
// Create or update will also delete and create the database if our key was incorrect.
switch self.createOrUpdate(self.schemaTable) {
case .failure:
log.error("Failed to create/update the schema table. Aborting.")
fatalError()
case .closed:
log.info("Database not created as the SQLiteConnection is closed.")
SentryIntegration.shared.send(message: "Database not created as the SQLiteConnection is closed.", tag: "BrowserDB", severity: .info)
case .success:
log.debug("db: \(self.filename) has been created")
}
}
// Creates a table and writes its table info into the table-table database.
fileprivate func createTable(_ conn: SQLiteDBConnection, table: SectionCreator) -> TableResult {
log.debug("Try create \(table.name) version \(table.version)")
if !table.create(conn) {
// If creating failed, we'll bail without storing the table info
log.debug("Creation failed.")
return .failed
}
var err: NSError? = nil
guard let result = schemaTable.insert(conn, item: table, err: &err) else {
return .failed
}
return result > -1 ? .created : .failed
}
// Updates a table and writes its table into the table-table database.
// Exposed internally for testing.
func updateTable(_ conn: SQLiteDBConnection, table: SectionUpdater) -> TableResult {
log.debug("Trying update \(table.name) version \(table.version)")
var from = 0
// Try to find the stored version of the table
let cursor = schemaTable.query(conn, options: QueryOptions(filter: table.name))
if cursor.count > 0 {
if let info = cursor[0] as? TableInfoWrapper {
from = info.version
}
}
// If the versions match, no need to update
if from == table.version {
return .exists
}
if !table.updateTable(conn, from: from) {
// If the update failed, we'll bail without writing the change to the table-table.
log.debug("Updating failed.")
return .failed
}
var err: NSError? = nil
// Yes, we UPDATE OR INSERT… because we might be transferring ownership of a database table
// to a different Table. It'll trigger exists, and thus take the update path, but we won't
// necessarily have an existing schema entry -- i.e., we'll be updating from 0.
let insertResult = schemaTable.insert(conn, item: table, err: &err) ?? 0
let updateResult = schemaTable.update(conn, item: table, err: &err)
if updateResult > 0 || insertResult > 0 {
return .updated
}
return .failed
}
// Utility for table classes. They should call this when they're initialized to force
// creation of the table in the database.
func createOrUpdate(_ tables: Table...) -> DatabaseOpResult {
guard !db.closed else {
log.info("Database is closed - skipping schema create/updates")
return .closed
}
var success = true
let doCreate = { (table: Table, connection: SQLiteDBConnection) -> Void in
switch self.createTable(connection, table: table) {
case .created:
success = true
return
case .exists:
log.debug("Table already exists.")
success = true
return
default:
success = false
}
}
if let error = self.db.transaction({ connection -> Bool in
let thread = Thread.current.description
// If the table doesn't exist, we'll create it.
for table in tables {
log.debug("Create or update \(table.name) version \(table.version) on \(thread).")
if !table.exists(connection) {
log.debug("Doesn't exist. Creating table \(table.name).")
doCreate(table, connection)
} else {
// Otherwise, we'll update it
switch self.updateTable(connection, table: table) {
case .updated:
log.debug("Updated table \(table.name).")
success = true
break
case .exists:
log.debug("Table \(table.name) already exists.")
success = true
break
default:
log.error("Update failed for \(table.name). Dropping and recreating.")
let _ = table.drop(connection)
var err: NSError? = nil
let _ = self.schemaTable.delete(connection, item: table, err: &err)
doCreate(table, connection)
}
}
if !success {
log.warning("Failed to configure multiple tables. Aborting.")
return false
}
}
return success
}) {
// Error getting a transaction
log.error("Unable to get a transaction: \(error.localizedDescription)")
SentryIntegration.shared.send(message: "Unable to get a transaction: \(error.localizedDescription)", tag: "BrowserDB", severity: .error)
success = false
}
// If we failed, move the file and try again. This will probably break things that are already
// attached and expecting a working DB, but at least we should be able to restart.
if !success {
// Make sure that we don't still have open the files that we want to move!
// Note that we use sqlite3_close_v2, which might actually _not_ close the
// database file yet. For this reason we move the -shm and -wal files, too.
db.forceClose()
// Attempt to make a backup as long as the DB file still exists
if self.files.exists(self.filename) {
log.warning("Couldn't create or update \(tables.map { $0.name }). Attempting to move \(self.filename) to another location.")
SentryIntegration.shared.send(message: "Couldn't create or update \(tables.map { $0.name }). Attempting to move \(self.filename) to another location.", tag: "BrowserDB", severity: .warning)
// Note that a backup file might already exist! We append a counter to avoid this.
var bakCounter = 0
var bak: String
repeat {
bakCounter += 1
bak = "\(self.filename).bak.\(bakCounter)"
} while self.files.exists(bak)
do {
try self.files.move(self.filename, toRelativePath: bak)
let shm = self.filename + "-shm"
let wal = self.filename + "-wal"
log.debug("Moving \(shm) and \(wal)…")
if self.files.exists(shm) {
log.debug("\(shm) exists.")
try self.files.move(shm, toRelativePath: bak + "-shm")
}
if self.files.exists(wal) {
log.debug("\(wal) exists.")
try self.files.move(wal, toRelativePath: bak + "-wal")
}
log.debug("Finished moving \(self.filename) successfully.")
} catch _ {
log.error("Unable to move \(self.filename) to another location.")
SentryIntegration.shared.send(message: "Unable to move \(self.filename) to another location.", tag: "BrowserDB", severity: .error)
}
} else {
// No backup was attempted since the DB file did not exist
log.error("The DB \(self.filename) has been deleted while previously in use.")
SentryIntegration.shared.send(message: "The DB \(self.filename) has been deleted while previously in use.", tag: "BrowserDB", severity: .info)
}
// Do this after the relevant tables have been created.
defer {
// Notify the world that we moved the database. This allows us to
// reset sync and start over in the case of corruption.
let notify = Notification(name: NotificationDatabaseWasRecreated, object: self.filename)
NotificationCenter.default.post(notify)
}
self.reopenIfClosed()
// Attempt to re-create the DB
success = true
// Re-create all previously-created tables
if let _ = db.transaction({ connection -> Bool in
doCreate(self.schemaTable, connection)
for table in tables {
doCreate(table, connection)
if !success {
log.error("Unable to re-create table '\(table.name)'.")
return false
}
}
return success
}) {
success = false
}
}
return success ? .success : .failure
}
open func attachDB(filename: String, as schemaName: String) {
let attachedDB = AttachedDB(filename: filename, schemaName: schemaName)
if let err = attachedDB.attach(to: self) {
log.error("Error attaching DB. \(err.localizedDescription)")
} else {
self.attachedDBs.append(attachedDB)
}
}
typealias IntCallback = (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> Int
func withConnection<T>(flags: SwiftData.Flags, err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> T {
var res: T!
err = db.withConnection(flags) { connection in
// An error may occur if the internet connection is dropped.
var err: NSError? = nil
res = callback(connection, &err)
return err
}
return res
}
func withConnection<T>(_ err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> T {
/*
* Opening a WAL-using database with a hot journal cannot complete in read-only mode.
* The supported mechanism for a read-only query against a WAL-using SQLite database is to use PRAGMA query_only,
* but this isn't all that useful for us, because we have a mixed read/write workload.
*/
return withConnection(flags: SwiftData.Flags.readWriteCreate, err: &err, callback: callback)
}
func transaction(_ err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> Bool) -> NSError? {
return self.transaction(synchronous: true, err: &err, callback: callback)
}
func transaction(synchronous: Bool=true, err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> Bool) -> NSError? {
return db.transaction(synchronous: synchronous) { connection in
var err: NSError? = nil
return callback(connection, &err)
}
}
}
extension BrowserDB {
func vacuum() {
log.debug("Vacuuming a BrowserDB.")
_ = db.withConnection(SwiftData.Flags.readWriteCreate, synchronous: true) { connection in
return connection.vacuum()
}
}
func checkpoint() {
log.debug("Checkpointing a BrowserDB.")
_ = db.transaction(synchronous: true) { connection in
connection.checkpoint()
return true
}
}
}
extension BrowserDB {
public class func varlist(_ count: Int) -> String {
return "(" + Array(repeating: "?", count: count).joined(separator: ", ") + ")"
}
enum InsertOperation: String {
case Insert = "INSERT"
case Replace = "REPLACE"
case InsertOrIgnore = "INSERT OR IGNORE"
case InsertOrReplace = "INSERT OR REPLACE"
case InsertOrRollback = "INSERT OR ROLLBACK"
case InsertOrAbort = "INSERT OR ABORT"
case InsertOrFail = "INSERT OR FAIL"
}
/**
* Insert multiple sets of values into the given table.
*
* Assumptions:
* 1. The table exists and contains the provided columns.
* 2. Every item in `values` is the same length.
* 3. That length is the same as the length of `columns`.
* 4. Every value in each element of `values` is non-nil.
*
* If there are too many items to insert, multiple individual queries will run
* in sequence.
*
* A failure anywhere in the sequence will cause immediate return of failure, but
* will not roll back — use a transaction if you need one.
*/
func bulkInsert(_ table: String, op: InsertOperation, columns: [String], values: [Args]) -> Success {
// Note that there's a limit to how many ?s can be in a single query!
// So here we execute 999 / (columns * rows) insertions per query.
// Note that we can't use variables for the column names, so those don't affect the count.
if values.isEmpty {
log.debug("No values to insert.")
return succeed()
}
let variablesPerRow = columns.count
// Sanity check.
assert(values[0].count == variablesPerRow)
let cols = columns.joined(separator: ", ")
let queryStart = "\(op.rawValue) INTO \(table) (\(cols)) VALUES "
let varString = BrowserDB.varlist(variablesPerRow)
let insertChunk: ([Args]) -> Success = { vals -> Success in
let valuesString = Array(repeating: varString, count: vals.count).joined(separator: ", ")
let args: Args = vals.flatMap { $0 }
return self.run(queryStart + valuesString, withArgs: args)
}
let rowCount = values.count
if (variablesPerRow * rowCount) < BrowserDB.MaxVariableNumber {
return insertChunk(values)
}
log.debug("Splitting bulk insert across multiple runs. I hope you started a transaction!")
let rowsPerInsert = (999 / variablesPerRow)
let chunks = chunk(values, by: rowsPerInsert)
log.debug("Inserting in \(chunks.count) chunks.")
// There's no real reason why we can't pass the ArraySlice here, except that I don't
// want to keep fighting Swift.
return walk(chunks, f: { insertChunk(Array($0)) })
}
func runWithConnection<T>(_ block: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> Deferred<Maybe<T>> {
return DeferredDBOperation(db: self.db, block: block).start()
}
func write(_ sql: String, withArgs args: Args? = nil) -> Deferred<Maybe<Int>> {
return self.runWithConnection() { (connection, err) -> Int in
err = connection.executeChange(sql, withArgs: args)
if err == nil {
let modified = connection.numberOfRowsModified
log.debug("Modified rows: \(modified).")
return modified
}
return 0
}
}
public func forceClose() {
db.forceClose()
}
public func reopenIfClosed() {
let wasClosed = db.closed
db.reopenIfClosed()
// Need to re-attach any previously-attached DBs if the DB was closed
if wasClosed {
for attachedDB in attachedDBs {
log.debug("Re-attaching DB \(attachedDB.filename) as \(attachedDB.schemaName).")
if let err = attachedDB.attach(to: self) {
log.error("Error re-attaching DB. \(err.localizedDescription)")
}
}
}
}
}
extension BrowserDB: Changeable {
func run(_ sql: String, withArgs args: Args? = nil) -> Success {
return run([(sql, args)])
}
func run(_ commands: [String]) -> Success {
return self.run(commands.map { (sql: $0, args: nil) })
}
/**
* Runs an array of SQL commands. Note: These will all run in order in a transaction and will block
* the caller's thread until they've finished. If any of them fail the operation will abort (no more
* commands will be run) and the transaction will roll back, returning a DatabaseError.
*/
func run(_ commands: [(sql: String, args: Args?)]) -> Success {
if commands.isEmpty {
return succeed()
}
var err: NSError? = nil
let errorResult = self.transaction(&err) { (conn, err) -> Bool in
for (sql, args) in commands {
err = conn.executeChange(sql, withArgs: args)
if let err = err {
log.warning("SQL operation failed: \(err.localizedDescription)")
return false
}
}
return true
}
if let err = err ?? errorResult {
return deferMaybe(DatabaseError(err: err))
}
return succeed()
}
}
extension BrowserDB: Queryable {
func runQuery<T>(_ sql: String, args: Args?, factory: @escaping (SDRow) -> T) -> Deferred<Maybe<Cursor<T>>> {
return runWithConnection { (connection, _) -> Cursor<T> in
return connection.executeQuery(sql, factory: factory, withArgs: args)
}
}
func queryReturnsResults(_ sql: String, args: Args? = nil) -> Deferred<Maybe<Bool>> {
return self.runQuery(sql, args: args, factory: { _ in true })
>>== { deferMaybe($0[0] ?? false) }
}
func queryReturnsNoResults(_ sql: String, args: Args? = nil) -> Deferred<Maybe<Bool>> {
return self.runQuery(sql, args: nil, factory: { _ in false })
>>== { deferMaybe($0[0] ?? true) }
}
}
extension SQLiteDBConnection {
func tablesExist(_ names: [String]) -> Bool {
let count = names.count
let inClause = BrowserDB.varlist(names.count)
let tablesSQL = "SELECT name FROM sqlite_master WHERE type = 'table' AND name IN \(inClause)"
let res = self.executeQuery(tablesSQL, factory: StringFactory, withArgs: names)
log.debug("\(res.count) tables exist. Expected \(count)")
return res.count > 0
}
}