From ac9ca5949e2c8d1e343ac1334b25c8fb4c4c46c2 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sun, 21 Mar 2021 12:10:27 +0200 Subject: [PATCH] Fixed JSONStorage sometimes fetching the wrong thing --- .../plan/delivery/webserver/cache/JSONFileStorage.java | 8 ++++---- .../plan/delivery/webserver/cache/JSONStorageTest.java | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java index bf928e4b8..93339ce5a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java @@ -124,7 +124,7 @@ public class JSONFileStorage implements JSONStorage { if (stored == null) return Optional.empty(); for (File file : stored) { String fileName = file.getName(); - if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) { + if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier + '-')) { return Optional.ofNullable(readStoredJSON(file)); } } @@ -173,7 +173,7 @@ public class JSONFileStorage implements JSONStorage { for (File file : stored) { try { String fileName = file.getName(); - if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) { + if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier + '-')) { Matcher timestampMatch = timestampRegex.matcher(fileName); if (timestampMatch.find() && timestampComparator.test(timestampMatch, timestamp)) { return Optional.ofNullable(readStoredJSON(file)); @@ -203,7 +203,7 @@ public class JSONFileStorage implements JSONStorage { private boolean shouldDeleteFile(String identifier, long timestamp, File file) { try { String fileName = file.getName(); - if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) { + if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier + '-')) { Matcher timestampMatch = timestampRegex.matcher(fileName); if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) { return true; @@ -237,7 +237,7 @@ public class JSONFileStorage implements JSONStorage { boolean isOlder = timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp; if (isOlder) { for (String ignoredIdentifier : ignoredIdentifiers) { - if (fileName.startsWith(ignoredIdentifier)) return false; + if (fileName.startsWith(ignoredIdentifier + "-")) return false; } return true; } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/cache/JSONStorageTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/cache/JSONStorageTest.java index 23debea19..e61acccac 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/cache/JSONStorageTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/cache/JSONStorageTest.java @@ -163,4 +163,11 @@ class JSONStorageTest { JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp); assertFalse(UNDER_TEST.fetchJsonMadeBefore("Identifier", timestamp - TimeUnit.DAYS.toMillis(1L)).isPresent()); } + + @Test + void doesNotFetchWrongThing() { + long timestamp = System.currentTimeMillis(); + JSONStorage.StoredJSON stored = UNDER_TEST.storeJson(DataID.SESSIONS_OVERVIEW.name(), Collections.singletonList("data"), timestamp); + assertFalse(UNDER_TEST.fetchJsonMadeBefore(DataID.SESSIONS.name(), timestamp + TimeUnit.DAYS.toMillis(1L)).isPresent()); + } } \ No newline at end of file