mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Transaction observers can handle commits in a shared read-only transaction, for increased consistency
This commit is contained in:
1 parent
d5b4ed72fa
commit
0bba534cc6
4 files changed
+87
-22
No files matched your search
@@ -480,8 +480,26 @@ class DatabaseObservationBroker {
|
||||
savepointStack.clear()
|
||||
|
||||
if !database.isReadOnly {
|
||||
for observation in transactionObservations {
|
||||
observation.databaseDidCommit(database)
|
||||
var remainingObservations = transactionObservations[...]
|
||||
while let commitHandling = remainingObservations.first?.commitHandling {
|
||||
remainingObservations.removeAll { $0.commitHandling == commitHandling }
|
||||
switch commitHandling {
|
||||
case .none:
|
||||
break
|
||||
case .detached:
|
||||
let observations = transactionObservations.filter { $0.commitHandling == commitHandling }
|
||||
for observation in observations {
|
||||
observation.databaseDidCommit(database)
|
||||
}
|
||||
case .coalescedInReadOnlyTransaction:
|
||||
#warning("TODO: how do we notify this error?")
|
||||
try? database.isolated(readOnly: true) {
|
||||
let observations = transactionObservations.filter { $0.commitHandling == commitHandling }
|
||||
for observation in observations {
|
||||
observation.databaseDidCommit(database)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -719,6 +737,7 @@ class DatabaseObservationBroker {
|
||||
///
|
||||
/// Adopting types must be a class.
|
||||
public protocol TransactionObserver: AnyObject {
|
||||
var commitHandling: CommitHandling { get }
|
||||
|
||||
/// Filters database changes that should be notified the the
|
||||
/// databaseDidChange(with:) method.
|
||||
@@ -797,7 +816,21 @@ public protocol TransactionObserver: AnyObject {
|
||||
#endif
|
||||
}
|
||||
|
||||
public enum CommitHandling {
|
||||
/// `databaseDidCommit` is not called.
|
||||
case none
|
||||
|
||||
/// `databaseDidCommit` is called outside of any transaction.
|
||||
case detached
|
||||
|
||||
/// `databaseDidCommit` is called within a read-only transaction, shared
|
||||
/// with all other transaction observers.
|
||||
case coalescedInReadOnlyTransaction
|
||||
}
|
||||
|
||||
extension TransactionObserver {
|
||||
var commitHandling: CommitHandling { .detached }
|
||||
|
||||
/// Default implementation does nothing
|
||||
public func databaseWillCommit() throws {
|
||||
}
|
||||
@@ -853,6 +886,7 @@ final class TransactionObservation {
|
||||
private weak var weakObserver: (any TransactionObserver)?
|
||||
private var strongObserver: (any TransactionObserver)?
|
||||
private var observer: (any TransactionObserver)? { strongObserver ?? weakObserver }
|
||||
var commitHandling: CommitHandling { observer?.commitHandling ?? .none }
|
||||
|
||||
fileprivate var isObserving: Bool {
|
||||
observer != nil
|
||||
|
||||
@@ -499,11 +499,25 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ writerDB: Database) {
|
||||
var commitHandling: CommitHandling {
|
||||
// Ignore transaction unless database was modified
|
||||
guard observationState.isModified else { return }
|
||||
guard observationState.isModified else {
|
||||
return .none
|
||||
}
|
||||
|
||||
switch trackingMode {
|
||||
case .constantRegion, .constantRegionRecordedFromSelection:
|
||||
// No need to share a transaction with other observers
|
||||
return .detached
|
||||
case .nonConstantRegionRecordedFromSelection:
|
||||
// Let's share a common transaction with all observers
|
||||
return .coalescedInReadOnlyTransaction
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ writerDB: Database) {
|
||||
// Reset the isModified flag until next transaction
|
||||
assert(observationState.isModified)
|
||||
observationState.isModified = false
|
||||
|
||||
// Ignore transaction unless we are still notifying database events, and
|
||||
|
||||
@@ -496,11 +496,18 @@ extension ValueSnapshotObserver: TransactionObserver {
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ writerDB: Database) {
|
||||
var commitHandling: CommitHandling {
|
||||
// Ignore transaction unless database was modified
|
||||
guard observationState.isModified else { return }
|
||||
|
||||
guard observationState.isModified else {
|
||||
return .none
|
||||
}
|
||||
// No need to share a transaction with other observers
|
||||
return .detached
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ writerDB: Database) {
|
||||
// Reset the isModified flag until next transaction
|
||||
assert(observationState.isModified)
|
||||
observationState.isModified = false
|
||||
|
||||
// Ignore transaction unless we are still notifying database events, and
|
||||
|
||||
@@ -29,6 +29,9 @@ final class ValueWriteOnlyObserver<Writer: DatabaseWriter, Reducer: ValueReducer
|
||||
/// Configures the tracked database region.
|
||||
private let trackingMode: ValueObservationTrackingMode
|
||||
|
||||
/// If true, database values are fetched from a read-only access.
|
||||
private let readOnly: Bool
|
||||
|
||||
// MARK: - Mutable State
|
||||
//
|
||||
// The observer has four distinct mutable states that evolve independently,
|
||||
@@ -76,25 +79,21 @@ final class ValueWriteOnlyObserver<Writer: DatabaseWriter, Reducer: ValueReducer
|
||||
/// The observed DatabaseWriter.
|
||||
let writer: Writer
|
||||
|
||||
/// If true, database values are fetched from a read-only access.
|
||||
private let readOnly: Bool
|
||||
|
||||
/// A reducer that fetches database values.
|
||||
private let reducer: Reducer
|
||||
|
||||
init(writer: Writer, readOnly: Bool, reducer: Reducer) {
|
||||
init(writer: Writer, reducer: Reducer) {
|
||||
self.writer = writer
|
||||
self.readOnly = readOnly
|
||||
self.reducer = reducer
|
||||
}
|
||||
|
||||
func fetch(_ db: Database) throws -> Reducer.Fetched {
|
||||
func fetch(_ db: Database, readOnly: Bool) throws -> Reducer.Fetched {
|
||||
try db.isolated(readOnly: readOnly) {
|
||||
try reducer._fetch(db)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchRecordingObservedRegion(_ db: Database) throws -> (Reducer.Fetched, DatabaseRegion) {
|
||||
func fetchRecordingObservedRegion(_ db: Database, readOnly: Bool) throws -> (Reducer.Fetched, DatabaseRegion) {
|
||||
var region = DatabaseRegion()
|
||||
let fetchedValue = try db.isolated(readOnly: readOnly) {
|
||||
try db.recordingSelection(®ion) {
|
||||
@@ -151,11 +150,11 @@ final class ValueWriteOnlyObserver<Writer: DatabaseWriter, Reducer: ValueReducer
|
||||
// Configuration
|
||||
self.scheduler = scheduler
|
||||
self.trackingMode = trackingMode
|
||||
self.readOnly = readOnly
|
||||
|
||||
// State
|
||||
self.databaseAccess = DatabaseAccess(
|
||||
writer: writer,
|
||||
readOnly: readOnly,
|
||||
// ValueReducer semantics guarantees that reducer._fetch
|
||||
// is independent from the reducer state
|
||||
reducer: reducer)
|
||||
@@ -307,7 +306,7 @@ extension ValueWriteOnlyObserver {
|
||||
|
||||
switch trackingMode {
|
||||
case let .constantRegion(regions):
|
||||
let fetchedValue = try databaseAccess.fetch(db)
|
||||
let fetchedValue = try databaseAccess.fetch(db, readOnly: readOnly)
|
||||
let region = try DatabaseRegion.union(regions)(db)
|
||||
let observedRegion = try region.observableRegion(db)
|
||||
events.willTrackRegion?(observedRegion)
|
||||
@@ -316,7 +315,7 @@ extension ValueWriteOnlyObserver {
|
||||
|
||||
case .constantRegionRecordedFromSelection,
|
||||
.nonConstantRegionRecordedFromSelection:
|
||||
let (fetchedValue, observedRegion) = try databaseAccess.fetchRecordingObservedRegion(db)
|
||||
let (fetchedValue, observedRegion) = try databaseAccess.fetchRecordingObservedRegion(db, readOnly: readOnly)
|
||||
events.willTrackRegion?(observedRegion)
|
||||
startObservation(db, observedRegion: observedRegion)
|
||||
return fetchedValue
|
||||
@@ -350,11 +349,22 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ db: Database) {
|
||||
var commitHandling: CommitHandling {
|
||||
// Ignore transaction unless database was modified
|
||||
guard observationState.isModified else { return }
|
||||
|
||||
guard observationState.isModified else {
|
||||
return .none
|
||||
}
|
||||
if readOnly {
|
||||
// Let's share a common transaction with all observers
|
||||
return .coalescedInReadOnlyTransaction
|
||||
} else {
|
||||
return .detached
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(_ db: Database) {
|
||||
// Reset the isModified flag until next transaction
|
||||
assert(observationState.isModified)
|
||||
observationState.isModified = false
|
||||
|
||||
// Ignore transaction unless we are still notifying database events, and
|
||||
@@ -374,10 +384,10 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
switch trackingMode {
|
||||
case .constantRegion, .constantRegionRecordedFromSelection:
|
||||
// Tracked region is already known. Fetch only.
|
||||
fetchedValue = try databaseAccess.fetch(db)
|
||||
fetchedValue = try databaseAccess.fetch(db, readOnly: readOnly)
|
||||
case .nonConstantRegionRecordedFromSelection:
|
||||
// Fetch and update the tracked region.
|
||||
let (value, observedRegion) = try databaseAccess.fetchRecordingObservedRegion(db)
|
||||
let (value, observedRegion) = try databaseAccess.fetchRecordingObservedRegion(db, readOnly: readOnly)
|
||||
fetchedValue = value
|
||||
|
||||
// Don't spam the user with region tracking events: wait for an actual change
|
||||
|
||||
Reference in new issue
Block a user