ShouldQueryStrategy.java revision a65408ff321345557f93effa41395174640870eb
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package com.android.quicksearchbox; 18 19import android.util.Log; 20 21import java.util.HashMap; 22import java.util.Iterator; 23import java.util.Map; 24 25/** 26 * Decides whether a given source should be queried for a given query, taking 27 * into account the source's query threshold and query after zero results flag. 28 * 29 * This class is thread safe. 30 */ 31class ShouldQueryStrategy { 32 private static final boolean DBG = true; 33 private static final String TAG = "QSB.ShouldQueryStrategy"; 34 35 // The last query we've seen 36 private String mLastQuery = ""; 37 38 // The current implementation keeps a record of those corpora that have 39 // returned zero results for some prefix of the current query. mEmptyCorpora 40 // maps from corpus to the length of the query which returned 41 // zero results. When a query is shortened (e.g., by deleting characters) 42 // or changed entirely, mEmptyCorpora is pruned (in updateQuery) 43 private final HashMap<Corpus, Integer> mEmptyCorpora 44 = new HashMap<Corpus, Integer>(); 45 46 /** 47 * Returns whether we should query the given source for the given query. 48 */ 49 public boolean shouldQueryCorpus(Corpus corpus, String query) { 50 updateQuery(query); 51 if (query.length() >= corpus.getQueryThreshold()) { 52 if (!corpus.queryAfterZeroResults() && mEmptyCorpora.containsKey(corpus)) { 53 if (DBG) Log.i(TAG, "Not querying " + corpus + ", returned 0 after " 54 + mEmptyCorpora.get(corpus)); 55 return false; 56 } 57 return true; 58 } 59 return false; 60 } 61 62 /** 63 * Called to notify ShouldQueryStrategy when a source reports no results for a query. 64 */ 65 public void onZeroResults(Corpus corpus, String query) { 66 // Make sure this result is actually for a prefix of the current query. 67 if (mLastQuery.startsWith(query) && !corpus.queryAfterZeroResults()) { 68 if (DBG) Log.d(TAG, corpus + " returned 0 results for " + query); 69 mEmptyCorpora.put(corpus, query.length()); 70 } 71 } 72 73 private void updateQuery(String query) { 74 if (query.startsWith(mLastQuery)) { 75 // This is a refinement of the last query, no changes to mEmptyCorpora needed 76 } else if (mLastQuery.startsWith(query)) { 77 // This is a widening of the last query: clear out any sources 78 // that reported zero results after this query. 79 Iterator<Map.Entry<Corpus, Integer>> iter = mEmptyCorpora.entrySet().iterator(); 80 while (iter.hasNext()) { 81 if (iter.next().getValue() > query.length()) { 82 iter.remove(); 83 } 84 } 85 } else { 86 // This is a completely different query, clear everything. 87 mEmptyCorpora.clear(); 88 } 89 mLastQuery = query; 90 } 91} 92