Search in sources :

Example 1 with SearchInstance

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

the class RelatedContentManager method searchRCM.

public SearchInstance searchRCM(final Map<String, Object> search_parameters, final SearchObserver observer) throws SearchException {
    if (!initialisation_complete_sem.isReleasedForever()) {
        AsyncDispatcher dispatcher = new AsyncDispatcher();
        final boolean[] cancelled = { false };
        final boolean[] went_async = { false };
        final SearchInstance[] si = { null };
        final SearchException[] error = { null };
        final AESemaphore temp_sem = new AESemaphore("");
        dispatcher.dispatch(new AERunnable() {

            @Override
            public void runSupport() {
                try {
                    si[0] = searchRCMSupport(search_parameters, observer);
                    synchronized (cancelled) {
                        if (cancelled[0]) {
                            si[0].cancel();
                        }
                    }
                } catch (Throwable e) {
                    Debug.out(e);
                    SearchException se;
                    if (e instanceof SearchException) {
                        se = (SearchException) e;
                    } else {
                        se = new SearchException("Search failed", e);
                    }
                    synchronized (cancelled) {
                        error[0] = se;
                        if (went_async[0]) {
                            // error won't be returned to caller, signify that things
                            // ain't going anywhere
                            observer.complete();
                        }
                    }
                } finally {
                    temp_sem.release();
                }
            }
        });
        temp_sem.reserve(500);
        synchronized (cancelled) {
            if (si[0] != null) {
                return (si[0]);
            }
            if (error[0] != null) {
                throw (error[0]);
            }
            went_async[0] = true;
        }
        SearchInstance result = new SearchInstance() {

            @Override
            public void cancel() {
                synchronized (cancelled) {
                    if (si[0] != null) {
                        si[0].cancel();
                    }
                    cancelled[0] = true;
                }
            }
        };
        return (result);
    } else {
        return (searchRCMSupport(search_parameters, observer));
    }
}
Also used : SearchException(com.biglybt.pif.utils.search.SearchException) SearchInstance(com.biglybt.pif.utils.search.SearchInstance)

Example 2 with SearchInstance

use of com.biglybt.pif.utils.search.SearchInstance 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

SearchInstance (com.biglybt.pif.utils.search.SearchInstance)2 AESemaphore (com.biglybt.core.util.AESemaphore)1 SearchException (com.biglybt.pif.utils.search.SearchException)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