You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/04/28 22:17:15 UTC

svn commit: r1676608 - in /lucene/dev/trunk/lucene: ./ core/src/java/org/apache/lucene/search/ core/src/test/org/apache/lucene/search/

Author: jpountz
Date: Tue Apr 28 20:17:14 2015
New Revision: 1676608

URL: http://svn.apache.org/r1676608
Log:
LUCENE-6455: Require a minimum index size to enable query caching.

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1676608&r1=1676607&r2=1676608&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Tue Apr 28 20:17:14 2015
@@ -104,6 +104,9 @@ Optimizations
 * LUCENE-6456: Queries that generate doc id sets that are too large for the
   query cache are not cached instead of evicting everything. (Adrien Grand)
 
+* LUCENE-6455: Require a minimum index size to enable query caching in order
+  not to cache eg. on MemoryIndex. (Adrien Grand)
+
 Bug Fixes
 
 * LUCENE-6378: Fix all RuntimeExceptions to throw the underlying root cause.

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java?rev=1676608&r1=1676607&r2=1676608&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java Tue Apr 28 20:17:14 2015
@@ -62,20 +62,23 @@ public interface QueryCachingPolicy {
      *  cached while ensuring that at most <tt>33</tt> segments can make it to
      *  the cache (given that some implementations such as {@link LRUQueryCache}
      *  perform better when the number of cached segments is low). */
-    public static final CacheOnLargeSegments DEFAULT = new CacheOnLargeSegments(0.03f);
+    public static final CacheOnLargeSegments DEFAULT = new CacheOnLargeSegments(10000, 0.03f);
 
+    private final int minIndexSize;
     private final float minSizeRatio;
 
     /**
      * Create a {@link CacheOnLargeSegments} instance that only caches on a
-     * given segment if its number of documents divided by the total number of
-     * documents in the index is greater than or equal to
-     * <code>minSizeRatio</code>.
+     * given segment if the total number of documents in the index is greater
+     * than {@code minIndexSize} and the number of documents in the segment
+     * divided by the total number of documents in the index is greater than
+     * or equal to {@code minSizeRatio}.
      */
-    public CacheOnLargeSegments(float minSizeRatio) {
+    public CacheOnLargeSegments(int minIndexSize, float minSizeRatio) {
       if (minSizeRatio <= 0 || minSizeRatio >= 1) {
         throw new IllegalArgumentException("minSizeRatio must be in ]0, 1[, got " + minSizeRatio);
       }
+      this.minIndexSize = minIndexSize;
       this.minSizeRatio = minSizeRatio;
     }
 
@@ -85,6 +88,9 @@ public interface QueryCachingPolicy {
     @Override
     public boolean shouldCache(Query query, LeafReaderContext context) throws IOException {
       final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
+      if (topLevelContext.reader().maxDoc() < minIndexSize) {
+        return false;
+      }
       final float sizeRatio = (float) context.reader().maxDoc() / topLevelContext.reader().maxDoc();
       return sizeRatio >= minSizeRatio;
     }

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java?rev=1676608&r1=1676607&r2=1676608&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java Tue Apr 28 20:17:14 2015
@@ -58,13 +58,15 @@ public final class UsageTrackingQueryCac
   /**
    * Create a new instance.
    *
+   * @param minIndexSize              the minimum size of the top-level index
    * @param minSizeRatio              the minimum size ratio for segments to be cached, see {@link QueryCachingPolicy.CacheOnLargeSegments}
    * @param historySize               the number of recently used filters to track
    */
   public UsageTrackingQueryCachingPolicy(
+      int minIndexSize,
       float minSizeRatio,
       int historySize) {
-    this(new QueryCachingPolicy.CacheOnLargeSegments(minSizeRatio), historySize);
+    this(new QueryCachingPolicy.CacheOnLargeSegments(minIndexSize, minSizeRatio), historySize);
   }
 
   /** Create a new instance with an history size of 256. */

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java?rev=1676608&r1=1676607&r2=1676608&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java Tue Apr 28 20:17:14 2015
@@ -38,13 +38,14 @@ public class TestFilterCachingPolicy ext
     }
     final IndexReader reader = w.getReader();
     for (float minSizeRatio : new float[] {Float.MIN_VALUE, 0.01f, 0.1f, 0.9f}) {
-      final QueryCachingPolicy policy = new QueryCachingPolicy.CacheOnLargeSegments(minSizeRatio);
+      final QueryCachingPolicy policy = new QueryCachingPolicy.CacheOnLargeSegments(0, minSizeRatio);
       for (LeafReaderContext ctx : reader.leaves()) {
         final Filter filter = new QueryWrapperFilter(new TermQuery(new Term("field", "value")));
-        final DocIdSet set = null;
         final boolean shouldCache = policy.shouldCache(filter, ctx);
         final float sizeRatio = (float) ctx.reader().maxDoc() / reader.maxDoc();
         assertEquals(sizeRatio >= minSizeRatio, shouldCache);
+        assertTrue(new QueryCachingPolicy.CacheOnLargeSegments(numDocs, Float.MIN_VALUE).shouldCache(filter, ctx));
+        assertFalse(new QueryCachingPolicy.CacheOnLargeSegments(numDocs + 1, Float.MIN_VALUE).shouldCache(filter, ctx));
       }
     }
     reader.close();