Apache Solr open-source search software
LUCENE-6091: Fix UsageTrackingFilterCachingPolicy's handling of null DocIdSets.
Adrien Grandbd88a849c37d
diff --git a/lucene/core/src/java/org/apache/lucene/search/UsageTrackingFilterCachingPolicy.java b/lucene/core/src/java/org/apache/lucene/search/UsageTrackingFilterCachingPolicy.javaindex d3c6a5d1a3d..05a01b90a35 100644--- a/lucene/core/src/java/org/apache/lucene/search/UsageTrackingFilterCachingPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/search/UsageTrackingFilterCachingPolicy.java@@ -36,18 +36,18 @@ import org.apache.lucene.util.FrequencyTrackingRingBuffer;*/public final class UsageTrackingFilterCachingPolicy implements FilterCachingPolicy {- private static boolean isCostly(Filter filter) {+ static boolean isCostly(Filter filter) {// This does not measure the cost of iterating over the filter (for this we// already have the DocIdSetIterator#cost API) but the cost to build the// DocIdSet in the first placereturn filter instanceof MultiTermQueryWrapperFilter;}- private static boolean isCheapToCache(DocIdSet set) {+ static boolean isCheapToCache(DocIdSet set) {// the produced doc set is already cacheable, so caching has no// overhead at all. TODO: extend this to sets whose iterators have a low// cost?- return set.isCacheable();+ return set == null || set.isCacheable();}private final FilterCachingPolicy.CacheOnLargeSegments segmentPolicy;diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.javanew file mode 100644index 00000000000..4bf6cf76213--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java@@ -0,0 +1,55 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.IOException;++import org.apache.lucene.document.Document;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.index.RandomIndexWriter;+import org.apache.lucene.index.Term;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;++public class TestFilterCachingPolicy extends LuceneTestCase {++ public void testLargeSegmentDetection() throws IOException {+ Directory dir = newDirectory();+ RandomIndexWriter w = new RandomIndexWriter(random(), dir);+ final int numDocs = atLeast(100);+ for (int i = 0; i < numDocs; ++i) {+ w.addDocument(new Document());+ }+ final IndexReader reader = w.getReader();+ for (float minSizeRatio : new float[] {Float.MIN_VALUE, 0.01f, 0.1f, 0.9f}) {+ final FilterCachingPolicy policy = new FilterCachingPolicy.CacheOnLargeSegments(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, set);+ final float sizeRatio = (float) ctx.reader().maxDoc() / reader.maxDoc();+ assertEquals(sizeRatio >= minSizeRatio, shouldCache);+ }+ }+ reader.close();+ w.close();+ dir.close();+ }++}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestUsageTrackingFilterCachingPolicy.java b/lucene/core/src/test/org/apache/lucene/search/TestUsageTrackingFilterCachingPolicy.javanew file mode 100644index 00000000000..809518327cf--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestUsageTrackingFilterCachingPolicy.java@@ -0,0 +1,44 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.lucene.index.Term;+import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.RoaringDocIdSet;++public class TestUsageTrackingFilterCachingPolicy extends LuceneTestCase {++ public void testCheapToCache() {+ assertTrue(UsageTrackingFilterCachingPolicy.isCheapToCache(null));+ assertTrue(UsageTrackingFilterCachingPolicy.isCheapToCache(DocIdSet.EMPTY));+ assertTrue(UsageTrackingFilterCachingPolicy.isCheapToCache(new RoaringDocIdSet.Builder(5).add(3).build()));+ assertFalse(UsageTrackingFilterCachingPolicy.isCheapToCache(new DocValuesDocIdSet(5, null) {+ @Override+ protected boolean matchDoc(int doc) {+ return false;+ }+ }));+ }++ public void testCostlyFilter() {+ assertTrue(UsageTrackingFilterCachingPolicy.isCostly(new PrefixFilter(new Term("field", "prefix"))));+ assertTrue(UsageTrackingFilterCachingPolicy.isCostly(NumericRangeFilter.newIntRange("intField", 8, 1, 1000, true, true)));+ assertFalse(UsageTrackingFilterCachingPolicy.isCostly(new QueryWrapperFilter(new TermQuery(new Term("field", "value")))));+ }++}