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

Refactor get*Index methods

This commit is contained in:
Reinhard Pointner 2016-04-05 18:06:18 +00:00
parent 8e6688facd
commit e340c8dd0c

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()) { try {
seriesIndex.ensureCapacity(100000); return releaseInfo.getTheTVDBIndex();
try { } catch (Exception e) {
for (SearchResult it : releaseInfo.getTheTVDBIndex()) { debug.severe("Failed to load series index: " + e.getMessage());
seriesIndex.addAll(HighPerformanceMatcher.prepare(it)); return new SearchResult[0];
}
} catch (Exception e) {
// can't load movie index, just try again next time
debug.severe("Failed to load series index: " + e);
// 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()) { try {
animeIndex.ensureCapacity(50000); return releaseInfo.getAnidbIndex();
try { } catch (Exception e) {
for (SearchResult it : releaseInfo.getAnidbIndex()) { debug.severe("Failed to load anime index: " + e.getMessage());
animeIndex.addAll(HighPerformanceMatcher.prepare(it)); return new SearchResult[0];
}
} catch (Exception e) {
// can't load movie index, just try again next time
debug.severe("Failed to load anime index: " + e);
// 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,26 +823,28 @@ 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));
}
} 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
return emptyList();
}
} }
return movieIndex; return sink;
} }
} }
public static List<IndexEntry<Movie>> getMovieIndex() {
return getIndex(() -> {
try {
return releaseInfo.getMovieList();
} 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) {
// cross-reference file / folder name with movie list // cross-reference file / folder name with movie list
final HighPerformanceMatcher nameMatcher = new HighPerformanceMatcher(maxStartIndex); final HighPerformanceMatcher nameMatcher = new HighPerformanceMatcher(maxStartIndex);