diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java index db85668..a746649 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java @@ -2,11 +2,11 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigLoader; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CachedMkvFileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector; -import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -14,8 +14,8 @@ public class Main { public static void main(String[] args) { ConfigLoader.initConfig(args); AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null - ? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor()) - : new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor()); + ? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor()) + : new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor()); kernel.execute(); } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java new file mode 100644 index 0000000..83a46be --- /dev/null +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java @@ -0,0 +1,19 @@ +package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +public class Cache { + private final Map cache = new HashMap<>(); + + /** + * Retrieve {@link Value} from Cache or run creationFunction and return its value. + * @param key key of cache map + * @param creationFunction function to create missing values + * @return {@link Value} from Cache, or if missing result from creationFunction. + */ + public synchronized Value retrieve(Key key, Function creationFunction) { + return cache.computeIfAbsent(key, creationFunction::apply); + } +} diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java new file mode 100644 index 0000000..acd9198 --- /dev/null +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java @@ -0,0 +1,15 @@ +package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; + +import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; + +import java.io.File; +import java.util.List; + +public class CachedMkvFileProcessor extends MkvFileProcessor { + Cache> cache = new Cache<>(); + + @Override + public List loadAttributes(File file) { + return cache.retrieve(file, super::loadAttributes); + } +} diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java index 20b5c21..5edfe9b 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java @@ -21,6 +21,7 @@ public class FileFilter { return true; } + ResultStatistic.getInstance().total(); ResultStatistic.getInstance().excluded(); return false; } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java index 9a1be4f..bb31a33 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java @@ -115,6 +115,7 @@ public abstract class AttributeUpdaterKernel { * @param fileInfoDto contains information about file and desired configuration. */ protected void updateFile(FileInfoDto fileInfoDto) { + statistic.total(); switch (fileInfoDto.getStatus()) { case CHANGE_NECESSARY: statistic.shouldChange(); @@ -139,7 +140,6 @@ public abstract class AttributeUpdaterKernel { } try { - statistic.total(); processor.update(fileInfo.getFile(), fileInfo); statistic.success(); log.info("Updated {}", fileInfo.getFile().getAbsolutePath()); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java index 4fe1cc7..bc9eac5 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java @@ -34,8 +34,12 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { */ @Override List loadFiles(String path) { + return loadFiles(path, Config.getInstance().getCoherent()); + } + + List loadFiles(String path, int depth) { List excludedFiles = loadExcludedFiles(); - List directories = collector.loadDirectories(path, Config.getInstance().getCoherent()) + List directories = collector.loadDirectories(path, depth) .stream().filter(file -> !excludedFiles.contains(file)) .collect(Collectors.toList()); return directories.stream() @@ -59,25 +63,20 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { */ @Override void process(File file) { + process(file, Config.getInstance().getCoherent()); + } + + void process(File file, int depth) { // TODO: Implement level crawl if coherence is not possible on user entered depth // IMPL idea: recursive method call, cache needs to be implemented - List fileInfos = collector.loadFiles(file.getAbsolutePath()) - .stream().map(FileInfoDto::new) + List fileInfos = collector.loadFiles(file.getAbsolutePath()).stream() + .map(FileInfoDto::new) .collect(Collectors.toList()); - Map> fileAttributeCache = new HashMap<>(); - for (FileInfoDto fileInfo : fileInfos) { - if (!Config.getInstance().getIncludePattern().matcher(fileInfo.getFile().getAbsolutePath()).matches()) { - statistic.excluded(); - continue; - } - fileAttributeCache.put(fileInfo, processor.loadAttributes(fileInfo.getFile())); - } - for (AttributeConfig config : Config.getInstance().getAttributeConfig()) { for (FileInfoDto fileInfo : fileInfos) { - List attributes = fileAttributeCache.get(fileInfo); + List attributes = processor.loadAttributes(fileInfo.getFile()); List nonForcedTracks = processor.retrieveNonForcedTracks(attributes); List nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes); @@ -88,7 +87,6 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) { log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath()); - statistic.increaseTotalBy(fileInfos.size()); fileInfos.forEach(this::updateFile); return; // match found, end process here } @@ -99,9 +97,11 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { }); } + log.info("No coherent match found for {}", file.getAbsoluteFile()); + for (FileInfoDto fileInfo : fileInfos) { statistic.total(); - if (Config.getInstance().isForceCoherent()) { + if (!Config.getInstance().isForceCoherent()) { super.process(fileInfo.getFile()); } else { statistic.excluded(); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java index 98e4dfa..8147625 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java @@ -116,7 +116,7 @@ public class ResultStatistic { @Override public String toString() { - return "ResultStatistic[" + "filesTotal=" + filesTotal + + return "ResultStatistic: " + "filesTotal=" + filesTotal + ", excluded=" + excluded + ", shouldChange=" + shouldChange + " (failedChanging=" + failedChanging + @@ -124,7 +124,6 @@ public class ResultStatistic { "), noSuitableConfigFound=" + noSuitableConfigFound + ", alreadyFits=" + alreadyFits + ", failed=" + failed + - ", runtime=" + formatTimer() + - ']'; + ", runtime=" + formatTimer(); } }