From bd32afbed2a103bc8e28c73859f81d42499601e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 1 Dec 2016 08:55:47 +0100 Subject: [PATCH] Rename DatabaseWriter.readFromWrite to readFromCurrentState --- CHANGELOG.md | 7 +++ GRDB/Core/DatabasePool.swift | 8 +-- GRDB/Core/DatabaseQueue.swift | 4 +- GRDB/Core/DatabaseWriter.swift | 67 +++++++++++++++------ GRDB/Record/FetchedRecordsController.swift | 68 +++++++++++----------- 5 files changed, 97 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5b232c30..af78e40d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next Version + +**Breaking Change** + +- `DatabaseWriter.readFromWrite` has been renamed `DatabaseWriter.readFromCurrentState` + + ## 0.91.0 Released November 30, 2016 diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 738ae2a45..24e902667 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -433,13 +433,13 @@ extension DatabasePool : DatabaseWriter { /// /// This method must be called from the writing dispatch queue. /// - /// The *block* argument is guaranteed to see the database in the state it - /// has at the moment this method is called. Eventual concurrent + /// The *block* argument is guaranteed to see the database in the last + /// committed state at the moment this method is called. Eventual concurrent /// database updates are *not visible* inside the block. /// /// try dbPool.write { db in /// try db.execute("DELETE FROM persons") - /// try dbPool.readFromWrite { db in + /// try dbPool.readFromCurrentState { db in /// // Guaranteed to be zero /// try Int.fetchOne(db, "SELECT COUNT(*) FROM persons")! /// } @@ -451,7 +451,7 @@ extension DatabasePool : DatabaseWriter { /// /// The database pool releases the writing dispatch queue early, before the /// block has finished. - public func readFromWrite(_ block: @escaping (Database) -> Void) throws { + public func readFromCurrentState(_ block: @escaping (Database) -> Void) throws { writer.preconditionValidQueue() let semaphore = DispatchSemaphore(value: 0) diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 4a9aaf466..be70aec84 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -305,11 +305,11 @@ extension DatabaseQueue : DatabaseWriter { return try serializedDatabase.sync(block) } - /// Executes *block*. + /// Synchronously executes *block*. /// /// This method is part of the DatabaseWriter protocol adoption, and must /// be called from the protected database dispatch queue. - public func readFromWrite(_ block: @escaping (Database) -> Void) { + public func readFromCurrentState(_ block: @escaping (Database) -> Void) { serializedDatabase.execute(block) } } diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 8b5e0fc74..143466915 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -33,28 +33,61 @@ public protocol DatabaseWriter : DatabaseReader { /// This method must be called from a writing dispatch queue. /// /// The *block* argument is guaranteed to see the database in the state it - /// has at the moment this method is called. Eventual concurrent + /// has at the moment this method is called (see below). Eventual concurrent /// database updates are *not visible* inside the block. /// - /// try writer.write { db in - /// try db.execute("DELETE FROM persons") - /// try writer.readFromWrite { db in - /// // Guaranteed to be zero - /// try Int.fetchOne(db, "SELECT COUNT(*) FROM persons")! + /// What is the "current state" of the database? + /// + /// - When this method is called outside of any transaction, the current + /// state is the last committed state. + /// + /// try writer.write { db in + /// try db.execute("DELETE FROM persons") + /// try writer.readFromCurrentState { db in + /// // Guaranteed to be zero + /// try Int.fetchOne(db, "SELECT COUNT(*) FROM persons")! + /// } + /// try db.execute("INSERT INTO persons ...") /// } - /// try db.execute("INSERT INTO persons ...") - /// } /// - /// DatabasePool.readFromWrite runs *block* asynchronously in a concurrent - /// reader dispatch queue, and release the writing dispatch queue early, - /// before the block has finished. In the example above, the insertion runs - /// concurrently with the select (and yet the select is guaranteed not to - /// see the insertion). + /// - When this method is called inside an uncommitted transation, the + /// current state depends on the caller: /// - /// DatabaseQueue.readFromWrite simply runs *block* synchronously, and - /// returns when the block has completed. In the example above, the - /// insertion is run after the select. - func readFromWrite(_ block: @escaping (Database) -> Void) throws + /// DatabasePool.readFromCurrentState runs *block* asynchronously in a + /// concurrent reader dispatch queue, and release the writing dispatch + /// queue early, before the block has finished. In the example below, + /// the insertion runs concurrently with the select, and the select sees + /// the database in its last committed state. + /// + /// try dbPool.write { db in + /// try db.execute("DELETE FROM persons") + /// db.inTransaction { + /// try db.execute("INSERT INTO persons ...") + /// try dbPool.readFromCurrentState { db in + /// // Zero + /// try Int.fetchOne(db, "SELECT COUNT(*) FROM persons")! + /// } + /// return .commit + /// } + /// } + /// + /// DatabaseQueue.readFromCurrentState simply runs *block* synchronously, + /// and returns when the block has completed. In the example below, the + /// select sees the uncommitted state of the database, and the insertion + /// is run after the select. + /// + /// try dbQueue.write { db in + /// try db.execute("DELETE FROM persons") + /// db.inTransaction { + /// try db.execute("INSERT INTO persons ...") + /// try dbQueue.readFromCurrentState { db in + /// // One + /// try Int.fetchOne(db, "SELECT COUNT(*) FROM persons")! + /// } + /// return .commit + /// } + /// } + func readFromCurrentState(_ block: @escaping (Database) -> Void) throws } extension DatabaseWriter { diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 18e19e222..c9ca5a783 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -538,31 +538,31 @@ private final class FetchedRecordsObserver : Transaction // Fetch items. // - // This method is called from the database writer's serialized queue, so - // that we can fetch items before other writes have the opportunity to - // modify the database. + // This method is called from the database writer's serialized + // queue, so that we can fetch items before other writes have the + // opportunity to modify the database. // - // However, we don't have to block the writer queue for all the duration - // of the fetch. We just need to block the writer queue until we can - // perform a fetch in isolation. This is the role of the readFromWrite - // method (see below). + // However, we don't have to block the writer queue for all the + // duration of the fetch. We just need to block the writer queue + // until we can perform a fetch in isolation. This is the role of + // the readFromCurrentState method (see below). // - // However, our fetch will last for an unknown duration. And since we - // release the writer queue early, the next database modification will - // triggers this callback while our fetch is, maybe, still running. This - // next callback will also perform its own fetch, that will maybe end - // before our own fetch. + // However, our fetch will last for an unknown duration. And since + // we release the writer queue early, the next database modification + // will triggers this callback while our fetch is, maybe, still + // running. This next callback will also perform its own fetch, that + // will maybe end before our own fetch. // - // We have to make sure that our fetch is processed *before* the next - // fetch: let's immediately dispatch the processing task in our - // serialized FIFO queue, but have it wait for our fetch to complete, - // with a semaphore: + // We have to make sure that our fetch is processed *before* the + // next fetch: let's immediately dispatch the processing task in our + // serialized FIFO queue, but have it wait for our fetch to + // complete, with a semaphore: let semaphore = DispatchSemaphore(value: 0) var fetchedItems: [Item]! = nil var fetchedAlongside: T! = nil // TODO: handle error and don't crash - try! databaseWriter.readFromWrite { db in + try! databaseWriter.readFromCurrentState { db in // TODO: handle error and don't crash fetchedItems = try! Item.fetchAll(db, request) fetchedAlongside = try! fetchAlongside(db) @@ -786,31 +786,31 @@ private final class FetchedRecordsObserver : Transaction // Fetch items. // - // This method is called from the database writer's serialized queue, so - // that we can fetch items before other writes have the opportunity to - // modify the database. + // This method is called from the database writer's serialized + // queue, so that we can fetch items before other writes have the + // opportunity to modify the database. // - // However, we don't have to block the writer queue for all the duration - // of the fetch. We just need to block the writer queue until we can - // perform a fetch in isolation. This is the role of the readFromWrite - // method (see below). + // However, we don't have to block the writer queue for all the + // duration of the fetch. We just need to block the writer queue + // until we can perform a fetch in isolation. This is the role of + // the readFromCurrentState method (see below). // - // However, our fetch will last for an unknown duration. And since we - // release the writer queue early, the next database modification will - // triggers this callback while our fetch is, maybe, still running. This - // next callback will also perform its own fetch, that will maybe end - // before our own fetch. + // However, our fetch will last for an unknown duration. And since + // we release the writer queue early, the next database modification + // will triggers this callback while our fetch is, maybe, still + // running. This next callback will also perform its own fetch, that + // will maybe end before our own fetch. // - // We have to make sure that our fetch is processed *before* the next - // fetch: let's immediately dispatch the processing task in our - // serialized FIFO queue, but have it wait for our fetch to complete, - // with a semaphore: + // We have to make sure that our fetch is processed *before* the + // next fetch: let's immediately dispatch the processing task in our + // serialized FIFO queue, but have it wait for our fetch to + // complete, with a semaphore: let semaphore = DispatchSemaphore(value: 0) var fetchedItems: [Item]! = nil var fetchedAlongside: T! = nil // TODO: handle error and don't crash - try! databaseWriter.readFromWrite { db in + try! databaseWriter.readFromCurrentState { db in // TODO: handle error and don't crash fetchedItems = try! Item.fetchAll(db, request) fetchedAlongside = try! fetchAlongside(db)