mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Merge pull request #795 from groue/dev/more-checkpoint
Enhanced support for WAL Checkpoints
This commit is contained in:
7 files changed
+72
-35
No files matched your search
+2
-4
@@ -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.
|
||||
|
||||
<!--
|
||||
[Next Release](#next-release)
|
||||
-->
|
||||
|
||||
#### 5.x Releases
|
||||
|
||||
@@ -70,9 +68,9 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
- [0.110.0](#01100), ...
|
||||
|
||||
|
||||
<!--
|
||||
## Next Release
|
||||
-->
|
||||
|
||||
- [#795](https://github.com/groue/GRDB.swift/pull/795): **Breaking Change** Enhanced support for WAL Checkpoints
|
||||
|
||||
|
||||
## 5.0.0-beta.4
|
||||
|
||||
@@ -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()
|
||||
@@ -621,11 +621,35 @@ 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:
|
||||
/// - `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
|
||||
-> (walFrameCount: Int, checkpointedFrameCount: Int)
|
||||
{
|
||||
SchedulingWatchdog.preconditionValidQueue(self)
|
||||
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 (walFrameCount: Int(walFrameCount), checkpointedFrameCount: Int(checkpointedFrameCount))
|
||||
case SQLITE_MISUSE:
|
||||
throw DatabaseError(resultCode: code)
|
||||
default:
|
||||
throw DatabaseError(resultCode: code, message: lastErrorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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() }
|
||||
|
||||
@@ -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)")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.walFrameCount, -1)
|
||||
XCTAssertEqual(result.checkpointedFrameCount, -1)
|
||||
}
|
||||
do {
|
||||
// WAL database
|
||||
let dbPool = try makeDatabasePool()
|
||||
let result = try dbPool.writeWithoutTransaction {
|
||||
try $0.checkpoint()
|
||||
}
|
||||
XCTAssertGreaterThanOrEqual(result.walFrameCount, 0)
|
||||
XCTAssertGreaterThanOrEqual(result.checkpointedFrameCount, 0)
|
||||
}
|
||||
do {
|
||||
// WAL database + TRUNCATE
|
||||
let dbPool = try makeDatabasePool()
|
||||
let result = try dbPool.writeWithoutTransaction {
|
||||
try $0.checkpoint(.truncate)
|
||||
}
|
||||
XCTAssertEqual(result.walFrameCount, 0)
|
||||
XCTAssertEqual(result.checkpointedFrameCount, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user