2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.quicksearchbox;
19 import android.text.TextUtils;
20 import android.util.Log;
22 import java.util.HashMap;
23 import java.util.Iterator;
27 * Decides whether a given source should be queried for a given query, taking
28 * into account the source's query threshold and query after zero results flag.
30 * This class is thread safe.
32 class ShouldQueryStrategy {
34 private static final boolean DBG = false;
35 private static final String TAG = "QSB.ShouldQueryStrategy";
37 // The last query we've seen
38 private String mLastQuery = "";
40 // The current implementation keeps a record of those corpora that have
41 // returned zero results for some prefix of the current query. mEmptyCorpora
42 // maps from corpus to the length of the query which returned
43 // zero results. When a query is shortened (e.g., by deleting characters)
44 // or changed entirely, mEmptyCorpora is pruned (in updateQuery)
45 private final HashMap<Corpus, Integer> mEmptyCorpora
46 = new HashMap<Corpus, Integer>();
49 * Returns whether we should query the given source for the given query.
51 public boolean shouldQueryCorpus(Corpus corpus, String query) {
53 if (query.length() >= corpus.getQueryThreshold()) {
54 if (!corpus.queryAfterZeroResults() && mEmptyCorpora.containsKey(corpus)) {
55 if (DBG) Log.i(TAG, "Not querying " + corpus + ", returned 0 after "
56 + mEmptyCorpora.get(corpus));
61 if (DBG) Log.d(TAG, "Query too short for corpus " + corpus);
66 * Called to notify ShouldQueryStrategy when a source reports no results for a query.
68 public void onZeroResults(Corpus corpus, String query) {
69 // Make sure this result is actually for a prefix of the current query.
70 if (mLastQuery.startsWith(query) && !corpus.queryAfterZeroResults()
71 && !TextUtils.isEmpty(query)) {
72 if (DBG) Log.d(TAG, corpus + " returned 0 results for '" + query + "'");
73 mEmptyCorpora.put(corpus, query.length());
77 private void updateQuery(String query) {
78 if (query.startsWith(mLastQuery)) {
79 // This is a refinement of the last query, no changes to mEmptyCorpora needed
80 } else if (mLastQuery.startsWith(query)) {
81 // This is a widening of the last query: clear out any sources
82 // that reported zero results after this query.
83 Iterator<Map.Entry<Corpus, Integer>> iter = mEmptyCorpora.entrySet().iterator();
84 while (iter.hasNext()) {
85 if (iter.next().getValue() > query.length()) {
90 // This is a completely different query, clear everything.
91 mEmptyCorpora.clear();