Skip to content

Commit

Permalink
Add option to use multiple threads when using the cli
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 committed Oct 21, 2023
1 parent 841df40 commit 6f1c10f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/main/java/tc/oc/occ/autopruner/AutoPrunerCLIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import org.apache.commons.cli.ParseException;

import java.io.File;
import java.util.concurrent.ExecutionException;

public class AutoPrunerCLIMain {
public static void main(String[] args) {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CommandLine cmd = processOptions(args);
if (cmd == null) return;

Expand All @@ -20,7 +21,16 @@ public static void main(String[] args) {
AutoPruner.pruneMCAFileLogger(filePath);
} else if (cmd.hasOption("directory")) {
String directoryPath = cmd.getOptionValue("directory");
AutoPruner.recursivelyProcessFiles(new File(directoryPath), 0);

if (cmd.hasOption("threads")) {
int threads = Integer.parseInt(cmd.getOptionValue("threads"));

ThreadPoolAutoPruner threadPoolAutoPruner = new ThreadPoolAutoPruner(threads);
threadPoolAutoPruner.recursivelyProcessFiles(new File(directoryPath), 0);
threadPoolAutoPruner.close();
} else {
AutoPruner.recursivelyProcessFiles(new File(directoryPath), 0);
}
} else {
new AutoPrunerGui().buildAndRunGui();
}
Expand All @@ -45,6 +55,14 @@ private static CommandLine processOptions(String[] args) {
directoryOption.setRequired(false);
options.addOption(directoryOption);

Option threadOption = new Option(
"t",
"threads",
true,
"Number of threads to use");
threadOption.setRequired(false);
options.addOption(threadOption);

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
try {
Expand Down
85 changes: 84 additions & 1 deletion src/main/java/tc/oc/occ/autopruner/ThreadPoolAutoPruner.java
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();
}
}

0 comments on commit 6f1c10f

Please sign in to comment.