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 org.apache.lucene.analysis.MockAnalyzer;
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.util.LuceneTestCase;
26 import java.io.IOException;
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
30 public class TestFieldCache extends LuceneTestCase {
31 protected IndexReader reader;
32 private static final int NUM_DOCS = atLeast(1000);
33 private Directory directory;
36 public void setUp() throws Exception {
38 directory = newDirectory();
39 RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
40 long theLong = Long.MAX_VALUE;
41 double theDouble = Double.MAX_VALUE;
42 byte theByte = Byte.MAX_VALUE;
43 short theShort = Short.MAX_VALUE;
44 int theInt = Integer.MAX_VALUE;
45 float theFloat = Float.MAX_VALUE;
46 for (int i = 0; i < NUM_DOCS; i++){
47 Document doc = new Document();
48 doc.add(newField("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
49 doc.add(newField("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
50 doc.add(newField("theByte", String.valueOf(theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
51 doc.add(newField("theShort", String.valueOf(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
52 doc.add(newField("theInt", String.valueOf(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
53 doc.add(newField("theFloat", String.valueOf(theFloat--), Field.Store.NO, Field.Index.NOT_ANALYZED));
54 writer.addDocument(doc);
57 reader = IndexReader.open(directory, true);
61 public void tearDown() throws Exception {
67 public void testInfoStream() throws Exception {
69 FieldCache cache = FieldCache.DEFAULT;
70 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
71 cache.setInfoStream(new PrintStream(bos));
72 cache.getDoubles(reader, "theDouble");
73 cache.getFloats(reader, "theDouble");
74 assertTrue(bos.toString().indexOf("WARNING") != -1);
76 FieldCache.DEFAULT.purgeAllCaches();
80 public void test() throws IOException {
81 FieldCache cache = FieldCache.DEFAULT;
82 double [] doubles = cache.getDoubles(reader, "theDouble");
83 assertSame("Second request to cache return same array", doubles, cache.getDoubles(reader, "theDouble"));
84 assertSame("Second request with explicit parser return same array", doubles, cache.getDoubles(reader, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER));
85 assertTrue("doubles Size: " + doubles.length + " is not: " + NUM_DOCS, doubles.length == NUM_DOCS);
86 for (int i = 0; i < doubles.length; i++) {
87 assertTrue(doubles[i] + " does not equal: " + (Double.MAX_VALUE - i), doubles[i] == (Double.MAX_VALUE - i));
91 long [] longs = cache.getLongs(reader, "theLong");
92 assertSame("Second request to cache return same array", longs, cache.getLongs(reader, "theLong"));
93 assertSame("Second request with explicit parser return same array", longs, cache.getLongs(reader, "theLong", FieldCache.DEFAULT_LONG_PARSER));
94 assertTrue("longs Size: " + longs.length + " is not: " + NUM_DOCS, longs.length == NUM_DOCS);
95 for (int i = 0; i < longs.length; i++) {
96 assertTrue(longs[i] + " does not equal: " + (Long.MAX_VALUE - i), longs[i] == (Long.MAX_VALUE - i));
100 byte [] bytes = cache.getBytes(reader, "theByte");
101 assertSame("Second request to cache return same array", bytes, cache.getBytes(reader, "theByte"));
102 assertSame("Second request with explicit parser return same array", bytes, cache.getBytes(reader, "theByte", FieldCache.DEFAULT_BYTE_PARSER));
103 assertTrue("bytes Size: " + bytes.length + " is not: " + NUM_DOCS, bytes.length == NUM_DOCS);
104 for (int i = 0; i < bytes.length; i++) {
105 assertTrue(bytes[i] + " does not equal: " + (Byte.MAX_VALUE - i), bytes[i] == (byte) (Byte.MAX_VALUE - i));
109 short [] shorts = cache.getShorts(reader, "theShort");
110 assertSame("Second request to cache return same array", shorts, cache.getShorts(reader, "theShort"));
111 assertSame("Second request with explicit parser return same array", shorts, cache.getShorts(reader, "theShort", FieldCache.DEFAULT_SHORT_PARSER));
112 assertTrue("shorts Size: " + shorts.length + " is not: " + NUM_DOCS, shorts.length == NUM_DOCS);
113 for (int i = 0; i < shorts.length; i++) {
114 assertTrue(shorts[i] + " does not equal: " + (Short.MAX_VALUE - i), shorts[i] == (short) (Short.MAX_VALUE - i));
118 int [] ints = cache.getInts(reader, "theInt");
119 assertSame("Second request to cache return same array", ints, cache.getInts(reader, "theInt"));
120 assertSame("Second request with explicit parser return same array", ints, cache.getInts(reader, "theInt", FieldCache.DEFAULT_INT_PARSER));
121 assertTrue("ints Size: " + ints.length + " is not: " + NUM_DOCS, ints.length == NUM_DOCS);
122 for (int i = 0; i < ints.length; i++) {
123 assertTrue(ints[i] + " does not equal: " + (Integer.MAX_VALUE - i), ints[i] == (Integer.MAX_VALUE - i));
127 float [] floats = cache.getFloats(reader, "theFloat");
128 assertSame("Second request to cache return same array", floats, cache.getFloats(reader, "theFloat"));
129 assertSame("Second request with explicit parser return same array", floats, cache.getFloats(reader, "theFloat", FieldCache.DEFAULT_FLOAT_PARSER));
130 assertTrue("floats Size: " + floats.length + " is not: " + NUM_DOCS, floats.length == NUM_DOCS);
131 for (int i = 0; i < floats.length; i++) {
132 assertTrue(floats[i] + " does not equal: " + (Float.MAX_VALUE - i), floats[i] == (Float.MAX_VALUE - i));