Skip to content

Commit 29de5ee

Browse files
Cleaned up Index and Page URL handling
1 parent 46f993c commit 29de5ee

File tree

3 files changed

+70
-54
lines changed

3 files changed

+70
-54
lines changed

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

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,56 +55,11 @@ extension DiskPersistence.Datastore.Index {
5555
// MARK: - Common URL Accessors
5656

5757
extension DiskPersistence.Datastore.Index {
58-
/// The URL that points to the index.
59-
nonisolated var indexURL: URL {
60-
switch id {
61-
case .primary:
62-
return datastore
63-
.directIndexesURL
64-
.appendingPathComponent("Primary.datastoreindex", isDirectory: true)
65-
case .direct(let indexID, _):
66-
return datastore
67-
.directIndexesURL
68-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
69-
case .secondary(let indexID, _):
70-
return datastore
71-
.secondaryIndexesURL
72-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
73-
}
74-
}
75-
76-
/// The URL that points to the Manifest directory.
77-
nonisolated var manifestsURL: URL {
78-
indexURL.appendingPathComponent("Manifest", isDirectory: true)
79-
}
80-
81-
/// The URL that points to the Manifest directory.
82-
nonisolated var pagesURL: URL {
83-
indexURL.appendingPathComponent("Pages", isDirectory: true)
84-
}
85-
8658
/// The URL that points to the manifest.
8759
nonisolated var manifestURL: URL {
88-
switch id {
89-
case .primary(let manifestID):
90-
return datastore
91-
.directIndexesURL
92-
.appendingPathComponent("Primary.datastoreindex", isDirectory: true)
93-
.appendingPathComponent("Manifest", isDirectory: true)
94-
.appendingPathComponent("\(manifestID).indexmanifest", isDirectory: false)
95-
case .direct(let indexID, let manifestID):
96-
return datastore
97-
.directIndexesURL
98-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
99-
.appendingPathComponent("Manifest", isDirectory: true)
100-
.appendingPathComponent("\(manifestID).indexmanifest", isDirectory: false)
101-
case .secondary(let indexID, let manifestID):
102-
return datastore
103-
.secondaryIndexesURL
104-
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
105-
.appendingPathComponent("Manifest", isDirectory: true)
106-
.appendingPathComponent("\(manifestID).indexmanifest", isDirectory: false)
107-
}
60+
datastore
61+
.manifestsURL(for: id)
62+
.appendingPathComponent("\(id.manifestID).indexmanifest", isDirectory: false)
10863
}
10964
}
11065

@@ -134,10 +89,26 @@ extension DiskPersistence.Datastore.Index {
13489
}
13590

13691
/// Make sure the directories exists first.
137-
try FileManager.default.createDirectory(at: manifestsURL, withIntermediateDirectories: true)
92+
try FileManager.default.createDirectory(at: datastore.manifestsURL(for: id), withIntermediateDirectories: true)
13893

13994
/// Encode the provided manifest, and write it to disk.
14095
let data = Data(manifest.bytes)
14196
try data.write(to: manifestURL, options: .atomic)
14297
}
14398
}
99+
100+
// MARK: - Pages
101+
102+
extension DiskPersistence.Datastore.Index {
103+
var orderedPages: [DiskPersistence.Datastore.Page] {
104+
get async throws {
105+
var pages: [DiskPersistence.Datastore.Page] = []
106+
107+
for pageID in try await manifest.orderedPageIDs {
108+
pages.append(await datastore.page(for: .init(index: id, page: pageID)))
109+
}
110+
111+
return pages
112+
}
113+
}
114+
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ typealias DatastorePageIdentifier = DatedIdentifier<DiskPersistence<ReadOnly>.Da
1212

1313
extension DiskPersistence.Datastore {
1414
actor Page: Identifiable {
15-
struct ID: Hashable {
16-
let index: Index.ID
17-
let page: DatastorePageIdentifier
18-
}
19-
2015
unowned let datastore: DiskPersistence<AccessMode>.Datastore
2116

2217
let id: ID
@@ -30,3 +25,29 @@ extension DiskPersistence.Datastore {
3025
}
3126
}
3227
}
28+
29+
// MARK: - Helper Types
30+
31+
extension DiskPersistence.Datastore.Page {
32+
struct ID: Hashable {
33+
let index: DiskPersistence.Datastore.Index.ID
34+
let page: DatastorePageIdentifier
35+
}
36+
}
37+
38+
// MARK: - Common URL Accessors
39+
40+
extension DiskPersistence.Datastore.Page {
41+
/// The URL that points to the page.
42+
nonisolated var pageURL: URL {
43+
let baseURL = datastore.pagesURL(for: id.index)
44+
45+
guard let components = try? id.page.components else { preconditionFailure("Components could not be determined for Page.") }
46+
47+
return baseURL
48+
.appendingPathComponent(components.year, isDirectory: true)
49+
.appendingPathComponent(components.monthDay, isDirectory: true)
50+
.appendingPathComponent(components.hourMinute, isDirectory: true)
51+
.appendingPathComponent("\(id).datastorepage", isDirectory: false)
52+
}
53+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ extension DiskPersistence.Datastore {
6767
datastoreURL
6868
.appendingPathComponent("SecondaryIndexes", isDirectory: true)
6969
}
70+
71+
nonisolated func indexURL(for indexID: Index.ID) -> URL {
72+
switch indexID {
73+
case .primary:
74+
return directIndexesURL
75+
.appendingPathComponent("Primary.datastoreindex", isDirectory: true)
76+
case .direct(let indexID, _):
77+
return directIndexesURL
78+
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
79+
case .secondary(let indexID, _):
80+
return secondaryIndexesURL
81+
.appendingPathComponent("\(indexID).datastoreindex", isDirectory: true)
82+
}
83+
}
84+
85+
nonisolated func manifestsURL(for indexID: Index.ID) -> URL {
86+
indexURL(for: indexID)
87+
.appendingPathComponent("Manifest", isDirectory: true)
88+
}
89+
90+
nonisolated func pagesURL(for indexID: Index.ID) -> URL {
91+
indexURL(for: indexID)
92+
.appendingPathComponent("Pages", isDirectory: true)
93+
}
7094
}
7195

7296
// MARK: - Root Object Management

0 commit comments

Comments
 (0)