* fetch episode lists single-threaded and simple

This commit is contained in:
Reinhard Pointner 2013-03-15 19:53:09 +00:00
parent c4f82956e9
commit ba1b3f5026
1 changed files with 19 additions and 48 deletions

View File

@ -23,7 +23,6 @@ import java.util.AbstractMap;
import java.util.AbstractMap.SimpleImmutableEntry; import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -37,10 +36,6 @@ import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -250,58 +245,34 @@ public class CmdlineOperations implements CmdlineInterface {
private Set<Episode> fetchEpisodeSet(final EpisodeListProvider db, final Collection<String> names, final SortOrder sortOrder, final Locale locale, final boolean strict) throws Exception { private Set<Episode> fetchEpisodeSet(final EpisodeListProvider db, final Collection<String> names, final SortOrder sortOrder, final Locale locale, final boolean strict) throws Exception {
List<Callable<List<Episode>>> tasks = new ArrayList<Callable<List<Episode>>>(); Set<SearchResult> shows = new LinkedHashSet<SearchResult>();
Set<Episode> episodes = new LinkedHashSet<Episode>();
// detect series names and create episode list fetch tasks // detect series names and create episode list fetch tasks
for (final String query : names) { for (String query : names) {
tasks.add(new Callable<List<Episode>>() { List<SearchResult> results = db.search(query, locale);
@Override // select search result
public List<Episode> call() throws Exception { if (results.size() > 0) {
List<SearchResult> results = db.search(query, locale); List<SearchResult> selectedSearchResults = selectSearchResult(query, results, strict);
// select search result if (selectedSearchResults != null) {
if (results.size() > 0) { for (SearchResult it : selectedSearchResults) {
List<SearchResult> selectedSearchResults = selectSearchResult(query, results, strict); if (shows.add(it)) {
try {
if (selectedSearchResults != null) { CLILogger.fine(format("Fetching episode data for [%s]", it.getName()));
List<Episode> episodes = new ArrayList<Episode>(); episodes.addAll(db.getEpisodeList(it, sortOrder, locale));
for (SearchResult it : selectedSearchResults) { Analytics.trackEvent(db.getName(), "FetchEpisodeList", it.getName());
try { } catch (IOException e) {
CLILogger.fine(format("Fetching episode data for [%s]", it.getName())); CLILogger.log(Level.SEVERE, e.getMessage(), e);
episodes.addAll(db.getEpisodeList(it, sortOrder, locale));
Analytics.trackEvent(db.getName(), "FetchEpisodeList", it.getName());
} catch (IOException e) {
CLILogger.log(Level.SEVERE, e.getMessage(), e);
}
} }
return episodes;
} }
} }
return Collections.emptyList();
} }
});
}
// fetch episode lists concurrently
ExecutorService executor = Executors.newCachedThreadPool();
try {
// merge all episodes
Set<Episode> episodes = new LinkedHashSet<Episode>();
for (Future<List<Episode>> future : executor.invokeAll(tasks)) {
// if anything goes wrong make sure to unwind as a partial episode set possibly missing important data can lead to bad matches
episodes.addAll(future.get());
} }
// all background workers have finished
return episodes;
} finally {
// destroy background threads
executor.shutdownNow();
} }
return episodes;
} }