|
|
|
@ -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; |
|
|
|
|
|
|
|
|
@ -24,9 +39,35 @@ public class MetaSearcher extends Searcher |
|
|
|
|
private int minSearchTime = 10000; // ms
|
|
|
|
|
private int maxSearchTime = 30000; // ms
|
|
|
|
|
|
|
|
|
|
public MetaSearcher() |
|
|
|
|
public MetaSearcher(Collection<Searcher> searchers) |
|
|
|
|
{ |
|
|
|
|
super("meta"); |
|
|
|
|
this.searchers.addAll(searchers); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MetaSearcher loadYAML(File file) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
List<Searcher> 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<? extends Searcher> clazz = (Class<? extends Searcher>) Class.forName("moe.nekojimi.musicsearcher.providers." + type); |
|
|
|
|
Constructor<? extends Searcher> 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 |
|
|
|
@ -38,16 +79,26 @@ public class MetaSearcher extends Searcher |
|
|
|
|
{ |
|
|
|
|
CompletableFuture<List<Result>> search = searcher.search(query); |
|
|
|
|
searches.add(search); |
|
|
|
|
search.whenComplete((t, u) -> |
|
|
|
|
// search.whenComplete((t, u) ->
|
|
|
|
|
// {
|
|
|
|
|
// if (u == null)
|
|
|
|
|
// {
|
|
|
|
|
// results.addAll(t);
|
|
|
|
|
//// searches.remove(search);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
} |
|
|
|
|
// CompletableFuture.allOf((CompletableFuture<?>[]) searches.toArray());
|
|
|
|
|
for (CompletableFuture<List<Result>> search : searches) |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (u == null) |
|
|
|
|
List<Result> result = search.get(30, TimeUnit.SECONDS); |
|
|
|
|
results.addAll(result); |
|
|
|
|
} catch (TimeoutException | InterruptedException | ExecutionException ex) |
|
|
|
|
{ |
|
|
|
|
results.addAll(t); |
|
|
|
|
// searches.remove(search);
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
Logger.getLogger(MetaSearcher.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
|
} |
|
|
|
|
CompletableFuture.allOf((CompletableFuture<?>[]) searches.toArray()); |
|
|
|
|
|
|
|
|
|
return results; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|