Merge pull request #502 from groue/feature/rename-future

Rename Future to DatabaseFuture
This commit is contained in:
Gwendal Roué authored and GitHub committed 2019-03-15 07:56:56 +01:00
commit 0074adc023
7 files changed
+30 -18

No files matched your search

+1
View File
@@ -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
+3 -3
View File
@@ -357,7 +357,7 @@ extension DatabasePool : DatabaseReader {
}
}
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> Future<T> {
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T> {
// 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()
+2 -2
View File
@@ -201,10 +201,10 @@ extension DatabaseQueue {
return try writer.reentrantSync(block)
}
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> Future<T> {
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T> {
// 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.
+19 -8
View File
@@ -94,7 +94,7 @@ public protocol DatabaseWriter : DatabaseReader {
/// // Guaranteed to be zero
/// let count = try future.wait()
/// }
func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> Future<T>
func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T>
}
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<Reducer.Fetched> {
fileprivate func fetchAfterChange(in writer: DatabaseWriter) -> (Database, Reducer) -> DatabaseFuture<Reducer.Fetched> {
// 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<Value> {
/// A future database value, returned by the DatabaseWriter.concurrentRead(_:)
/// method.
///
/// let futureCount: Future<Int> = 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<Value> {
private var consumed = false
private let _wait: () throws -> Value
@@ -302,7 +313,7 @@ public class Future<Value> {
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<T>(_ block: @escaping (Database) throws -> T) -> Future<T> {
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T> {
return base.concurrentRead(block)
}
+2 -2
View File
@@ -5,7 +5,7 @@ import Foundation
class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
/* private */ let region: DatabaseRegion // Internal for testability
private var reducer: Reducer
private let fetch: (Database, Reducer) -> Future<Reducer.Fetched>
private let fetch: (Database, Reducer) -> DatabaseFuture<Reducer.Fetched>
private let notificationQueue: DispatchQueue?
private let onError: ((Error) -> Void)?
private let onChange: (Reducer.Value) -> Void
@@ -16,7 +16,7 @@ class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
region: DatabaseRegion,
reducer: Reducer,
configuration: Configuration,
fetch: @escaping (Database, Reducer) -> Future<Reducer.Fetched>,
fetch: @escaping (Database, Reducer) -> DatabaseFuture<Reducer.Fetched>,
notificationQueue: DispatchQueue?,
onError: ((Error) -> Void)?,
onChange: @escaping (Reducer.Value) -> Void)
+1 -1
View File
@@ -8049,7 +8049,7 @@ The correct solution is the `concurrentRead` method, which must be called from w
```swift
// CORRECT
let futureCount: Future<Int> = try dbPool.writeWithoutTransaction { db in
let futureCount: DatabaseFuture<Int> = try dbPool.writeWithoutTransaction { db in
// increment the number of players
try Player(...).insert(db)
@@ -855,8 +855,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// <
// }
let future: Future<Int> = try dbPool.writeWithoutTransaction { db in
let future: Future<Int> = dbPool.concurrentRead { db in
let future: DatabaseFuture<Int> = try dbPool.writeWithoutTransaction { db in
let future: DatabaseFuture<Int> = dbPool.concurrentRead { db in
_ = s1.wait(timeout: .distantFuture)
return try! Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
}