From 9dbf992c40292c81d03293a08f48b79b76601349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 19 May 2016 18:10:36 +0200 Subject: [PATCH] Configuration.maximumReaderCount replaces DatabasePool initializer option --- CHANGELOG.md | 13 ++++++++ GRDB/Core/Configuration.swift | 56 ++++++++++++++++++++++------------- GRDB/Core/Database.swift | 4 +-- GRDB/Core/DatabasePool.swift | 6 ++-- TODO.md | 1 - 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b774390..aab6e07fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ Release Notes ============= +## Next Release + +**Breaking Changes** + +- The maximum number of reader connections in a database pool is now configured in a Configuration object. + + ```diff + final class DatabasePool { + - init(path: String, configuration: Configuration = default) throws + + init(path: String, configuration: Configuration = default, maximumReaderCount: Int = default) throws + ``` + + ## 0.65.0 Released May 19, 2016 diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index f1237ecc3..a3b04808e 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -17,20 +17,32 @@ public struct Configuration { // MARK: - Misc options + /// If true, foreign key constraints are checked. + /// + /// Default: true public var foreignKeysEnabled: Bool = true + + /// If true, database modifications are disallowed. + /// + /// Default: false public var readonly: Bool = false /// A function that is called on every statement executed by the database. + /// + /// Default: nil public var trace: TraceFunction? // MARK: - Encryption #if SQLITE_HAS_CODEC - /// The passphrase for encrypted database + /// The passphrase for encrypted database. + /// + /// Default: nil public var passphrase: String? #endif + // MARK: - File Attributes /// The file attributes that should be applied to the database files (see @@ -43,45 +55,47 @@ public struct Configuration { /// `-shm` and `-wal` files. /// /// GRDB will apply file attributes to all those files. + /// + /// Default: nil public var fileAttributes: [String: AnyObject]? = nil // MARK: - Transactions /// The default kind of transaction. + /// + /// Default: Immediate public var defaultTransactionKind: TransactionKind = .Immediate // MARK: - Concurrency + /// The behavior in case of SQLITE_BUSY error. See https://www.sqlite.org/rescode.html#busy + /// + /// Default: ImmediateError public var busyMode: BusyMode = .ImmediateError + /// The maximum number of concurrent readers (applies to database + /// pools only). + /// + /// Default: 5 + public var maximumReaderCount: Int = 5 - // MARK: - Internal + + // MARK: - Factory Configuration + + /// Creates a factory configuration + public init() { } + + + // MARK: - Not Public var threadingMode: ThreadingMode = .Default var SQLiteConnectionDidOpen: (() -> ())? var SQLiteConnectionWillClose: ((SQLiteConnection) -> ())? var SQLiteConnectionDidClose: (() -> ())? - - - // MARK: - Factory Configuration - - /// Returns a factory configuration: - /// - /// - readonly: false - /// - fileAttributes: nil - /// - foreignKeysEnabled: true - /// - trace: nil - /// - defaultTransactionKind: .Immediate - /// - busyMode: .ImmediateError - public init() { } - - - // MARK: - SQLite flags - - var sqliteOpenFlags: Int32 { - return threadingMode.sqliteOpenFlags | (readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)) + var SQLiteOpenFlags: Int32 { + return threadingMode.SQLiteOpenFlags | (readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)) } } diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 7b59e8ad4..4d234e33f 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -104,7 +104,7 @@ public final class Database { init(path: String, configuration: Configuration, schemaCache: DatabaseSchemaCacheType) throws { // See https://www.sqlite.org/c3ref/open.html var sqliteConnection: SQLiteConnection = nil - let code = sqlite3_open_v2(path, &sqliteConnection, configuration.sqliteOpenFlags, nil) + let code = sqlite3_open_v2(path, &sqliteConnection, configuration.SQLiteOpenFlags, nil) guard code == SQLITE_OK else { throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) } @@ -280,7 +280,7 @@ enum ThreadingMode { case MultiThread case Serialized - var sqliteOpenFlags: Int32 { + var SQLiteOpenFlags: Int32 { switch self { case .Default: return 0 diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index c9565b523..7e392c272 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -30,8 +30,8 @@ public final class DatabasePool { /// - configuration: A configuration. /// - maximumReaderCount: The maximum number of readers. Default is 5. /// - throws: A DatabaseError whenever an SQLite error occurs. - public init(path: String, configuration: Configuration = Configuration(), maximumReaderCount: Int = 5) throws { - GRDBPrecondition(maximumReaderCount > 1, "maximumReaderCount must be at least 1") + public init(path: String, configuration: Configuration = Configuration()) throws { + GRDBPrecondition(configuration.maximumReaderCount > 0, "configuration.maximumReaderCount must be at least 1") // Database Store store = try DatabaseStore(path: path, attributes: configuration.fileAttributes) @@ -64,7 +64,7 @@ public final class DatabasePool { readerConfig.readonly = true readerConfig.defaultTransactionKind = .Deferred // Make it the default. Other transaction kinds are forbidden by SQLite in read-only connections. - readerPool = Pool(maximumCount: maximumReaderCount) + readerPool = Pool(maximumCount: configuration.maximumReaderCount) readerPool.makeElement = { [unowned self] in let serializedDatabase = try! SerializedDatabase( path: path, diff --git a/TODO.md b/TODO.md index f9b0c6b46..49aa56935 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ - [ ] FetchedRecordsController: see if we can replace identity comparison function with a function that returns an Equatable value (beware the generics trouble). See if this can help optimize memory use, and use a sortedMerge() algorithm. -- [ ] DatabasePool: move maximumReaderCount to Configuration - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft - [ ] What is the SQLITE_OPEN_WAL open flag?