From 270dd70a0fa80fbea0f7985a551c1b453e3f9903 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 22 Jan 2021 20:08:29 +0800 Subject: Fix a potential bug caused by the wrong implementation of compareTo method in ObjectElement --- .../backup/incremental/ObjectCollectionFactory.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java') 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 { private final FileIdentifierProvider identifierFactory; private final int threads; private Exception exception = null; // fail in async + private final int minParallelProcessFileCountThreshold; - public ObjectCollectionFactory(FileIdentifierProvider identifierFactory, int threads) { + public ObjectCollectionFactory(FileIdentifierProvider 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 ignoredFiles) throws IOException { - final int minParallelProcessFileCountThreshold = 0; - - final Set subFiles = new ConcurrentSkipListSet<>(); - final Map subCollections = new HashMap<>(); if (!Objects.requireNonNull(directory).isDirectory()) @@ -61,6 +61,8 @@ public class ObjectCollectionFactory { } } + final Set 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 { } } - return new ObjectCollection(directory.getName(), new HashSet<>(subFiles), subCollections); + return new ObjectCollection(directory.getName(), subFiles, subCollections); } public ObjectCollection fromDirectory(File directory) throws IOException { -- cgit v1.2.3