mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Implement multi threading to improve throughput
This commit is contained in:
@@ -5,41 +5,57 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigProcessor;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl.MkvFileCollector;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Log4j2
|
||||
public class AttributeUpdaterKernel {
|
||||
MkvFileCollector collector = new MkvFileCollector();
|
||||
int filesChangedAmount = 0;
|
||||
int filesNotChangedAmount = 0;
|
||||
long runtime = 0;
|
||||
|
||||
@SneakyThrows
|
||||
public void execute(String path) {
|
||||
List<AttributeConfig> configPattern = ConfigUtil.loadConfig();
|
||||
List<File> allValidPaths = collector.loadFiles(path);
|
||||
ExecutorService executor = Executors.newFixedThreadPool(ConfigUtil.getThreadCount());
|
||||
|
||||
long beforeTimer = System.currentTimeMillis();
|
||||
if(allValidPaths != null && configPattern != null){
|
||||
for(File file : allValidPaths){
|
||||
List<FileAttribute> attributes = collector.loadAttributes(file);
|
||||
if (attributes.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
boolean fileHasChanged = false;
|
||||
for(AttributeConfig config : configPattern){
|
||||
/*
|
||||
* Creating new ArrayList, because the method removes elements from the list by reference
|
||||
*/
|
||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
|
||||
if(fileHasChanged){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(! fileHasChanged){
|
||||
log.info(file.getName() + " didn't change!");
|
||||
}
|
||||
}
|
||||
System.out.print("Running");
|
||||
allValidPaths.forEach(file -> executor.submit(() -> process(configPattern, file)));
|
||||
}else{
|
||||
log.error("Path is not valid or config has errors!");
|
||||
}
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||
runtime = System.currentTimeMillis() - beforeTimer;
|
||||
|
||||
System.out.printf("%nFiles changed: %s%n", filesChangedAmount);
|
||||
System.out.printf("Files not changed: %s%n", filesNotChangedAmount);
|
||||
System.out.printf("Runtime: %ss%n", runtime / 1000);
|
||||
}
|
||||
|
||||
private void process(List<AttributeConfig> configPattern, File file) {
|
||||
List<FileAttribute> attributes = collector.loadAttributes(file);
|
||||
boolean fileHasChanged = false;
|
||||
|
||||
if (attributes.isEmpty()) return;
|
||||
for(AttributeConfig config : configPattern){
|
||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, attributes);
|
||||
if(fileHasChanged) break;
|
||||
}
|
||||
if(!fileHasChanged){
|
||||
log.info("File didn't change: {}", file.getName());
|
||||
filesNotChangedAmount++;
|
||||
} else {
|
||||
filesChangedAmount++;
|
||||
}
|
||||
System.out.print(".");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@@ -31,10 +32,11 @@ public class ConfigProcessor {
|
||||
public boolean processConfig(File file, List<FileAttribute> attributes) {
|
||||
// check if size is bigger or equal 2 to make sure that there is at least one audio and subtitle line
|
||||
// TODO: implement empty audio or subtitle line
|
||||
if(attributes.size() >= 2){
|
||||
TransferObject transfer = filterAttributes(attributes);
|
||||
if(! attributes.isEmpty()){
|
||||
return updateFile(file, attributes, transfer);
|
||||
List<FileAttribute> attributesCopy = new ArrayList<>(attributes);
|
||||
if(attributesCopy.size() >= 2){
|
||||
TransferObject transfer = filterAttributes(attributesCopy);
|
||||
if(! attributesCopy.isEmpty()){
|
||||
return updateFile(file, attributesCopy, transfer);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -112,7 +114,7 @@ public class ConfigProcessor {
|
||||
* In this case the file would be change to the exact same audio and subtitle lines and we want to
|
||||
* avoid unnecessary changes to the file
|
||||
*/
|
||||
log.info(file.getName() + " already fits config!");
|
||||
log.info("File already fits config: {}", file.getName());
|
||||
return true;
|
||||
}
|
||||
try{
|
||||
@@ -124,7 +126,7 @@ public class ConfigProcessor {
|
||||
* We return true even if there was an error. If there was an error, the chances that this file is still
|
||||
* busy later.
|
||||
*/
|
||||
log.info(file.getName() + " was updated");
|
||||
log.info("Updated {}", file.getName());
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
|
||||
@@ -37,4 +37,15 @@ public class ConfigUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getThreadCount() {
|
||||
try {
|
||||
return Integer.parseInt(new YAML(new File("config.yaml")).getString("threadCount"));
|
||||
} catch (YamlInvalidContentException | IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (YamlKeyNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
Configuration:
|
||||
name: DefaultLogger
|
||||
Appenders:
|
||||
Console:
|
||||
name: Console_Out
|
||||
PatternLayout:
|
||||
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
|
||||
ThresholdFilter:
|
||||
level: debug
|
||||
File:
|
||||
name: FileAppender
|
||||
fileName: default.log
|
||||
@@ -18,11 +12,9 @@ Configuration:
|
||||
Root:
|
||||
level: debug
|
||||
AppenderRef:
|
||||
- ref: Console_Out
|
||||
- ref: FileAppender
|
||||
Logger:
|
||||
name: "com.zaxxer.hikari.HikariConfig"
|
||||
level: info
|
||||
AppenderRef:
|
||||
- ref: Console_Out
|
||||
- ref: FileAppender
|
||||
Reference in New Issue
Block a user