diff options
author | Keuin <[email protected]> | 2021-01-22 20:08:29 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-22 20:08:29 +0800 |
commit | 270dd70a0fa80fbea0f7985a551c1b453e3f9903 (patch) | |
tree | 2ea04b72edeb0c1eeb5b3f9e012448a33e8d3948 /src/main/java/com/keuin | |
parent | 1c23fc14be8a0ac9542f1412448c4d896756ba01 (diff) |
Fix a potential bug caused by the wrong implementation of compareTo method in ObjectElement
Diffstat (limited to 'src/main/java/com/keuin')
3 files changed, 11 insertions, 14 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java index 56fc052..17eddaf 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java @@ -9,8 +9,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ConcurrentSkipListSet; import java.util.function.Consumer; import java.util.function.Supplier; @@ -25,20 +25,20 @@ public class ObjectCollectionFactory<T extends ObjectIdentifier> { private final FileIdentifierProvider<T> identifierFactory; private final int threads; private Exception exception = null; // fail in async + private final int minParallelProcessFileCountThreshold; - public ObjectCollectionFactory(FileIdentifierProvider<T> identifierFactory, int threads) { + public ObjectCollectionFactory(FileIdentifierProvider<T> identifierFactory, int threads, int minParallelProcessFileCountThreshold) { this.identifierFactory = identifierFactory; this.threads = threads; + this.minParallelProcessFileCountThreshold = minParallelProcessFileCountThreshold; if (threads <= 0) throw new IllegalArgumentException("thread count must be positive."); + if (minParallelProcessFileCountThreshold < 0) + throw new IllegalArgumentException("minParallelProcessFileCountThreshold must not be negative."); } public ObjectCollection fromDirectory(File directory, Set<String> ignoredFiles) throws IOException { - final int minParallelProcessFileCountThreshold = 0; - - final Set<ObjectElement> subFiles = new ConcurrentSkipListSet<>(); - final Map<String, ObjectCollection> subCollections = new HashMap<>(); if (!Objects.requireNonNull(directory).isDirectory()) @@ -61,6 +61,8 @@ public class ObjectCollectionFactory<T extends ObjectIdentifier> { } } + final Set<ObjectElement> subFiles = ConcurrentHashMap.newKeySet(files.size()); + // deal with all direct sub files if (threads == 1 || files.size() < minParallelProcessFileCountThreshold) { for (File file : files) { @@ -90,7 +92,7 @@ public class ObjectCollectionFactory<T extends ObjectIdentifier> { } } - return new ObjectCollection(directory.getName(), new HashSet<>(subFiles), subCollections); + return new ObjectCollection(directory.getName(), subFiles, subCollections); } public ObjectCollection fromDirectory(File directory) throws IOException { diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectElement.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectElement.java index ee677fa..ff998f0 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectElement.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectElement.java @@ -1,7 +1,6 @@ package com.keuin.kbackupfabric.backup.incremental; import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier; -import org.jetbrains.annotations.NotNull; import java.io.Serializable; import java.util.Objects; @@ -10,7 +9,7 @@ import java.util.Objects; * Representing a file in a ObjectCollection. * Immutable. */ -public class ObjectElement implements Serializable, Comparable<ObjectElement> { +public class ObjectElement implements Serializable { private final String name; private final ObjectIdentifier identifier; @@ -59,8 +58,4 @@ public class ObjectElement implements Serializable, Comparable<ObjectElement> { '}'; } - @Override - public int compareTo(@NotNull ObjectElement objectElement) { - return name.compareTo(objectElement.name); - } } 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 ab0c114..569c4c6 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 @@ -46,7 +46,7 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod // construct incremental backup index PrintUtil.info("Hashing files..."); - ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory(), hashFactoryThreads) + ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory(), hashFactoryThreads, 16) .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata"))); // update storage |