From 7c684d06c142ade6abb48b5e300fe50d70b49287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 29 Feb 2020 17:53:49 +0100 Subject: [PATCH] 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. --- GRDB/Core/Database+Schema.swift | 7 --- GRDB/Core/DatabasePool.swift | 44 +++++++++---------- GRDB/Core/DatabaseQueue.swift | 4 +- GRDB/Core/DatabaseSchemaCache.swift | 43 +----------------- GRDB/Core/DatabaseSnapshot.swift | 2 +- .../DatabasePoolSchemaCacheTests.swift | 10 ++--- 6 files changed, 29 insertions(+), 81 deletions(-) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 6b69b7737..2b770dc67 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -10,13 +10,6 @@ extension Database { // MARK: - Database Schema - func withSchemaCache(_ 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 diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 511f812b7..991e2f111 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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() diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index e00ae5ed5..0c8e82819 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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") } diff --git a/GRDB/Core/DatabaseSchemaCache.swift b/GRDB/Core/DatabaseSchemaCache.swift index 4f29cef90..dd7916ec7 100644 --- a/GRDB/Core/DatabaseSchemaCache.swift +++ b/GRDB/Core/DatabaseSchemaCache.swift @@ -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) { } -} diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index df92854f9..d5427ed6c 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -22,7 +22,7 @@ public class DatabaseSnapshot: DatabaseReader { serializedDatabase = try SerializedDatabase( path: path, configuration: configuration, - schemaCache: SimpleDatabaseSchemaCache(), + schemaCache: DatabaseSchemaCache(), defaultLabel: defaultLabel, purpose: purpose) diff --git a/Tests/GRDBTests/DatabasePoolSchemaCacheTests.swift b/Tests/GRDBTests/DatabasePoolSchemaCacheTests.swift index 4f3e588fc..69d1ff40b 100644 --- a/Tests/GRDBTests/DatabasePoolSchemaCacheTests.swift +++ b/Tests/GRDBTests/DatabasePoolSchemaCacheTests.swift @@ -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) } } }