diff options
Diffstat (limited to 'src')
10 files changed, 47 insertions, 58 deletions
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<ServerCommandSource> 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<ObjectCollection2> fromDirectory(File directory) throws IOException { - if (!directory.isDirectory()) { - throw new IllegalArgumentException("Given directory is invalid."); - } - List<Path> 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<ObjectCollection2> objectList = new ArrayList<>(); - for (Path path : pathList) { - SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile()); - objectList.add(info.getObjectCollection()); - } - return Collections.unmodifiableCollection(objectList); + List<ObjectCollection2> 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<ObjectIdentifier, File> 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<ObjectCollection2> otherExistingCollections) { + // TODO: test this Iterator<ObjectElement> iter = new ObjectCollectionIterator(collection); Set<ObjectElement> 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<SavedIncrementalBackup> fromDirectory(File directory) throws IOException { + if (!directory.isDirectory()) { + throw new IllegalArgumentException("Given directory is invalid."); + } + List<Path> 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<SavedIncrementalBackup> 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 ..."); diff --git a/src/test/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoderTest.java b/src/test/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoderTest.java index bb5f64b..2eae446 100644 --- a/src/test/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoderTest.java +++ b/src/test/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoderTest.java @@ -13,7 +13,7 @@ public class PrimitiveBackupFileNameEncoderTest { public void testConsistency() { LocalDateTime time = LocalDateTime.ofEpochSecond(System.currentTimeMillis() / 1000, 0, ZoneOffset.UTC); String name = "Test Na_me"; - PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + PrimitiveBackupFileNameEncoder encoder = PrimitiveBackupFileNameEncoder.INSTANCE; BackupFileNameEncoder.BackupBasicInformation information = encoder.decode(encoder.encode(name, time)); assertEquals(time, information.time); assertEquals(name, information.customName); @@ -23,7 +23,7 @@ public class PrimitiveBackupFileNameEncoderTest { public void testEncode() { LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); String customName = "name"; - PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + PrimitiveBackupFileNameEncoder encoder = PrimitiveBackupFileNameEncoder.INSTANCE; assertEquals("kbackup-0001-01-01_01-01-01_name.zip", encoder.encode(customName, time)); } @@ -31,7 +31,7 @@ public class PrimitiveBackupFileNameEncoderTest { public void testDecode() { LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); String customName = "name"; - PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + PrimitiveBackupFileNameEncoder encoder = PrimitiveBackupFileNameEncoder.INSTANCE; BackupFileNameEncoder.BackupBasicInformation information = encoder.decode("kbackup-0001-01-01_01-01-01_name.zip"); assertEquals(time, information.time); assertEquals(customName, information.customName); @@ -39,7 +39,7 @@ public class PrimitiveBackupFileNameEncoderTest { @Test public void isValid() { - PrimitiveBackupFileNameEncoder encoder = new PrimitiveBackupFileNameEncoder(); + PrimitiveBackupFileNameEncoder encoder = PrimitiveBackupFileNameEncoder.INSTANCE; assertTrue(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_name.zip")); assertTrue(encoder.isValidFileName("kbackup-0001-01-01_01-01-01_0001-01-01_01-01-01_name.zip")); assertFalse(encoder.isValidFileName("kbackup-0001-01-01_01-01-01kbackup-0001-01-01_01-01-01_name.zip")); |