Rename DatabaseWriter.readFromWrite to readFromCurrentState

This commit is contained in:
Gwendal Roué committed 2016-12-01 08:55:47 +01:00
1 parent 226d98cf6b
commit bd32afbed2
5 files changed
+97 -57

No files matched your search

+7
View File
@@ -1,6 +1,13 @@
Release Notes
=============
## Next Version
**Breaking Change**
- `DatabaseWriter.readFromWrite` has been renamed `DatabaseWriter.readFromCurrentState`
## 0.91.0
Released November 30, 2016
+4 -4
View File
@@ -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)
+2 -2
View File
@@ -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)
}
}
+50 -17
View File
@@ -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 {
+34 -34
View File
@@ -538,31 +538,31 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : 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<Record>]! = 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<Record>.fetchAll(db, request)
fetchedAlongside = try! fetchAlongside(db)
@@ -786,31 +786,31 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : 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<Record>]! = 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<Record>.fetchAll(db, request)
fetchedAlongside = try! fetchAlongside(db)