diff --git a/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java b/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java index 5d3e0a3..9fb216d 100644 --- a/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java +++ b/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java @@ -5,11 +5,26 @@ */ package moe.nekojimi.musicsearcher.providers; +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlInput; +import com.amihaiemil.eoyaml.YamlMapping; +import com.amihaiemil.eoyaml.YamlSequence; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Collection; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.logging.Level; +import java.util.logging.Logger; +import moe.nekojimi.musicsearcher.Main; import moe.nekojimi.musicsearcher.Query; import moe.nekojimi.musicsearcher.Result; @@ -19,18 +34,44 @@ import moe.nekojimi.musicsearcher.Result; */ public class MetaSearcher extends Searcher { - + private final Set searchers = new HashSet<>(); private int minSearchTime = 10000; // ms private int maxSearchTime = 30000; // ms - public MetaSearcher() + public MetaSearcher(Collection searchers) { super("meta"); + this.searchers.addAll(searchers); + } + + public static MetaSearcher loadYAML(File file) + { + try + { + List searchers = new ArrayList<>(); + YamlInput input = Yaml.createYamlInput(file); + YamlSequence seq = input.readYamlSequence(); + for (int i = 0; i < seq.size(); i++) + { + YamlMapping map = seq.yamlMapping(i); + String type = map.string("type"); + Class clazz = (Class) Class.forName("moe.nekojimi.musicsearcher.providers." + type); + Constructor constructor = clazz.getConstructor(YamlMapping.class); + Searcher searcher = constructor.newInstance(map); + searchers.add(searcher); + } + + MetaSearcher metaSearch = new MetaSearcher(searchers); + return metaSearch; + } catch (IOException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) + { + throw new IllegalArgumentException("Could not load MetaSearcher config from YAML file " + file.getAbsolutePath(), ex); + } } @Override - protected List doSearch(Query query) + protected List doSearch(Query query) { List results = new ArrayList<>(); List>> searches = new ArrayList<>(); @@ -38,18 +79,28 @@ public class MetaSearcher extends Searcher { CompletableFuture> search = searcher.search(query); searches.add(search); - search.whenComplete((t, u) -> - { - if (u == null) - { - results.addAll(t); -// searches.remove(search); - } - }); +// search.whenComplete((t, u) -> +// { +// if (u == null) +// { +// results.addAll(t); +//// searches.remove(search); +// } +// }); } - CompletableFuture.allOf((CompletableFuture[]) searches.toArray()); +// CompletableFuture.allOf((CompletableFuture[]) searches.toArray()); + for (CompletableFuture> search : searches) + try + { + List result = search.get(30, TimeUnit.SECONDS); + results.addAll(result); + } catch (TimeoutException | InterruptedException | ExecutionException ex) + { + Logger.getLogger(MetaSearcher.class.getName()).log(Level.SEVERE, null, ex); + } + return results; } - + }