Simplify schema cache management

We don't need EmptyDatabaseSchemaCache. It does not help unsafe reads to work better if concurrent writes modify the schema anyway.
This commit is contained in:
Gwendal Roué committed 2020-03-01 15:51:37 +01:00
1 parent c8321201d8
commit 7c684d06c1
6 files changed
+29 -81

No files matched your search

-7
View File
@@ -10,13 +10,6 @@ extension Database {
// MARK: - Database Schema
func withSchemaCache<T>(_ schemaCache: DatabaseSchemaCache, _ block: () throws -> T) rethrows -> T {
var oldCache = self.schemaCache
defer { self.schemaCache = oldCache }
self.schemaCache = schemaCache
return try block()
}
/// Clears the database schema cache.
///
/// You may need to clear the cache manually if the database schema is
+20 -24
View File
@@ -54,7 +54,7 @@ public final class DatabasePool: DatabaseWriter {
writer = try SerializedDatabase(
path: path,
configuration: configuration,
schemaCache: SimpleDatabaseSchemaCache(),
schemaCache: DatabaseSchemaCache(),
defaultLabel: "GRDB.DatabasePool",
purpose: "writer")
@@ -97,7 +97,7 @@ public final class DatabasePool: DatabaseWriter {
let reader = try SerializedDatabase(
path: path,
configuration: self.readerConfiguration,
schemaCache: SimpleDatabaseSchemaCache(),
schemaCache: DatabaseSchemaCache(),
defaultLabel: "GRDB.DatabasePool",
purpose: "reader.\(readerCount)")
reader.sync { self.setupDatabase($0) }
@@ -372,9 +372,8 @@ extension DatabasePool: DatabaseReader {
// See DatabasePoolTests.testReadMethodIsolationOfBlock().
try db.inTransaction(.deferred) {
// Reset the schema cache before running user code in snapshot isolation
result = try db.withSchemaCache(SimpleDatabaseSchemaCache()) {
try block(db)
}
db.clearSchemaCache()
result = try block(db)
return .commit
}
return result!
@@ -420,9 +419,8 @@ extension DatabasePool: DatabaseReader {
try db.beginTransaction(.deferred)
// Reset the schema cache before running user code in snapshot isolation
db.withSchemaCache(SimpleDatabaseSchemaCache()) {
block(.success(db))
}
db.clearSchemaCache()
block(.success(db))
} catch {
block(.failure(error))
}
@@ -464,10 +462,9 @@ extension DatabasePool: DatabaseReader {
GRDBPrecondition(currentReader == nil, "Database methods are not reentrant.")
return try readerPool.get { reader in
try reader.sync { db in
// No schema cache when snapshot isolation is not established
return try db.withSchemaCache(EmptyDatabaseSchemaCache()) {
try block(db)
}
// Reset the schema cache
db.clearSchemaCache()
return try block(db)
}
}
}
@@ -505,10 +502,9 @@ extension DatabasePool: DatabaseReader {
} else {
return try readerPool.get { reader in
try reader.sync { db in
// No schema cache when snapshot isolation is not established
return try db.withSchemaCache(EmptyDatabaseSchemaCache()) {
try block(db)
}
// Reset the schema cache
db.clearSchemaCache()
return try block(db)
}
}
}
@@ -560,11 +556,12 @@ extension DatabasePool: DatabaseReader {
// Release the writer queue
isolationSemaphore.signal()
// Reset the schema cache before running user code in snapshot isolation
db.schemaCache = SimpleDatabaseSchemaCache()
// Fetch and release the future
futureResult = DatabaseResult { try block(db) }
futureResult = DatabaseResult {
// Reset the schema cache before running user code in snapshot isolation
db.clearSchemaCache()
return try block(db)
}
futureSemaphore.signal()
}
} catch {
@@ -664,10 +661,9 @@ extension DatabasePool: DatabaseReader {
isolationSemaphore.signal()
// Reset the schema cache before running user code in snapshot isolation
db.withSchemaCache(SimpleDatabaseSchemaCache()) {
// Fetch and release the future
block(.success(db))
}
db.clearSchemaCache()
block(.success(db))
}
} catch {
isolationSemaphore.signal()
+2 -2
View File
@@ -36,7 +36,7 @@ public final class DatabaseQueue: DatabaseWriter {
writer = try SerializedDatabase(
path: path,
configuration: configuration,
schemaCache: SimpleDatabaseSchemaCache(),
schemaCache: DatabaseSchemaCache(),
defaultLabel: "GRDB.DatabaseQueue")
setupSuspension()
@@ -60,7 +60,7 @@ public final class DatabaseQueue: DatabaseWriter {
writer = try! SerializedDatabase(
path: ":memory:",
configuration: configuration,
schemaCache: SimpleDatabaseSchemaCache(),
schemaCache: DatabaseSchemaCache(),
defaultLabel: "GRDB.DatabaseQueue")
}
+1 -42
View File
@@ -1,24 +1,5 @@
/// The protocol for database schema cache.
protocol DatabaseSchemaCache {
mutating func clear()
var schemaInfo: SchemaInfo? { get set }
func primaryKey(_ table: String) -> PrimaryKeyInfo?
mutating func set(primaryKey: PrimaryKeyInfo, forTable table: String)
func columns(in table: String) -> [ColumnInfo]?
mutating func set(columns: [ColumnInfo], forTable table: String)
func indexes(on table: String) -> [IndexInfo]?
mutating func set(indexes: [IndexInfo], forTable table: String)
func foreignKeys(on table: String) -> [ForeignKeyInfo]?
mutating func set(foreignKeys: [ForeignKeyInfo], forTable table: String)
}
/// A thread-unsafe database schema cache
struct SimpleDatabaseSchemaCache: DatabaseSchemaCache {
struct DatabaseSchemaCache {
var schemaInfo: SchemaInfo?
private var primaryKeys: [String: PrimaryKeyInfo] = [:]
private var columns: [String: [ColumnInfo]] = [:]
@@ -65,25 +46,3 @@ struct SimpleDatabaseSchemaCache: DatabaseSchemaCache {
self.foreignKeys[table] = foreignKeys
}
}
/// An always empty database schema cache
struct EmptyDatabaseSchemaCache: DatabaseSchemaCache {
func clear() { }
var schemaInfo: SchemaInfo? {
get { return nil }
set { }
}
func primaryKey(_ table: String) -> PrimaryKeyInfo? { return nil }
func set(primaryKey: PrimaryKeyInfo, forTable table: String) { }
func columns(in table: String) -> [ColumnInfo]? { return nil }
func set(columns: [ColumnInfo], forTable table: String) { }
func indexes(on table: String) -> [IndexInfo]? { return nil }
func set(indexes: [IndexInfo], forTable table: String) { }
func foreignKeys(on table: String) -> [ForeignKeyInfo]? { return nil }
func set(foreignKeys: [ForeignKeyInfo], forTable table: String) { }
}
+1 -1
View File
@@ -22,7 +22,7 @@ public class DatabaseSnapshot: DatabaseReader {
serializedDatabase = try SerializedDatabase(
path: path,
configuration: configuration,
schemaCache: SimpleDatabaseSchemaCache(),
schemaCache: DatabaseSchemaCache(),
defaultLabel: defaultLabel,
purpose: purpose)
@@ -187,7 +187,7 @@ class DatabasePoolSchemaCacheTests : GRDBTestCase {
}
}
func testUnsafeReaderHasNoDatabaseCache() throws {
func testUnsafeReaderHasDatabaseCache() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -206,10 +206,10 @@ class DatabasePoolSchemaCacheTests : GRDBTestCase {
_ = try db.columns(in: "items")
_ = try db.indexes(on: "items")
// Assert that a reader cache is still empty
XCTAssertTrue(db.schemaCache.primaryKey("items") == nil)
XCTAssertTrue(db.schemaCache.columns(in: "items") == nil)
XCTAssertTrue(db.schemaCache.indexes(on: "items") == nil)
// Assert that a reader cache is warmed
XCTAssertFalse(db.schemaCache.primaryKey("items") == nil)
XCTAssertFalse(db.schemaCache.columns(in: "items") == nil)
XCTAssertFalse(db.schemaCache.indexes(on: "items") == nil)
}
}
}