diff options
author | Keuin <[email protected]> | 2021-01-25 03:12:16 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-25 03:12:16 +0800 |
commit | a817715ad4f625e355b285d67ee323e8b2006ae4 (patch) | |
tree | 5f9ffa5b18152b7efc056e5b01f9126505b69179 /src/main/java/com/keuin/kbackupfabric/backup/incremental | |
parent | 93494e7e04e05beb4f07cfabdd14c674b97a12e5 (diff) |
Code refactor
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental')
3 files changed, 33 insertions, 30 deletions
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); + } } |