diff --git a/Sources/CodableDatastore/Datastore/Datastore.swift b/Sources/CodableDatastore/Datastore/Datastore.swift index 11e2814..8641d75 100644 --- a/Sources/CodableDatastore/Datastore/Datastore.swift +++ b/Sources/CodableDatastore/Datastore/Datastore.swift @@ -192,7 +192,7 @@ extension Datastore { /// Check existing direct indexes for compatibility for (_, persistedIndex) in persistedDescriptor.directIndexes { - if let updatedIndex = updatedDescriptor.directIndexes[persistedIndex.name.rawValue] { + if let updatedIndex = updatedDescriptor.directIndexes[persistedIndex.name] { /// If the index still exists, make sure it is compatible by checking their types, or checking if the primary index must be re-built. if persistedIndex.type != updatedIndex.type || rebuildPrimaryIndex { /// They were not compatible, so delete the bad index, and queue it to be re-built. @@ -207,14 +207,14 @@ extension Datastore { /// Check for new direct indexes to build for (_, updatedIndex) in updatedDescriptor.directIndexes { - guard persistedDescriptor.directIndexes[updatedIndex.name.rawValue] == nil else { continue } + guard persistedDescriptor.directIndexes[updatedIndex.name] == nil else { continue } /// The index does not yet exist, so queue it to be built. directIndexesToBuild.insert(updatedIndex.name) } /// Check existing secondary indexes for compatibility for (_, persistedIndex) in persistedDescriptor.secondaryIndexes { - if let updatedIndex = updatedDescriptor.secondaryIndexes[persistedIndex.name.rawValue] { + if let updatedIndex = updatedDescriptor.secondaryIndexes[persistedIndex.name] { /// If the index still exists, make sure it is compatible if persistedIndex.type != updatedIndex.type { /// They were not compatible, so delete the bad index, and queue it to be re-built. @@ -229,7 +229,7 @@ extension Datastore { /// Check for new secondary indexes to build for (_, updatedIndex) in updatedDescriptor.secondaryIndexes { - guard persistedDescriptor.secondaryIndexes[updatedIndex.name.rawValue] == nil else { continue } + guard persistedDescriptor.secondaryIndexes[updatedIndex.name] == nil else { continue } /// The index does not yet exist, so queue it to be built. secondaryIndexesToBuild.insert(updatedIndex.name) } @@ -384,7 +384,7 @@ extension Datastore where AccessMode == ReadWrite { let descriptor = try await transaction.datastoreDescriptor(for: self.key), descriptor.size > 0, /// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type. - let matchingIndex = descriptor.directIndexes[index.path.rawValue] ?? descriptor.secondaryIndexes[index.path.rawValue], + let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path], /// We don't care in this method of the version is incompatible — the index will be discarded. let version = try? Version(matchingIndex.version), /// Make sure the stored version is smaller than the one we require, otherwise stop early. @@ -403,7 +403,7 @@ extension Datastore where AccessMode == ReadWrite { let descriptor = try await transaction.datastoreDescriptor(for: self.key), descriptor.size > 0, /// If we don't have an index stored, there is nothing to do here. This means we can skip checking it on the type. - let matchingIndex = descriptor.directIndexes[index.path.rawValue] ?? descriptor.secondaryIndexes[index.path.rawValue], + let matchingIndex = descriptor.directIndexes[index.path] ?? descriptor.secondaryIndexes[index.path], /// We don't care in this method of the version is incompatible — the index will be discarded. let version = try? Version(matchingIndex.version), /// Make sure the stored version is smaller than the one we require, otherwise stop early. diff --git a/Sources/CodableDatastore/Datastore/Dictionary+RawRepresentable.swift b/Sources/CodableDatastore/Datastore/Dictionary+RawRepresentable.swift new file mode 100644 index 0000000..dc57c69 --- /dev/null +++ b/Sources/CodableDatastore/Datastore/Dictionary+RawRepresentable.swift @@ -0,0 +1,25 @@ +// +// Dictionary+RawRepresentable.swift +// CodableDatastore +// +// Created by Dimitri Bouniol on 2023-07-20. +// Copyright © 2023 Mochi Development, Inc. All rights reserved. +// + +import Foundation + +extension Dictionary { + @usableFromInline + subscript(key: some RawRepresentable) -> Value? { + get { + return self[key.rawValue] + } + set(newValue) { + self[key.rawValue] = newValue + } + _modify { + defer { _fixLifetime(self) } + yield &self[key.rawValue] + } + } +} diff --git a/Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastoreRoot.swift b/Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastoreRoot.swift index f9a3947..5343a48 100644 --- a/Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastoreRoot.swift +++ b/Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastoreRoot.swift @@ -167,7 +167,7 @@ extension DiskPersistence.Datastore.RootObject { let indexType = indexDescriptor.type var version = indexDescriptor.version - if let originalVersion = originalManifest.descriptor.directIndexes[indexName.rawValue]?.version { + if let originalVersion = originalManifest.descriptor.directIndexes[indexName]?.version { version = originalVersion } else { let indexInfo = DatastoreRootManifest.IndexInfo( @@ -189,7 +189,7 @@ extension DiskPersistence.Datastore.RootObject { manifest.directIndexManifests.append(indexInfo) } - manifest.descriptor.directIndexes[indexName.rawValue] = DatastoreDescriptor.IndexDescriptor( + manifest.descriptor.directIndexes[indexName] = DatastoreDescriptor.IndexDescriptor( version: version, name: indexName, type: indexType @@ -201,7 +201,7 @@ extension DiskPersistence.Datastore.RootObject { let indexType = indexDescriptor.type var version = indexDescriptor.version - if let originalVersion = originalManifest.descriptor.secondaryIndexes[indexName.rawValue]?.version { + if let originalVersion = originalManifest.descriptor.secondaryIndexes[indexName]?.version { version = originalVersion } else { let indexInfo = DatastoreRootManifest.IndexInfo( @@ -223,7 +223,7 @@ extension DiskPersistence.Datastore.RootObject { manifest.secondaryIndexManifests.append(indexInfo) } - manifest.descriptor.secondaryIndexes[indexName.rawValue] = DatastoreDescriptor.IndexDescriptor( + manifest.descriptor.secondaryIndexes[indexName] = DatastoreDescriptor.IndexDescriptor( version: version, name: indexName, type: indexType diff --git a/Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift b/Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift index 7a3879b..759533c 100644 --- a/Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift +++ b/Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift @@ -421,7 +421,7 @@ extension DiskPersistence { let (datastore, rootID) = try await self.updatingCurrentSnapshot { snapshot in try await snapshot.updatingManifest { snapshotManifest, currentIteration in let (datastore, root) = await snapshot.loadDatastore(for: datastoreKey, from: currentIteration) - currentIteration.dataStores[datastoreKey.rawValue] = .init(key: datastoreKey, id: datastore.id, root: root) + currentIteration.dataStores[datastoreKey] = .init(key: datastoreKey, id: datastore.id, root: root) return (datastore, root) } } @@ -469,7 +469,7 @@ extension DiskPersistence { let containsEdits = try await readingCurrentSnapshot { snapshot in try await snapshot.readingManifest { manifest, iteration in for (key, root) in roots { - guard iteration.dataStores[key.rawValue]?.root == root.id + guard iteration.dataStores[key]?.root == root.id else { return true } } return false @@ -490,7 +490,7 @@ extension DiskPersistence { iteration.addedDatastoreRoots = addedDatastoreRoots iteration.removedDatastoreRoots = removedDatastoreRoots for (key, root) in roots { - iteration.dataStores[key.rawValue] = SnapshotIteration.DatastoreInfo( + iteration.dataStores[key] = SnapshotIteration.DatastoreInfo( key: key, id: root.datastore.id, root: root.id diff --git a/Sources/CodableDatastore/Persistence/Disk Persistence/Snapshot/Snapshot.swift b/Sources/CodableDatastore/Persistence/Disk Persistence/Snapshot/Snapshot.swift index 4b6f629..c55eeef 100644 --- a/Sources/CodableDatastore/Persistence/Disk Persistence/Snapshot/Snapshot.swift +++ b/Sources/CodableDatastore/Persistence/Disk Persistence/Snapshot/Snapshot.swift @@ -315,7 +315,7 @@ extension Snapshot { /// Load the datastore for the given key. func loadDatastore(for key: DatastoreKey, from iteration: SnapshotIteration) -> (DiskPersistence.Datastore, DatastoreRootIdentifier?) { let datastoreInfo: (id: DatastoreIdentifier, root: DatastoreRootIdentifier?) = { - if let info = iteration.dataStores[key.rawValue] { + if let info = iteration.dataStores[key] { return (info.id, info.root) } else { return (DatastoreIdentifier(name: key.rawValue), nil)