forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorterTestBase.cs
More file actions
436 lines (391 loc) · 17.6 KB
/
Copy pathSorterTestBase.cs
File metadata and controls
436 lines (391 loc) · 17.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using J2N.Collections.Generic.Extensions;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using Lucene.Net.Search;
using Lucene.Net.Search.Similarities;
using Lucene.Net.Store;
using Lucene.Net.Support;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Index.Sorter
{
/*
* 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.
*/
[SuppressCodecs("Lucene3x")]
public abstract class SorterTestBase : LuceneTestCase
{
internal class NormsSimilarity : Similarity
{
private readonly Similarity @in;
public NormsSimilarity(Similarity @in)
{
this.@in = @in;
}
public override long ComputeNorm(FieldInvertState state)
{
if (state.Name.Equals(NORMS_FIELD, StringComparison.Ordinal))
{
return J2N.BitConversion.SingleToInt32Bits(state.Boost);
}
else
{
return @in.ComputeNorm(state);
}
}
public override SimWeight ComputeWeight(float queryBoost, CollectionStatistics collectionStats, params TermStatistics[] termStats)
{
return @in.ComputeWeight(queryBoost, collectionStats, termStats);
}
public override SimScorer GetSimScorer(SimWeight weight, AtomicReaderContext context)
{
return @in.GetSimScorer(weight, context);
}
}
internal sealed class PositionsTokenStream : TokenStream
{
private readonly ICharTermAttribute term;
private readonly IPayloadAttribute payload;
private readonly IOffsetAttribute offset;
private int pos, off;
public PositionsTokenStream()
{
term = AddAttribute<ICharTermAttribute>();
payload = AddAttribute<IPayloadAttribute>();
offset = AddAttribute<IOffsetAttribute>();
}
public override bool IncrementToken()
{
if (pos == 0)
{
return false;
}
ClearAttributes();
term.Append(DOC_POSITIONS_TERM);
payload.Payload = new BytesRef(pos.ToString());
offset.SetOffset(off, off);
--pos;
++off;
return true;
}
internal void SetId(int id)
{
pos = id / 10 + 1;
off = 0;
}
}
protected static readonly string ID_FIELD = "id";
protected static readonly string DOCS_ENUM_FIELD = "docs";
protected static readonly string DOCS_ENUM_TERM = "$all$";
protected static readonly string DOC_POSITIONS_FIELD = "positions";
protected static readonly string DOC_POSITIONS_TERM = "$all$";
protected static readonly string NUMERIC_DV_FIELD = "numeric";
protected static readonly string NORMS_FIELD = "norm";
protected static readonly string BINARY_DV_FIELD = "binary";
protected static readonly string SORTED_DV_FIELD = "sorted";
protected static readonly string SORTED_SET_DV_FIELD = "sorted_set";
protected static readonly string TERM_VECTORS_FIELD = "term_vectors";
// LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
private static readonly FieldType TERM_VECTORS_TYPE = new FieldType(TextField.TYPE_NOT_STORED) { StoreTermVectors = true }.Freeze();
private static readonly FieldType POSITIONS_TYPE = new FieldType(TextField.TYPE_NOT_STORED) { IndexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS }.Freeze();
protected static Directory dir;
protected static AtomicReader reader;
protected static int[] sortedValues;
private static Document Doc(int id, PositionsTokenStream positions)
{
Document doc = new Document();
doc.Add(new StringField(ID_FIELD, id.ToString(), Field.Store.YES));
doc.Add(new StringField(DOCS_ENUM_FIELD, DOCS_ENUM_TERM, Field.Store.NO));
positions.SetId(id);
if (DoesntSupportOffsets.Contains(TestUtil.GetPostingsFormat(DOC_POSITIONS_FIELD)))
{
// codec doesn't support offsets: just index positions for the field
doc.Add(new Field(DOC_POSITIONS_FIELD, positions, TextField.TYPE_NOT_STORED));
}
else
{
doc.Add(new Field(DOC_POSITIONS_FIELD, positions, POSITIONS_TYPE));
}
doc.Add(new NumericDocValuesField(NUMERIC_DV_FIELD, id));
TextField norms = new TextField(NORMS_FIELD, id.ToString(), Field.Store.NO);
norms.Boost = (J2N.BitConversion.Int32BitsToSingle(id));
doc.Add(norms);
doc.Add(new BinaryDocValuesField(BINARY_DV_FIELD, new BytesRef(id.ToString())));
doc.Add(new SortedDocValuesField(SORTED_DV_FIELD, new BytesRef(id.ToString())));
if (DefaultCodecSupportsSortedSet)
{
doc.Add(new SortedSetDocValuesField(SORTED_SET_DV_FIELD, new BytesRef(id.ToString())));
doc.Add(new SortedSetDocValuesField(SORTED_SET_DV_FIELD, new BytesRef((id + 1).ToString())));
}
doc.Add(new Field(TERM_VECTORS_FIELD, id.ToString(), TERM_VECTORS_TYPE));
return doc;
}
/** Creates an index for sorting. */
public void CreateIndex(Directory dir, int numDocs, Random random)
{
IList<int> ids = new JCG.List<int>();
for (int i = 0; i < numDocs; i++)
{
ids.Add(i * 10);
}
// shuffle them for indexing
ids.Shuffle(Random);
if (Verbose)
{
Console.WriteLine("Shuffled IDs for indexing: " + Collections.ToString(ids));
}
PositionsTokenStream positions = new PositionsTokenStream();
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
conf.SetMaxBufferedDocs(4); // create some segments
conf.SetSimilarity(new NormsSimilarity(conf.Similarity)); // for testing norms field
using RandomIndexWriter writer = new RandomIndexWriter(random, dir, conf);
writer.DoRandomForceMerge = (false);
foreach (int id in ids)
{
writer.AddDocument(Doc(id, positions));
}
// delete some documents
writer.Commit();
foreach (int id in ids)
{
if (random.NextDouble() < 0.2)
{
if (Verbose)
{
Console.WriteLine("delete doc_id " + id);
}
writer.DeleteDocuments(new Term(ID_FIELD, id.ToString()));
}
}
}
[OneTimeSetUp]
public override void OneTimeSetUp() // LUCENENET specific - renamed from BeforeClassSorterTestBase() to ensure calling order vs base class
{
base.OneTimeSetUp();
dir = NewDirectory();
int numDocs = AtLeast(20);
CreateIndex(dir, numDocs, Random);
reader = SlowCompositeReaderWrapper.Wrap(DirectoryReader.Open(dir));
}
[OneTimeTearDown]
public override void OneTimeTearDown() // LUCENENET specific - renamed from AfterClassSorterTestBase() to ensure calling order vs base class
{
reader.Dispose();
dir.Dispose();
base.OneTimeTearDown();
}
[Test]
public virtual void TestBinaryDocValuesField()
{
BinaryDocValues dv = reader.GetBinaryDocValues(BINARY_DV_FIELD);
BytesRef bytes = new BytesRef();
for (int i = 0; i < reader.MaxDoc; i++)
{
dv.Get(i, bytes);
assertEquals("incorrect binary DocValues for doc " + i, sortedValues[i].ToString(), bytes.Utf8ToString());
}
}
[Test]
public virtual void TestDocsAndPositionsEnum()
{
TermsEnum termsEnum = reader.GetTerms(DOC_POSITIONS_FIELD).GetEnumerator();
assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.SeekCeil(new BytesRef(DOC_POSITIONS_TERM)));
DocsAndPositionsEnum sortedPositions = termsEnum.DocsAndPositions(null, null);
int doc;
// test nextDoc()
while ((doc = sortedPositions.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS)
{
int freq = sortedPositions.Freq;
assertEquals("incorrect freq for doc=" + doc, sortedValues[doc] / 10 + 1, freq);
for (int i = 0; i < freq; i++)
{
assertEquals("incorrect position for doc=" + doc, i, sortedPositions.NextPosition());
if (!DoesntSupportOffsets.Contains(TestUtil.GetPostingsFormat(DOC_POSITIONS_FIELD)))
{
assertEquals("incorrect startOffset for doc=" + doc, i, sortedPositions.StartOffset);
assertEquals("incorrect endOffset for doc=" + doc, i, sortedPositions.EndOffset);
}
assertEquals("incorrect payload for doc=" + doc, freq - i, int.Parse(sortedPositions.GetPayload().Utf8ToString(), CultureInfo.InvariantCulture));
}
}
// test advance()
DocsAndPositionsEnum reuse = sortedPositions;
sortedPositions = termsEnum.DocsAndPositions(null, reuse);
if (sortedPositions is SortingAtomicReader.SortingDocsAndPositionsEnum positionsEnum)
{
assertTrue(positionsEnum.Reused(reuse)); // make sure reuse worked
}
doc = 0;
while ((doc = sortedPositions.Advance(doc + TestUtil.NextInt32(Random, 1, 5))) != DocIdSetIterator.NO_MORE_DOCS)
{
int freq = sortedPositions.Freq;
assertEquals("incorrect freq for doc=" + doc, sortedValues[doc] / 10 + 1, freq);
for (int i = 0; i < freq; i++)
{
assertEquals("incorrect position for doc=" + doc, i, sortedPositions.NextPosition());
if (!DoesntSupportOffsets.Contains(TestUtil.GetPostingsFormat(DOC_POSITIONS_FIELD)))
{
assertEquals("incorrect startOffset for doc=" + doc, i, sortedPositions.StartOffset);
assertEquals("incorrect endOffset for doc=" + doc, i, sortedPositions.EndOffset);
}
assertEquals("incorrect payload for doc=" + doc, freq - i, int.Parse(sortedPositions.GetPayload().Utf8ToString(), CultureInfo.InvariantCulture));
}
}
}
internal IBits RandomLiveDocs(int maxDoc)
{
if (Rarely())
{
if (Random.nextBoolean())
{
return null;
}
else
{
return new Bits.MatchNoBits(maxDoc);
}
}
FixedBitSet bits = new FixedBitSet(maxDoc);
int bitsSet = TestUtil.NextInt32(Random, 1, maxDoc - 1);
for (int i = 0; i < bitsSet; ++i)
{
while (true)
{
int index = Random.nextInt(maxDoc);
if (!bits.Get(index))
{
bits.Set(index);
break;
}
}
}
return bits;
}
[Test]
public virtual void TestDocsEnum()
{
IBits mappedLiveDocs = RandomLiveDocs(reader.MaxDoc);
TermsEnum termsEnum = reader.GetTerms(DOCS_ENUM_FIELD).GetEnumerator();
assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.SeekCeil(new BytesRef(DOCS_ENUM_TERM)));
DocsEnum docs = termsEnum.Docs(mappedLiveDocs, null);
int doc;
int prev = -1;
while ((doc = docs.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS)
{
assertTrue("document " + doc + " marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(doc));
assertEquals("incorrect value; doc " + doc, sortedValues[doc], int.Parse(reader.Document(doc).Get(ID_FIELD)));
while (++prev < doc)
{
assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(prev));
}
}
while (++prev < reader.MaxDoc)
{
assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(prev));
}
DocsEnum reuse = docs;
docs = termsEnum.Docs(mappedLiveDocs, reuse);
if (docs is SortingAtomicReader.SortingDocsEnum sortingDocsEnum)
{
assertTrue(sortingDocsEnum.Reused(reuse)); // make sure reuse worked
}
doc = -1;
prev = -1;
while ((doc = docs.Advance(doc + 1)) != DocIdSetIterator.NO_MORE_DOCS)
{
assertTrue("document " + doc + " marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(doc));
assertEquals("incorrect value; doc " + doc, sortedValues[doc], int.Parse(reader.Document(doc).Get(ID_FIELD)));
while (++prev < doc)
{
assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(prev));
}
}
while (++prev < reader.MaxDoc)
{
assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs is null || mappedLiveDocs.Get(prev));
}
}
[Test]
public virtual void TestNormValues()
{
NumericDocValues dv = reader.GetNormValues(NORMS_FIELD);
int maxDoc = reader.MaxDoc;
for (int i = 0; i < maxDoc; i++)
{
assertEquals("incorrect norm value for doc " + i, sortedValues[i], dv.Get(i));
}
}
[Test]
public virtual void TestNumericDocValuesField()
{
NumericDocValues dv = reader.GetNumericDocValues(NUMERIC_DV_FIELD);
int maxDoc = reader.MaxDoc;
for (int i = 0; i < maxDoc; i++)
{
assertEquals("incorrect numeric DocValues for doc " + i, sortedValues[i], dv.Get(i));
}
}
[Test]
public virtual void TestSortedDocValuesField()
{
SortedDocValues dv = reader.GetSortedDocValues(SORTED_DV_FIELD);
int maxDoc = reader.MaxDoc;
BytesRef bytes = new BytesRef();
for (int i = 0; i < maxDoc; i++)
{
dv.Get(i, bytes);
assertEquals("incorrect sorted DocValues for doc " + i, sortedValues[i].ToString(), bytes.Utf8ToString());
}
}
[Test]
public virtual void TestSortedSetDocValuesField()
{
AssumeTrue("default codec does not support SORTED_SET", DefaultCodecSupportsSortedSet);
SortedSetDocValues dv = reader.GetSortedSetDocValues(SORTED_SET_DV_FIELD);
int maxDoc = reader.MaxDoc;
BytesRef bytes = new BytesRef();
for (int i = 0; i < maxDoc; i++)
{
dv.SetDocument(i);
dv.LookupOrd(dv.NextOrd(), bytes);
int value = sortedValues[i];
assertEquals("incorrect sorted-set DocValues for doc " + i, value.toString(), bytes.Utf8ToString());
dv.LookupOrd(dv.NextOrd(), bytes);
assertEquals("incorrect sorted-set DocValues for doc " + i, (value + 1).ToString(), bytes.Utf8ToString());
assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.NextOrd());
}
}
[Test]
public virtual void TestTermVectors()
{
int maxDoc = reader.MaxDoc;
for (int i = 0; i < maxDoc; i++)
{
Terms terms = reader.GetTermVector(i, TERM_VECTORS_FIELD);
assertNotNull("term vectors not found for doc " + i + " field [" + TERM_VECTORS_FIELD + "]", terms);
var iter = terms.GetEnumerator();
iter.MoveNext();
assertEquals("incorrect term vector for doc " + i, sortedValues[i].toString(), iter.Term.Utf8ToString());
}
}
}
}