Skip to content

Commit

Permalink
Don't return values for non-existent keys
Browse files Browse the repository at this point in the history
Fixes mergesort#24

Using `zip()` loses the association between keys and values, because the array returned from `read(keys:)` does not provide information about _skipped_ keys (See mergesort#26).
  • Loading branch information
dannynorth committed Oct 12, 2023
1 parent 424eb42 commit 2c18369
Show file tree
Hide file tree
Showing 2 changed files with 18 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

0 comments on commit 2c18369

Please sign in to comment.