Skip to content

Commit 56b979b

Browse files
Added implementation to withUnsafeTransaction now that we have transactions to lean on
1 parent b2bd93e commit 56b979b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public actor DiskPersistence<AccessMode: _AccessMode>: Persistence {
2323

2424
var registeredDatastores: [String: [WeakDatastore]] = [:]
2525

26+
var lastTransaction: Transaction?
27+
2628
/// Initialize a ``DiskPersistence`` with a read-write URL.
2729
///
2830
/// Use this initializer when creating a persistence from the main process that will access it, such as your app. To access the same persistence from another process, use ``init(readOnlyURL:)`` instead.
@@ -580,6 +582,23 @@ extension DiskPersistence {
580582
}
581583
}
582584

585+
// MARK: - Transactions
586+
587+
extension DiskPersistence {
588+
public func withUnsafeTransaction(options: TransactionOptions, transaction: @escaping (_ persistence: DiskPersistence) async throws -> ()) async throws {
589+
let (transacrion, task) = await Transaction.makeTransaction(persistence: self, lastTransaction: lastTransaction, options: options) {
590+
try await transaction(self)
591+
}
592+
593+
/// Save the last non-concurrent transaction from the list. Note that disk persistence currently does not support concurrent idempotent transactions.
594+
if !options.contains(.readOnly) {
595+
lastTransaction = transacrion
596+
}
597+
598+
try await task.value
599+
}
600+
}
601+
583602
// MARK: - Helper Types
584603

585604
class WeakDatastore {
@@ -638,8 +657,3 @@ private enum DiskPersistenceTaskLocals {
638657
static var storeInfo: StoreInfo?
639658
}
640659

641-
extension DiskPersistence: _Persistence {
642-
public func withUnsafeTransaction(options: TransactionOptions, transaction: @escaping (_ persistence: DiskPersistence) async throws -> ()) async throws {
643-
644-
}
645-
}

Sources/CodableDatastore/Persistence/Disk Persistence/Transaction/Transaction.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ extension DiskPersistence {
1616
var childTransactions: [Transaction] = []
1717

1818
private(set) var task: Task<Void, Error>!
19-
var options: TransactionOptions = []
19+
let options: TransactionOptions
2020

2121
private init(
2222
persistence: DiskPersistence,
23-
parent: Transaction?
23+
parent: Transaction?,
24+
options: TransactionOptions
2425
) {
2526
self.persistence = persistence
2627
self.parent = parent
28+
self.options = options
2729
}
2830

2931
private func attachTask<T>(
@@ -43,7 +45,6 @@ extension DiskPersistence {
4345
return returnValue
4446
}
4547

46-
self.options = options
4748
self.task = Task {
4849
_ = try await task.value
4950

@@ -73,7 +74,8 @@ extension DiskPersistence {
7374

7475
let transaction = Transaction(
7576
persistence: persistence,
76-
parent: nil
77+
parent: nil,
78+
options: options
7779
)
7880

7981
let task = await transaction.attachTask(options: options) {
@@ -93,10 +95,12 @@ extension DiskPersistence {
9395
) async -> (Transaction, Task<T, Error>) {
9496
let transaction = Transaction(
9597
persistence: persistence,
96-
parent: self
98+
parent: self,
99+
options: options
97100
)
98101

99-
let lastChild = childTransactions.last
102+
/// Get the last non-concurrent transaction from the list. Note that disk persistence currently does not support concurrent idempotent transactions.
103+
let lastChild = childTransactions.last { !$0.options.contains(.readOnly) }
100104
childTransactions.append(transaction)
101105

102106
let task = await transaction.attachTask(options: options) {

0 commit comments

Comments
 (0)