Skip to content
Snippets Groups Projects
Unverified Commit fccc32a8 authored by Philipp Woessner's avatar Philipp Woessner Committed by GitHub
Browse files

Fixed the outdated check for the locally persisted files (#839)

* remove all files from the cache if they are no longer present in the server date index

* Set version to 1.0.5 for hotfix release
parent c62b4500
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ package de.rki.coronawarnapp.storage.keycache ...@@ -21,7 +21,7 @@ package de.rki.coronawarnapp.storage.keycache
import android.content.Context import android.content.Context
import de.rki.coronawarnapp.storage.AppDatabase import de.rki.coronawarnapp.storage.AppDatabase
import de.rki.coronawarnapp.storage.FileStorageHelper.isOutdated import timber.log.Timber
import java.io.File import java.io.File
import java.net.URI import java.net.URI
...@@ -58,13 +58,16 @@ class KeyCacheRepository(private val keyCacheDao: KeyCacheDao) { ...@@ -58,13 +58,16 @@ class KeyCacheRepository(private val keyCacheDao: KeyCacheDao) {
} }
) )
suspend fun deleteOutdatedEntries() = keyCacheDao.getAllEntries().forEach { suspend fun deleteOutdatedEntries(validEntries: List<String>) =
val file = File(it.path) keyCacheDao.getAllEntries().forEach {
if (file.isOutdated() || !file.exists()) { Timber.v("valid entries for cache from server: $validEntries")
deleteFileForEntry(it) val file = File(it.path)
keyCacheDao.deleteEntry(it) if (!validEntries.contains(it.id) || !file.exists()) {
Timber.w("${it.id} will be deleted from the cache")
deleteFileForEntry(it)
keyCacheDao.deleteEntry(it)
}
} }
}
private fun deleteFileForEntry(entry: KeyCacheEntity) = File(entry.path).delete() private fun deleteFileForEntry(entry: KeyCacheEntity) = File(entry.path).delete()
......
...@@ -91,9 +91,11 @@ object CachedKeyFileHolder { ...@@ -91,9 +91,11 @@ object CachedKeyFileHolder {
) )
} }
} else { } else {
val uuidListFromServer = serverDates
.map { getURLForDay(it).generateCacheKeyFromString() }
// queries will be executed after the "query plan" was set // queries will be executed after the "query plan" was set
val deferredQueries: MutableCollection<Deferred<Any>> = mutableListOf() val deferredQueries: MutableCollection<Deferred<Any>> = mutableListOf()
keyCache.deleteOutdatedEntries() keyCache.deleteOutdatedEntries(uuidListFromServer)
val missingDays = getMissingDaysFromDiff(serverDates) val missingDays = getMissingDaysFromDiff(serverDates)
if (missingDays.isNotEmpty()) { if (missingDays.isNotEmpty()) {
// we have a date difference // we have a date difference
......
...@@ -37,7 +37,7 @@ class CachedKeyFileHolderTest { ...@@ -37,7 +37,7 @@ class CachedKeyFileHolderTest {
every { CoronaWarnApplication.getAppContext() } returns context every { CoronaWarnApplication.getAppContext() } returns context
every { KeyCacheRepository.getDateRepository(any()) } returns keyCacheRepository every { KeyCacheRepository.getDateRepository(any()) } returns keyCacheRepository
mockkObject(CachedKeyFileHolder) mockkObject(CachedKeyFileHolder)
coEvery { keyCacheRepository.deleteOutdatedEntries() } just Runs coEvery { keyCacheRepository.deleteOutdatedEntries(any()) } just Runs
} }
/** /**
...@@ -60,7 +60,7 @@ class CachedKeyFileHolderTest { ...@@ -60,7 +60,7 @@ class CachedKeyFileHolderTest {
coVerifyOrder { coVerifyOrder {
CachedKeyFileHolder.asyncFetchFiles(date) CachedKeyFileHolder.asyncFetchFiles(date)
CachedKeyFileHolder["getDatesFromServer"]() CachedKeyFileHolder["getDatesFromServer"]()
keyCacheRepository.deleteOutdatedEntries() keyCacheRepository.deleteOutdatedEntries(any())
CachedKeyFileHolder["getMissingDaysFromDiff"](arrayListOf<String>()) CachedKeyFileHolder["getMissingDaysFromDiff"](arrayListOf<String>())
keyCacheRepository.getDates() keyCacheRepository.getDates()
keyCacheRepository.getFilesFromEntries() keyCacheRepository.getFilesFromEntries()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment