Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manually set created at timestamp #10

Merged
merged 1 commit into from
Sep 23, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>classNames</key>
<dict>
<key>PerformanceTests</key>
<dict>
<key>test_save()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.750000</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>runDestinationsByUUID</key>
<dict>
<key>1EF546CB-F067-404D-B700-2091BBA72C20</key>
<dict>
<key>localComputer</key>
<dict>
<key>busSpeedInMHz</key>
<integer>0</integer>
<key>cpuCount</key>
<integer>1</integer>
<key>cpuKind</key>
<string>Apple M1 Ultra</string>
<key>cpuSpeedInMHz</key>
<integer>0</integer>
<key>logicalCPUCoresPerPackage</key>
<integer>20</integer>
<key>modelCode</key>
<string>Mac13,2</string>
<key>physicalCPUCoresPerPackage</key>
<integer>20</integer>
<key>platformIdentifier</key>
<string>com.apple.platform.macosx</string>
</dict>
<key>targetArchitecture</key>
<string>arm64</string>
</dict>
</dict>
</dict>
</plist>
2 changes: 1 addition & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/Papyrus.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
default = "YES">
</TestPlanReference>
<TestPlanReference
reference = "container:Tests/Performance/Supporting Filles/Performance.xctestplan">
reference = "container:Tests/Performance/Supporting Files/Performance.xctestplan">
</TestPlanReference>
</TestPlans>
<Testables>
Expand Down
22 changes: 21 additions & 1 deletion Sources/Papyrus/PapyrusStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public struct PapyrusStore: Sendable {
self.logger.debug("Created directory: \(url.absoluteString)")
}

private func setCreatedAt(_ timestamp: Date, for url: URL) throws {
try self.fileManager.setAttributes(
[.creationDate: timestamp],
ofItemAtPath: url.path
)
}

// MARK: Saving

/// Saves the object to the store.
Expand Down Expand Up @@ -129,9 +136,22 @@ public struct PapyrusStore: Sendable {
/// - Parameter objects: An array of objects to add to the store.
public func save<T: Papyrus>(objects: [T]) async throws where T: Sendable {
try await withThrowingTaskGroup(of: Void.self) { group in
for object in objects {
let timestamp = Date.now

for (index, object) in objects.enumerated() {
group.addTask {
let url = self.fileURL(for: object.typeDescription, filename: object.filename)
let fileAlreadyExists = self.fileManager.fileExists(atPath: url.path)
try self.save(object, touchDirectory: false)

if !fileAlreadyExists {
// Because the aren't guaranteed to happen in order
// we need to manually set the created at timstamp.
try self.setCreatedAt(
timestamp.addingTimeInterval(TimeInterval(index) / 100000.0),
for: url
)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Papyrus/Queries/CollectionQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public class CollectionQuery<T> where T: Papyrus {
let url = self.directoryURL.appendingPathComponent(filename)
let data = try Data(contentsOf: url)
let model = try self.decoder.decode(T.self, from: data)
let modifiedDate = try self.fileManager.attributesOfItem(
let creationDate = try self.fileManager.attributesOfItem(
atPath: url.path
)[.creationDate] as? Date ?? .now
result.append((modifiedDate, model))
result.append((creationDate, model))
} catch {
self.logger.error("Failed to read cached data. error: \(error)")
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/PapyrusStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ final class PapyrusStoreTests: XCTestCase {
}

func test_fetchingObjects() async throws {
let objects = (0..<3).map { _ in
let objects = (0..<10).map { _ in
ExampleD()
}
try await self.store.save(objects: objects)

let fetchedObjects = self.store.objects(type: ExampleD.self).execute()
XCTAssertEqual(fetchedObjects.count, 3)
XCTAssertEqual(fetchedObjects, objects)
}

// MARK: Deleting
Expand Down
Loading