Skip to content

Commit 83b3508

Browse files
Refactored RawRepresentable access within Codable Dictionaries with String keys
1 parent 9296b2e commit 83b3508

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extension Datastore {
192192

193193
/// Check existing direct indexes for compatibility
194194
for (_, persistedIndex) in persistedDescriptor.directIndexes {
195-
if let updatedIndex = updatedDescriptor.directIndexes[persistedIndex.name.rawValue] {
195+
if let updatedIndex = updatedDescriptor.directIndexes[persistedIndex.name] {
196196
/// If the index still exists, make sure it is compatible by checking their types, or checking if the primary index must be re-built.
197197
if persistedIndex.type != updatedIndex.type || rebuildPrimaryIndex {
198198
/// They were not compatible, so delete the bad index, and queue it to be re-built.
@@ -207,14 +207,14 @@ extension Datastore {
207207

208208
/// Check for new direct indexes to build
209209
for (_, updatedIndex) in updatedDescriptor.directIndexes {
210-
guard persistedDescriptor.directIndexes[updatedIndex.name.rawValue] == nil else { continue }
210+
guard persistedDescriptor.directIndexes[updatedIndex.name] == nil else { continue }
211211
/// The index does not yet exist, so queue it to be built.
212212
directIndexesToBuild.insert(updatedIndex.name)
213213
}
214214

215215
/// Check existing secondary indexes for compatibility
216216
for (_, persistedIndex) in persistedDescriptor.secondaryIndexes {
217-
if let updatedIndex = updatedDescriptor.secondaryIndexes[persistedIndex.name.rawValue] {
217+
if let updatedIndex = updatedDescriptor.secondaryIndexes[persistedIndex.name] {
218218
/// If the index still exists, make sure it is compatible
219219
if persistedIndex.type != updatedIndex.type {
220220
/// They were not compatible, so delete the bad index, and queue it to be re-built.
@@ -229,7 +229,7 @@ extension Datastore {
229229

230230
/// Check for new secondary indexes to build
231231
for (_, updatedIndex) in updatedDescriptor.secondaryIndexes {
232-
guard persistedDescriptor.secondaryIndexes[updatedIndex.name.rawValue] == nil else { continue }
232+
guard persistedDescriptor.secondaryIndexes[updatedIndex.name] == nil else { continue }
233233
/// The index does not yet exist, so queue it to be built.
234234
secondaryIndexesToBuild.insert(updatedIndex.name)
235235
}
@@ -384,7 +384,7 @@ extension Datastore where AccessMode == ReadWrite {
384384
let descriptor = try await transaction.datastoreDescriptor(for: self.key),
385385
descriptor.size > 0,
386386
/// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type.
387-
let matchingIndex = descriptor.directIndexes[index.path.rawValue] ?? descriptor.secondaryIndexes[index.path.rawValue],
387+
let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path],
388388
/// We don't care in this method of the version is incompatible — the index will be discarded.
389389
let version = try? Version(matchingIndex.version),
390390
/// Make sure the stored version is smaller than the one we require, otherwise stop early.
@@ -403,7 +403,7 @@ extension Datastore where AccessMode == ReadWrite {
403403
let descriptor = try await transaction.datastoreDescriptor(for: self.key),
404404
descriptor.size > 0,
405405
/// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type.
406-
let matchingIndex = descriptor.directIndexes[index.path.rawValue] ?? descriptor.secondaryIndexes[index.path.rawValue],
406+
let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path],
407407
/// We don't care in this method of the version is incompatible — the index will be discarded.
408408
let version = try? Version(matchingIndex.version),
409409
/// Make sure the stored version is smaller than the one we require, otherwise stop early.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Dictionary+RawRepresentable.swift
3+
// CodableDatastore
4+
//
5+
// Created by Dimitri Bouniol on 2023-07-20.
6+
// Copyright © 2023 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Dictionary {
12+
@usableFromInline
13+
subscript(key: some RawRepresentable<Key>) -> Value? {
14+
get {
15+
return self[key.rawValue]
16+
}
17+
set(newValue) {
18+
self[key.rawValue] = newValue
19+
}
20+
_modify {
21+
defer { _fixLifetime(self) }
22+
yield &self[key.rawValue]
23+
}
24+
}
25+
}

Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastoreRoot.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extension DiskPersistence.Datastore.RootObject {
167167
let indexType = indexDescriptor.type
168168
var version = indexDescriptor.version
169169

170-
if let originalVersion = originalManifest.descriptor.directIndexes[indexName.rawValue]?.version {
170+
if let originalVersion = originalManifest.descriptor.directIndexes[indexName]?.version {
171171
version = originalVersion
172172
} else {
173173
let indexInfo = DatastoreRootManifest.IndexInfo(
@@ -189,7 +189,7 @@ extension DiskPersistence.Datastore.RootObject {
189189
manifest.directIndexManifests.append(indexInfo)
190190
}
191191

192-
manifest.descriptor.directIndexes[indexName.rawValue] = DatastoreDescriptor.IndexDescriptor(
192+
manifest.descriptor.directIndexes[indexName] = DatastoreDescriptor.IndexDescriptor(
193193
version: version,
194194
name: indexName,
195195
type: indexType
@@ -201,7 +201,7 @@ extension DiskPersistence.Datastore.RootObject {
201201
let indexType = indexDescriptor.type
202202
var version = indexDescriptor.version
203203

204-
if let originalVersion = originalManifest.descriptor.secondaryIndexes[indexName.rawValue]?.version {
204+
if let originalVersion = originalManifest.descriptor.secondaryIndexes[indexName]?.version {
205205
version = originalVersion
206206
} else {
207207
let indexInfo = DatastoreRootManifest.IndexInfo(
@@ -223,7 +223,7 @@ extension DiskPersistence.Datastore.RootObject {
223223
manifest.secondaryIndexManifests.append(indexInfo)
224224
}
225225

226-
manifest.descriptor.secondaryIndexes[indexName.rawValue] = DatastoreDescriptor.IndexDescriptor(
226+
manifest.descriptor.secondaryIndexes[indexName] = DatastoreDescriptor.IndexDescriptor(
227227
version: version,
228228
name: indexName,
229229
type: indexType

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ extension DiskPersistence {
421421
let (datastore, rootID) = try await self.updatingCurrentSnapshot { snapshot in
422422
try await snapshot.updatingManifest { snapshotManifest, currentIteration in
423423
let (datastore, root) = await snapshot.loadDatastore(for: datastoreKey, from: currentIteration)
424-
currentIteration.dataStores[datastoreKey.rawValue] = .init(key: datastoreKey, id: datastore.id, root: root)
424+
currentIteration.dataStores[datastoreKey] = .init(key: datastoreKey, id: datastore.id, root: root)
425425
return (datastore, root)
426426
}
427427
}
@@ -469,7 +469,7 @@ extension DiskPersistence {
469469
let containsEdits = try await readingCurrentSnapshot { snapshot in
470470
try await snapshot.readingManifest { manifest, iteration in
471471
for (key, root) in roots {
472-
guard iteration.dataStores[key.rawValue]?.root == root.id
472+
guard iteration.dataStores[key]?.root == root.id
473473
else { return true }
474474
}
475475
return false
@@ -490,7 +490,7 @@ extension DiskPersistence {
490490
iteration.addedDatastoreRoots = addedDatastoreRoots
491491
iteration.removedDatastoreRoots = removedDatastoreRoots
492492
for (key, root) in roots {
493-
iteration.dataStores[key.rawValue] = SnapshotIteration.DatastoreInfo(
493+
iteration.dataStores[key] = SnapshotIteration.DatastoreInfo(
494494
key: key,
495495
id: root.datastore.id,
496496
root: root.id

Sources/CodableDatastore/Persistence/Disk Persistence/Snapshot/Snapshot.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ extension Snapshot {
315315
/// Load the datastore for the given key.
316316
func loadDatastore(for key: DatastoreKey, from iteration: SnapshotIteration) -> (DiskPersistence<AccessMode>.Datastore, DatastoreRootIdentifier?) {
317317
let datastoreInfo: (id: DatastoreIdentifier, root: DatastoreRootIdentifier?) = {
318-
if let info = iteration.dataStores[key.rawValue] {
318+
if let info = iteration.dataStores[key] {
319319
return (info.id, info.root)
320320
} else {
321321
return (DatastoreIdentifier(name: key.rawValue), nil)

0 commit comments

Comments
 (0)