~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestFieldCache.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search;
2
 
 
3
 
/**
4
 
 * Copyright 2004 The Apache Software Foundation
5
 
 *
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
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
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.
17
 
 */
18
 
 
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;
25
 
 
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;
35
 
 
36
 
public class TestFieldCache extends LuceneTestCase {
37
 
  protected IndexReader reader;
38
 
  private int NUM_DOCS;
39
 
  private Directory directory;
40
 
 
41
 
  @Override
42
 
  public void setUp() throws Exception {
43
 
    super.setUp();
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));
61
 
      if (i%2 == 0) {
62
 
        doc.add(newField("sparse", String.valueOf(i), Field.Store.NO, Field.Index.NOT_ANALYZED));
63
 
      }
64
 
      if (i%2 == 0) {
65
 
        doc.add(new NumericField("numInt").setIntValue(i));
66
 
      }
67
 
      writer.addDocument(doc);
68
 
    }
69
 
    writer.close();
70
 
    reader = IndexReader.open(directory, true);
71
 
  }
72
 
 
73
 
  @Override
74
 
  public void tearDown() throws Exception {
75
 
    reader.close();
76
 
    directory.close();
77
 
    super.tearDown();
78
 
  }
79
 
  
80
 
  public void testInfoStream() throws Exception {
81
 
    try {
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);
88
 
    } finally {
89
 
      FieldCache.DEFAULT.purgeAllCaches();
90
 
    }
91
 
  }
92
 
 
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));
101
 
 
102
 
    }
103
 
    
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));
110
 
 
111
 
    }
112
 
    
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));
119
 
 
120
 
    }
121
 
    
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));
128
 
 
129
 
    }
130
 
    
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));
137
 
 
138
 
    }
139
 
    
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));
146
 
 
147
 
    }
148
 
    
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));
155
 
    }
156
 
    
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));
163
 
    }
164
 
  }
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);
170
 
 
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");
176
 
 
177
 
    // No new entries should appear:
178
 
    assertEquals(3, cache.getCacheEntries().length);
179
 
    assertTrue(bits instanceof Bits.MatchAllBits);
180
 
 
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++) {
186
 
      if (i%2 == 0) {
187
 
        assertTrue(docsWithField.get(i));
188
 
        assertEquals(i, ints[i]);
189
 
      } else {
190
 
        assertFalse(docsWithField.get(i));
191
 
      }
192
 
    }
193
 
 
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++) {
197
 
      if (i%2 == 0) {
198
 
        assertTrue(docsWithField.get(i));
199
 
        assertEquals(i, numInts[i]);
200
 
      } else {
201
 
        assertFalse(docsWithField.get(i));
202
 
      }
203
 
    }
204
 
  }
205
 
  
206
 
  public void testGetDocsWithFieldThreadSafety() throws Exception {
207
 
    final FieldCache cache = FieldCache.DEFAULT;
208
 
    cache.purgeAllCaches();
209
 
 
210
 
    int NUM_THREADS = 3;
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,
216
 
                                                    new Runnable() {
217
 
                                                      // @Override not until java 1.6
218
 
                                                      public void run() {
219
 
                                                        cache.purgeAllCaches();
220
 
                                                        iters.incrementAndGet();
221
 
                                                      }
222
 
                                                    });
223
 
    for(int threadIDX=0;threadIDX<NUM_THREADS;threadIDX++) {
224
 
      threads[threadIDX] = new Thread() {
225
 
          @Override
226
 
          public void run() {
227
 
 
228
 
            try {
229
 
              while(!failed.get()) {
230
 
                final int op = random.nextInt(3);
231
 
                if (op == 0) {
232
 
                  // Purge all caches & resume, once all
233
 
                  // threads get here:
234
 
                  restart.await();
235
 
                  if (iters.get() >= NUM_ITER) {
236
 
                    break;
237
 
                  }
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));
242
 
                  }
243
 
                } else {
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++) {
247
 
                    if (i%2 == 0) {
248
 
                      assertTrue(docsWithField.get(i));
249
 
                      assertEquals(i, ints[i]);
250
 
                    } else {
251
 
                      assertFalse(docsWithField.get(i));
252
 
                    }
253
 
                  }
254
 
                }
255
 
              }
256
 
            } catch (Throwable t) {
257
 
              failed.set(true);
258
 
              restart.reset();
259
 
              throw new RuntimeException(t);
260
 
            }
261
 
          }
262
 
        };
263
 
      threads[threadIDX].start();
264
 
    }
265
 
 
266
 
    for(int threadIDX=0;threadIDX<NUM_THREADS;threadIDX++) {
267
 
      threads[threadIDX].join();
268
 
    }
269
 
    assertFalse(failed.get());
270
 
  }
271
 
}