From 61ade9adad4668494dfc32da7751e1b86251dc97 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 30 Nov 2020 13:15:15 +0800 Subject: Implementing incremental backup --- .../util/backup/BackupNameSuggestionProvider.java | 77 --------------------- .../kbackupfabric/util/backup/BackupType.java | 22 ++++-- .../util/backup/builder/BackupFileNameBuilder.java | 3 +- .../backup/formatter/BackupFileNameFormatter.java | 3 +- .../ObjectTreeBackupFileNameFormatter.java | 5 +- .../PrimitiveZipBackupFileNameFormatter.java | 5 +- .../suggestion/BackupMethodSuggestionProvider.java | 36 ++++++++++ .../suggestion/BackupNameSuggestionProvider.java | 78 ++++++++++++++++++++++ 8 files changed, 135 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup') diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java deleted file mode 100644 index 4639e99..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.keuin.kbackupfabric.util.backup; - -import com.mojang.brigadier.suggestion.SuggestionProvider; -import com.mojang.brigadier.suggestion.Suggestions; -import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import net.minecraft.server.command.ServerCommandSource; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.CompletableFuture; - -public class BackupNameSuggestionProvider { - - private static final List candidateCacheList = new ArrayList<>(); - private static final Object syncSetDirectory = new Object(); - private static final Object syncCache = new Object(); - private static final long CACHE_TTL = 8000; - private static String backupSaveDirectory; - private static long cacheUpdateTime = 0; - - public static void setBackupSaveDirectory(String backupSaveDirectory) { - synchronized (syncSetDirectory) { - BackupNameSuggestionProvider.backupSaveDirectory = backupSaveDirectory; - } - // Immediately perform an update - updateCandidateList(); - } - - public static void updateCandidateList() { - synchronized (syncCache) { - try { - File file = new File(backupSaveDirectory); - candidateCacheList.clear(); - File[] files = file.listFiles(); - if (files == null) - return; - for (File f : files) - candidateCacheList.add(BackupFilesystemUtil.getBackupName(f.getName())); - cacheUpdateTime = System.currentTimeMillis(); - } catch (NullPointerException ignored) { - } - } - } - -// private static void updateCandidateList(Collection stringCollection) { -// candidateList.clear(); -// candidateList.addAll(stringCollection); -// } - - public static SuggestionProvider getProvider() { - return (context, builder) -> getCompletableFuture(builder); - } - - private static CompletableFuture getCompletableFuture(SuggestionsBuilder builder) { - if (isCacheExpired()) - updateCandidateList(); - String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); - synchronized (syncCache) { - if (candidateCacheList.isEmpty()) { // If the list is empty then return no suggestions - return Suggestions.empty(); // No suggestions - } - - for (String string : candidateCacheList) { // Iterate through the supplied list - if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { - builder.suggest(string); // Add every single entry to suggestions list. - } - } - } - return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions - } - - private static boolean isCacheExpired() { - return System.currentTimeMillis() - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java index 95a32ae..d02ce77 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java @@ -4,18 +4,30 @@ package com.keuin.kbackupfabric.util.backup; * Representing the backup type. * Should only be used in BackupFileNameBuilder and BackupFileNameFormatter */ +@Deprecated public enum BackupType { - PRIMITIVE_ZIP_BACKUP("Primitive Zip Backup"), - OBJECT_TREE_BACKUP("Object Tree Backup"); + PRIMITIVE_ZIP_BACKUP("Primitive Zip Backup", "zip"), + OBJECT_TREE_BACKUP("Object Tree Backup", "incremental"); - private final String name; - BackupType(String name) { + private final String friendlyName; // e.g. Primitive Zip Backup + private final String name; // e.g. zip + + BackupType(String friendlyName, String name) { + this.friendlyName = friendlyName; this.name = name; } + /** + * Get name used in command. + * @return name (such as "zip", "incremental"). + */ + public String getName() { + return name; + } + @Override public String toString() { - return name; + return friendlyName; } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java index 16c598b..f57302c 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java @@ -1,6 +1,5 @@ package com.keuin.kbackupfabric.util.backup.builder; -import com.sun.istack.internal.NotNull; import java.time.LocalDateTime; @@ -20,6 +19,6 @@ public interface BackupFileNameBuilder { * @param backupName the custom name of this backup. Note that this should be a valid file name in current file system. * @return the backup file name string. */ - String build(@NotNull LocalDateTime time, @NotNull String backupName); + String build(LocalDateTime time, String backupName); } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java index eae3639..a437629 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java @@ -1,7 +1,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; import java.time.LocalDateTime; import java.util.regex.Matcher; @@ -9,7 +8,7 @@ import java.util.regex.Pattern; public interface BackupFileNameFormatter { - BackupFileName format(@NotNull String fileName); + BackupFileName format(String fileName); class BackupFileName { public final LocalDateTime time; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java index e5503a9..08805b2 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java @@ -2,8 +2,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; -import com.sun.istack.internal.Nullable; import org.spongepowered.asm.mixin.Overwrite; import java.time.LocalDateTime; @@ -19,13 +17,12 @@ public class ObjectTreeBackupFileNameFormatter implements BackupFileNameFormatte } @Override - public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) { + public BackupFileNameFormatter.BackupFileName format(String fileName) { LocalDateTime time = getTime(fileName); String name = getBackupName(fileName); return new BackupFileNameFormatter.BackupFileName(time,name); } - @Nullable private LocalDateTime getTime(String fileName) { Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); if (matcher.find()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java index 40450eb..2d50d17 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java @@ -2,8 +2,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; -import com.sun.istack.internal.Nullable; import java.time.LocalDateTime; import java.util.regex.Matcher; @@ -18,13 +16,12 @@ public class PrimitiveZipBackupFileNameFormatter implements BackupFileNameFormat } @Override - public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) { + public BackupFileNameFormatter.BackupFileName format(String fileName) { LocalDateTime time = getTime(fileName); String name = getBackupName(fileName); return new BackupFileNameFormatter.BackupFileName(time,name); } - @Nullable private LocalDateTime getTime(String fileName) { Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); if (matcher.find()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java new file mode 100644 index 0000000..320d9bf --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java @@ -0,0 +1,36 @@ +package com.keuin.kbackupfabric.util.backup.suggestion; + +import com.keuin.kbackupfabric.util.backup.BackupType; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.ServerCommandSource; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; + +public class BackupMethodSuggestionProvider { + + private static final List suggestions = Arrays.asList( + BackupType.OBJECT_TREE_BACKUP.getName(), + BackupType.PRIMITIVE_ZIP_BACKUP.getName() + ); // All backup methods + + public static SuggestionProvider getProvider() { + return (context, builder) -> getCompletableFuture(builder); + } + + private static CompletableFuture getCompletableFuture(SuggestionsBuilder builder) { + String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); + for (String string : suggestions) { // Iterate through the supplied list + if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { + builder.suggest(string); // Add every single entry to suggestions list. + } + } + return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java new file mode 100644 index 0000000..f6f4056 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java @@ -0,0 +1,78 @@ +package com.keuin.kbackupfabric.util.backup.suggestion; + +import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.ServerCommandSource; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; + +public class BackupNameSuggestionProvider { + + private static final List candidateCacheList = new ArrayList<>(); + private static final Object syncSetDirectory = new Object(); + private static final Object syncCache = new Object(); + private static final long CACHE_TTL = 8000; + private static String backupSaveDirectory; + private static long cacheUpdateTime = 0; + + public static void setBackupSaveDirectory(String backupSaveDirectory) { + synchronized (syncSetDirectory) { + BackupNameSuggestionProvider.backupSaveDirectory = backupSaveDirectory; + } + // Immediately perform an update + updateCandidateList(); + } + + public static void updateCandidateList() { + synchronized (syncCache) { + try { + File file = new File(backupSaveDirectory); + candidateCacheList.clear(); + File[] files = file.listFiles(); + if (files == null) + return; + for (File f : files) + candidateCacheList.add(BackupFilesystemUtil.getBackupName(f.getName())); + cacheUpdateTime = System.currentTimeMillis(); + } catch (NullPointerException ignored) { + } + } + } + +// private static void updateCandidateList(Collection stringCollection) { +// candidateList.clear(); +// candidateList.addAll(stringCollection); +// } + + public static SuggestionProvider getProvider() { + return (context, builder) -> getCompletableFuture(builder); + } + + private static CompletableFuture getCompletableFuture(SuggestionsBuilder builder) { + if (isCacheExpired()) + updateCandidateList(); + String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); + synchronized (syncCache) { + if (candidateCacheList.isEmpty()) { // If the list is empty then return no suggestions + return Suggestions.empty(); // No suggestions + } + + for (String string : candidateCacheList) { // Iterate through the supplied list + if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { + builder.suggest(string); // Add every single entry to suggestions list. + } + } + } + return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions + } + + private static boolean isCacheExpired() { + return System.currentTimeMillis() - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0; + } +} -- cgit v1.2.3