From 7183457217d1cec04617461dd2fc0bcfce5405d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 14 Mar 2019 06:43:40 +0100 Subject: [PATCH 1/3] Rename Future to DatabaseFuture --- GRDB/Core/DatabasePool.swift | 6 ++--- GRDB/Core/DatabaseQueue.swift | 4 +-- GRDB/Core/DatabaseWriter.swift | 27 +++++++++++++------ GRDB/ValueObservation/ValueObserver.swift | 4 +-- .../DatabasePoolConcurrencyTests.swift | 4 +-- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 3e4a1080e..be673377e 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -357,7 +357,7 @@ extension DatabasePool : DatabaseReader { } } - public func concurrentRead(_ block: @escaping (Database) throws -> T) -> Future { + public func concurrentRead(_ block: @escaping (Database) throws -> T) -> DatabaseFuture { // Check that we're on the writer queue... writer.execute { db in // ... and that no transaction is opened. @@ -404,13 +404,13 @@ extension DatabasePool : DatabaseReader { futureSemaphore.signal() } } catch { - return Future { throw error } + return DatabaseFuture { throw error } } // Block the writer queue until snapshot isolation success or error _ = isolationSemaphore.wait(timeout: .distantFuture) - return Future { + return DatabaseFuture { // Block the future until results are fetched _ = futureSemaphore.wait(timeout: .distantFuture) return try futureResult!.get() diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index f856519b1..7d87f698a 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -201,10 +201,10 @@ extension DatabaseQueue { return try writer.reentrantSync(block) } - public func concurrentRead(_ block: @escaping (Database) throws -> T) -> Future { + public func concurrentRead(_ block: @escaping (Database) throws -> T) -> DatabaseFuture { // DatabaseQueue can't perform parallel reads. // Perform a blocking read instead. - return Future(Result { + return DatabaseFuture(Result { // Check that we're on the writer queue... try writer.execute { db in // ... and that no transaction is opened. diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index a3090e6e5..c313ef2ba 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -94,7 +94,7 @@ public protocol DatabaseWriter : DatabaseReader { /// // Guaranteed to be zero /// let count = try future.wait() /// } - func concurrentRead(_ block: @escaping (Database) throws -> T) -> Future + func concurrentRead(_ block: @escaping (Database) throws -> T) -> DatabaseFuture } extension DatabaseWriter { @@ -256,13 +256,13 @@ extension ValueReducer { extension ValueObservation where Reducer: ValueReducer { /// Helper method for DatabaseWriter.add(observation:onError:onChange:) - fileprivate func fetchAfterChange(in writer: DatabaseWriter) -> (Database, Reducer) -> Future { + fileprivate func fetchAfterChange(in writer: DatabaseWriter) -> (Database, Reducer) -> DatabaseFuture { // The technique to return a future value after database has changed // depends on the requiresWriteAccess flag: if requiresWriteAccess { // Synchronous fetch return { (db, reducer) in - Future(Result { + DatabaseFuture(Result { var fetchedValue: Reducer.Fetched! try db.inTransaction { fetchedValue = try reducer.fetch(db) @@ -280,9 +280,20 @@ extension ValueObservation where Reducer: ValueReducer { } } -// TODO: Rename in order to avoid conflict with "real" Future libraries. -/// A future value. -public class Future { +/// A future database value, returned by the DatabaseWriter.concurrentRead(_:) +/// method. +/// +/// let futureCount: Future = try writer.writeWithoutTransaction { db in +/// try Player(...).insert() +/// +/// // Count players concurrently +/// return writer.concurrentRead { db in +/// return try Player.fetchCount() +/// } +/// } +/// +/// let count: Int = try futureCount.wait() +public class DatabaseFuture { private var consumed = false private let _wait: () throws -> Value @@ -302,7 +313,7 @@ public class Future { public func wait() throws -> Value { // Not thread-safe and quick and dirty. // Goal is that users learn not to call this method twice. - GRDBPrecondition(consumed == false, "Future.wait() must be called only once") + GRDBPrecondition(consumed == false, "DatabaseFuture.wait() must be called only once") consumed = true return try _wait() } @@ -338,7 +349,7 @@ public final class AnyDatabaseWriter : DatabaseWriter { } /// :nodoc: - public func concurrentRead(_ block: @escaping (Database) throws -> T) -> Future { + public func concurrentRead(_ block: @escaping (Database) throws -> T) -> DatabaseFuture { return base.concurrentRead(block) } diff --git a/GRDB/ValueObservation/ValueObserver.swift b/GRDB/ValueObservation/ValueObserver.swift index c17adf6a5..a25e77827 100644 --- a/GRDB/ValueObservation/ValueObserver.swift +++ b/GRDB/ValueObservation/ValueObserver.swift @@ -5,7 +5,7 @@ import Foundation class ValueObserver: TransactionObserver { /* private */ let region: DatabaseRegion // Internal for testability private var reducer: Reducer - private let fetch: (Database, Reducer) -> Future + private let fetch: (Database, Reducer) -> DatabaseFuture private let notificationQueue: DispatchQueue? private let onError: ((Error) -> Void)? private let onChange: (Reducer.Value) -> Void @@ -16,7 +16,7 @@ class ValueObserver: TransactionObserver { region: DatabaseRegion, reducer: Reducer, configuration: Configuration, - fetch: @escaping (Database, Reducer) -> Future, + fetch: @escaping (Database, Reducer) -> DatabaseFuture, notificationQueue: DispatchQueue?, onError: ((Error) -> Void)?, onChange: @escaping (Reducer.Value) -> Void) diff --git a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift index 8220c5b3d..33b80880c 100644 --- a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift +++ b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift @@ -855,8 +855,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // < // } - let future: Future = try dbPool.writeWithoutTransaction { db in - let future: Future = dbPool.concurrentRead { db in + let future: DatabaseFuture = try dbPool.writeWithoutTransaction { db in + let future: DatabaseFuture = dbPool.concurrentRead { db in _ = s1.wait(timeout: .distantFuture) return try! Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")! } From 11fd8d6b87d0e308861dada9d85d1a0c2cedacec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 14 Mar 2019 07:01:39 +0100 Subject: [PATCH 2/3] Update documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d0cddfe0..5867f9bd2 100644 --- a/README.md +++ b/README.md @@ -8049,7 +8049,7 @@ The correct solution is the `concurrentRead` method, which must be called from w ```swift // CORRECT -let futureCount: Future = try dbPool.writeWithoutTransaction { db in +let futureCount: DatabaseFuture = try dbPool.writeWithoutTransaction { db in // increment the number of players try Player(...).insert(db) From d04fc073379b242640b4f39d365fd3e22bc3c6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 14 Mar 2019 07:02:39 +0100 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f7b3a654..75668ea98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: - [#490](https://github.com/groue/GRDB.swift/pull/490): Indirect Associations - [#493](https://github.com/groue/GRDB.swift/pull/493): Bump SQLite to [3.27.2](https://www.sqlite.org/releaselog/3_27_2.html) - [#499](https://github.com/groue/GRDB.swift/pull/499): Extract EncodableRecord from MutablePersistableRecord +- [#502](https://github.com/groue/GRDB.swift/pull/502): Rename Future to DatabaseFuture ### Breaking Changes