mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Implement caching & fix minor bugs
This commit is contained in:
@@ -2,11 +2,11 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger;
|
|||||||
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigLoader;
|
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.AttributeUpdaterKernel;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -14,8 +14,8 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConfigLoader.initConfig(args);
|
ConfigLoader.initConfig(args);
|
||||||
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null
|
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null
|
||||||
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor())
|
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor())
|
||||||
: new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor());
|
: new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor());
|
||||||
kernel.execute();
|
kernel.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<Key, Value> {
|
||||||
|
private final Map<Key, Value> 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<Key, Value> creationFunction) {
|
||||||
|
return cache.computeIfAbsent(key, creationFunction::apply);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<File, List<FileAttribute>> cache = new Cache<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FileAttribute> loadAttributes(File file) {
|
||||||
|
return cache.retrieve(file, super::loadAttributes);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ public class FileFilter {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResultStatistic.getInstance().total();
|
||||||
ResultStatistic.getInstance().excluded();
|
ResultStatistic.getInstance().excluded();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ public abstract class AttributeUpdaterKernel {
|
|||||||
* @param fileInfoDto contains information about file and desired configuration.
|
* @param fileInfoDto contains information about file and desired configuration.
|
||||||
*/
|
*/
|
||||||
protected void updateFile(FileInfoDto fileInfoDto) {
|
protected void updateFile(FileInfoDto fileInfoDto) {
|
||||||
|
statistic.total();
|
||||||
switch (fileInfoDto.getStatus()) {
|
switch (fileInfoDto.getStatus()) {
|
||||||
case CHANGE_NECESSARY:
|
case CHANGE_NECESSARY:
|
||||||
statistic.shouldChange();
|
statistic.shouldChange();
|
||||||
@@ -139,7 +140,6 @@ public abstract class AttributeUpdaterKernel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
statistic.total();
|
|
||||||
processor.update(fileInfo.getFile(), fileInfo);
|
processor.update(fileInfo.getFile(), fileInfo);
|
||||||
statistic.success();
|
statistic.success();
|
||||||
log.info("Updated {}", fileInfo.getFile().getAbsolutePath());
|
log.info("Updated {}", fileInfo.getFile().getAbsolutePath());
|
||||||
|
|||||||
@@ -34,8 +34,12 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
List<File> loadFiles(String path) {
|
List<File> loadFiles(String path) {
|
||||||
|
return loadFiles(path, Config.getInstance().getCoherent());
|
||||||
|
}
|
||||||
|
|
||||||
|
List<File> loadFiles(String path, int depth) {
|
||||||
List<File> excludedFiles = loadExcludedFiles();
|
List<File> excludedFiles = loadExcludedFiles();
|
||||||
List<File> directories = collector.loadDirectories(path, Config.getInstance().getCoherent())
|
List<File> directories = collector.loadDirectories(path, depth)
|
||||||
.stream().filter(file -> !excludedFiles.contains(file))
|
.stream().filter(file -> !excludedFiles.contains(file))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return directories.stream()
|
return directories.stream()
|
||||||
@@ -59,25 +63,20 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
void process(File file) {
|
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
|
// TODO: Implement level crawl if coherence is not possible on user entered depth
|
||||||
// IMPL idea: recursive method call, cache needs to be implemented
|
// IMPL idea: recursive method call, cache needs to be implemented
|
||||||
List<FileInfoDto> fileInfos = collector.loadFiles(file.getAbsolutePath())
|
List<FileInfoDto> fileInfos = collector.loadFiles(file.getAbsolutePath()).stream()
|
||||||
.stream().map(FileInfoDto::new)
|
.map(FileInfoDto::new)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
Map<FileInfoDto, List<FileAttribute>> 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 (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
|
||||||
|
|
||||||
for (FileInfoDto fileInfo : fileInfos) {
|
for (FileInfoDto fileInfo : fileInfos) {
|
||||||
List<FileAttribute> attributes = fileAttributeCache.get(fileInfo);
|
List<FileAttribute> attributes = processor.loadAttributes(fileInfo.getFile());
|
||||||
|
|
||||||
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
|
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
|
||||||
List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
|
List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
|
||||||
@@ -88,7 +87,6 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
|
|||||||
|
|
||||||
if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) {
|
if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) {
|
||||||
log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
|
log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
|
||||||
statistic.increaseTotalBy(fileInfos.size());
|
|
||||||
fileInfos.forEach(this::updateFile);
|
fileInfos.forEach(this::updateFile);
|
||||||
return; // match found, end process here
|
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) {
|
for (FileInfoDto fileInfo : fileInfos) {
|
||||||
statistic.total();
|
statistic.total();
|
||||||
if (Config.getInstance().isForceCoherent()) {
|
if (!Config.getInstance().isForceCoherent()) {
|
||||||
super.process(fileInfo.getFile());
|
super.process(fileInfo.getFile());
|
||||||
} else {
|
} else {
|
||||||
statistic.excluded();
|
statistic.excluded();
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ public class ResultStatistic {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ResultStatistic[" + "filesTotal=" + filesTotal +
|
return "ResultStatistic: " + "filesTotal=" + filesTotal +
|
||||||
", excluded=" + excluded +
|
", excluded=" + excluded +
|
||||||
", shouldChange=" + shouldChange +
|
", shouldChange=" + shouldChange +
|
||||||
" (failedChanging=" + failedChanging +
|
" (failedChanging=" + failedChanging +
|
||||||
@@ -124,7 +124,6 @@ public class ResultStatistic {
|
|||||||
"), noSuitableConfigFound=" + noSuitableConfigFound +
|
"), noSuitableConfigFound=" + noSuitableConfigFound +
|
||||||
", alreadyFits=" + alreadyFits +
|
", alreadyFits=" + alreadyFits +
|
||||||
", failed=" + failed +
|
", failed=" + failed +
|
||||||
", runtime=" + formatTimer() +
|
", runtime=" + formatTimer();
|
||||||
']';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user