From e02ba2f44f68daad52239dd0a08ceb2528c29a51 Mon Sep 17 00:00:00 2001 From: Kamil Bobrowski Date: Sun, 14 Jun 2020 16:55:24 +0600 Subject: [PATCH 1/4] copied implementation --- .../coronawarnapp/util/CachedKeyFileHolder.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt index be5b2edf70b..6791f0c6b35 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt @@ -28,6 +28,7 @@ import de.rki.coronawarnapp.storage.LocalData import de.rki.coronawarnapp.storage.keycache.KeyCacheEntity import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository.DateEntryType.DAY +import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository.DateEntryType.HOUR import de.rki.coronawarnapp.util.CachedKeyFileHolder.asyncFetchFiles import de.rki.coronawarnapp.util.TimeAndDateExtensions.toServerFormat import kotlinx.coroutines.Deferred @@ -103,6 +104,14 @@ object CachedKeyFileHolder { .map { url -> async { url.createDayEntryForUrl() } } ) } + val missingHours = getMissingHoursFromDiff(currentDate) + if (missingHours.isNotEmpty()) { + deferredQueries.addAll( + missingHours + .map { getURLForHour(currentDate.toServerFormat(), it) } + .map { url -> async { url.createHourEntryForUrl() }} + ) + } // execute the query plan try { deferredQueries.awaitAll() @@ -130,6 +139,18 @@ object CachedKeyFileHolder { .also { Timber.d("${it.size} missing days") } } + /** + * Calculates the missing hours based on current missing entries in the cache + */ + private suspend fun getMissingHoursFromDiff(day: Date): List { + val cacheEntries = keyCache.getHours() + return getHoursFromServer(day) + .also { Timber.v(TAG, "${it.size} hours from server") } + .filter { it.hourEntryCacheMiss(cacheEntries, day) } + .toList() + .also { Timber.d(TAG, "${it.size} missing hours") } + } + /** * TODO remove before Release */ @@ -155,6 +176,16 @@ object CachedKeyFileHolder { .map { date -> date.id } .contains(getURLForDay(this).generateCacheKeyFromString()) + /** + * Determines whether a given String has an existing hour cache entry under a unique name + * given from the URL that is based on this String + * + * @param cache the given cache entries + */ + private fun String.hourEntryCacheMiss(cache: List, day: Date) = !cache + .map { hour -> hour.id } + .contains(getURLForHour(day.toServerFormat(), this).generateCacheKeyFromString()) + /** * Creates a date entry in the Key Cache for a given String with a unique Key Name derived from the URL * and the URI of the downloaded File for that given key @@ -165,6 +196,16 @@ object CachedKeyFileHolder { DAY ) + /** + * Creates an hour entry in the Key Cache for a given String with a unique Key Name derived from the URL + * and the URI of the downloaded File for that given key + */ + private suspend fun String.createHourEntryForUrl() = keyCache.createEntry( + this.generateCacheKeyFromString(), + WebRequestBuilder.getInstance().asyncGetKeyFilesFromServer(this).toURI(), + HOUR + ) + /** * Generates a unique key name (UUIDv3) for the cache entry based out of a string (e.g. an url) */ From f407efda3fa610c0fa8bd605812235a963528ca4 Mon Sep 17 00:00:00 2001 From: Kamil Bobrowski Date: Sun, 14 Jun 2020 18:20:26 +0600 Subject: [PATCH 2/4] updated unit test for CachedKeyFileHolder to test for fetching hours --- .../java/de/rki/coronawarnapp/util/CachedKeyFileHolderTest.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/util/CachedKeyFileHolderTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/util/CachedKeyFileHolderTest.kt index d796b4c7e66..f2e6f61a757 100644 --- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/util/CachedKeyFileHolderTest.kt +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/util/CachedKeyFileHolderTest.kt @@ -48,10 +48,12 @@ class CachedKeyFileHolderTest { val date = Date() coEvery { keyCacheRepository.getDates() } returns listOf() + coEvery { keyCacheRepository.getHours() } returns listOf() coEvery { keyCacheRepository.getFilesFromEntries() } returns listOf() every { CachedKeyFileHolder["isLast3HourFetchEnabled"]() } returns false every { CachedKeyFileHolder["checkForFreeSpace"]() } returns Unit every { CachedKeyFileHolder["getDatesFromServer"]() } returns arrayListOf() + every { CachedKeyFileHolder["getHoursFromServer"](any()) } returns arrayListOf() runBlocking { @@ -63,6 +65,8 @@ class CachedKeyFileHolderTest { keyCacheRepository.deleteOutdatedEntries() CachedKeyFileHolder["getMissingDaysFromDiff"](arrayListOf()) keyCacheRepository.getDates() + CachedKeyFileHolder["getMissingHoursFromDiff"](any()) + keyCacheRepository.getHours() keyCacheRepository.getFilesFromEntries() } } From 938bce249cdd4f3296c61845cd6445d3fe5d5733 Mon Sep 17 00:00:00 2001 From: Kamil Bobrowski Date: Sun, 14 Jun 2020 18:29:54 +0600 Subject: [PATCH 3/4] fixed style, added comment --- .../java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt index 6791f0c6b35..ed74c472465 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt @@ -104,12 +104,14 @@ object CachedKeyFileHolder { .map { url -> async { url.createDayEntryForUrl() } } ) } + // there is no check if the date is available, since we want to get + // Diagnosis Keys for a day which is not yet complete val missingHours = getMissingHoursFromDiff(currentDate) if (missingHours.isNotEmpty()) { deferredQueries.addAll( missingHours .map { getURLForHour(currentDate.toServerFormat(), it) } - .map { url -> async { url.createHourEntryForUrl() }} + .map { url -> async { url.createHourEntryForUrl() } } ) } // execute the query plan From fd3c76326bfa7ef2ea7547286c2a3a52941f9a57 Mon Sep 17 00:00:00 2001 From: Kamil Bobrowski Date: Sun, 14 Jun 2020 18:46:50 +0600 Subject: [PATCH 4/4] updated missing params in doc --- .../main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt index ed74c472465..7e0d15dd0f1 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/CachedKeyFileHolder.kt @@ -143,6 +143,8 @@ object CachedKeyFileHolder { /** * Calculates the missing hours based on current missing entries in the cache + * + * @param day current day */ private suspend fun getMissingHoursFromDiff(day: Date): List { val cacheEntries = keyCache.getHours() @@ -183,6 +185,7 @@ object CachedKeyFileHolder { * given from the URL that is based on this String * * @param cache the given cache entries + * @param day current day */ private fun String.hourEntryCacheMiss(cache: List, day: Date) = !cache .map { hour -> hour.id }