LUCENE-7243: Removed the LeafReaderContext parameter from QueryCachingPolicy#shouldCache.

This commit is contained in:
Adrien Grand 2016-04-22 13:08:48 +02:00
parent 8dbdc3878a
commit 58852beb8f
9 changed files with 23 additions and 54 deletions

View File

@ -33,6 +33,9 @@ API Changes
* LUCENE-7150: Spatial3d gets useful APIs to create common shape * LUCENE-7150: Spatial3d gets useful APIs to create common shape
queries, matching LatLonPoint. (Karl Wright via Mike McCandless) queries, matching LatLonPoint. (Karl Wright via Mike McCandless)
* LUCENE-7243: Removed the LeafReaderContext parameter from
QueryCachingPolicy#shouldCache. (Adrien Grand)
Optimizations Optimizations
* LUCENE-7071: Reduce bytes copying in OfflineSorter, giving ~10% * LUCENE-7071: Reduce bytes copying in OfflineSorter, giving ~10%

View File

@ -661,7 +661,7 @@ public class LRUQueryCache implements QueryCache, Accountable {
DocIdSet docIdSet = get(in.getQuery(), context); DocIdSet docIdSet = get(in.getQuery(), context);
if (docIdSet == null) { if (docIdSet == null) {
if (policy.shouldCache(in.getQuery(), context)) { if (policy.shouldCache(in.getQuery())) {
docIdSet = cache(context); docIdSet = cache(context);
putIfAbsent(in.getQuery(), context, docIdSet); putIfAbsent(in.getQuery(), context, docIdSet);
} else { } else {
@ -694,7 +694,7 @@ public class LRUQueryCache implements QueryCache, Accountable {
DocIdSet docIdSet = get(in.getQuery(), context); DocIdSet docIdSet = get(in.getQuery(), context);
if (docIdSet == null) { if (docIdSet == null) {
if (policy.shouldCache(in.getQuery(), context)) { if (policy.shouldCache(in.getQuery())) {
docIdSet = cache(context); docIdSet = cache(context);
putIfAbsent(in.getQuery(), context, docIdSet); putIfAbsent(in.getQuery(), context, docIdSet);
} else { } else {

View File

@ -19,8 +19,6 @@ package org.apache.lucene.search;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.index.LeafReaderContext;
/** /**
* A policy defining which filters should be cached. * A policy defining which filters should be cached.
* *
@ -40,7 +38,7 @@ public interface QueryCachingPolicy {
public void onUse(Query query) {} public void onUse(Query query) {}
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return true; return true;
} }
@ -51,12 +49,12 @@ public interface QueryCachingPolicy {
* in order to make decisions. */ * in order to make decisions. */
void onUse(Query query); void onUse(Query query);
/** Whether the given {@link DocIdSet} should be cached on a given segment. /** Whether the given {@link Query} is worth caching.
* This method will be called on each leaf context to know if the filter * This method will be called by the {@link QueryCache} to know whether to
* should be cached on this particular leaf. The filter cache will first * cache. It will first attempt to load a {@link DocIdSet} from the cache.
* attempt to load a {@link DocIdSet} from the cache. If it is not cached * If it is not cached yet and this method returns <tt>true</tt> then a
* yet and this method returns <tt>true</tt> then a cache entry will be * cache entry will be generated. Otherwise an uncached scorer will be
* generated. Otherwise an uncached set will be returned. */ * returned. */
boolean shouldCache(Query query, LeafReaderContext context) throws IOException; boolean shouldCache(Query query) throws IOException;
} }

View File

@ -19,17 +19,12 @@ package org.apache.lucene.search;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.util.FrequencyTrackingRingBuffer; import org.apache.lucene.util.FrequencyTrackingRingBuffer;
/** /**
* A {@link QueryCachingPolicy} that tracks usage statistics of recently-used * A {@link QueryCachingPolicy} that tracks usage statistics of recently-used
* filters in order to decide on which filters are worth caching. * filters in order to decide on which filters are worth caching.
* *
* It also uses some heuristics on segments, filters and the doc id sets that
* they produce in order to cache more aggressively when the execution cost
* significantly outweighs the caching overhead.
*
* @lucene.experimental * @lucene.experimental
*/ */
public final class UsageTrackingQueryCachingPolicy implements QueryCachingPolicy { public final class UsageTrackingQueryCachingPolicy implements QueryCachingPolicy {
@ -128,7 +123,7 @@ public final class UsageTrackingQueryCachingPolicy implements QueryCachingPolicy
} }
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
if (query instanceof MatchAllDocsQuery if (query instanceof MatchAllDocsQuery
// MatchNoDocsQuery currently rewrites to a BooleanQuery, // MatchNoDocsQuery currently rewrites to a BooleanQuery,
// but who knows, it might get its own Weight one day // but who knows, it might get its own Weight one day

View File

@ -207,7 +207,7 @@ public class TestIndexSearcher extends LuceneTestCase {
assertEquals(IndexSearcher.getDefaultQueryCachingPolicy(), searcher.getQueryCachingPolicy()); assertEquals(IndexSearcher.getDefaultQueryCachingPolicy(), searcher.getQueryCachingPolicy());
QueryCachingPolicy dummyPolicy = new QueryCachingPolicy() { QueryCachingPolicy dummyPolicy = new QueryCachingPolicy() {
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return false; return false;
} }
@Override @Override

View File

@ -65,7 +65,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
public void onUse(Query query) {} public void onUse(Query query) {}
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return false; return false;
} }
@ -455,7 +455,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
final QueryCachingPolicy countingPolicy = new QueryCachingPolicy() { final QueryCachingPolicy countingPolicy = new QueryCachingPolicy() {
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return random().nextBoolean(); return random().nextBoolean();
} }
@ -762,7 +762,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
final QueryCachingPolicy policy = new QueryCachingPolicy() { final QueryCachingPolicy policy = new QueryCachingPolicy() {
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
assertEquals(expectedCacheKey, query); assertEquals(expectedCacheKey, query);
return true; return true;
} }
@ -1080,7 +1080,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
} }
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return true; return true;
} }
} }

View File

@ -16,15 +16,8 @@
*/ */
package org.apache.lucene.search; package org.apache.lucene.search;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
public class TestUsageTrackingFilterCachingPolicy extends LuceneTestCase { public class TestUsageTrackingFilterCachingPolicy extends LuceneTestCase {
@ -41,15 +34,7 @@ public class TestUsageTrackingFilterCachingPolicy extends LuceneTestCase {
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
policy.onUse(q); policy.onUse(q);
} }
Directory dir = newDirectory(); assertFalse(policy.shouldCache(q));
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
w.addDocument(new Document());
IndexReader r = DirectoryReader.open(w);
assertFalse(policy.shouldCache(q, getOnlyLeafReader(r).getContext()));
r.close();
w.close();
dir.close();
} }
} }

