1
package org.apache.lucene.search;
4
* Copyright 2004 The Apache Software Foundation
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
import java.io.ByteArrayOutputStream;
20
import java.io.IOException;
21
import java.io.PrintStream;
22
import java.util.concurrent.CyclicBarrier;
23
import java.util.concurrent.atomic.AtomicBoolean;
24
import java.util.concurrent.atomic.AtomicInteger;
26
import org.apache.lucene.analysis.MockAnalyzer;
27
import org.apache.lucene.document.Document;
28
import org.apache.lucene.document.Field;
29
import org.apache.lucene.document.NumericField;
30
import org.apache.lucene.index.IndexReader;
31
import org.apache.lucene.index.RandomIndexWriter;
32
import org.apache.lucene.store.Directory;
33
import org.apache.lucene.util.Bits;
34
import org.apache.lucene.util.LuceneTestCase;
36
public class TestFieldCache extends LuceneTestCase {
37
protected IndexReader reader;
39
private Directory directory;
42
public void setUp() throws Exception {
44
NUM_DOCS = atLeast(1000);
45
directory = newDirectory();
46
RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
47
long theLong = Long.MAX_VALUE;
48
double theDouble = Double.MAX_VALUE;
49
byte theByte = Byte.MAX_VALUE;
50
short theShort = Short.MAX_VALUE;
51
int theInt = Integer.MAX_VALUE;
52
float theFloat = Float.MAX_VALUE;
53
for (int i = 0; i < NUM_DOCS; i++){
54
Document doc = new Document();
55
doc.add(newField("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
56
doc.add(newField("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
57
doc.add(newField("theByte", String.valueOf(theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
58
doc.add(newField("theShort", String.valueOf(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
59
doc.add(newField("theInt", String.valueOf(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
60
doc.add(newField("theFloat", String.valueOf(theFloat--), Field.Store.NO, Field.Index.NOT_ANALYZED));
62
doc.add(newField("sparse", String.valueOf(i), Field.Store.NO, Field.Index.NOT_ANALYZED));
65
doc.add(new NumericField("numInt").setIntValue(i));
67
writer.addDocument(doc);
70
reader = IndexReader.open(directory, true);
74
public void tearDown() throws Exception {
80
public void testInfoStream() throws Exception {
82
FieldCache cache = FieldCache.DEFAULT;
83
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
84
cache.setInfoStream(new PrintStream(bos));
85
cache.getDoubles(reader, "theDouble");
86
cache.getFloats(reader, "theDouble");
87
assertTrue(bos.toString().indexOf("WARNING") != -1);
89
FieldCache.DEFAULT.purgeAllCaches();
93
public void test() throws IOException {
94
FieldCache cache = FieldCache.DEFAULT;
95
double [] doubles = cache.getDoubles(reader, "theDouble", null, random.nextBoolean());
96
assertSame("Second request to cache return same array", doubles, cache.getDoubles(reader, "theDouble"));
97
assertSame("Second request with explicit parser return same array", doubles, cache.getDoubles(reader, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER));
98
assertTrue("doubles Size: " + doubles.length + " is not: " + NUM_DOCS, doubles.length == NUM_DOCS);
99
for (int i = 0; i < doubles.length; i++) {
100
assertTrue(doubles[i] + " does not equal: " + (Double.MAX_VALUE - i), doubles[i] == (Double.MAX_VALUE - i));
104
long [] longs = cache.getLongs(reader, "theLong", null, random.nextBoolean());
105
assertSame("Second request to cache return same array", longs, cache.getLongs(reader, "theLong"));
106
assertSame("Second request with explicit parser return same array", longs, cache.getLongs(reader, "theLong", FieldCache.DEFAULT_LONG_PARSER));
107
assertTrue("longs Size: " + longs.length + " is not: " + NUM_DOCS, longs.length == NUM_DOCS);
108
for (int i = 0; i < longs.length; i++) {
109
assertTrue(longs[i] + " does not equal: " + (Long.MAX_VALUE - i), longs[i] == (Long.MAX_VALUE - i));
113
byte [] bytes = cache.getBytes(reader, "theByte", null, random.nextBoolean());
114
assertSame("Second request to cache return same array", bytes, cache.getBytes(reader, "theByte"));
115
assertSame("Second request with explicit parser return same array", bytes, cache.getBytes(reader, "theByte", FieldCache.DEFAULT_BYTE_PARSER));
116
assertTrue("bytes Size: " + bytes.length + " is not: " + NUM_DOCS, bytes.length == NUM_DOCS);
117
for (int i = 0; i < bytes.length; i++) {
118
assertTrue(bytes[i] + " does not equal: " + (Byte.MAX_VALUE - i), bytes[i] == (byte) (Byte.MAX_VALUE - i));
122
short [] shorts = cache.getShorts(reader, "theShort", null, random.nextBoolean());
123
assertSame("Second request to cache return same array", shorts, cache.getShorts(reader, "theShort"));
124
assertSame("Second request with explicit parser return same array", shorts, cache.getShorts(reader, "theShort", FieldCache.DEFAULT_SHORT_PARSER));
125
assertTrue("shorts Size: " + shorts.length + " is not: " + NUM_DOCS, shorts.length == NUM_DOCS);
126
for (int i = 0; i < shorts.length; i++) {
127
assertTrue(shorts[i] + " does not equal: " + (Short.MAX_VALUE - i), shorts[i] == (short) (Short.MAX_VALUE - i));
131
int [] ints = cache.getInts(reader, "theInt", null, random.nextBoolean());
132
assertSame("Second request to cache return same array", ints, cache.getInts(reader, "theInt"));
133
assertSame("Second request with explicit parser return same array", ints, cache.getInts(reader, "theInt", FieldCache.DEFAULT_INT_PARSER));
134
assertTrue("ints Size: " + ints.length + " is not: " + NUM_DOCS, ints.length == NUM_DOCS);
135
for (int i = 0; i < ints.length; i++) {
136
assertTrue(ints[i] + " does not equal: " + (Integer.MAX_VALUE - i), ints[i] == (Integer.MAX_VALUE - i));
140
float [] floats = cache.getFloats(reader, "theFloat", null, random.nextBoolean());
141
assertSame("Second request to cache return same array", floats, cache.getFloats(reader, "theFloat"));
142
assertSame("Second request with explicit parser return same array", floats, cache.getFloats(reader, "theFloat", FieldCache.DEFAULT_FLOAT_PARSER));
143
assertTrue("floats Size: " + floats.length + " is not: " + NUM_DOCS, floats.length == NUM_DOCS);
144
for (int i = 0; i < floats.length; i++) {
145
assertTrue(floats[i] + " does not equal: " + (Float.MAX_VALUE - i), floats[i] == (Float.MAX_VALUE - i));
149
Bits docsWithField = cache.getDocsWithField(reader, "theLong");
150
assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "theLong"));
151
assertTrue("docsWithField(theLong) must be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
152
assertTrue("docsWithField(theLong) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
153
for (int i = 0; i < docsWithField.length(); i++) {
154
assertTrue(docsWithField.get(i));
157
docsWithField = cache.getDocsWithField(reader, "sparse");
158
assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "sparse"));
159
assertFalse("docsWithField(sparse) must not be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
160
assertTrue("docsWithField(sparse) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
161
for (int i = 0; i < docsWithField.length(); i++) {
162
assertEquals(i%2 == 0, docsWithField.get(i));
165
public void testDocsWithField() throws Exception {
166
FieldCache cache = FieldCache.DEFAULT;
167
cache.purgeAllCaches();
168
assertEquals(0, cache.getCacheEntries().length);
169
double[] doubles = cache.getDoubles(reader, "theDouble", null, true);
171
// The double[] takes two slots (one w/ null parser, one
172
// w/ real parser), and docsWithField should also
173
// have been populated:
174
assertEquals(3, cache.getCacheEntries().length);
175
Bits bits = cache.getDocsWithField(reader, "theDouble");
177
// No new entries should appear:
178
assertEquals(3, cache.getCacheEntries().length);
179
assertTrue(bits instanceof Bits.MatchAllBits);
181
int[] ints = cache.getInts(reader, "sparse", null, true);
182
assertEquals(6, cache.getCacheEntries().length);
183
Bits docsWithField = cache.getDocsWithField(reader, "sparse");
184
assertEquals(6, cache.getCacheEntries().length);
185
for (int i = 0; i < docsWithField.length(); i++) {
187
assertTrue(docsWithField.get(i));
188
assertEquals(i, ints[i]);
190
assertFalse(docsWithField.get(i));
194
int[] numInts = cache.getInts(reader, "numInt", null, random.nextBoolean());
195
docsWithField = cache.getDocsWithField(reader, "numInt");
196
for (int i = 0; i < docsWithField.length(); i++) {
198
assertTrue(docsWithField.get(i));
199
assertEquals(i, numInts[i]);
201
assertFalse(docsWithField.get(i));
206
public void testGetDocsWithFieldThreadSafety() throws Exception {
207
final FieldCache cache = FieldCache.DEFAULT;
208
cache.purgeAllCaches();
211
Thread[] threads = new Thread[NUM_THREADS];
212
final AtomicBoolean failed = new AtomicBoolean();
213
final AtomicInteger iters = new AtomicInteger();
214
final int NUM_ITER = 200 * RANDOM_MULTIPLIER;
215
final CyclicBarrier restart = new CyclicBarrier(NUM_THREADS,
217
// @Override not until java 1.6
219
cache.purgeAllCaches();
220
iters.incrementAndGet();
223
for(int threadIDX=0;threadIDX<NUM_THREADS;threadIDX++) {
224
threads[threadIDX] = new Thread() {
229
while(!failed.get()) {
230
final int op = random.nextInt(3);
232
// Purge all caches & resume, once all
235
if (iters.get() >= NUM_ITER) {
238
} else if (op == 1) {
239
Bits docsWithField = cache.getDocsWithField(reader, "sparse");
240
for (int i = 0; i < docsWithField.length(); i++) {
241
assertEquals(i%2 == 0, docsWithField.get(i));
244
int[] ints = cache.getInts(reader, "sparse", null, true);
245
Bits docsWithField = cache.getDocsWithField(reader, "sparse");
246
for (int i = 0; i < docsWithField.length(); i++) {
248
assertTrue(docsWithField.get(i));
249
assertEquals(i, ints[i]);
251
assertFalse(docsWithField.get(i));
256
} catch (Throwable t) {
259
throw new RuntimeException(t);
263
threads[threadIDX].start();
266
for(int threadIDX=0;threadIDX<NUM_THREADS;threadIDX++) {
267
threads[threadIDX].join();
269
assertFalse(failed.get());