Skip to content

Add an optional target queue to for database operations #801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions Sources/SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,21 @@ public final class Connection {
///
/// Default: `false`.
///
/// - target: An optional target queue for database operations to be executed on.
/// Set this to a custom serial queue if you need to schedule work that must be
/// synchronized with respect to database operations.
///
/// **Important:** The target queue **must not** be a concurrent queue, or access
/// to the returned connection is no longer guaranteed be thread-safe.
///
/// - Throws: `Result.Error` iff a connection cannot be established.
///
/// - Returns: A new database connection.
public init(_ location: Location = .inMemory, readonly: Bool = false) throws {
public init(_ location: Location = .inMemory, readonly: Bool = false, target: DispatchQueue? = nil) throws {
queue = DispatchQueue(label: "SQLite.Database", target: target)
let flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
try check(sqlite3_open_v2(location.description, &_handle, flags | SQLITE_OPEN_FULLMUTEX, nil))
queue.setSpecific(key: Connection.queueKey, value: queueContext)
(target ?? queue).setSpecific(key: queueKey, value: queueContext)
}

/// Initializes a new connection to a database.
Expand All @@ -119,11 +129,18 @@ public final class Connection {
///
/// Default: `false`.
///
/// - target: An optional target queue for database operations to be executed on.
/// Set this to a custom serial queue if you need to schedule work that must be
/// synchronized with respect to database operations.
///
/// **Important:** The target queue **must not** be a concurrent queue, or access
/// to the returned connection is no longer guaranteed be thread-safe.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be asserted in code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be nice! Currently I'm not aware of public API to check whether a queue is serial or concurrent. 🤔

///
/// - Throws: `Result.Error` iff a connection cannot be established.
///
/// - Returns: A new database connection.
public convenience init(_ filename: String, readonly: Bool = false) throws {
try self.init(.uri(filename), readonly: readonly)
public convenience init(_ filename: String, readonly: Bool = false, target: DispatchQueue? = nil) throws {
try self.init(.uri(filename), readonly: readonly, target: target)
}

deinit {
Expand Down Expand Up @@ -631,7 +648,7 @@ public final class Connection {
// MARK: - Error Handling

func sync<T>(_ block: () throws -> T) rethrows -> T {
if DispatchQueue.getSpecific(key: Connection.queueKey) == queueContext {
if DispatchQueue.getSpecific(key: queueKey) == queueContext {
return try block()
} else {
return try queue.sync(execute: block)
Expand All @@ -646,9 +663,9 @@ public final class Connection {
throw error
}

fileprivate var queue = DispatchQueue(label: "SQLite.Database", attributes: [])
fileprivate var queue: DispatchQueue

fileprivate static let queueKey = DispatchSpecificKey<Int>()
fileprivate let queueKey = DispatchSpecificKey<Int>()

fileprivate lazy var queueContext: Int = unsafeBitCast(self, to: Int.self)

Expand Down