Skip to content

Commit

Permalink
Refactor PackageSearchApiPackageCache handling
Browse files Browse the repository at this point in the history
The code for handling the network results in the PackageSearchApiPackageCache class has been refactored. Instead of performing a removal of old entries followed by an insertion of new entries, the code now performs an update operation for each new entry. This makes the handling of network results more efficient and concise.
  • Loading branch information
lamba92 committed Feb 19, 2024
1 parent 5d9ee41 commit 91bff07
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,12 @@ class PackageSearchApiPackageCache(
.suspendSafe()
.onFailure { logDebug("${this::class.qualifiedName}#getPackages", it) }
if (networkResults.isSuccess) {
val packageEntries = networkResults.getOrThrow()
val cacheEntriesFromNetwork = networkResults.getOrThrow()
.values
.map { it.asCacheEntry() }
if (packageEntries.isNotEmpty()) {
logDebug(contextName) { "No packages found | missingIds.size = ${missingIds.size}" }

// remove the old entries
apiPackageCache.remove(
filter = NitriteFilters.Object.`in`(
path = packageIdSelector,
value = packageEntries.mapNotNull { it.packageId }
)
)
logDebug(contextName) {
"Removing old entries | packageEntries.size = ${packageEntries.size}"
}
}
// evaluate packages that are missing from our backend
val retrievedPackageIds =
packageEntries.mapNotNull { if (useHashes) it.packageIdHash else it.packageId }
cacheEntriesFromNetwork.mapNotNull { if (useHashes) it.packageIdHash else it.packageId }
.toSet()
val unknownPackages = missingIds.minus(retrievedPackageIds)
.map { id ->
Expand All @@ -165,8 +151,22 @@ class PackageSearchApiPackageCache(
"New unknown packages | unknownPackages.size = ${unknownPackages.size}"
}
// insert the new entries
val toInsert = packageEntries + unknownPackages
if (toInsert.isNotEmpty()) apiPackageCache.insert(toInsert)
val toInsert = cacheEntriesFromNetwork + unknownPackages
if (toInsert.isNotEmpty()) {
toInsert.forEach { insert ->
apiPackageCache.update(
filter = NitriteFilters.Object.eq(
path = packageIdSelector,
value = when {
useHashes -> insert.packageIdHash
else -> insert.packageId
}
),
update = insert,
upsert = true
)
}
}
}
val networkResultsData = networkResults.getOrDefault(emptyMap())
logDebug(contextName) {
Expand Down

0 comments on commit 91bff07

Please sign in to comment.