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 java.io.IOException;
21
import java.util.Random;
23
import org.apache.lucene.analysis.MockAnalyzer;
24
import org.apache.lucene.document.Document;
25
import org.apache.lucene.document.Field;
26
import org.apache.lucene.index.IndexReader;
27
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
28
import org.apache.lucene.index.RandomIndexWriter;
29
import org.apache.lucene.store.Directory;
30
import org.apache.lucene.util.LuceneTestCase;
31
import org.apache.lucene.util._TestUtil;
32
import org.junit.AfterClass;
33
import org.junit.BeforeClass;
34
import org.junit.Test;
36
public class BaseTestRangeFilter extends LuceneTestCase {
38
public static final boolean F = false;
39
public static final boolean T = true;
42
* Collation interacts badly with hyphens -- collation produces different
43
* ordering than Unicode code-point ordering -- so two indexes are created:
44
* one which can't have negative random integers, for testing collated ranges,
45
* and the other which can have negative random integers, for all other tests.
47
static class TestIndex {
50
boolean allowNegativeRandomInts;
53
TestIndex(Random random, int minR, int maxR, boolean allowNegativeRandomInts) {
56
this.allowNegativeRandomInts = allowNegativeRandomInts;
58
index = newDirectory(random);
59
} catch (IOException e) { throw new RuntimeException(e); }
63
static IndexReader signedIndexReader;
64
static IndexReader unsignedIndexReader;
66
static TestIndex signedIndexDir;
67
static TestIndex unsignedIndexDir;
72
static final int intLength = Integer.toString(Integer.MAX_VALUE).length();
75
* a simple padding function that should work with any int
77
public static String pad(int n) {
78
StringBuilder b = new StringBuilder(40);
82
n = Integer.MAX_VALUE + n + 1;
85
String s = Integer.toString(n);
86
for (int i = s.length(); i <= intLength; i++) {
95
public static void beforeClassBaseTestRangeFilter() throws Exception {
97
signedIndexDir = new TestIndex(random, Integer.MAX_VALUE, Integer.MIN_VALUE, true);
98
unsignedIndexDir = new TestIndex(random, Integer.MAX_VALUE, 0, false);
99
signedIndexReader = build(random, signedIndexDir);
100
unsignedIndexReader = build(random, unsignedIndexDir);
104
public static void afterClassBaseTestRangeFilter() throws Exception {
105
signedIndexReader.close();
106
unsignedIndexReader.close();
107
signedIndexDir.index.close();
108
unsignedIndexDir.index.close();
109
signedIndexReader = null;
110
unsignedIndexReader = null;
111
signedIndexDir = null;
112
unsignedIndexDir = null;
115
private static IndexReader build(Random random, TestIndex index) throws IOException {
118
Document doc = new Document();
119
Field idField = newField(random, "id", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
120
Field randField = newField(random, "rand", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
121
Field bodyField = newField(random, "body", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
126
RandomIndexWriter writer = new RandomIndexWriter(random, index.index,
127
newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random))
128
.setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)).setMergePolicy(newLogMergePolicy()));
129
_TestUtil.reduceOpenFiles(writer.w);
135
for (int d = minId; d <= maxId; d++) {
136
idField.setValue(pad(d));
137
int r = index.allowNegativeRandomInts ? random.nextInt() : random
138
.nextInt(Integer.MAX_VALUE);
139
if (index.maxR < r) {
142
} else if (index.maxR == r) {
146
if (r < index.minR) {
149
} else if (r == index.minR) {
152
randField.setValue(pad(r));
153
bodyField.setValue("body");
154
writer.addDocument(doc);
157
if (minCount == 1 && maxCount == 1) {
158
// our subclasses rely on only 1 doc having the min or
159
// max, so, we loop until we satisfy that. it should be
160
// exceedingly rare (Yonik calculates 1 in ~429,000)
161
// times) that this loop requires more than one try:
162
IndexReader ir = writer.getReader();
173
public void testPad() {
175
int[] tests = new int[] {-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000,
177
for (int i = 0; i < tests.length - 1; i++) {
179
int b = tests[i + 1];
182
String label = a + ":" + aa + " vs " + b + ":" + bb;
183
assertEquals("length of " + label, aa.length(), bb.length());
184
assertTrue("compare less than " + label, aa.compareTo(bb) < 0);