Search in sources :

Example 1 with SearchResult

use of com.biglybt.pif.utils.search.SearchResult in project BiglyBT by BiglySoftware.

the class PluginEngine method searchSupport.

@Override
protected Result[] searchSupport(SearchParameter[] params, Map searchContext, final int desired_max_matches, final int absolute_max_matches, String headers, final ResultListener listener) throws SearchException {
    if (provider == null) {
        provider = getMetaSearch().resolveProvider(this);
        if (provider == null) {
            return (new Result[0]);
        }
    }
    Map search_parameters = new HashMap();
    String term = null;
    for (int i = 0; i < params.length; i++) {
        SearchParameter param = params[i];
        String pattern = param.getMatchPattern();
        String value = param.getValue();
        if (pattern.equals("s")) {
            term = value;
            search_parameters.put(SearchProvider.SP_SEARCH_TERM, value);
        } else if (pattern.equals("m")) {
            search_parameters.put(SearchProvider.SP_MATURE, Boolean.valueOf(value));
        } else if (pattern.equals("n")) {
            String[] networks = value.split(",");
            search_parameters.put(SearchProvider.SP_NETWORKS, networks);
        } else {
            Debug.out("Unrecognised search parameter '" + pattern + "=" + value + "' ignored");
        }
    }
    final String f_term = term;
    try {
        final List<PluginResult> results = new ArrayList<>();
        final AESemaphore sem = new AESemaphore("waiter");
        provider.search(search_parameters, new SearchObserver() {

            private boolean complete = false;

            @Override
            public void resultReceived(SearchInstance search, SearchResult result) {
                PluginResult p_result = new PluginResult(PluginEngine.this, result, f_term);
                synchronized (this) {
                    if (complete) {
                        return;
                    }
                    results.add(p_result);
                }
                if (listener != null) {
                    listener.resultsReceived(PluginEngine.this, new Result[] { p_result });
                }
                synchronized (this) {
                    if (absolute_max_matches >= 0 && results.size() >= absolute_max_matches) {
                        complete = true;
                        sem.release();
                    }
                }
            }

            @Override
            public void cancelled() {
                sem.release();
            }

            @Override
            public void complete() {
                sem.release();
            }

            @Override
            public Object getProperty(int property) {
                if (property == PR_MAX_RESULTS_WANTED) {
                    return (new Long(desired_max_matches));
                }
                return (null);
            }
        });
        sem.reserve();
        if (listener != null) {
            listener.resultsComplete(this);
        }
        return ((Result[]) results.toArray(new Result[results.size()]));
    } catch (Throwable e) {
        throw (new SearchException("Search failed", e));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchObserver(com.biglybt.pif.utils.search.SearchObserver) SearchResult(com.biglybt.pif.utils.search.SearchResult) AESemaphore(com.biglybt.core.util.AESemaphore) SearchResult(com.biglybt.pif.utils.search.SearchResult) SearchInstance(com.biglybt.pif.utils.search.SearchInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AESemaphore (com.biglybt.core.util.AESemaphore)1 SearchInstance (com.biglybt.pif.utils.search.SearchInstance)1 SearchObserver (com.biglybt.pif.utils.search.SearchObserver)1 SearchResult (com.biglybt.pif.utils.search.SearchResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1