-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to use multiple threads when using the cli
Signed-off-by: Christopher White <[email protected]>
- Loading branch information
1 parent
841df40
commit 6f1c10f
Showing
2 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 84 additions & 1 deletion
85
src/main/java/tc/oc/occ/autopruner/ThreadPoolAutoPruner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,85 @@ | ||
package tc.oc.occ.autopruner;public class ThreadPoolAutoPruner { | ||
package tc.oc.occ.autopruner; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Logger; | ||
|
||
public class ThreadPoolAutoPruner { | ||
private final ExecutorService threadPoolExecutor; | ||
private final Logger logger; | ||
|
||
public ThreadPoolAutoPruner(int threadCount) { | ||
this.threadPoolExecutor = Executors.newFixedThreadPool(threadCount); | ||
System.setProperty("java.util.logging.SimpleFormatter.format", | ||
"%1$tF %1$tT %4$s %2$s %5$s%6$s%n"); | ||
logger = Logger.getLogger("AutoPruner"); | ||
} | ||
|
||
/** | ||
* Recursively prune directories using consumer to log | ||
* | ||
* @param file | ||
* @param depth | ||
* @return bytes removed | ||
*/ | ||
public long recursivelyProcessFiles(File file, long depth) throws ExecutionException, InterruptedException { | ||
long sizeDeleted = recursivelyProcessFiles(file, depth, logger::info, logger::warning); | ||
logger.info("Deleted " + AutoPruner.readableFileSize(sizeDeleted) + " from: " + file.getAbsolutePath()); | ||
return sizeDeleted; | ||
} | ||
|
||
/** | ||
* Recursively prune directories using consumer to log | ||
* | ||
* @param file | ||
* @param depth | ||
* @param logging | ||
* @return bytes removed | ||
*/ | ||
public long recursivelyProcessFiles(File file, long depth, Consumer<String> logging, Consumer<String> warnLogging) throws ExecutionException, InterruptedException { | ||
List<Future<Long>> futures = recursivelyProcessFilesInternal(file, depth, logging, warnLogging); | ||
|
||
long sizeDeleted = 0; | ||
|
||
for (Future<Long> future : futures) { | ||
sizeDeleted += future.get(); | ||
} | ||
|
||
return sizeDeleted; | ||
} | ||
|
||
private List<Future<Long>> recursivelyProcessFilesInternal( | ||
File file, | ||
long depth, | ||
Consumer<String> infoLogging, | ||
Consumer<String> warnLogging) { | ||
if (depth > 30) { | ||
return Collections.emptyList(); | ||
} | ||
List<Future<Long>> futures = new ArrayList<>(); | ||
File[] files = file.listFiles(); | ||
if (files != null) { | ||
for (File childFile : files) { | ||
if (childFile.isDirectory()) { | ||
futures.addAll(recursivelyProcessFilesInternal(childFile, depth, infoLogging, warnLogging)); | ||
} else if (childFile.isFile() && childFile.getName().endsWith(".mca")) { | ||
Callable<Long> callable = () -> AutoPruner.pruneMCAFile(childFile.getAbsolutePath(), infoLogging, warnLogging); | ||
futures.add(threadPoolExecutor.submit(callable)); | ||
} | ||
} | ||
} | ||
return futures; | ||
} | ||
|
||
public void close() { | ||
threadPoolExecutor.shutdownNow(); | ||
} | ||
} |