From b58a4e90da92458cf40b41e2af8ccd83abea59e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 1 Dec 2016 18:13:00 +0100 Subject: [PATCH] WIP DatabaseWriter/DatabaseReader improvements --- GRDB/Core/DatabasePool.swift | 84 +++++++++++++++++++++++---- GRDB/Core/DatabaseQueue.swift | 10 +++- GRDB/Core/DatabaseReader.swift | 25 ++++---- GRDB/Core/DatabaseWriter.swift | 103 +++++++++++++++++++-------------- 4 files changed, 152 insertions(+), 70 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index c158d2500..23add5be0 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -417,21 +417,63 @@ extension DatabasePool : DatabaseWriter { // MARK: - Writing in Database - /// Synchronously executes an update block in a protected dispatch queue, - /// and returns its result. + /// Synchronously executes a block in a protected dispatch queue, wrapped + /// inside a deferred transaction. /// - /// try dbPool.unsafeWrite { db in - /// try db.execute(...) - /// } + /// Eventual concurrent database updates are postponed until the block + /// has executed. + /// + /// Eventual concurrent readers do not see partial changes: + /// + /// dbPool.write { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } + /// + /// dbPool.read { db in + /// // Here the balance is guaranteed to be zero + /// } /// /// This method is *not* reentrant. + public func write(_ block: (Database) throws -> T) throws -> T { + var result: T! = nil + try writeInTransaction(.deferred) { db in + result = try block(db) + return .commit + } + return result! + } + + /// Synchronously executes a block that takes a database connection, without + /// opening any transaction, and returns its result. /// - /// - warning: As soon as you execute several update statements, readers may - /// see the database in an inconsistent state. To preserve readers - /// integrity, do not perform more than one database modification with - /// this method, or use write() or writeInTransaction(). - /// - parameter block: A block that accesses the database. - /// - throws: The error thrown by the block. + /// Eventual concurrent database updates are postponed until the block + /// has executed. + /// + /// Eventual concurrent readers do not see partial changes: + /// + /// - warning: This method poses a threat to concurrent reads if it modifies + /// the database outside of a transaction. Readers may see the database + /// in an inconsistent state: + /// + /// dbPool.unsafeWrite { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } + /// + /// dbPool.read { db in + /// // Here the balance may not be zero + /// } + /// + /// To use this unsafe method safely, don't modify the database, or make + /// sure you wrap in a transaction changes that must occur together: + /// + /// // A safe usage of the unsafeWrite method + /// dbPool.unsafeWrite { db in + /// db.inTransaction { ... } + /// } public func unsafeWrite(_ block: (Database) throws -> T) rethrows -> T { return try writer.sync(block) } @@ -439,14 +481,30 @@ extension DatabasePool : DatabaseWriter { /// Synchronously executes a block in a protected dispatch queue, wrapped /// inside a transaction. /// + /// Eventual concurrent database updates are postponed until the block + /// has executed. + /// /// If the block throws an error, the transaction is rollbacked and the - /// error is rethrown. + /// error is rethrown. If the block returns .rollback, the transaction is + /// also rollbacked, but no error is thrown. /// /// try dbPool.writeInTransaction { db in /// db.execute(...) /// return .commit /// } /// + /// Eventual concurrent readers do not see partial changes: + /// + /// dbPool.writeInTransaction { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } + /// + /// dbPool.read { db in + /// // Here the balance is guaranteed to be zero + /// } + /// /// This method is *not* reentrant. /// /// - parameters: @@ -477,6 +535,8 @@ extension DatabasePool : DatabaseWriter { /// committed state at the moment this method is called. Eventual concurrent /// database updates are *not visible* inside the block. /// + /// TODO: THIS IS INACCURATE + /// /// try dbPool.write { db in /// try db.execute("DELETE FROM persons") /// try dbPool.readFromCurrentState { db in diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index f26061ce2..59a406156 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -88,7 +88,8 @@ public final class DatabaseQueue { /// inside a transaction. /// /// If the block throws an error, the transaction is rollbacked and the - /// error is rethrown. + /// error is rethrown. If the block returns .rollback, the transaction is + /// also rollbacked, but no error is thrown. /// /// try dbQueue.inTransaction { db in /// db.execute(...) @@ -305,6 +306,13 @@ extension DatabaseQueue : DatabaseWriter { return try inDatabase(block) } + /// Alias for inDatabase + /// + /// This method is part of the DatabaseWriter protocol adoption. + public func write(_ block: (Database) throws -> T) rethrows -> T { + return try inDatabase(block) + } + /// Alias for inTransaction /// /// This method is part of the DatabaseWriter protocol adoption. diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 7e449a85f..e0d2ef80f 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -38,8 +38,8 @@ public protocol DatabaseReader : class { /// Synchronously executes a read-only block that takes a database /// connection, and returns its result. /// - /// The *block* argument is completely isolated. Eventual concurrent - /// database updates are *not visible* inside the block: + /// The block argument is isolated. Eventual concurrent database updates are + /// not visible inside the block: /// /// try reader.read { db in /// // Those two values are guaranteed to be equal, even if the @@ -61,17 +61,8 @@ public protocol DatabaseReader : class { /// Synchronously executes a read-only block that takes a database /// connection, and returns its result. /// - /// Individual statements executed in the *block* argument are executed - /// in isolation from eventual concurrent updates: - /// - /// try reader.unsafeRead { db in - /// // no external update can mess with this iteration: - /// let rows = try Row.fetchCursor(db, ...) - /// while let row = try rows.next() { ... } - /// } - /// - /// However, there is no guarantee that consecutive statements have the - /// same results: + /// The block argument is not isolated: eventual concurrent database updates + /// are visible inside the block: /// /// try reader.unsafeRead { db in /// // Those two ints may be different: @@ -80,6 +71,14 @@ public protocol DatabaseReader : class { /// let int2 = try Int.fetchOne(db, sql) /// } /// + /// Cursor iteration is safe, though: + /// + /// try reader.unsafeRead { db in + /// // No concurrent update can mess with this iteration: + /// let rows = try Row.fetchCursor(db, "SELECT ...") + /// while let row = try rows.next() { ... } + /// } + /// /// - parameter block: A block that accesses the database. /// - throws: The error thrown by the block, or any DatabaseError that would /// happen while establishing the read access to the database. diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index db5731c7c..e940c2e8a 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -18,24 +18,54 @@ public protocol DatabaseWriter : DatabaseReader { // MARK: - Writing in Database /// Synchronously executes a block that takes a database connection, and - /// returns its result. + /// returns its result. The block may or may not be wrapped in a + /// transaction. /// - /// The *block* argument is completely isolated. Eventual concurrent - /// database updates are postponed until the block has executed. + /// Eventual concurrent database updates are postponed until the block + /// has executed. + /// + /// Eventual concurrent readers are isolated and do not see partial changes: + /// + /// writer.write { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } + /// + /// writer.read { db in + /// // Here the balance is guaranteed to be zero + /// } + /// + /// This method is *not* reentrant. func write(_ block: (Database) throws -> T) throws -> T - /// Synchronously executes a block in a protected dispatch queue, wrapped /// inside a transaction. /// + /// Eventual concurrent database updates are postponed until the block + /// has executed. + /// /// If the block throws an error, the transaction is rollbacked and the - /// error is rethrown. + /// error is rethrown. If the block returns .rollback, the transaction is + /// also rollbacked, but no error is thrown. /// /// try writer.writeInTransaction { db in /// db.execute(...) /// return .commit /// } /// + /// Eventual concurrent readers are isolated and do not see partial changes: + /// + /// writer.writeInTransaction { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } + /// + /// writer.read { db in + /// // Here the balance is guaranteed to be zero + /// } + /// /// This method is *not* reentrant. /// /// - parameters: @@ -46,26 +76,33 @@ public protocol DatabaseWriter : DatabaseReader { /// - throws: The error thrown by the block. func writeInTransaction(_ kind: Database.TransactionKind?, _ block: (Database) throws -> Database.TransactionCompletion) throws - /// Synchronously executes a block that takes a database connection, and - /// returns its result. + /// Synchronously executes a block that takes a database connection, without + /// opening any transaction, and returns its result. /// - /// The *block* argument is completely isolated: eventual concurrent - /// database updates are postponed until the block has executed. + /// Eventual concurrent database updates are postponed until the block + /// has executed. /// - /// However, this method poses a threat to concurrent reads if it modifies - /// the database. Readers may seen the database in a constant, but - /// inconsistent state: + /// - warning: This method poses a threat to concurrent reads if it modifies + /// the database outside of a transaction. Readers may see the database + /// in constant but inconsistent state: /// - /// writer.unsafeWrite { db in - /// // Preserve a zero balance - /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) - /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) - /// } + /// writer.unsafeWrite { db in + /// // Eventually preserve a zero balance + /// try db.execute(db, "INSERT INTO credits ...", arguments: [amount]) + /// try db.execute(db, "INSERT INTO debits ...", arguments: [amount]) + /// } /// - /// writer.read { db in - /// // Here the balance may not be zero - /// } + /// writer.read { db in + /// // Here the balance may not be zero + /// } /// + /// To use this unsafe method safely, don't modify the database, or make + /// sure you wrap in a transaction changes that must occur together: + /// + /// // A safe usage of the unsafeWrite method + /// writer.unsafeWrite { db in + /// db.inTransaction { ... } + /// } func unsafeWrite(_ block: (Database) throws -> T) rethrows -> T @@ -85,6 +122,8 @@ public protocol DatabaseWriter : DatabaseReader { /// - When this method is called outside of any transaction, the current /// state is the last committed state. /// + /// TODO: THIS IS INACCURATE + /// /// try writer.write { db in /// try db.execute("DELETE FROM persons") /// try writer.readFromCurrentState { db in @@ -134,30 +173,6 @@ public protocol DatabaseWriter : DatabaseReader { func readFromCurrentState(_ block: @escaping (Database) -> Void) throws } -extension DatabaseWriter { - - /// Default implementation for DatabaseWriter.write(). Synchronously - /// executes an update block in a protected dispatch queue, - /// wrapped inside a deferred transaction, and returns its result. - /// - /// try dbPool.write { db in - /// try db.execute(...) - /// } - /// - /// This method is *not* reentrant. - /// - /// - parameter block: A block that accesses the database. - /// - throws: The error thrown by the block. - public func write(_ block: (Database) throws -> T) throws -> T { - var result: T! = nil - try writeInTransaction(.deferred) { db in - result = try block(db) - return .commit - } - return result! - } -} - extension DatabaseWriter { // MARK: - Transaction Observers