From 91ed7d762bc7ed8c10143d9ec0f24987365c2617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 09:18:15 +0200 Subject: [PATCH 1/8] Setup busy handler as soon as possible --- GRDB/Core/Database.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index fbe6a3605..e27682996 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -211,9 +211,9 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib /// This method must be called after database initialization func setup() throws { + setupBusyMode() setupDoubleQuotedStringLiterals() try setupForeignKeys() - setupBusyMode() setupDefaultFunctions() setupDefaultCollations() setupAuthorizer() From 5f88a400d51a691f00615bd4000450b85421a737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 10:08:19 +0200 Subject: [PATCH 2/8] DatabasePool.checkpoint(_:) is unavailable --- GRDB/Core/DatabasePool.swift | 18 ------------------ GRDB/Fixit/GRDB-5.0.swift | 3 +++ 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 2cab90a07..bf4d43f63 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -167,24 +167,6 @@ public final class DatabasePool: DatabaseWriter { } } -extension DatabasePool { - - // MARK: - WAL Checkpoints - - /// Runs a WAL checkpoint - /// - /// See https://www.sqlite.org/wal.html and - /// https://www.sqlite.org/c3ref/wal_checkpoint_v2.html) for - /// more information. - /// - /// - parameter kind: The checkpoint mode (default passive) - public func checkpoint(_ kind: Database.CheckpointMode = .passive) throws { - try writer.sync { db in - try db.checkpoint(kind) - } - } -} - extension DatabasePool { // MARK: - Memory management diff --git a/GRDB/Fixit/GRDB-5.0.swift b/GRDB/Fixit/GRDB-5.0.swift index f240beeff..caffee00c 100644 --- a/GRDB/Fixit/GRDB-5.0.swift +++ b/GRDB/Fixit/GRDB-5.0.swift @@ -86,6 +86,9 @@ extension DatabaseMigrator { } extension DatabasePool { + @available(*, unavailable, message: "Use pool.writeWithoutTransaction { $0.checkpoint() } instead") + public func checkpoint(_ kind: Database.CheckpointMode = .passive) throws { preconditionFailure() } + #if os(iOS) @available(*, unavailable, message: "Memory management is now enabled by default. This method does nothing.") public func setupMemoryManagement(in application: UIApplication) { preconditionFailure() } From 56f6984f9d076bd89a0b6fa8a7a8a368f216b204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 10:08:54 +0200 Subject: [PATCH 3/8] Introduce Database.checkpoint(_:on:) --- GRDB/Core/Database.swift | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index e27682996..da30e4bad 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -621,11 +621,34 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib } } - // MARK: - Checkpoints + // MARK: - WAL Checkpoints - func checkpoint(_ kind: Database.CheckpointMode) throws { - let code = sqlite3_wal_checkpoint_v2(sqliteConnection, nil, kind.rawValue, nil, nil) - guard code == SQLITE_OK else { + /// Runs a WAL checkpoint. + /// + /// See https://www.sqlite.org/wal.html and + /// https://www.sqlite.org/c3ref/wal_checkpoint_v2.html for + /// more information. + /// + /// - parameter kind: The checkpoint mode (default passive) + /// - parameter dbName: The database name (default "main") + /// - returns: A tuple: + /// - `logCount`: the total number of frames in the log file + /// - `checkpointCount`: the total number of checkpointed frames in the + /// log file + @discardableResult + public func checkpoint(_ kind: Database.CheckpointMode = .passive, on dbName: String? = "main") throws + -> (logCount: Int, checkpointCount: Int) + { + SchedulingWatchdog.preconditionValidQueue(self) + var logCount: CInt = -1 + var checkpointCount: CInt = -1 + let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, &logCount, &checkpointCount) + switch code { + case SQLITE_OK: + return (logCount: Int(logCount), checkpointCount: Int(checkpointCount)) + case SQLITE_MISUSE: + throw DatabaseError(resultCode: code) + default: throw DatabaseError(resultCode: code, message: lastErrorMessage) } } From 6f3ec89bcd0dbb8404dc11830102ec108b100310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 10:09:24 +0200 Subject: [PATCH 4/8] Update checkpointing tests --- Tests/GRDBTests/DatabasePoolConcurrencyTests.swift | 8 ++++---- Tests/GRDBTests/DatabaseSnapshotTests.swift | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift index 73e31caab..842da567e 100644 --- a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift +++ b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift @@ -442,8 +442,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "DELETE FROM items") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -538,8 +538,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -759,8 +759,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "DELETE FROM items") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -803,8 +803,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } diff --git a/Tests/GRDBTests/DatabaseSnapshotTests.swift b/Tests/GRDBTests/DatabaseSnapshotTests.swift index dc7bdb8e2..df3c8cb4a 100644 --- a/Tests/GRDBTests/DatabaseSnapshotTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotTests.swift @@ -270,7 +270,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.passive) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.passive) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -281,7 +281,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.full) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.full) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -292,7 +292,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.restart) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.restart) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -303,7 +303,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.truncate) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.truncate) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) From 3e5a21b49b249fec93e8c646d39c525c45bd155e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 10:09:48 +0200 Subject: [PATCH 5/8] Tests for Database.checkpoint(_:on:) --- Tests/GRDBTests/DatabaseTests.swift | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/GRDBTests/DatabaseTests.swift b/Tests/GRDBTests/DatabaseTests.swift index 14e735724..7db71fbc2 100644 --- a/Tests/GRDBTests/DatabaseTests.swift +++ b/Tests/GRDBTests/DatabaseTests.swift @@ -345,4 +345,34 @@ class DatabaseTests : GRDBTestCase { try db.execute(sql: "INSERT INTO t DEFAULT VALUES") } } + + func testCheckpoint() throws { + do { + // Not a WAL database + let dbQueue = try makeDatabaseQueue() + let result = try dbQueue.inDatabase { + try $0.checkpoint() + } + XCTAssertEqual(result.logCount, -1) + XCTAssertEqual(result.checkpointCount, -1) + } + do { + // WAL database + let dbPool = try makeDatabasePool() + let result = try dbPool.writeWithoutTransaction { + try $0.checkpoint() + } + XCTAssertGreaterThanOrEqual(result.logCount, 0) + XCTAssertGreaterThanOrEqual(result.checkpointCount, 0) + } + do { + // WAL database + TRUNCATE + let dbPool = try makeDatabasePool() + let result = try dbPool.writeWithoutTransaction { + try $0.checkpoint(.truncate) + } + XCTAssertEqual(result.logCount, 0) + XCTAssertEqual(result.checkpointCount, 0) + } + } } From 3a42a4bf9f98481a3bb4d65e5379e1fe0116257b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 10:18:52 +0200 Subject: [PATCH 6/8] Rename checkpoint tuple members --- GRDB/Core/Database.swift | 16 ++++++++-------- Tests/GRDBTests/DatabaseTests.swift | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index da30e4bad..6ffbe95e0 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -632,20 +632,20 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib /// - parameter kind: The checkpoint mode (default passive) /// - parameter dbName: The database name (default "main") /// - returns: A tuple: - /// - `logCount`: the total number of frames in the log file - /// - `checkpointCount`: the total number of checkpointed frames in the - /// log file + /// - `walFrameCount`: the total number of frames in the log file + /// - `checkpointedFrameCount`: the total number of checkpointed frames + /// in the log file @discardableResult public func checkpoint(_ kind: Database.CheckpointMode = .passive, on dbName: String? = "main") throws - -> (logCount: Int, checkpointCount: Int) + -> (walFrameCount: Int, checkpointedFrameCount: Int) { SchedulingWatchdog.preconditionValidQueue(self) - var logCount: CInt = -1 - var checkpointCount: CInt = -1 - let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, &logCount, &checkpointCount) + var walFrameCount: CInt = -1 + var checkpointedFrameCount: CInt = -1 + let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, &walFrameCount, &checkpointedFrameCount) switch code { case SQLITE_OK: - return (logCount: Int(logCount), checkpointCount: Int(checkpointCount)) + return (walFrameCount: Int(walFrameCount), checkpointedFrameCount: Int(checkpointedFrameCount)) case SQLITE_MISUSE: throw DatabaseError(resultCode: code) default: diff --git a/Tests/GRDBTests/DatabaseTests.swift b/Tests/GRDBTests/DatabaseTests.swift index 7db71fbc2..6bb8f265a 100644 --- a/Tests/GRDBTests/DatabaseTests.swift +++ b/Tests/GRDBTests/DatabaseTests.swift @@ -353,8 +353,8 @@ class DatabaseTests : GRDBTestCase { let result = try dbQueue.inDatabase { try $0.checkpoint() } - XCTAssertEqual(result.logCount, -1) - XCTAssertEqual(result.checkpointCount, -1) + XCTAssertEqual(result.walFrameCount, -1) + XCTAssertEqual(result.checkpointedFrameCount, -1) } do { // WAL database @@ -362,8 +362,8 @@ class DatabaseTests : GRDBTestCase { let result = try dbPool.writeWithoutTransaction { try $0.checkpoint() } - XCTAssertGreaterThanOrEqual(result.logCount, 0) - XCTAssertGreaterThanOrEqual(result.checkpointCount, 0) + XCTAssertGreaterThanOrEqual(result.walFrameCount, 0) + XCTAssertGreaterThanOrEqual(result.checkpointedFrameCount, 0) } do { // WAL database + TRUNCATE @@ -371,8 +371,8 @@ class DatabaseTests : GRDBTestCase { let result = try dbPool.writeWithoutTransaction { try $0.checkpoint(.truncate) } - XCTAssertEqual(result.logCount, 0) - XCTAssertEqual(result.checkpointCount, 0) + XCTAssertEqual(result.walFrameCount, 0) + XCTAssertEqual(result.checkpointedFrameCount, 0) } } } From 3103e6e373f7c52eee0ddba47ddaf6d4cec291be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 15:55:46 +0200 Subject: [PATCH 7/8] Fix Swiftlint warning --- GRDB/Core/Database.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 6ffbe95e0..da522e3f4 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -642,7 +642,8 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib SchedulingWatchdog.preconditionValidQueue(self) var walFrameCount: CInt = -1 var checkpointedFrameCount: CInt = -1 - let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, &walFrameCount, &checkpointedFrameCount) + let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, + &walFrameCount, &checkpointedFrameCount) switch code { case SQLITE_OK: return (walFrameCount: Int(walFrameCount), checkpointedFrameCount: Int(checkpointedFrameCount)) From e31f7a4763b63a2cd4bec73e4abb8796c2b2ed36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Jun 2020 16:39:13 +0200 Subject: [PATCH 8/8] CHANGELOG --- CHANGELOG.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be6021f2c..705dfa9f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,7 @@ All notable changes to this project will be documented in this file. GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library. - #### 5.x Releases @@ -70,9 +68,9 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - [0.110.0](#01100), ... - + +- [#795](https://github.com/groue/GRDB.swift/pull/795): **Breaking Change** Enhanced support for WAL Checkpoints ## 5.0.0-beta.4