Skip to content

Commit af44db3

Browse files
Refactored _Persistence into new public DatastoreInterfaceProtocol
1 parent 9eb36c9 commit af44db3

File tree

9 files changed

+753
-588
lines changed

9 files changed

+753
-588
lines changed

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension Datastore {
128128
warmupProgressHandlers.append(progressHandler)
129129
}
130130
let warmupTask = Task {
131-
let descriptor = try await persistence.register(datastore: self)
131+
let descriptor = try await persistence._datastoreInterface.register(datastore: self)
132132
print("\(String(describing: descriptor))")
133133

134134
/// Only operate on read-write datastores beyond this point.
@@ -169,7 +169,7 @@ extension Datastore where AccessMode == ReadWrite {
169169
public func migrate(index: IndexPath<CodedType>, ifLessThan minimumVersion: Version, progressHandler: ProgressHandler? = nil) async throws {
170170
guard
171171
/// If we have no descriptor, then no data exists to be migrated.
172-
let descriptor = try await persistence.datastoreDescriptor(for: self),
172+
let descriptor = try await persistence._datastoreInterface.datastoreDescriptor(for: self),
173173
descriptor.size > 0,
174174
/// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type.
175175
let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path],
@@ -188,7 +188,7 @@ extension Datastore where AccessMode == ReadWrite {
188188
/// Make sure we still need to do the work, as the warm up may have made changes anyways due to incompatible types.
189189
guard
190190
/// If we have no descriptor, then no data exists to be migrated.
191-
let descriptor = try await persistence.datastoreDescriptor(for: self),
191+
let descriptor = try await persistence._datastoreInterface.datastoreDescriptor(for: self),
192192
descriptor.size > 0,
193193
/// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type.
194194
let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path],
@@ -288,20 +288,20 @@ extension Datastore where AccessMode == ReadWrite {
288288
let versionData = try Data(self.version)
289289
let instanceData = try await self.encoder(instance)
290290

291-
try await persistence.withUnsafeTransaction(options: [.idempotent]) { persistence in
291+
try await persistence._datastoreInterface.withTransaction(options: [.idempotent]) { transaction in
292292
/// Create any missing indexes or prime the datastore for writing.
293-
try await persistence.apply(descriptor: updatedDescriptor, for: self.key)
293+
try await transaction.apply(descriptor: updatedDescriptor, for: self.key)
294294

295295
let existingEntry: (cursor: any InstanceCursorProtocol, instance: CodedType)? = try await {
296296
do {
297-
let existingEntry = try await persistence.primaryIndexCursor(for: idenfifier, datastoreKey: self.key)
297+
let existingEntry = try await transaction.primaryIndexCursor(for: idenfifier, datastoreKey: self.key)
298298

299299
let existingVersion = try Version(existingEntry.versionData)
300300
let decoder = try self.decoder(for: existingVersion)
301301
let existingInstance = try await decoder(existingEntry.instanceData)
302302

303303
return (cursor: existingEntry.cursor, instance: existingInstance)
304-
} catch PersistenceError.instanceNotFound {
304+
} catch DatastoreInterfaceError.instanceNotFound {
305305
return nil
306306
} catch {
307307
throw error
@@ -312,11 +312,11 @@ extension Datastore where AccessMode == ReadWrite {
312312
let existingInstance = existingEntry?.instance
313313
let insertionCursor: any InsertionCursorProtocol = try await {
314314
if let existingEntry { return existingEntry.cursor }
315-
return try await persistence.primaryIndexCursor(inserting: idenfifier, datastoreKey: self.key)
315+
return try await transaction.primaryIndexCursor(inserting: idenfifier, datastoreKey: self.key)
316316
}()
317317

318318
/// Persist the entry in the primary index
319-
try await persistence.persistPrimaryIndexEntry(
319+
try await transaction.persistPrimaryIndexEntry(
320320
versionData: versionData,
321321
identifierValue: idenfifier,
322322
instanceData: instanceData,
@@ -338,31 +338,31 @@ extension Datastore where AccessMode == ReadWrite {
338338

339339
if let existingValue {
340340
/// Grab a cursor to the old value in the index.
341-
let existingValueCursor = try await persistence.directIndexCursor(
341+
let existingValueCursor = try await transaction.directIndexCursor(
342342
for: existingValue.indexed,
343343
identifier: idenfifier,
344344
indexName: indexName,
345345
datastoreKey: self.key
346346
)
347347

348348
/// Delete it.
349-
try await persistence.deleteDirectIndexEntry(
349+
try await transaction.deleteDirectIndexEntry(
350350
cursor: existingValueCursor.cursor,
351351
indexName: indexName,
352352
datastoreKey: self.key
353353
)
354354
}
355355

356356
/// Grab a cursor to insert the new value in the index.
357-
let updatedValueCursor = try await persistence.directIndexCursor(
357+
let updatedValueCursor = try await transaction.directIndexCursor(
358358
inserting: updatedValue.indexed,
359359
identifier: idenfifier,
360360
indexName: indexName,
361361
datastoreKey: self.key
362362
)
363363

364364
/// Insert it.
365-
try await persistence.persistDirectIndexEntry(
365+
try await transaction.persistDirectIndexEntry(
366366
versionData: versionData,
367367
indexValue: updatedValue.indexed,
368368
identifierValue: idenfifier,
@@ -385,31 +385,31 @@ extension Datastore where AccessMode == ReadWrite {
385385

386386
if let existingValue {
387387
/// Grab a cursor to the old value in the index.
388-
let existingValueCursor = try await persistence.secondaryIndexCursor(
388+
let existingValueCursor = try await transaction.secondaryIndexCursor(
389389
for: existingValue.indexed,
390390
identifier: idenfifier,
391391
indexName: indexName,
392392
datastoreKey: self.key
393393
)
394394

395395
/// Delete it.
396-
try await persistence.deleteSecondaryIndexEntry(
396+
try await transaction.deleteSecondaryIndexEntry(
397397
cursor: existingValueCursor,
398398
indexName: indexName,
399399
datastoreKey: self.key
400400
)
401401
}
402402

403403
/// Grab a cursor to insert the new value in the index.
404-
let updatedValueCursor = try await persistence.secondaryIndexCursor(
404+
let updatedValueCursor = try await transaction.secondaryIndexCursor(
405405
inserting: updatedValue.indexed,
406406
identifier: idenfifier,
407407
indexName: indexName,
408408
datastoreKey: self.key
409409
)
410410

411411
/// Insert it.
412-
try await persistence.persistSecondaryIndexEntry(
412+
try await transaction.persistSecondaryIndexEntry(
413413
indexValue: updatedValue.indexed,
414414
identifierValue: idenfifier,
415415
cursor: updatedValueCursor,
@@ -423,15 +423,15 @@ extension Datastore where AccessMode == ReadWrite {
423423
guard !queriedIndexes.contains(indexName) else { return }
424424

425425
/// Grab a cursor to the old value in the index.
426-
let existingValueCursor = try await persistence.secondaryIndexCursor(
426+
let existingValueCursor = try await transaction.secondaryIndexCursor(
427427
for: value,
428428
identifier: idenfifier,
429429
indexName: indexName,
430430
datastoreKey: self.key
431431
)
432432

433433
/// Delete it.
434-
try await persistence.deleteSecondaryIndexEntry(
434+
try await transaction.deleteSecondaryIndexEntry(
435435
cursor: existingValueCursor,
436436
indexName: indexName,
437437
datastoreKey: self.key
@@ -443,15 +443,15 @@ extension Datastore where AccessMode == ReadWrite {
443443
guard !queriedIndexes.contains(indexName) else { return }
444444

445445
/// Grab a cursor to insert the new value in the index.
446-
let updatedValueCursor = try await persistence.secondaryIndexCursor(
446+
let updatedValueCursor = try await transaction.secondaryIndexCursor(
447447
inserting: value,
448448
identifier: idenfifier,
449449
indexName: indexName,
450450
datastoreKey: self.key
451451
)
452452

453453
/// Insert it.
454-
try await persistence.persistSecondaryIndexEntry(
454+
try await transaction.persistSecondaryIndexEntry(
455455
indexValue: value,
456456
identifierValue: idenfifier,
457457
cursor: updatedValueCursor,

Sources/CodableDatastore/Persistence/Cursor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
///
1111
/// - Note: A cursor is only valid within the same transaction for the same persistence it was created for.
1212
public protocol CursorProtocol<P> {
13-
associatedtype P: _Persistence
13+
associatedtype P: Persistence
1414
var persistence: P { get }
1515

1616
// var transaction: Transaction<P> { get }

Sources/CodableDatastore/Persistence/DatastoreInterfaceError.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// PersistenceError.swift
2+
// DatastoreInterfaceError.swift
33
// CodableDatastore
44
//
55
// Created by Dimitri Bouniol on 2023-06-13.
@@ -8,8 +8,8 @@
88

99
import Foundation
1010

11-
/// A ``Persistence``-specific error.
12-
public enum PersistenceError: LocalizedError {
11+
/// An error that may be returned from ``DatastoreInterfaceProtocol`` methods.
12+
public enum DatastoreInterfaceError: LocalizedError {
1313
/// The datastore has already been registered with another persistence.
1414
case multipleRegistrations
1515

0 commit comments

Comments
 (0)