mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-27 18:41:40 +01:00
Refactored file deletion checks for JSONFileStorage
This commit is contained in:
parent
5c01cde102
commit
c0a210956d
@ -198,47 +198,61 @@ public class JSONFileStorage implements JSONStorage {
|
|||||||
|
|
||||||
List<File> toDelete = new ArrayList<>();
|
List<File> toDelete = new ArrayList<>();
|
||||||
for (File file : stored) {
|
for (File file : stored) {
|
||||||
try {
|
if (shouldDeleteFile(identifier, timestamp, toDelete, file)) {
|
||||||
String fileName = file.getName();
|
toDelete.add(file);
|
||||||
if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) {
|
|
||||||
Matcher timestampMatch = timestampRegex.matcher(fileName);
|
|
||||||
if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) {
|
|
||||||
toDelete.add(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
// Ignore this file, malformed timestamp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deleteFiles(toDelete);
|
deleteFiles(toDelete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean shouldDeleteFile(String identifier, long timestamp, List<File> toDelete, File file) {
|
||||||
|
try {
|
||||||
|
String fileName = file.getName();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Ignore this file, malformed timestamp
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void invalidateOlderButIgnore(long timestamp, String... ignoredIdentifiers) {
|
private void invalidateOlderButIgnore(long timestamp, String... ignoredIdentifiers) {
|
||||||
File[] stored = jsonDirectory.toFile().listFiles();
|
File[] stored = jsonDirectory.toFile().listFiles();
|
||||||
if (stored == null) return;
|
if (stored == null) return;
|
||||||
|
|
||||||
List<File> toDelete = new ArrayList<>();
|
List<File> toDelete = new ArrayList<>();
|
||||||
outer:
|
|
||||||
for (File file : stored) {
|
for (File file : stored) {
|
||||||
try {
|
if (shouldDeleteFile(timestamp, file, ignoredIdentifiers)) {
|
||||||
String fileName = file.getName();
|
toDelete.add(file);
|
||||||
if (fileName.endsWith(JSON_FILE_EXTENSION)) {
|
|
||||||
Matcher timestampMatch = timestampRegex.matcher(fileName);
|
|
||||||
if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) {
|
|
||||||
for (String ignoredIdentifier : ignoredIdentifiers) {
|
|
||||||
if (fileName.startsWith(ignoredIdentifier)) continue outer;
|
|
||||||
}
|
|
||||||
toDelete.add(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
// Ignore this file, malformed timestamp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteFiles(toDelete);
|
deleteFiles(toDelete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean shouldDeleteFile(long timestamp, File file, String[] ignoredIdentifiers) {
|
||||||
|
try {
|
||||||
|
String fileName = file.getName();
|
||||||
|
if (fileName.endsWith(JSON_FILE_EXTENSION)) {
|
||||||
|
Matcher timestampMatch = timestampRegex.matcher(fileName);
|
||||||
|
boolean isOlder = timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp;
|
||||||
|
if (isOlder) {
|
||||||
|
for (String ignoredIdentifier : ignoredIdentifiers) {
|
||||||
|
if (fileName.startsWith(ignoredIdentifier)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Ignore this file, malformed timestamp
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void deleteFiles(List<File> toDelete) {
|
private void deleteFiles(List<File> toDelete) {
|
||||||
for (File fileToDelete : toDelete) {
|
for (File fileToDelete : toDelete) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user