1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.util.LuceneTestCase;
23 import java.io.IOException;
25 public class TestCachingCollector extends LuceneTestCase {
27 private static final double ONE_BYTE = 1.0 / (1024 * 1024); // 1 byte out of MB
29 private static class MockScorer extends Scorer {
31 private MockScorer() {
36 public float score() throws IOException { return 0; }
39 public int docID() { return 0; }
42 public int nextDoc() throws IOException { return 0; }
45 public int advance(int target) throws IOException { return 0; }
49 private static class NoOpCollector extends Collector {
51 private final boolean acceptDocsOutOfOrder;
53 public NoOpCollector(boolean acceptDocsOutOfOrder) {
54 this.acceptDocsOutOfOrder = acceptDocsOutOfOrder;
58 public void setScorer(Scorer scorer) throws IOException {}
61 public void collect(int doc) throws IOException {}
64 public void setNextReader(IndexReader reader, int docBase) throws IOException {}
67 public boolean acceptsDocsOutOfOrder() {
68 return acceptDocsOutOfOrder;
73 public void testBasic() throws Exception {
74 for (boolean cacheScores : new boolean[] { false, true }) {
75 CachingCollector cc = CachingCollector.create(new NoOpCollector(false), cacheScores, 1.0);
76 cc.setScorer(new MockScorer());
79 for (int i = 0; i < 1000; i++) {
84 cc.replay(new Collector() {
88 public void setScorer(Scorer scorer) throws IOException {}
91 public void setNextReader(IndexReader reader, int docBase) throws IOException {}
94 public void collect(int doc) throws IOException {
95 assertEquals(prevDocID + 1, doc);
100 public boolean acceptsDocsOutOfOrder() {
107 public void testIllegalStateOnReplay() throws Exception {
108 CachingCollector cc = CachingCollector.create(new NoOpCollector(false), true, 50 * ONE_BYTE);
109 cc.setScorer(new MockScorer());
111 // collect 130 docs, this should be enough for triggering cache abort.
112 for (int i = 0; i < 130; i++) {
116 assertFalse("CachingCollector should not be cached due to low memory limit", cc.isCached());
119 cc.replay(new NoOpCollector(false));
120 fail("replay should fail if CachingCollector is not cached");
121 } catch (IllegalStateException e) {
126 public void testIllegalCollectorOnReplay() throws Exception {
127 // tests that the Collector passed to replay() has an out-of-order mode that
128 // is valid with the Collector passed to the ctor
130 // 'src' Collector does not support out-of-order
131 CachingCollector cc = CachingCollector.create(new NoOpCollector(false), true, 50 * ONE_BYTE);
132 cc.setScorer(new MockScorer());
133 for (int i = 0; i < 10; i++) cc.collect(i);
134 cc.replay(new NoOpCollector(true)); // this call should not fail
135 cc.replay(new NoOpCollector(false)); // this call should not fail
137 // 'src' Collector supports out-of-order
138 cc = CachingCollector.create(new NoOpCollector(true), true, 50 * ONE_BYTE);
139 cc.setScorer(new MockScorer());
140 for (int i = 0; i < 10; i++) cc.collect(i);
141 cc.replay(new NoOpCollector(true)); // this call should not fail
143 cc.replay(new NoOpCollector(false)); // this call should fail
144 fail("should have failed if an in-order Collector was given to replay(), " +
145 "while CachingCollector was initialized with out-of-order collection");
146 } catch (IllegalArgumentException e) {
151 public void testCachedArraysAllocation() throws Exception {
152 // tests the cached arrays allocation -- if the 'nextLength' was too high,
153 // caching would terminate even if a smaller length would suffice.
155 // set RAM limit enough for 150 docs + random(10000)
156 int numDocs = random.nextInt(10000) + 150;
157 for (boolean cacheScores : new boolean[] { false, true }) {
158 int bytesPerDoc = cacheScores ? 8 : 4;
159 CachingCollector cc = CachingCollector.create(new NoOpCollector(false),
160 cacheScores, bytesPerDoc * ONE_BYTE * numDocs);
161 cc.setScorer(new MockScorer());
162 for (int i = 0; i < numDocs; i++) cc.collect(i);
163 assertTrue(cc.isCached());
165 // The 151's document should terminate caching
167 assertFalse(cc.isCached());
171 public void testNoWrappedCollector() throws Exception {
172 for (boolean cacheScores : new boolean[] { false, true }) {
173 // create w/ null wrapped collector, and test that the methods work
174 CachingCollector cc = CachingCollector.create(true, cacheScores, 50 * ONE_BYTE);
175 cc.setNextReader(null, 0);
176 cc.setScorer(new MockScorer());
179 assertTrue(cc.isCached());
180 cc.replay(new NoOpCollector(true));