1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-10 11:25:04 -05:00

Cache null values properly

This commit is contained in:
Reinhard Pointner 2019-02-05 15:10:36 +07:00
parent d0c77c65fc
commit 431966cbbd
3 changed files with 19 additions and 24 deletions

View File

@ -2,12 +2,12 @@ package net.filebot.similarity;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.filebot.Logging.*;
import static net.filebot.web.EpisodeUtilities.*; import static net.filebot.web.EpisodeUtilities.*;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.List; import java.util.List;
@ -15,13 +15,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.filebot.media.SmartSeasonEpisodeMatcher; import net.filebot.media.SmartSeasonEpisodeMatcher;
import net.filebot.similarity.SeasonEpisodeMatcher.SxE; import net.filebot.similarity.SeasonEpisodeMatcher.SxE;
@ -91,18 +85,13 @@ public class EpisodeMatcher extends Matcher<File, Object> {
} }
private final SeasonEpisodeMatcher seasonEpisodeMatcher = new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.LENIENT_SANITY, false); private final SeasonEpisodeMatcher seasonEpisodeMatcher = new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.LENIENT_SANITY, false);
private final Cache<File, Set<SxE>> parseEpisodeIdentiferCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build(); private final Map<File, Set<SxE>> cache = synchronizedMap(new HashMap<>(64, 4));
private Set<SxE> parseEpisodeIdentifer(File file) { private Set<SxE> parseEpisodeIdentifer(File file) {
try { return cache.computeIfAbsent(file, f -> {
return parseEpisodeIdentiferCache.get(file, () -> { List<SxE> sxe = seasonEpisodeMatcher.match(f.getName());
List<SxE> sxe = seasonEpisodeMatcher.match(file.getName()); return sxe == null ? emptySet() : new HashSet<SxE>(sxe);
return sxe == null ? emptySet() : new HashSet<SxE>(sxe); });
});
} catch (ExecutionException e) {
debug.log(Level.SEVERE, e, e::toString);
}
return emptySet();
} }
private Set<Integer> normalizeIdentifierSet(Set<SxE> numbers) { private Set<Integer> normalizeIdentifierSet(Set<SxE> numbers) {

View File

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -44,7 +45,7 @@ public class EpisodeMetrics {
// Match by season / episode numbers // Match by season / episode numbers
public final SimilarityMetric SeasonEpisode = new SeasonEpisodeMetric(new SmartSeasonEpisodeMatcher(null, false)) { public final SimilarityMetric SeasonEpisode = new SeasonEpisodeMetric(new SmartSeasonEpisodeMatcher(null, false)) {
private final Map<Object, Collection<SxE>> cache = synchronizedMap(new HashMap<Object, Collection<SxE>>(64, 4)); private final Map<Object, Collection<SxE>> cache = synchronizedMap(new HashMap<>(64, 4));
@Override @Override
protected Collection<SxE> parse(Object object) { protected Collection<SxE> parse(Object object) {
@ -58,7 +59,10 @@ public class EpisodeMetrics {
return emptySet(); return emptySet();
} }
return cache.computeIfAbsent(object, super::parse); return cache.computeIfAbsent(object, o -> {
Collection<SxE> sxe = super.parse(o);
return sxe == null ? emptySet() : sxe;
});
} }
private Set<SxE> parse(Episode e) { private Set<SxE> parse(Episode e) {
@ -88,7 +92,7 @@ public class EpisodeMetrics {
// Match episode airdate // Match episode airdate
public final SimilarityMetric AirDate = new DateMetric(getDateMatcher()) { public final SimilarityMetric AirDate = new DateMetric(getDateMatcher()) {
private final Map<Object, SimpleDate> cache = synchronizedMap(new HashMap<Object, SimpleDate>(64, 4)); private final Map<Object, Optional<SimpleDate>> cache = synchronizedMap(new HashMap<>(64, 4));
@Override @Override
public SimpleDate parse(Object object) { public SimpleDate parse(Object object) {
@ -101,7 +105,9 @@ public class EpisodeMetrics {
return null; return null;
} }
return cache.computeIfAbsent(object, super::parse); return cache.computeIfAbsent(object, o -> {
return Optional.ofNullable(super.parse(o));
}).orElse(null);
} }
}; };
@ -701,7 +707,7 @@ public class EpisodeMetrics {
}; };
protected final Map<Object, String> transformCache = synchronizedMap(new HashMap<Object, String>(64, 4)); protected final Map<Object, String> transformCache = synchronizedMap(new HashMap<>(64, 4));
protected final Transliterator transliterator = Transliterator.getInstance("Any-Latin;Latin-ASCII;[:Diacritic:]remove"); protected final Transliterator transliterator = Transliterator.getInstance("Any-Latin;Latin-ASCII;[:Diacritic:]remove");
@ -716,7 +722,7 @@ public class EpisodeMetrics {
// 3. remove obvious release info // 3. remove obvious release info
// 4. apply transliterator // 4. apply transliterator
// 5. remove or normalize special characters // 5. remove or normalize special characters
return normalizePunctuation(transliterator.transform(stripFormatInfo(removeEmbeddedChecksum(normalizeFileName(object))))).toLowerCase(); return normalizePunctuation(transliterator.transform(stripFormatInfo(removeEmbeddedChecksum(normalizeFileName(o))))).toLowerCase();
}); });
} }

View File

@ -183,7 +183,7 @@ public class SubtitleMetrics extends EpisodeMetrics {
return emptyMap(); return emptyMap();
} }
private final Map<File, Map<String, Object>> cache = synchronizedMap(new HashMap<File, Map<String, Object>>(64, 4)); private final Map<File, Map<String, Object>> cache = synchronizedMap(new HashMap<>(64, 4));
private Map<String, Object> getVideoProperties(File file) { private Map<String, Object> getVideoProperties(File file) {
return cache.computeIfAbsent(file, f -> { return cache.computeIfAbsent(file, f -> {