generated from Nekojimi/JavaMavenTemplate
Metasearcher: load searchproviders.yml.
This commit is contained in:
parent
eff1f5bfdc
commit
b8c3972f54
|
@ -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<Searcher> searchers = new HashSet<>();
|
||||
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
|
||||
protected List<Result> doSearch(Query query)
|
||||
protected List<Result> doSearch(Query query)
|
||||
{
|
||||
List<Result> results = new ArrayList<>();
|
||||
List<CompletableFuture<List<Result>>> searches = new ArrayList<>();
|
||||
|
@ -38,18 +79,28 @@ public class MetaSearcher extends Searcher
|
|||
{
|
||||
CompletableFuture<List<Result>> 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<List<Result>> search : searches)
|
||||
try
|
||||
{
|
||||
List<Result> 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue