add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestCachingCollector.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.util.LuceneTestCase;
22
23 import java.io.IOException;
24
25 public class TestCachingCollector extends LuceneTestCase {
26
27   private static final double ONE_BYTE = 1.0 / (1024 * 1024); // 1 byte out of MB
28   
29   private static class MockScorer extends Scorer {
30     
31     private MockScorer() {
32       super((Weight) null);
33     }
34     
35     @Override
36     public float score() throws IOException { return 0; }
37
38     @Override
39     public int docID() { return 0; }
40
41     @Override
42     public int nextDoc() throws IOException { return 0; }
43
44     @Override
45     public int advance(int target) throws IOException { return 0; }
46     
47   }
48   
49   private static class NoOpCollector extends Collector {
50
51     private final boolean acceptDocsOutOfOrder;
52     
53     public NoOpCollector(boolean acceptDocsOutOfOrder) {
54       this.acceptDocsOutOfOrder = acceptDocsOutOfOrder;
55     }
56     
57     @Override
58     public void setScorer(Scorer scorer) throws IOException {}
59
60     @Override
61     public void collect(int doc) throws IOException {}
62
63     @Override
64     public void setNextReader(IndexReader reader, int docBase) throws IOException {}
65
66     @Override
67     public boolean acceptsDocsOutOfOrder() {
68       return acceptDocsOutOfOrder;
69     }
70     
71   }
72
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());
77       
78       // collect 1000 docs
79       for (int i = 0; i < 1000; i++) {
80         cc.collect(i);
81       }
82       
83       // now replay them
84       cc.replay(new Collector() {
85         int prevDocID = -1;
86         
87         @Override
88         public void setScorer(Scorer scorer) throws IOException {}
89         
90         @Override
91         public void setNextReader(IndexReader reader, int docBase) throws IOException {}
92         
93         @Override
94         public void collect(int doc) throws IOException {
95           assertEquals(prevDocID + 1, doc);
96           prevDocID = doc;
97         }
98         
99         @Override
100         public boolean acceptsDocsOutOfOrder() {
101           return false;
102         }
103       });
104     }
105   }
106   
107   public void testIllegalStateOnReplay() throws Exception {
108     CachingCollector cc = CachingCollector.create(new NoOpCollector(false), true, 50 * ONE_BYTE);
109     cc.setScorer(new MockScorer());
110     
111     // collect 130 docs, this should be enough for triggering cache abort.
112     for (int i = 0; i < 130; i++) {
113       cc.collect(i);
114     }
115     
116     assertFalse("CachingCollector should not be cached due to low memory limit", cc.isCached());
117     
118     try {
119       cc.replay(new NoOpCollector(false));
120       fail("replay should fail if CachingCollector is not cached");
121     } catch (IllegalStateException e) {
122       // expected
123     }
124   }
125   
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
129     
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
136
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
142     try {
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) {
147       // ok
148     }
149   }
150   
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.
154     
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());
164       
165       // The 151's document should terminate caching
166       cc.collect(numDocs);
167       assertFalse(cc.isCached());
168     }
169   }
170
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());
177       cc.collect(0);
178       
179       assertTrue(cc.isCached());
180       cc.replay(new NoOpCollector(true));
181     }
182   }
183   
184 }