From a817715ad4f625e355b285d67ee323e8b2006ae4 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 25 Jan 2021 03:12:16 +0800 Subject: Code refactor --- .../java/com/keuin/kbackupfabric/KBCommands.java | 6 +++-- .../incremental/ObjectCollectionSerializer.java | 27 ++++++---------------- .../manager/IncrementalBackupStorageManager.java | 14 ++++------- .../serializer/IncBackupInfoSerializer.java | 22 ++++++++++++++++++ .../name/PrimitiveBackupFileNameEncoder.java | 5 ++++ .../backup/provider/AvailableBackupProvider.java | 8 ------- .../provider/IncrementalBackupInformation.java | 12 ---------- .../method/ConfiguredIncrementalBackupMethod.java | 1 - .../method/ConfiguredPrimitiveBackupMethod.java | 2 +- 9 files changed, 43 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/backup/provider/AvailableBackupProvider.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/backup/provider/IncrementalBackupInformation.java (limited to 'src/main') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 01231be..fe5ea7f 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -87,6 +87,8 @@ public final class KBCommands { * @return stat code. */ public static int list(CommandContext context) { + // lazy: it just works as expected. Don't try to refactor, it's a waste of time. Just improve display and + // that's enough. // TODO: Show real name and size and etc info for incremental backup // TODO: Show concrete info from metadata for `.zip` backup MinecraftServer server = context.getSource().getMinecraftServer(); @@ -288,7 +290,7 @@ public final class KBCommands { // configure backup method MinecraftServer server = context.getSource().getMinecraftServer(); ConfiguredBackupMethod method = !incremental ? new ConfiguredPrimitiveBackupMethod( - new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), + PrimitiveBackupFileNameEncoder.INSTANCE.encode(customBackupName, LocalDateTime.now()), getLevelPath(server), getBackupSaveDirectory(server).getCanonicalPath() ) : new ConfiguredIncrementalBackupMethod( @@ -396,7 +398,7 @@ public final class KBCommands { private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { return String.format( "(ZIP) %s , size: %s", - new PrimitiveBackupFileNameEncoder().decode(backupFileName), + PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName), getFriendlyFileSizeString(backupFileSizeBytes) ); } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java index 3be1bfe..42e7c0c 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java @@ -7,12 +7,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; +import java.util.*; /** * Serialize and deserialize ObjectCollection from/to the disk file. @@ -53,19 +48,11 @@ public class ObjectCollectionSerializer { } public static Iterable fromDirectory(File directory) throws IOException { - if (!directory.isDirectory()) { - throw new IllegalArgumentException("Given directory is invalid."); - } - List pathList = new ArrayList<>(); - Files.walk(directory.toPath(), 1).filter(p -> { - File f = p.toFile(); - return f.isFile() && f.getName().endsWith(".kbi"); - }).forEach(pathList::add); - List objectList = new ArrayList<>(); - for (Path path : pathList) { - SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile()); - objectList.add(info.getObjectCollection()); - } - return Collections.unmodifiableCollection(objectList); + List list = new ArrayList<>(); + IncBackupInfoSerializer.fromDirectory(directory) + .forEach(o -> Optional.ofNullable(o) + .map(SavedIncrementalBackup::getObjectCollection) + .ifPresent(list::add)); + return Collections.unmodifiableCollection(list); } } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index 0a99bf0..ff75d8c 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -3,7 +3,6 @@ package com.keuin.kbackupfabric.backup.incremental.manager; import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator; import com.keuin.kbackupfabric.backup.incremental.ObjectElement; -import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; import org.jetbrains.annotations.Nullable; @@ -22,9 +21,6 @@ import static org.apache.commons.io.FileUtils.forceDelete; public class IncrementalBackupStorageManager { private final Path backupStorageBase; - private final Map map = new HashMap<>(); - private boolean loaded = false; - private final Logger LOGGER = Logger.getLogger(IncrementalBackupStorageManager.class.getName()); public IncrementalBackupStorageManager(Path backupStorageBase) { @@ -40,7 +36,6 @@ public class IncrementalBackupStorageManager { */ public @Nullable IncCopyResult addObjectCollection(ObjectCollection2 collection, File collectionBasePath) throws IOException { - // TODO: add failure detection if (!backupStorageBase.toFile().isDirectory()) { if (!backupStorageBase.toFile().mkdirs()) throw new IOException("Backup storage base directory does not exist, and failed to create it."); @@ -79,9 +74,8 @@ public class IncrementalBackupStorageManager { * * @param collection the collection containing files to be deleted. * @return files deleted - * @throws IOException I/O error. */ - public int deleteObjectCollection(ObjectCollection2 collection) throws IOException { + public int deleteObjectCollection(ObjectCollection2 collection) { return deleteObjectCollection(collection, Collections.emptySet()); } @@ -94,6 +88,7 @@ public class IncrementalBackupStorageManager { */ public int deleteObjectCollection(ObjectCollection2 collection, Iterable otherExistingCollections) { + // TODO: test this Iterator iter = new ObjectCollectionIterator(collection); Set unusedElementSet = new HashSet<>(); iter.forEachRemaining(unusedElementSet::add); @@ -146,7 +141,7 @@ public class IncrementalBackupStorageManager { File copyTarget = new File(collectionBasePath.getAbsolutePath(), entry.getKey()); if (!baseContainsObject(entry.getValue())) { - throw new IOException(String.format("File %s does not exist in the base.", copySource.getName())); + throw new IOException(String.format("File %s is missing in the backup storage. Cannot restore.", copySource.getName())); } if (copyTarget.exists()) { boolean successDeleting = false; @@ -188,8 +183,7 @@ public class IncrementalBackupStorageManager { * @return true or false. */ private boolean baseContainsObject(ObjectElement objectElement) { - // This may be extended to use more variants of hash functions and combinations of other attributes (such as file size) - //TODO: optimize this by using in-memory data structure + // This can be extended to use more variants of hash functions and combinations of other attributes (such as file size) return (new File(backupStorageBase.toFile(), objectElement.getIdentifier().getIdentification())).exists(); } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java index e79c02f..f17217a 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java @@ -7,6 +7,11 @@ import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Objects; public class IncBackupInfoSerializer { @@ -66,4 +71,21 @@ public class IncBackupInfoSerializer { } } } + + public static Iterable fromDirectory(File directory) throws IOException { + if (!directory.isDirectory()) { + throw new IllegalArgumentException("Given directory is invalid."); + } + List pathList = new ArrayList<>(); + Files.walk(directory.toPath(), 1).filter(p -> { + File f = p.toFile(); + return f.isFile() && f.getName().endsWith(".kbi"); + }).forEach(pathList::add); + List objectList = new ArrayList<>(); + for (Path path : pathList) { + SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile()); + objectList.add(info); + } + return Collections.unmodifiableCollection(objectList); + } } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java index 1deec8f..9e3a97e 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java @@ -9,6 +9,11 @@ import java.util.regex.Pattern; public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { private static final String backupFileNamePrefix = "kbackup"; + public static final PrimitiveBackupFileNameEncoder INSTANCE = new PrimitiveBackupFileNameEncoder(); + + private PrimitiveBackupFileNameEncoder() { + } + @Override public String encode(String customName, LocalDateTime time) { if (!isValidCustomName(customName)) diff --git a/src/main/java/com/keuin/kbackupfabric/backup/provider/AvailableBackupProvider.java b/src/main/java/com/keuin/kbackupfabric/backup/provider/AvailableBackupProvider.java deleted file mode 100644 index b6082ba..0000000 --- a/src/main/java/com/keuin/kbackupfabric/backup/provider/AvailableBackupProvider.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.keuin.kbackupfabric.backup.provider; - -/** - * List all backup in disk. Provide their basic information as soon as possible. - */ -public class AvailableBackupProvider { - // TODO: remove obsolete implementation in command user interface. Use this instead. -} diff --git a/src/main/java/com/keuin/kbackupfabric/backup/provider/IncrementalBackupInformation.java b/src/main/java/com/keuin/kbackupfabric/backup/provider/IncrementalBackupInformation.java deleted file mode 100644 index bd85ba2..0000000 --- a/src/main/java/com/keuin/kbackupfabric/backup/provider/IncrementalBackupInformation.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.keuin.kbackupfabric.backup.provider; - -import com.keuin.kbackupfabric.backup.name.BackupFileNameEncoder; - -import java.time.LocalDateTime; - -public class IncrementalBackupInformation extends BackupFileNameEncoder.BackupBasicInformation { - // TODO: show total size for incremental backups - public IncrementalBackupInformation(String customName, LocalDateTime time) { - super(customName, time); - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java index 5cd58aa..5fe532a 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -57,7 +57,6 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod try { // construct incremental backup index PrintUtil.info("Hashing files..."); - // TODO collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory(), hashFactoryThreads, 16) .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata"))); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java index 9f0632d..0047c44 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java @@ -42,7 +42,7 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { PrimitiveBackupFeedback feedback; try { - String customBackupName = new PrimitiveBackupFileNameEncoder().decode(backupFileName).customName; + String customBackupName = PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName).customName; BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); PrintUtil.info("Compressing level ..."); -- cgit v1.2.3