Implement caching & fix minor bugs

This commit is contained in:
2023-04-16 13:07:58 +02:00
parent e19f780ff0
commit 686a9a0da1
7 changed files with 56 additions and 22 deletions

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -21,6 +21,7 @@ public class FileFilter {
return true;
}
ResultStatistic.getInstance().total();
ResultStatistic.getInstance().excluded();
return false;
}

View File

@@ -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());

View File

@@ -34,8 +34,12 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/
@Override
List<File> loadFiles(String path) {
return loadFiles(path, Config.getInstance().getCoherent());
}
List<File> loadFiles(String path, int depth) {
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))
.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<FileInfoDto> fileInfos = collector.loadFiles(file.getAbsolutePath())
.stream().map(FileInfoDto::new)
List<FileInfoDto> fileInfos = collector.loadFiles(file.getAbsolutePath()).stream()
.map(FileInfoDto::new)
.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 (FileInfoDto fileInfo : fileInfos) {
List<FileAttribute> attributes = fileAttributeCache.get(fileInfo);
List<FileAttribute> attributes = processor.loadAttributes(fileInfo.getFile());
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(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)) {
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();

View File

@@ -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();
}
}