diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GrepLogCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GrepLogCommand.java index 23df70d1..c9513362 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GrepLogCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GrepLogCommand.java @@ -43,18 +43,21 @@ public class GrepLogCommand implements Command { public Component execute(CommandContext context, String[] _args, String[] fullArgs) { final Bot bot = context.bot(); - if (bot.grepLog().thread() != null) return Component.text("Another query is already running").color(NamedTextColor.RED); - String[] args = _args; boolean ignoreCase = false; boolean regex = false; if (_args[0].equals("stop")) { + if (bot.grepLog().thread() == null) return Component.text("No query is running").color(NamedTextColor.RED); + bot.grepLog().thread().interrupt(); + context.sendOutput(Component.text("Log query stopped")); return Component.text("success"); } + if (bot.grepLog().thread() != null) return Component.text("Another query is already running").color(NamedTextColor.RED); + // this is a mess if (_args[0].equals("-ignorecase")) { ignoreCase = true; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/GrepLogPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/GrepLogPlugin.java index 2504158c..e78858bd 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/GrepLogPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/GrepLogPlugin.java @@ -9,8 +9,10 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; @@ -52,7 +54,11 @@ public class GrepLogPlugin { pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE); } else { - pattern = Pattern.compile(query); + try { + pattern = Pattern.compile(query); + } catch (Exception e) { + bot.chat().tellraw(Component.text(e.toString()).color(NamedTextColor.RED)); + } } } else { if (ignoreCase) { @@ -74,34 +80,52 @@ public class GrepLogPlugin { Arrays.sort(fileList, Comparator.comparing(File::getName)); // VERY IMPORTANT - for (File file : fileList) { - final String fileName = file.getName(); - if (fileName.matches(".*\\.txt\\.gz")) { - try ( - FileInputStream fin = new FileInputStream(file); - GZIPInputStream gzin = new GZIPInputStream(fin, 65536); - BufferedReader br = new BufferedReader(new InputStreamReader(gzin, StandardCharsets.UTF_8)) - ) { - br.readLine(); - readFile(br); - } catch (Exception ignored) { - } // TODO: Handle exception - } else { - try ( - FileInputStream fin = new FileInputStream(file); - BufferedReader br = new BufferedReader(new InputStreamReader(fin, StandardCharsets.UTF_8)) - ) { - br.readLine(); - readFile(br); - } catch (Exception ignored) { - } // TODO: Handle exception - } + // split the list into smaller chunks + int chunkSize = fileList.length / Runtime.getRuntime().availableProcessors(); + final List fileChunks = new ArrayList<>(); + for (int i = 0; i < fileList.length; i += chunkSize) { + int end = Math.min(i + chunkSize, fileList.length); + fileChunks.add(Arrays.copyOfRange(fileList, i, end)); + } - if (Thread.interrupted()) { - bot.chat().tellraw(Component.text("Log query stopped")); - } else if (queryStopped) { - break; - } + final List threads = new ArrayList<>(); + for (File[] chunk : fileChunks) { + final Thread thread = new Thread(() -> { + for (File file : chunk) { + final String fileName = file.getName(); + if (fileName.matches(".*\\.txt\\.gz")) { + try ( + FileInputStream fin = new FileInputStream(file); + GZIPInputStream gzin = new GZIPInputStream(fin, 65536); + BufferedReader br = new BufferedReader(new InputStreamReader(gzin, StandardCharsets.UTF_8)) + ) { + br.readLine(); + readFile(br); + } catch (Exception ignored) { + } // TODO: Handle exception + } else { + try ( + FileInputStream fin = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(fin, StandardCharsets.UTF_8)) + ) { + br.readLine(); + readFile(br); + } catch (Exception ignored) { + } // TODO: Handle exception + } + + if (queryStopped) break; + } + }); + thread.start(); + threads.add(thread); + } + + // wait for all of the threads to finish + for (Thread thread : threads) { + try { + thread.join(); + } catch (InterruptedException ignored) {} } finish(); @@ -156,6 +180,8 @@ public class GrepLogPlugin { } private void finish () { + thread = null; + if (results.toString().split("\n").length < 100) { // ig lazy fix for removing \n lol bot.chat().tellraw( Component.empty()