Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Sources/CodableDatastore/Datastore/Datastore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Key>) -> Value? {
get {
return self[key.rawValue]
}
set(newValue) {
self[key.rawValue] = newValue
}
_modify {
defer { _fixLifetime(self) }
yield &self[key.rawValue]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ extension Snapshot {
/// Load the datastore for the given key.
func loadDatastore(for key: DatastoreKey, from iteration: SnapshotIteration) -> (DiskPersistence<AccessMode>.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)
Expand Down