Skip to content

Commit 15db473

Browse files
Refactored url fetching so it is more centralized
1 parent 0353f62 commit 15db473

File tree

6 files changed

+91
-45
lines changed

6 files changed

+91
-45
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,35 @@ extension DiskPersistence.Datastore.Index {
6767
case direct(index: DatastoreIndexIdentifier, manifest: DatastoreIndexManifestIdentifier)
6868
case secondary(index: DatastoreIndexIdentifier, manifest: DatastoreIndexManifestIdentifier)
6969

70+
var indexID: DatastoreRootManifest.IndexID {
71+
switch self {
72+
case .primary(_): .primary
73+
case .direct(let id, _): .direct(index: id)
74+
case .secondary(let id, _): .secondary(index: id)
75+
}
76+
}
77+
7078
var manifestID: DatastoreIndexManifestIdentifier {
7179
switch self {
72-
case .primary(let id),
73-
.direct(_, let id),
74-
.secondary(_, let id):
75-
return id
80+
case .primary(let id): id
81+
case .direct(_, let id): id
82+
case .secondary(_, let id): id
7683
}
7784
}
7885

7986
func with(manifestID: DatastoreIndexManifestIdentifier) -> Self {
8087
switch self {
81-
case .primary: return .primary(manifest: manifestID)
82-
case .direct(let indexID, _): return .direct(index: indexID, manifest: manifestID)
83-
case .secondary(let indexID, _): return .secondary(index: indexID, manifest: manifestID)
88+
case .primary: .primary(manifest: manifestID)
89+
case .direct(let indexID, _): .direct(index: indexID, manifest: manifestID)
90+
case .secondary(let indexID, _): .secondary(index: indexID, manifest: manifestID)
91+
}
92+
}
93+
94+
init(_ id: DatastoreRootManifest.IndexManifestID) {
95+
switch id {
96+
case .primary(let manifest): self = .primary(manifest: manifest)
97+
case .direct(let index, let manifest): self = .direct(index: index, manifest: manifest)
98+
case .secondary(let index, let manifest): self = .secondary(index: index, manifest: manifest)
8499
}
85100
}
86101
}
@@ -91,9 +106,7 @@ extension DiskPersistence.Datastore.Index {
91106
extension DiskPersistence.Datastore.Index {
92107
/// The URL that points to the manifest.
93108
nonisolated var manifestURL: URL {
94-
datastore
95-
.manifestsURL(for: id)
96-
.appendingPathComponent("\(id.manifestID).indexmanifest", isDirectory: false)
109+
datastore.manifestURL(for: id)
97110
}
98111
}
99112

@@ -129,7 +142,7 @@ extension DiskPersistence.Datastore.Index {
129142
}
130143

131144
/// Make sure the directories exists first.
132-
try FileManager.default.createDirectory(at: datastore.manifestsURL(for: id), withIntermediateDirectories: true)
145+
try FileManager.default.createDirectory(at: datastore.manifestsURL(for: id.indexID), withIntermediateDirectories: true)
133146

134147
/// Encode the provided manifest, and write it to disk.
135148
let data = Data(manifest.bytes)

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,7 @@ extension DiskPersistence.Datastore.Page {
7878
extension DiskPersistence.Datastore.Page {
7979
/// The URL that points to the page.
8080
nonisolated var pageURL: URL {
81-
let baseURL = datastore.pagesURL(for: id.index)
82-
83-
guard let components = try? id.page.components else { preconditionFailure("Components could not be determined for Page.") }
84-
85-
return baseURL
86-
.appendingPathComponent(components.year, isDirectory: true)
87-
.appendingPathComponent(components.monthDay, isDirectory: true)
88-
.appendingPathComponent(components.hourMinute, isDirectory: true)
89-
.appendingPathComponent("\(id.page).datastorepage", isDirectory: false)
81+
datastore.pageURL(for: id)
9082
}
9183
}
9284

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extension DiskPersistence.Datastore.RootObject: Hashable {
5656
extension DiskPersistence.Datastore.RootObject {
5757
/// The URL that points to the root object on disk.
5858
nonisolated var rootObjectURL: URL {
59-
datastore.rootURL.appendingPathComponent("\(id).json", isDirectory: false)
59+
datastore.rootURL(for: id)
6060
}
6161
}
6262

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,21 @@ extension DatastoreRootManifest {
8383
self = .secondary(index: index, manifest: manifest)
8484
}
8585
}
86+
87+
var indexID: DatastoreRootManifest.IndexID {
88+
switch self {
89+
case .primary(_): .primary
90+
case .direct(let id, _): .direct(index: id)
91+
case .secondary(let id, _): .secondary(index: id)
92+
}
93+
}
94+
95+
var manifestID: DatastoreIndexManifestIdentifier {
96+
switch self {
97+
case .primary(let id): id
98+
case .direct(_, let id): id
99+
case .secondary(_, let id): id
100+
}
101+
}
86102
}
87103
}

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

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ extension DiskPersistence {
5757
extension DiskPersistence.Datastore {
5858
/// The URL that points to the Datastore directory.
5959
nonisolated var datastoreURL: URL {
60-
snapshot
61-
.datastoresURL
62-
.appendingPathComponent("\(id).datastore", isDirectory: true)
60+
snapshot.datastoreURL(for: id)
6361
}
6462

6563
/// The URL that points to the Root directory.
@@ -68,40 +66,62 @@ extension DiskPersistence.Datastore {
6866
.appendingPathComponent("Root", isDirectory: true)
6967
}
7068

69+
/// The URL for the specified root.
70+
nonisolated func rootURL(for id: DatastoreRootIdentifier) -> URL {
71+
rootURL.appendingPathComponent("\(id).json", isDirectory: false)
72+
}
73+
7174
/// The URL that points to the DirectIndexes directory.
7275
nonisolated var directIndexesURL: URL {
73-
datastoreURL
74-
.appendingPathComponent("DirectIndexes", isDirectory: true)
76+
datastoreURL.appendingPathComponent("DirectIndexes", isDirectory: true)
7577
}
7678

7779
/// The URL that points to the SecondaryIndexes directory.
7880
nonisolated var secondaryIndexesURL: URL {
79-
datastoreURL
80-
.appendingPathComponent("SecondaryIndexes", isDirectory: true)
81+
datastoreURL.appendingPathComponent("SecondaryIndexes", isDirectory: true)
8182
}
8283

83-
nonisolated func indexURL(for indexID: Index.ID) -> URL {
84+
/// The root URL of a partifular index directory.
85+
nonisolated func indexURL(for indexID: DatastoreRootManifest.IndexID) -> URL {
8486
switch indexID {
8587
case .primary:
86-
return directIndexesURL
87-
.appendingPathComponent("Primary.datastoreindex", isDirectory: true)
88-
case .direct(let indexID, _):
89-
return directIndexesURL
90-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
91-
case .secondary(let indexID, _):
92-
return secondaryIndexesURL
93-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
88+
directIndexesURL.appendingPathComponent("Primary.datastoreindex", isDirectory: true)
89+
case .direct(let indexID):
90+
directIndexesURL.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
91+
case .secondary(let indexID):
92+
secondaryIndexesURL.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
9493
}
9594
}
9695

97-
nonisolated func manifestsURL(for indexID: Index.ID) -> URL {
98-
indexURL(for: indexID)
99-
.appendingPathComponent("Manifest", isDirectory: true)
96+
/// The URL of an index's manifests directory.
97+
nonisolated func manifestsURL(for id: DatastoreRootManifest.IndexID) -> URL {
98+
indexURL(for: id).appendingPathComponent("Manifest", isDirectory: true)
99+
}
100+
101+
/// The URL of an index's root manifest.
102+
nonisolated func manifestURL(for id: Index.ID) -> URL {
103+
manifestURL(for: DatastoreRootManifest.IndexManifestID(id))
100104
}
101105

102-
nonisolated func pagesURL(for indexID: Index.ID) -> URL {
103-
indexURL(for: indexID)
104-
.appendingPathComponent("Pages", isDirectory: true)
106+
/// The URL of an index's root manifest.
107+
nonisolated func manifestURL(for id: DatastoreRootManifest.IndexManifestID) -> URL {
108+
manifestsURL(for: id.indexID).appendingPathComponent("\(id.manifestID).indexmanifest", isDirectory: false)
109+
}
110+
111+
/// The URL of an index's pages directory..
112+
nonisolated func pagesURL(for id: Index.ID) -> URL {
113+
indexURL(for: id.indexID).appendingPathComponent("Pages", isDirectory: true)
114+
}
115+
116+
/// The URL of a particular page.
117+
nonisolated func pageURL(for id: Page.ID) -> URL {
118+
guard let components = try? id.page.components else { preconditionFailure("Components could not be determined for Page.") }
119+
120+
return pagesURL(for: id.index)
121+
.appendingPathComponent(components.year, isDirectory: true)
122+
.appendingPathComponent(components.monthDay, isDirectory: true)
123+
.appendingPathComponent(components.hourMinute, isDirectory: true)
124+
.appendingPathComponent("\(id.page).datastorepage", isDirectory: false)
105125
}
106126
}
107127

@@ -198,7 +218,7 @@ extension DiskPersistence.Datastore {
198218
extension DiskPersistence.Datastore {
199219
/// Load the root object from disk for the given identifier.
200220
func loadRootObject(for rootIdentifier: DatastoreRootIdentifier) throws -> DatastoreRootManifest {
201-
let rootObjectURL = rootURL.appendingPathComponent("\(rootIdentifier).json", isDirectory: false)
221+
let rootObjectURL = rootURL(for: rootIdentifier)
202222

203223
let data = try Data(contentsOf: rootObjectURL)
204224

@@ -218,7 +238,7 @@ extension DiskPersistence.Datastore {
218238
try FileManager.default.createDirectory(at: secondaryIndexesURL, withIntermediateDirectories: true)
219239
}
220240

221-
let rootObjectURL = rootURL.appendingPathComponent("\(manifest.id).json", isDirectory: false)
241+
let rootObjectURL = rootURL(for: manifest.id)
222242

223243
/// Encode the provided manifest, and write it to disk.
224244
let data = try JSONEncoder.shared.encode(manifest)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ extension Snapshot {
9595
snapshotURL.appendingPathComponent("Datastores", isDirectory: true)
9696
}
9797

98+
/// The URL for a specific datastore within the snapshot.
99+
nonisolated func datastoreURL(for id: DatastoreIdentifier) -> URL {
100+
datastoresURL.appendingPathComponent("\(id).datastore", isDirectory: true)
101+
}
102+
98103
/// The URL that points to the Inbox directory.
99104
nonisolated var inboxURL: URL {
100105
snapshotURL.appendingPathComponent("Inbox", isDirectory: true)

0 commit comments

Comments
 (0)