DatabaseReader WIP

This commit is contained in:
Gwendal Roué committed 2016-03-11 08:13:10 +01:00
1 parent 6f4a29e6f8
commit 5fd2212278
6 files changed
+36 -72

No files matched your search

-6
View File
@@ -231,8 +231,6 @@
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */; };
56E8CE101BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */; };
56E8CE111BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */; };
56EA868D1C91D988002BB4DF /* DatabaseReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA868C1C91D988002BB4DF /* DatabaseReader.swift */; };
56EA868E1C91D988002BB4DF /* DatabaseReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA868C1C91D988002BB4DF /* DatabaseReader.swift */; };
56EA86941C91DFE7002BB4DF /* DatabaseReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA86931C91DFE7002BB4DF /* DatabaseReaderTests.swift */; };
56EA86951C91DFE7002BB4DF /* DatabaseReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA86931C91DFE7002BB4DF /* DatabaseReaderTests.swift */; };
56EB0AB21BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */; };
@@ -465,7 +463,6 @@
56E5D7F91B4D422D00430942 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleFetchTests.swift; sourceTree = "<group>"; };
56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConvertibleFetchTests.swift; sourceTree = "<group>"; };
56EA868C1C91D988002BB4DF /* DatabaseReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseReader.swift; sourceTree = "<group>"; };
56EA86931C91DFE7002BB4DF /* DatabaseReaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseReaderTests.swift; sourceTree = "<group>"; };
56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataMemoryTests.swift; sourceTree = "<group>"; };
56EE573C1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConversionTests.swift; sourceTree = "<group>"; };
@@ -828,7 +825,6 @@
56A238731B9C75030082EB20 /* DatabaseError.swift */,
560A37A31C8F625000949E71 /* DatabasePool.swift */,
56A238741B9C75030082EB20 /* DatabaseQueue.swift */,
56EA868C1C91D988002BB4DF /* DatabaseReader.swift */,
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */,
56A238751B9C75030082EB20 /* DatabaseValue.swift */,
560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */,
@@ -1395,7 +1391,6 @@
56A238881B9C75030082EB20 /* Row.swift in Sources */,
569531201C907A8C00CF1A2B /* DatabaseSchemaCache.swift in Sources */,
5605F15E1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */,
56EA868E1C91D988002BB4DF /* DatabaseReader.swift in Sources */,
5605F1661C672E4000235C62 /* NSNull.swift in Sources */,
56A238841B9C75030082EB20 /* DatabaseQueue.swift in Sources */,
56A238821B9C75030082EB20 /* DatabaseError.swift in Sources */,
@@ -1581,7 +1576,6 @@
56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */,
5695311F1C907A8C00CF1A2B /* DatabaseSchemaCache.swift in Sources */,
5605F15D1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */,
56EA868D1C91D988002BB4DF /* DatabaseReader.swift in Sources */,
5605F1651C672E4000235C62 /* NSNull.swift in Sources */,
56A2387D1B9C75030082EB20 /* Database.swift in Sources */,
56A238831B9C75030082EB20 /* DatabaseQueue.swift in Sources */,
+17
View File
@@ -1155,6 +1155,23 @@ public struct DatabaseEvent {
// =========================================================================
// MARK: - DatabaseReader
/// The protocol for all types that can fetch values from a database.
///
/// It is adopted by DatabaseQueue, and DatabasePool, and Database.
///
/// You don't use the protocol directly. Instead, you provide a DatabaseReader
/// to fetching methods:
///
/// let persons = Person.fetchAll(dbQueue)
/// let persons = Person.fetchAll(dbPool)
/// dbQueue.inDatabase { db in
/// let persons = Person.fetchAll(db)
/// }
public protocol DatabaseReader {
/// This method is an implementation detail: do not use it directly.
func _readSingleStatement<T>(block: (db: Database) throws -> T) rethrows -> T
}
extension Database : DatabaseReader {
/// This method is an implementation detail: do not use it directly.
public func _readSingleStatement<T>(block: (db: Database) throws -> T) rethrows -> T {
-39
View File
@@ -88,45 +88,6 @@ public final class DatabasePool {
}
}
/// Synchronously executes a read-only block in the database, and returns
/// its result.
///
/// let persons = dbPool.nonIsolatedRead { db in
/// Person.fetchAll(...)
/// }
///
/// This method is *not* reentrant.
///
/// Unlike the `read` method, which provides full isolation, the
/// `nonIsolatedRead` method lets concurrent updates alter consecutive
/// statements:
///
/// dbPool.nonIsolatedRead { db in
/// // Those two values may be different:
/// let count1 = Person.fetchCount(db)
/// let count2 = Person.fetchCount(db)
/// }
///
/// Yet all statements are individually isolated from concurrent updates:
///
/// dbPool.nonIsolatedRead { db in
/// // No update can alter this iteration during the loop:
/// for person in Person.fetch(db) { ...}
///
/// // But now it may iterate a different set of results:
/// for person in Person.fetch(db) { ... }
/// }
///
/// - parameter block: A block that accesses the database.
/// - throws: The error thrown by the block.
public func nonIsolatedRead<T>(block: (db: Database) throws -> T) rethrows -> T {
return try readerPool.get { reader in
try reader.inDatabase { db in
try block(db: db)
}
}
}
/// Synchronously executes an update block in the database, and returns
/// its result.
///
-16
View File
@@ -1,16 +0,0 @@
/// The protocol for all types that can fetch values from a database.
///
/// It is adopted by DatabaseQueue, and DatabasePool, and Database.
///
/// You don't use the protocol directly. Instead, you provide a DatabaseReader
/// to fetching methods:
///
/// let persons = Person.fetchAll(dbQueue)
/// let persons = Person.fetchAll(dbPool)
/// dbQueue.inDatabase { db in
/// let persons = Person.fetchAll(db)
/// }
public protocol DatabaseReader {
/// This method is an implementation detail: do not use it directly.
func _readSingleStatement<T>(block: (db: Database) throws -> T) rethrows -> T
}
+7 -5
View File
@@ -157,7 +157,7 @@ public protocol MutablePersistable : TableMapping {
/// that they invoke the performExists() method.
///
/// - returns: Whether the primary key matches a row in the database.
func exists(db: Database) -> Bool
func exists(reader: DatabaseReader) -> Bool
}
public extension MutablePersistable {
@@ -202,8 +202,8 @@ public extension MutablePersistable {
/// the database.
///
/// The default implementation for exists() invokes performExists().
func exists(db: Database) -> Bool {
return performExists(db)
func exists(reader: DatabaseReader) -> Bool {
return performExists(reader)
}
@@ -297,8 +297,10 @@ public extension MutablePersistable {
/// that adopt MutablePersistable can invoke performExists() in
/// their implementation of exists(). They should not provide their own
/// implementation of performExists().
func performExists(db: Database) -> Bool {
return (Row.fetchOne(DataMapper(db, self).existsStatement()) != nil)
func performExists(reader: DatabaseReader) -> Bool {
return reader._readSingleStatement { db in
(Row.fetchOne(DataMapper(db, self).existsStatement()) != nil)
}
}
}
@@ -207,7 +207,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
func testNonIsolatedReadMethodIsolationOfStatement() {
// TODO: The _readSingleStatement method is public but its use is
// discouraged. This test should move to the Private folder.
func testReadSingleStatementMethodIsolationOfStatement() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
@@ -228,7 +230,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// end
let block1 = { () in
self.dbPool.nonIsolatedRead { db in
self.dbPool._readSingleStatement { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s1)
@@ -255,7 +257,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
func testNonIsolatedReadMethodIsolationOfStatementWithCheckpoint() {
// TODO: The _readSingleStatement method is public but its use is
// discouraged. This test should move to the Private folder.
func testReadSingleStatementMethodIsolationOfStatementWithCheckpoint() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
@@ -277,7 +281,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// end
let block1 = { () in
self.dbPool.nonIsolatedRead { db in
self.dbPool._readSingleStatement { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s1)
@@ -305,7 +309,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
func testNonIsolatedReadMethodIsolationOfBlock() {
// TODO: The _readSingleStatement method is public but its use is
// discouraged. This test should move to the Private folder.
func testReadSingleStatementMethodIsolationOfBlock() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
@@ -322,7 +328,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// SELECT COUNT(*) FROM items
let block1 = { () in
self.dbPool.nonIsolatedRead { db in
self.dbPool._readSingleStatement { db in
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 0)
dispatch_semaphore_signal(s1)
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)