Refactor get*Index methods

This commit is contained in:
Reinhard Pointner 2016-04-05 18:06:18 +00:00
parent 8e6688facd
commit e340c8dd0c
1 changed files with 34 additions and 49 deletions

View File

@ -39,6 +39,7 @@ import java.util.SortedSet;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -481,45 +482,27 @@ public class MediaDetection {
private static final ArrayList<IndexEntry<SearchResult>> seriesIndex = new ArrayList<IndexEntry<SearchResult>>(); private static final ArrayList<IndexEntry<SearchResult>> seriesIndex = new ArrayList<IndexEntry<SearchResult>>();
public static List<IndexEntry<SearchResult>> getSeriesIndex() throws IOException { public static List<IndexEntry<SearchResult>> getSeriesIndex() throws IOException {
synchronized (seriesIndex) { return getIndex(() -> {
if (seriesIndex.isEmpty()) {
seriesIndex.ensureCapacity(100000);
try { try {
for (SearchResult it : releaseInfo.getTheTVDBIndex()) { return releaseInfo.getTheTVDBIndex();
seriesIndex.addAll(HighPerformanceMatcher.prepare(it));
}
} catch (Exception e) { } catch (Exception e) {
// can't load movie index, just try again next time debug.severe("Failed to load series index: " + e.getMessage());
debug.severe("Failed to load series index: " + e); return new SearchResult[0];
// rely on online search
return emptyList();
}
}
return seriesIndex;
} }
}, HighPerformanceMatcher::prepare, seriesIndex);
} }
private static final ArrayList<IndexEntry<SearchResult>> animeIndex = new ArrayList<IndexEntry<SearchResult>>(); private static final ArrayList<IndexEntry<SearchResult>> animeIndex = new ArrayList<IndexEntry<SearchResult>>();
public static List<IndexEntry<SearchResult>> getAnimeIndex() throws IOException { public static List<IndexEntry<SearchResult>> getAnimeIndex() {
synchronized (animeIndex) { return getIndex(() -> {
if (animeIndex.isEmpty()) {
animeIndex.ensureCapacity(50000);
try { try {
for (SearchResult it : releaseInfo.getAnidbIndex()) { return releaseInfo.getAnidbIndex();
animeIndex.addAll(HighPerformanceMatcher.prepare(it));
}
} catch (Exception e) { } catch (Exception e) {
// can't load movie index, just try again next time debug.severe("Failed to load anime index: " + e.getMessage());
debug.severe("Failed to load anime index: " + e); return new SearchResult[0];
// rely on online search
return emptyList();
}
}
return animeIndex;
} }
}, HighPerformanceMatcher::prepare, animeIndex);
} }
public static List<String> matchSeriesByName(Collection<String> files, int maxStartIndex, List<IndexEntry<SearchResult>> index) throws Exception { public static List<String> matchSeriesByName(Collection<String> files, int maxStartIndex, List<IndexEntry<SearchResult>> index) throws Exception {
@ -840,24 +823,26 @@ public class MediaDetection {
private static final ArrayList<IndexEntry<Movie>> movieIndex = new ArrayList<IndexEntry<Movie>>(); private static final ArrayList<IndexEntry<Movie>> movieIndex = new ArrayList<IndexEntry<Movie>>();
public static List<IndexEntry<Movie>> getMovieIndex() { private static <T extends SearchResult> List<IndexEntry<T>> getIndex(Supplier<T[]> function, Function<T, List<IndexEntry<T>>> mapper, ArrayList<IndexEntry<T>> sink) {
synchronized (movieIndex) { synchronized (sink) {
if (movieIndex.isEmpty()) { if (sink.isEmpty()) {
movieIndex.ensureCapacity(100000); T[] index = function.get();
try { sink.ensureCapacity(index.length * 4); // alias names
for (Movie it : releaseInfo.getMovieList()) { stream(index).map(mapper).forEach(sink::addAll);
movieIndex.addAll(HighPerformanceMatcher.prepare(it)); }
return sink;
}
} }
} catch (Exception e) {
// can't load movie index, just try again next time
debug.severe("Failed to load movie index: " + e);
// if we can't use internal index we can only rely on online search public static List<IndexEntry<Movie>> getMovieIndex() {
return emptyList(); return getIndex(() -> {
} try {
} return releaseInfo.getMovieList();
return movieIndex; } catch (Exception e) {
debug.severe("Failed to load movie index: " + e.getMessage());
return new Movie[0];
} }
}, HighPerformanceMatcher::prepare, movieIndex);
} }
public static List<Movie> matchMovieName(Collection<String> files, boolean strict, int maxStartIndex) { public static List<Movie> matchMovieName(Collection<String> files, boolean strict, int maxStartIndex) {