forked from apache/lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestSTBlockReader.java
369 lines (320 loc) · 11.5 KB
/
TestSTBlockReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.codecs.uniformsplit.sharedterms;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.codecs.BlockTermState;
import org.apache.lucene.codecs.PostingsReaderBase;
import org.apache.lucene.codecs.lucene90.tests.MockTermStateFactory;
import org.apache.lucene.codecs.uniformsplit.BlockHeader;
import org.apache.lucene.codecs.uniformsplit.BlockLine;
import org.apache.lucene.codecs.uniformsplit.FSTDictionary;
import org.apache.lucene.codecs.uniformsplit.FieldMetadata;
import org.apache.lucene.codecs.uniformsplit.IndexDictionary;
import org.apache.lucene.codecs.uniformsplit.TermBytes;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.BytesRef;
public class TestSTBlockReader extends LuceneTestCase {
private static final String MOCK_BLOCK_OUTPUT_NAME = "TestSTBlockReader.tmp";
private FieldInfos fieldInfos;
private List<MockSTBlockLine> blockLines;
private IndexDictionary.BrowserSupplier supplier;
private ByteBuffersDirectory directory;
@Override
public void setUp() throws Exception {
super.setUp();
fieldInfos = mockFieldInfos();
List<BlockLineDefinition> vocab = new ArrayList<>();
vocab.add(blockLineDef(1, "abaco", "f1", "f3"));
vocab.add(blockLineDef(2, "amiga", "f1", "f2", "f4"));
vocab.add(blockLineDef(5, "amigo", "f1", "f2", "f3", "f4"));
vocab.add(blockLineDef(2, "arco", "f1"));
vocab.add(blockLineDef(1, "bloom", "f2"));
vocab.add(blockLineDef(1, "frien", "f2"));
vocab.add(blockLineDef(6, "frienchies", "f3"));
blockLines = generateBlockLines(vocab);
directory = new ByteBuffersDirectory();
try (IndexOutput blockOutput =
directory.createOutput(MOCK_BLOCK_OUTPUT_NAME, IOContext.DEFAULT)) {
blockOutput.writeVInt(5);
}
IndexDictionary.Builder builder = new FSTDictionary.Builder();
builder.add(new BytesRef("a"), 0);
IndexDictionary indexDictionary = builder.build();
supplier =
new IndexDictionary.BrowserSupplier() {
@Override
public IndexDictionary.Browser get() throws IOException {
return indexDictionary.browser();
}
};
}
@Override
public void tearDown() throws Exception {
try {
blockLines.clear();
directory.close();
} finally {
super.tearDown();
}
}
public void testSeekExactIgnoreFieldF1() throws IOException {
// when block reader for field 1 -> f1
MockSTBlockReader blockReader =
new MockSTBlockReader(
supplier,
blockLines,
directory,
fieldInfos.fieldInfo("f1"), // last term "arco"
fieldInfos);
// when seekCeil
blockReader.seekCeil(new BytesRef("arco2"));
// then
assertNull(blockReader.term());
// when seekCeilIgnoreField
blockReader.seekCeilIgnoreField(new BytesRef("arco2"));
// then
assertEquals("bloom", blockReader.term().utf8ToString());
}
public void testSeekExactIgnoreFieldF2() throws IOException {
MockSTBlockReader blockReader =
new MockSTBlockReader(
supplier,
blockLines,
directory,
fieldInfos.fieldInfo("f2"), // last term "frien"
fieldInfos);
// when seekCeil
blockReader.seekCeilIgnoreField(new BytesRef("arco2"));
// then
assertEquals("bloom", blockReader.term().utf8ToString());
}
public void testSeekExactIgnoreFieldF3() throws IOException {
MockSTBlockReader blockReader =
new MockSTBlockReader(
supplier,
blockLines,
directory,
fieldInfos.fieldInfo("f3"), // last term "frienchies"
fieldInfos);
// when seekCeilIgnoreField
blockReader.seekCeilIgnoreField(new BytesRef("arco2"));
// then
assertEquals("bloom", blockReader.term().utf8ToString());
// when seekCeil
blockReader.seekCeil(new BytesRef("arco2"));
// then
assertEquals("frienchies", blockReader.term().utf8ToString());
}
public void testSeekExactIgnoreFieldF4() throws IOException {
MockSTBlockReader blockReader =
new MockSTBlockReader(
supplier,
blockLines,
directory,
fieldInfos.fieldInfo("f4"), // last term "amigo"
fieldInfos);
// when seekCeilIgnoreField
blockReader.seekCeilIgnoreField(new BytesRef("abaco"));
// then
assertEquals("abaco", blockReader.term().utf8ToString());
// when seekCeil
blockReader.seekCeil(new BytesRef("abaco"));
// then
assertEquals("amiga", blockReader.term().utf8ToString());
}
private static FieldInfos mockFieldInfos() {
return new FieldInfos(
new FieldInfo[] {
mockFieldInfo("f1", 0),
mockFieldInfo("f2", 1),
mockFieldInfo("f3", 2),
mockFieldInfo("f4", 3),
});
}
private static FieldInfo mockFieldInfo(String fieldName, int number) {
return new FieldInfo(
fieldName,
number,
false,
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.NONE,
-1,
Collections.emptyMap(),
0,
0,
0,
0,
VectorSimilarityFunction.EUCLIDEAN,
false);
}
private BlockLineDefinition blockLineDef(int mdpLength, String term, String... fields) {
return new BlockLineDefinition(
new TermBytes(mdpLength, new BytesRef(term)), Arrays.asList(fields));
}
private static List<MockSTBlockLine> generateBlockLines(
Iterable<BlockLineDefinition> blockLineDefinitions) {
List<MockSTBlockLine> lines = new ArrayList<>();
for (BlockLineDefinition blockLineDefinition : blockLineDefinitions) {
lines.add(new MockSTBlockLine(blockLineDefinition.termBytes, blockLineDefinition.fields));
}
return lines;
}
private static class BlockLineDefinition {
final TermBytes termBytes;
final List<String> fields;
BlockLineDefinition(TermBytes termBytes, List<String> fields) {
this.termBytes = termBytes;
this.fields = fields;
}
}
private static class MockSTBlockLine extends STBlockLine {
final Map<String, BlockTermState> termStates;
MockSTBlockLine(TermBytes termBytes, List<String> fields) {
super(termBytes, Collections.singletonList(new FieldMetadataTermState(null, null)));
this.termStates = new HashMap<>();
for (String field : fields) {
termStates.put(field, MockTermStateFactory.create());
}
}
Set<String> getFields() {
return termStates.keySet();
}
}
private static class MockSTBlockReader extends STBlockReader {
List<MockSTBlockLine> lines;
MockSTBlockReader(
IndexDictionary.BrowserSupplier supplier,
List<MockSTBlockLine> lines,
Directory directory,
FieldInfo fieldInfo,
FieldInfos fieldInfos)
throws IOException {
super(
supplier,
directory.openInput(MOCK_BLOCK_OUTPUT_NAME, IOContext.DEFAULT),
getMockPostingReaderBase(),
mockFieldMetadata(fieldInfo, getLastTermForField(lines, fieldInfo.name)),
null,
fieldInfos);
this.lines = lines;
}
static PostingsReaderBase getMockPostingReaderBase() {
return new PostingsReaderBase() {
@Override
public void init(IndexInput termsIn, SegmentReadState state) {}
@Override
public BlockTermState newTermState() {
return null;
}
@Override
public void decodeTerm(
DataInput in, FieldInfo fieldInfo, BlockTermState state, boolean absolute) {}
@Override
public PostingsEnum postings(
FieldInfo fieldInfo, BlockTermState state, PostingsEnum reuse, int flags) {
return null;
}
@Override
public ImpactsEnum impacts(FieldInfo fieldInfo, BlockTermState state, int flags) {
return null;
}
@Override
public void checkIntegrity() {}
@Override
public void close() {}
};
}
static FieldMetadata mockFieldMetadata(FieldInfo fieldInfo, BytesRef lastTerm) {
FieldMetadata fieldMetadata = new FieldMetadata(fieldInfo, 1);
fieldMetadata.setLastTerm(lastTerm);
fieldMetadata.setLastBlockStartFP(1);
return fieldMetadata;
}
static BytesRef getLastTermForField(List<MockSTBlockLine> lines, String fieldName) {
BytesRef lastTerm = null;
for (MockSTBlockLine line : lines) {
if (line.getFields().contains(fieldName)) {
lastTerm = line.getTermBytes().getTerm();
}
}
return lastTerm;
}
@Override
protected BlockTermState readTermState() {
return termState =
lines.get(lineIndexInBlock - 1).termStates.get(fieldMetadata.getFieldInfo().name);
}
@Override
protected int compareToMiddleAndJump(BytesRef searchedTerm) {
blockLine = lines.get(lines.size() >> 1);
lineIndexInBlock = blockHeader.getMiddleLineIndex();
int compare = searchedTerm.compareTo(term());
if (compare < 0) {
lineIndexInBlock = 0;
}
return compare;
}
@Override
protected BlockLine readLineInBlock() {
if (lineIndexInBlock >= lines.size()) {
return blockLine = null;
}
return blockLine = lines.get(lineIndexInBlock++);
}
@Override
protected void initializeHeader(BytesRef searchedTerm, long startBlockLinePos)
throws IOException {
// Force blockStartFP to an impossible value so we never trigger the optimization
// that keeps the current block with our mock block reader.
blockStartFP = -1;
super.initializeHeader(searchedTerm, startBlockLinePos);
}
@Override
protected BlockHeader readHeader() {
return blockHeader =
lineIndexInBlock >= lines.size() ? null : new MockBlockHeader(lines.size());
}
}
private static class MockBlockHeader extends BlockHeader {
MockBlockHeader(int linesCount) {
super(linesCount, 0, 0, 0, 1, 0);
}
}
}