View File

@ -330,24 +330,12 @@ public class TermsQueryTest extends LuceneTestCase {
} }
public void testIsConsideredCostlyByQueryCache() throws IOException { public void testIsConsideredCostlyByQueryCache() throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();
IndexWriter w = new IndexWriter(dir, iwc);
Document doc = new Document();
for (int i = 0; i < 10000; ++i) {
w.addDocument(doc);
}
w.forceMerge(1);
DirectoryReader reader = DirectoryReader.open(w);
w.close();
TermsQuery query = new TermsQuery(new Term("foo", "bar"), new Term("foo", "baz")); TermsQuery query = new TermsQuery(new Term("foo", "bar"), new Term("foo", "baz"));
UsageTrackingQueryCachingPolicy policy = new UsageTrackingQueryCachingPolicy(); UsageTrackingQueryCachingPolicy policy = new UsageTrackingQueryCachingPolicy();
assertFalse(policy.shouldCache(query, getOnlyLeafReader(reader).getContext())); assertFalse(policy.shouldCache(query));
policy.onUse(query); policy.onUse(query);
policy.onUse(query); policy.onUse(query);
// cached after two uses // cached after two uses
assertTrue(policy.shouldCache(query, getOnlyLeafReader(reader).getContext())); assertTrue(policy.shouldCache(query));
reader.close();
dir.close();
} }
} }

View File

@ -476,7 +476,7 @@ public abstract class LuceneTestCase extends Assert {
public void onUse(Query query) {} public void onUse(Query query) {}
@Override @Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { public boolean shouldCache(Query query) throws IOException {
return random().nextBoolean(); return random().nextBoolean();
} }