mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
DispatchQueue creation cleanup
This commit is contained in:
1 parent
769b247da9
commit
f07eee3ca7
7 files changed
+30
-37
No files matched your search
@@ -171,6 +171,15 @@ public struct Configuration {
|
||||
let readWriteFlags = readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)
|
||||
return threadingMode.SQLiteOpenFlags | readWriteFlags
|
||||
}
|
||||
|
||||
func makeDispatchQueue(defaultLabel: String, purpose: String? = nil) -> DispatchQueue {
|
||||
let label = (self.label ?? defaultLabel) + (purpose.map { "." + $0 } ?? "")
|
||||
if let targetQueue = targetQueue {
|
||||
return DispatchQueue(label: label, target: targetQueue)
|
||||
} else {
|
||||
return DispatchQueue(label: label, qos: qos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A tracing function that takes an SQL string.
|
||||
|
||||
@@ -52,7 +52,8 @@ public final class DatabasePool: DatabaseWriter {
|
||||
path: path,
|
||||
configuration: configuration,
|
||||
schemaCache: SimpleDatabaseSchemaCache(),
|
||||
label: (configuration.label ?? "GRDB.DatabasePool") + ".writer")
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "writer")
|
||||
|
||||
// Activate WAL Mode unless readonly
|
||||
if !configuration.readonly {
|
||||
@@ -89,7 +90,8 @@ public final class DatabasePool: DatabaseWriter {
|
||||
path: path,
|
||||
configuration: self.readerConfig,
|
||||
schemaCache: SimpleDatabaseSchemaCache(),
|
||||
label: (self.readerConfig.label ?? "GRDB.DatabasePool") + ".reader.\(readerCount)")
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "reader.\(readerCount)")
|
||||
reader.sync { self.setupDatabase($0) }
|
||||
return reader
|
||||
})
|
||||
@@ -757,7 +759,8 @@ extension DatabasePool {
|
||||
let snapshot = try DatabaseSnapshot(
|
||||
path: path,
|
||||
configuration: writer.configuration,
|
||||
labelSuffix: ".snapshot.\(snapshotCount.increment())")
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "snapshot.\(snapshotCount.increment())")
|
||||
snapshot.read { setupDatabase($0) }
|
||||
return snapshot
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public final class DatabaseQueue: DatabaseWriter {
|
||||
path: path,
|
||||
configuration: configuration,
|
||||
schemaCache: SimpleDatabaseSchemaCache(),
|
||||
label: configuration.label ?? "GRDB.DatabaseQueue")
|
||||
defaultLabel: "GRDB.DatabaseQueue")
|
||||
}
|
||||
|
||||
/// Opens an in-memory SQLite database.
|
||||
@@ -56,7 +56,7 @@ public final class DatabaseQueue: DatabaseWriter {
|
||||
path: ":memory:",
|
||||
configuration: configuration,
|
||||
schemaCache: SimpleDatabaseSchemaCache(),
|
||||
label: configuration.label ?? "GRDB.DatabaseQueue")
|
||||
defaultLabel: "GRDB.DatabaseQueue")
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
@@ -14,7 +14,7 @@ public class DatabaseSnapshot : DatabaseReader {
|
||||
return serializedDatabase.configuration
|
||||
}
|
||||
|
||||
init(path: String, configuration: Configuration = Configuration(), labelSuffix: String) throws {
|
||||
init(path: String, configuration: Configuration = Configuration(), defaultLabel: String, purpose: String) throws {
|
||||
var configuration = configuration
|
||||
configuration.readonly = true
|
||||
configuration.allowsUnsafeTransactions = true // Snaphost keeps a long-lived transaction
|
||||
@@ -23,7 +23,8 @@ public class DatabaseSnapshot : DatabaseReader {
|
||||
path: path,
|
||||
configuration: configuration,
|
||||
schemaCache: SimpleDatabaseSchemaCache(),
|
||||
label: (configuration.label ?? "GRDB.DatabasePool") + labelSuffix)
|
||||
defaultLabel: defaultLabel,
|
||||
purpose: purpose)
|
||||
|
||||
try serializedDatabase.sync { db in
|
||||
// Assert WAL mode
|
||||
|
||||
@@ -22,7 +22,7 @@ import Dispatch
|
||||
/// - preconditionValidQueue() crashes whenever a database is used in an invalid
|
||||
/// dispatch queue.
|
||||
final class SchedulingWatchdog {
|
||||
private static let specificKey = DispatchSpecificKey<SchedulingWatchdog>()
|
||||
private static let watchDogKey = DispatchSpecificKey<SchedulingWatchdog>()
|
||||
private(set) var allowedDatabases: [Database]
|
||||
var databaseObservationBroker: DatabaseObservationBroker?
|
||||
|
||||
@@ -30,22 +30,10 @@ final class SchedulingWatchdog {
|
||||
allowedDatabases = [database]
|
||||
}
|
||||
|
||||
static func makeSerializedQueue(
|
||||
allowingDatabase database: Database,
|
||||
label: String,
|
||||
qos: DispatchQoS,
|
||||
targetQueue: DispatchQueue?)
|
||||
-> DispatchQueue
|
||||
{
|
||||
let queue: DispatchQueue
|
||||
if let targetQueue = targetQueue {
|
||||
queue = DispatchQueue(label: label, target: targetQueue)
|
||||
} else {
|
||||
queue = DispatchQueue(label: label, qos: qos)
|
||||
}
|
||||
static func allowDatabase(_ database: Database, onQueue queue: DispatchQueue) {
|
||||
precondition(queue.getSpecific(key: watchDogKey) == nil)
|
||||
let watchdog = SchedulingWatchdog(allowedDatabase: database)
|
||||
queue.setSpecific(key: specificKey, value: watchdog)
|
||||
return queue
|
||||
queue.setSpecific(key: watchDogKey, value: watchdog)
|
||||
}
|
||||
|
||||
func inheritingAllowedDatabases<T>(from other: SchedulingWatchdog, execute body: () throws -> T) rethrows -> T {
|
||||
@@ -60,7 +48,7 @@ final class SchedulingWatchdog {
|
||||
}
|
||||
|
||||
static var current: SchedulingWatchdog? {
|
||||
return DispatchQueue.getSpecific(key: specificKey)
|
||||
return DispatchQueue.getSpecific(key: watchDogKey)
|
||||
}
|
||||
|
||||
func allows(_ db: Database) -> Bool {
|
||||
|
||||
@@ -16,7 +16,7 @@ final class SerializedDatabase {
|
||||
/// The dispatch queue
|
||||
private let queue: DispatchQueue
|
||||
|
||||
init(path: String, configuration: Configuration = Configuration(), schemaCache: DatabaseSchemaCache, label: String) throws {
|
||||
init(path: String, configuration: Configuration = Configuration(), schemaCache: DatabaseSchemaCache, defaultLabel: String, purpose: String? = nil) throws {
|
||||
// According to https://www.sqlite.org/threadsafe.html
|
||||
//
|
||||
// > SQLite support three different threading modes:
|
||||
@@ -37,13 +37,10 @@ final class SerializedDatabase {
|
||||
var config = configuration
|
||||
config.threadingMode = .multiThread
|
||||
|
||||
db = try Database(path: path, configuration: config, schemaCache: schemaCache)
|
||||
queue = SchedulingWatchdog.makeSerializedQueue(
|
||||
allowingDatabase: db,
|
||||
label: label,
|
||||
qos: configuration.qos,
|
||||
targetQueue: configuration.targetQueue)
|
||||
self.path = path
|
||||
self.db = try Database(path: path, configuration: config, schemaCache: schemaCache)
|
||||
self.queue = configuration.makeDispatchQueue(defaultLabel: defaultLabel, purpose: purpose)
|
||||
SchedulingWatchdog.allowDatabase(db, onQueue: queue)
|
||||
try queue.sync {
|
||||
try db.setup()
|
||||
}
|
||||
|
||||
@@ -27,12 +27,7 @@ class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
|
||||
self.notificationQueue = notificationQueue
|
||||
self.onChange = onChange
|
||||
self.onError = onError
|
||||
let reduceQueueLabel = (configuration.label ?? "GRDB") + ".ValueObservation.reducer"
|
||||
if let targetQueue = configuration.targetQueue {
|
||||
self.reduceQueue = DispatchQueue(label: reduceQueueLabel, target: targetQueue)
|
||||
} else {
|
||||
self.reduceQueue = DispatchQueue(label: reduceQueueLabel, qos: configuration.qos)
|
||||
}
|
||||
self.reduceQueue = configuration.makeDispatchQueue(defaultLabel: "GRDB", purpose: "ValueObservation.reducer")
|
||||
}
|
||||
|
||||
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {
|
||||
|
||||
Reference in new issue
Block a user