Skip to content

Commit

Permalink
feat: by default order results by modified at (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis authored Mar 9, 2023
1 parent a929848 commit dc0170f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
13 changes: 4 additions & 9 deletions Sources/Papyrus/PapyrusStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,9 @@ public struct PapyrusStore: Sendable {
/// Saves all objects to the store.
/// - Parameter objects: An array of objects to add to the store.
public func save<T: Papyrus>(objects: [T]) async where T: Sendable {
await withTaskGroup(of: Void.self, body: { group in
for object in objects {
group.addTask {
await self.save(object)
}
}
})
for object in objects {
await self.save(object)
}
}

private func save(_ object: PapyrusEncodingWrapper, filename: String) {
Expand All @@ -161,6 +157,7 @@ public struct PapyrusStore: Sendable {
let data = try self.encoder.encode(object)
let url = self.fileURL(for: object.typeDescription, filename: filename)
try data.write(to: url)
try self.fileManager.setAttributes([.modificationDate: Date.now], ofItemAtPath: url.path)
self.logger.debug("Saved: \(object.typeDescription) [Filename: \(filename)]")
} catch {
self.logger.fault("Failed to save: \(error)")
Expand Down Expand Up @@ -277,7 +274,6 @@ public struct PapyrusStore: Sendable {
group.addTask {
await self.delete(objects: objectsToDelete)
}

group.addTask {
await self.save(objects: objects)
}
Expand Down Expand Up @@ -308,7 +304,6 @@ public struct PapyrusStore: Sendable {
group.addTask {
await self.delete(objects: objectsToDelete)
}

group.addTask {
await self.save(objects: objects)
}
Expand Down
38 changes: 21 additions & 17 deletions Sources/Papyrus/Queries/CollectionQuery.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Combine
import Foundation

/// `PapyrusStore.CollectionQuery<T>` is a mechanism for querying `Papyrus` objects.
Expand All @@ -21,28 +20,33 @@ public class CollectionQuery<T> where T: Papyrus {
}

// MARK: API

/// Executes the query. If filter or sort parameters are
/// set, they will be applied to the results.
/// - Returns: The results of the query.
public func execute() async -> [T] {
guard let directoryNames = try? self.fileManager.contentsOfDirectory(atPath: self.directoryURL.path) else { return [] }
guard let filenames = try? self.fileManager.contentsOfDirectory(atPath: self.directoryURL.path)
else { return [] }

var results: [(Date, T)] = []
for filename in filenames {
let url = self.directoryURL.appendingPathComponent(filename)
do {
let data = try Data(contentsOf: url)
let model = try decoder.decode(T.self, from: data)
let modifiedDate = try self.fileManager.attributesOfItem(
atPath: url.path
)[.modificationDate] as? Date ?? .now
results.append((modifiedDate, model))
} catch {
continue
}
}

do {
return try directoryNames
.map { self.directoryURL.appendingPathComponent($0) }
.compactMap {
do
{
let data = try Data(contentsOf: $0)
return try decoder.decode(T.self, from: data)
}
catch
{
// TODO: Log error.
return nil
}
}
return try results
.sorted { $0.0 < $1.0 }
.map(\.1)
.filter(self.filter)
.sorted(by: self.sort)
} catch {
Expand Down
15 changes: 8 additions & 7 deletions Tests/PapyrusTests/CollectionQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,31 @@ final class CollectionQueryTests: XCTestCase {

// MARK: Tests

func testFetchingAll() async throws {
func test_fetchingAll() async throws {
let query = CollectionQuery<ExampleB>(directoryURL: self.storeDirectory)
let results = await query.execute().count
let results = await query.execute()

XCTAssertEqual(results, self.numberOfDummyObjects)
XCTAssertEqual(results.count, self.numberOfDummyObjects)
XCTAssertEqual(results.map(\.integerValue), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
}

func testFiltering() async throws {
func test_filter() async throws {
let query = CollectionQuery<ExampleB>(directoryURL: self.storeDirectory)
.filter { $0.integerValue > 5 }
let results = await query.execute().count

XCTAssertEqual(results, 5)
}

func testSorting() async throws {
func test_sort() async throws {
let query = CollectionQuery<ExampleB>(directoryURL: self.storeDirectory)
.sort { $0.integerValue > $1.integerValue }
let results = await query.execute()

XCTAssertEqual(results.first?.integerValue, 10)
}

func testFiltersAppliedToObserverPublisher() async throws {
func test_filter_whenAppliedToStream() async throws {
let collection = try await CollectionQuery<ExampleB>(directoryURL: self.storeDirectory)
.filter { $0.integerValue > 5 }
.stream()
Expand All @@ -58,7 +59,7 @@ final class CollectionQueryTests: XCTestCase {
XCTAssertEqual(collection?.count, 5)
}

func testSortAppliedToStream() async throws {
func test_sort_whenAppliedToStream() async throws {
let collection = try await CollectionQuery<ExampleB>(directoryURL: self.storeDirectory)
.sort { $0.integerValue > $1.integerValue }
.stream()
Expand Down

0 comments on commit dc0170f

Please sign in to comment.