From 70a4825d9688b7986c21e4213376e40560814b7c Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 15 Oct 2015 15:03:46 +0000 Subject: [PATCH] LUCENE-6838: Added getters for IndexSearcher's query cache and caching policy. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1708832 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../apache/lucene/search/IndexSearcher.java | 21 +++++++++ .../lucene/search/TestIndexSearcher.java | 44 +++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index e45bfc6ee22..8bae8d75fa0 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -88,6 +88,9 @@ New Features fast, very accurate query to find all indexed points within an earth-surface shape (Karl Wright, Mike McCandless) +* LUCENE-6838: Added IndexSearcher#getQueryCache and #getQueryCachingPolicy. + (Adrien Grand) + API Changes * LUCENE-6590: Query.setBoost(), Query.getBoost() and Query.clone() are gone. diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java index 7c84f9f1efe..bd6115e8a59 100644 --- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java @@ -268,6 +268,17 @@ public class IndexSearcher { this.queryCache = queryCache; } + /** + * Return the query cache of this {@link IndexSearcher}. This will be either + * the {@link #getDefaultQueryCache() default query cache} or the query cache + * that was last set through {@link #setQueryCache(QueryCache)}. A return + * value of {@code null} indicates that caching is disabled. + * @lucene.experimental + */ + public QueryCache getQueryCache() { + return queryCache; + } + /** * Set the {@link QueryCachingPolicy} to use for query caching. * This method should be called before starting using this @@ -279,6 +290,16 @@ public class IndexSearcher { this.queryCachingPolicy = Objects.requireNonNull(queryCachingPolicy); } + /** + * Return the query cache of this {@link IndexSearcher}. This will be either + * the {@link #getDefaultQueryCachingPolicy() default policy} or the policy + * that was last set through {@link #setQueryCachingPolicy(QueryCachingPolicy)}. + * @lucene.experimental + */ + public QueryCachingPolicy getQueryCachingPolicy() { + return queryCachingPolicy; + } + /** * Expert: Creates an array of leaf slices each holding a subset of the given leaves. * Each {@link LeafSlice} is executed in a single thread. By default there diff --git a/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java b/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java index ea2c53efdee..45010e2be82 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java @@ -30,6 +30,8 @@ import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.StringField; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.MultiReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause.Occur; @@ -179,4 +181,46 @@ public class TestIndexSearcher extends LuceneTestCase { dir.close(); } + public void testGetQueryCache() throws IOException { + IndexSearcher searcher = new IndexSearcher(new MultiReader()); + assertEquals(IndexSearcher.getDefaultQueryCache(), searcher.getQueryCache()); + QueryCache dummyCache = new QueryCache() { + @Override + public Weight doCache(Weight weight, QueryCachingPolicy policy) { + return weight; + } + }; + searcher.setQueryCache(dummyCache); + assertEquals(dummyCache, searcher.getQueryCache()); + + IndexSearcher.setDefaultQueryCache(dummyCache); + searcher = new IndexSearcher(new MultiReader()); + assertEquals(dummyCache, searcher.getQueryCache()); + + searcher.setQueryCache(null); + assertNull(searcher.getQueryCache()); + + IndexSearcher.setDefaultQueryCache(null); + searcher = new IndexSearcher(new MultiReader()); + assertNull(searcher.getQueryCache()); + } + + public void testGetQueryCachingPolicy() throws IOException { + IndexSearcher searcher = new IndexSearcher(new MultiReader()); + assertEquals(IndexSearcher.getDefaultQueryCachingPolicy(), searcher.getQueryCachingPolicy()); + QueryCachingPolicy dummyPolicy = new QueryCachingPolicy() { + @Override + public boolean shouldCache(Query query, LeafReaderContext context) throws IOException { + return false; + } + @Override + public void onUse(Query query) {} + }; + searcher.setQueryCachingPolicy(dummyPolicy); + assertEquals(dummyPolicy, searcher.getQueryCachingPolicy()); + + IndexSearcher.setDefaultQueryCachingPolicy(dummyPolicy); + searcher = new IndexSearcher(new MultiReader()); + assertEquals(dummyPolicy, searcher.getQueryCachingPolicy()); + } }