55/// `SerializedDatabase` (TODO: make it a move-only type eventually).
66final class WALSnapshotTransaction : @unchecked Sendable {
77 // @unchecked because `databaseAccess` is protected by a mutex.
8-
8+
99 private struct DatabaseAccess {
1010 let reader : SerializedDatabase
1111 let release : @Sendable ( _ isInsideTransaction: Bool ) -> Void
12-
12+
1313 // MUST be called only once
1414 func commitAndRelease( ) {
1515 // WALSnapshotTransaction may be deinitialized in the dispatch
@@ -28,14 +28,14 @@ final class WALSnapshotTransaction: @unchecked Sendable {
2828 release ( isInsideTransaction)
2929 }
3030 }
31-
31+
3232 // TODO: consider using the serialized DispatchQueue of reader instead of a lock.
3333 /// nil when closed
3434 private let databaseAccessMutex : Mutex < DatabaseAccess ? >
35-
35+
3636 /// The state of the database at the beginning of the transaction.
3737 let walSnapshot : WALSnapshot
38-
38+
3939 /// Creates a long-live WAL transaction on a read-only connection.
4040 ///
4141 /// The `release` closure is always called. It is called when the
@@ -68,7 +68,7 @@ final class WALSnapshotTransaction: @unchecked Sendable {
6868 {
6969 assert ( reader. configuration. readonly)
7070 let databaseAccess = DatabaseAccess ( reader: reader, release: release)
71-
71+
7272 do {
7373 // Open a long-lived transaction, and enter snapshot isolation
7474 self . walSnapshot = try reader. sync ( allowingLongLivedTransaction: true ) { db in
@@ -85,24 +85,24 @@ final class WALSnapshotTransaction: @unchecked Sendable {
8585 throw error
8686 }
8787 }
88-
88+
8989 deinit {
9090 close ( )
9191 }
92-
92+
9393 /// Executes database operations in the snapshot transaction, and
9494 /// returns their result after they have finished executing.
9595 func read< T> ( _ value: ( Database ) throws -> T ) throws -> T {
9696 try databaseAccessMutex. withLock { databaseAccess in
9797 guard let databaseAccess else {
9898 throw DatabaseError . snapshotIsLost ( )
9999 }
100-
100+
101101 // We should check the validity of the snapshot, as DatabaseSnapshotPool does.
102102 return try databaseAccess. reader. sync ( value)
103103 }
104104 }
105-
105+
106106 /// Schedules database operations for execution, and
107107 /// returns immediately.
108108 func asyncRead( _ value: @escaping @Sendable ( Result < Database , Error > ) -> Void ) {
@@ -111,7 +111,7 @@ final class WALSnapshotTransaction: @unchecked Sendable {
111111 value ( . failure( DatabaseError . snapshotIsLost ( ) ) )
112112 return
113113 }
114-
114+
115115 databaseAccess. reader. async { db in
116116 // We should check the validity of the snapshot, as DatabaseSnapshotPool does.
117117 // At least check if self was closed:
@@ -122,7 +122,7 @@ final class WALSnapshotTransaction: @unchecked Sendable {
122122 }
123123 }
124124 }
125-
125+
126126 func close( ) {
127127 databaseAccessMutex. withLock { databaseAccess in
128128 databaseAccess? . commitAndRelease ( )
0 commit comments