Skip to content

Commit

Permalink
Merge pull request #27 from dannynorth/fix-read-data-and-keys
Browse files Browse the repository at this point in the history
Fix readDataAndKeys
  • Loading branch information
mergesort authored Oct 20, 2023
2 parents b8468ee + 2c18369 commit a415bc2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
13 changes: 9 additions & 4 deletions Sources/Bodega/DiskStorageEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ public actor DiskStorageEngine: StorageEngine {
/// - Returns: An array of `[(CacheKey, Data)]` read from disk if the ``CacheKey``s exist,
/// and an empty array if there are no `Data` items matching the `keys` passed in.
public func readDataAndKeys(keys: [CacheKey]) async -> [(key: CacheKey, data: Data)] {
return zip(
keys,
await self.read(keys: keys)
).map { ($0, $1) }
var dataAndKeys: [(key: CacheKey, data: Data)] = []

for key in keys {
if let data = self.read(key: key) {
dataAndKeys.append((key, data))
}
}

return dataAndKeys
}

/// Reads all the `[Data]` located in the `directory`.
Expand Down
13 changes: 9 additions & 4 deletions Sources/Bodega/ObjectStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ public actor ObjectStorage<Object: Codable> {
/// - Returns: An array of `[(CacheKey, Object)]` read if it exists,
/// and an empty array if there are no `Objects`s matching the `keys` passed in.
public func objectsAndKeys(keys: [CacheKey]) async -> [(key: CacheKey, object: Object)] {
return zip(
keys,
await self.objects(forKeys: keys)
).map { ($0, $1) }
var objectsAndKeys: [(key: CacheKey, object: Object)] = []

for key in keys {
if let object = await self.object(forKey: key) {
objectsAndKeys.append((key, object))
}
}

return objectsAndKeys
}

/// Reads all `[Object]` objects.
Expand Down
13 changes: 13 additions & 0 deletions Tests/BodegaTests/DiskStorageEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ final class DiskStorageEngineTests: XCTestCase {
"Value 9"
])
}

func testReadingDataAndNonExistentKeys() async throws {
try await self.writeItemsToDisk(count: 5)
let allDataAndKeys = await storage.readAllDataAndKeys()
.sorted(by: { $0.key.value > $1.key.value })

let goodAndBadKeys = (0 ..< 10).reversed().map({ CacheKey(verbatim: "\($0)") })
let validDataAndKeys = await storage.readDataAndKeys(keys: goodAndBadKeys)
.sorted(by: { $0.key.value > $1.key.value })

XCTAssertEqual(allDataAndKeys.map(\.key), validDataAndKeys.map(\.key))
XCTAssertEqual(allDataAndKeys.map(\.data), validDataAndKeys.map(\.data))
}

func testReadingAllDataSucceeds() async throws {
try await self.writeItemsToDisk(count: 10)
Expand Down
13 changes: 13 additions & 0 deletions Tests/BodegaTests/ObjectStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ class ObjectStorageTests: XCTestCase {
])
}

func testReadingObjectsAndNonExistentKeys() async throws {
try await self.writeObjectsToDisk(count: 5)
let allDataAndKeys = await storage.allObjectsAndKeys()
.sorted(by: { $0.key.value > $1.key.value })

let goodAndBadKeys = (0 ..< 10).reversed().map({ CacheKey(verbatim: "\($0)") })
let validObjectsAndKeys = await storage.objectsAndKeys(keys: goodAndBadKeys)
.sorted(by: { $0.key.value > $1.key.value })

XCTAssertEqual(allDataAndKeys.map(\.key), validObjectsAndKeys.map(\.key))
XCTAssertEqual(allDataAndKeys.map(\.object), validObjectsAndKeys.map(\.object))
}

func testReadingAllObjectsSucceeds() async throws {
try await self.writeObjectsToDisk(count: 10)

Expand Down
13 changes: 13 additions & 0 deletions Tests/BodegaTests/SQLiteStorageEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ final class SQLiteStorageEngineTests: XCTestCase {
"Value 9"
])
}

func testReadingDataAndNonExistentKeys() async throws {
try await self.writeItemsToDatabase(count: 5)
let allDataAndKeys = await storage.readAllDataAndKeys()
.sorted(by: { $0.key.value > $1.key.value })

let goodAndBadKeys = (0 ..< 10).reversed().map({ CacheKey(verbatim: "\($0)") })
let validDataAndKeys = await storage.readDataAndKeys(keys: goodAndBadKeys)
.sorted(by: { $0.key.value > $1.key.value })

XCTAssertEqual(allDataAndKeys.map(\.key), validDataAndKeys.map(\.key))
XCTAssertEqual(allDataAndKeys.map(\.data), validDataAndKeys.map(\.data))
}

func testReadingAllDataSucceeds() async throws {
try await self.writeItemsToDatabase(count: 10)
Expand Down

0 comments on commit a415bc2

Please sign in to comment.