forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedGapTermsIndexReader.cs
More file actions
496 lines (417 loc) · 20.7 KB
/
Copy pathFixedGapTermsIndexReader.cs
File metadata and controls
496 lines (417 loc) · 20.7 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Support;
using Lucene.Net.Util;
using Lucene.Net.Util.Packed;
using System.Collections.Generic;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Codecs.BlockTerms
{
/*
* 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.
*/
/// <summary>
/// <see cref="TermsIndexReaderBase"/> for simple every Nth terms indexes.
/// <para/>
/// @lucene.experimental
/// </summary>
/// <seealso cref="FixedGapTermsIndexWriter"/>
public class FixedGapTermsIndexReader : TermsIndexReaderBase
{
// NOTE: long is overkill here, since this number is 128
// by default and only indexDivisor * 128 if you change
// the indexDivisor at search time. But, we use this in a
// number of places to multiply out the actual ord, and we
// will overflow int during those multiplies. So to avoid
// having to upgrade each multiple to long in multiple
// places (error prone), we use long here:
private readonly long totalIndexInterval;
private readonly int indexDivisor;
private readonly int indexInterval;
// Closed if indexLoaded is true:
private readonly IndexInput input;
private readonly /*volatile*/ bool indexLoaded;
private readonly IComparer<BytesRef> termComp;
private const int PAGED_BYTES_BITS = 15;
// all fields share this single logical byte[]
private readonly PagedBytes termBytes = new PagedBytes(PAGED_BYTES_BITS);
private readonly PagedBytes.Reader termBytesReader;
private readonly IDictionary<FieldInfo, FieldIndexData> fields = new JCG.Dictionary<FieldInfo, FieldIndexData>();
// start of the field info data
private long dirOffset;
private readonly int version;
public FixedGapTermsIndexReader(Directory dir, FieldInfos fieldInfos, string segment, int indexDivisor,
IComparer<BytesRef> termComp, string segmentSuffix, IOContext context)
{
this.termComp = termComp;
if (Debugging.AssertsEnabled) Debugging.Assert(indexDivisor == -1 || indexDivisor > 0);
input = dir.OpenInput(IndexFileNames.SegmentFileName(segment, segmentSuffix, FixedGapTermsIndexWriter.TERMS_INDEX_EXTENSION), context);
bool success = false;
try
{
version = ReadHeader(input);
if (version >= FixedGapTermsIndexWriter.VERSION_CHECKSUM)
{
CodecUtil.ChecksumEntireFile(input);
}
indexInterval = input.ReadInt32();
if (indexInterval < 1)
{
throw new CorruptIndexException("invalid indexInterval: " + indexInterval + " (resource=" + input + ")");
}
this.indexDivisor = indexDivisor;
if (indexDivisor < 0)
{
totalIndexInterval = indexInterval;
}
else
{
// In case terms index gets loaded, later, on demand
totalIndexInterval = indexInterval * indexDivisor;
}
if (Debugging.AssertsEnabled) Debugging.Assert(totalIndexInterval > 0);
SeekDir(input, dirOffset);
// Read directory
int numFields = input.ReadVInt32();
if (numFields < 0)
{
throw new CorruptIndexException("invalid numFields: " + numFields + " (resource=" + input + ")");
}
//System.out.println("FGR: init seg=" + segment + " div=" + indexDivisor + " nF=" + numFields);
for (int i = 0; i < numFields; i++)
{
int field = input.ReadVInt32();
int numIndexTerms = input.ReadVInt32();
if (numIndexTerms < 0)
{
throw new CorruptIndexException("invalid numIndexTerms: " + numIndexTerms + " (resource=" + input + ")");
}
long termsStart = input.ReadVInt64();
long indexStart = input.ReadVInt64();
long packedIndexStart = input.ReadVInt64();
long packedOffsetsStart = input.ReadVInt64();
if (packedIndexStart < indexStart)
{
throw new CorruptIndexException("invalid packedIndexStart: " + packedIndexStart + " indexStart: " + indexStart + "numIndexTerms: " + numIndexTerms + " (resource=" + input + ")");
}
FieldInfo fieldInfo = fieldInfos.FieldInfo(field);
FieldIndexData previous = fields.Put(fieldInfo, new FieldIndexData(this, /* fieldInfo, // LUCENENET: Not referenced */
numIndexTerms, indexStart, termsStart, packedIndexStart, packedOffsetsStart));
if (previous != null)
{
throw new CorruptIndexException("duplicate field: " + fieldInfo.Name + " (resource=" + input + ")");
}
}
success = true;
}
finally
{
if (!success)
{
IOUtils.DisposeWhileHandlingException(input);
}
if (indexDivisor > 0)
{
input.Dispose();
input = null;
if (success)
{
indexLoaded = true;
}
termBytesReader = termBytes.Freeze(true);
}
}
}
public override int Divisor => indexDivisor;
private int ReadHeader(DataInput input)
{
int version = CodecUtil.CheckHeader(input, FixedGapTermsIndexWriter.CODEC_NAME,
FixedGapTermsIndexWriter.VERSION_START, FixedGapTermsIndexWriter.VERSION_CURRENT);
if (version < FixedGapTermsIndexWriter.VERSION_APPEND_ONLY)
{
dirOffset = input.ReadInt64();
}
return version;
}
private class IndexEnum : FieldIndexEnum
{
// Outer intstance
private readonly FixedGapTermsIndexReader outerInstance;
private readonly FieldIndexData.CoreFieldIndex fieldIndex;
private readonly BytesRef term = new BytesRef();
private long ord;
public IndexEnum(FixedGapTermsIndexReader outerInstance, FieldIndexData.CoreFieldIndex fieldIndex)
{
this.outerInstance = outerInstance;
this.fieldIndex = fieldIndex;
}
public override sealed BytesRef Term => term;
public override long Seek(BytesRef target)
{
int lo = 0; // binary search
int hi = fieldIndex.numIndexTerms - 1;
if (Debugging.AssertsEnabled) Debugging.Assert(outerInstance.totalIndexInterval > 0, "totalIndexInterval={0}", outerInstance.totalIndexInterval);
while (hi >= lo)
{
int mid = (lo + hi) >>> 1;
long offset2 = fieldIndex.termOffsets.Get(mid);
int length2 = (int)(fieldIndex.termOffsets.Get(1 + mid) - offset2);
outerInstance.termBytesReader.FillSlice(term, fieldIndex.termBytesStart + offset2, length2);
int delta = outerInstance.termComp.Compare(target, term);
if (delta < 0)
{
hi = mid - 1;
}
else if (delta > 0)
{
lo = mid + 1;
}
else
{
if (Debugging.AssertsEnabled) Debugging.Assert(mid >= 0);
ord = mid * outerInstance.totalIndexInterval;
return fieldIndex.termsStart + fieldIndex.termsDictOffsets.Get(mid);
}
}
if (hi < 0)
{
if (Debugging.AssertsEnabled) Debugging.Assert(hi == -1);
hi = 0;
}
long offset = fieldIndex.termOffsets.Get(hi);
int length = (int)(fieldIndex.termOffsets.Get(1 + hi) - offset);
outerInstance.termBytesReader.FillSlice(term, fieldIndex.termBytesStart + offset, length);
ord = hi * outerInstance.totalIndexInterval;
return fieldIndex.termsStart + fieldIndex.termsDictOffsets.Get(hi);
}
public override long Next()
{
int idx = 1 + (int)(ord / outerInstance.totalIndexInterval);
if (idx >= fieldIndex.numIndexTerms)
{
return -1;
}
ord += outerInstance.totalIndexInterval;
long offset = fieldIndex.termOffsets.Get(idx);
int length = (int)(fieldIndex.termOffsets.Get(1 + idx) - offset);
outerInstance.termBytesReader.FillSlice(term, fieldIndex.termBytesStart + offset, length);
return fieldIndex.termsStart + fieldIndex.termsDictOffsets.Get(idx);
}
public override long Ord => ord;
public override long Seek(long ord)
{
int idx = (int)(ord / outerInstance.totalIndexInterval);
// caller must ensure ord is in bounds
if (Debugging.AssertsEnabled) Debugging.Assert(idx < fieldIndex.numIndexTerms);
long offset = fieldIndex.termOffsets.Get(idx);
int length = (int)(fieldIndex.termOffsets.Get(1 + idx) - offset);
outerInstance.termBytesReader.FillSlice(term, fieldIndex.termBytesStart + offset, length);
this.ord = idx * outerInstance.totalIndexInterval;
return fieldIndex.termsStart + fieldIndex.termsDictOffsets.Get(idx);
}
}
public override bool SupportsOrd => true;
private class FieldIndexData
{
private readonly FixedGapTermsIndexReader outerInstance;
internal volatile CoreFieldIndex coreIndex;
private readonly long indexStart;
private readonly long termsStart;
private readonly long packedIndexStart;
private readonly long packedOffsetsStart;
private readonly int numIndexTerms;
public FieldIndexData(FixedGapTermsIndexReader outerInstance, /*FieldInfo fieldInfo, // LUCENENET: Not Referenced */
int numIndexTerms, long indexStart, long termsStart,
long packedIndexStart, long packedOffsetsStart)
{
this.outerInstance = outerInstance;
this.termsStart = termsStart;
this.indexStart = indexStart;
this.packedIndexStart = packedIndexStart;
this.packedOffsetsStart = packedOffsetsStart;
this.numIndexTerms = numIndexTerms;
if (outerInstance.indexDivisor > 0)
{
LoadTermsIndex();
}
}
private void LoadTermsIndex()
{
if (coreIndex is null)
{
coreIndex = new CoreFieldIndex(this, indexStart, termsStart, packedIndexStart, packedOffsetsStart, numIndexTerms);
}
}
internal sealed class CoreFieldIndex
{
// where this field's terms begin in the packed byte[]
// data
internal readonly long termBytesStart;
// offset into index termBytes
internal readonly PackedInt32s.Reader termOffsets;
// index pointers into main terms dict
internal readonly PackedInt32s.Reader termsDictOffsets;
internal readonly int numIndexTerms;
internal readonly long termsStart;
public CoreFieldIndex(FieldIndexData outerInstance, long indexStart, long termsStart, long packedIndexStart, long packedOffsetsStart,
int numIndexTerms)
{
this.termsStart = termsStart;
termBytesStart = outerInstance.outerInstance.termBytes.GetPointer();
IndexInput clone = (IndexInput)outerInstance.outerInstance.input.Clone();
clone.Seek(indexStart);
// -1 is passed to mean "don't load term index", but
// if we are then later loaded it's overwritten with
// a real value
if (Debugging.AssertsEnabled) Debugging.Assert(outerInstance.outerInstance.indexDivisor > 0);
this.numIndexTerms = 1 + (numIndexTerms - 1) / outerInstance.outerInstance.indexDivisor;
if (Debugging.AssertsEnabled) Debugging.Assert(this.numIndexTerms > 0, "numIndexTerms={0} indexDivisor={1}", numIndexTerms, outerInstance.outerInstance.indexDivisor);
if (outerInstance.outerInstance.indexDivisor == 1)
{
// Default (load all index terms) is fast -- slurp in the images from disk:
try
{
long numTermBytes = packedIndexStart - indexStart;
outerInstance.outerInstance.termBytes.Copy(clone, numTermBytes);
// records offsets into main terms dict file
termsDictOffsets = PackedInt32s.GetReader(clone);
if (Debugging.AssertsEnabled) Debugging.Assert(termsDictOffsets.Count == numIndexTerms);
// records offsets into byte[] term data
termOffsets = PackedInt32s.GetReader(clone);
if (Debugging.AssertsEnabled) Debugging.Assert(termOffsets.Count == 1 + numIndexTerms);
}
finally
{
clone.Dispose();
}
}
else
{
// Get packed iterators
IndexInput clone1 = (IndexInput)outerInstance.outerInstance.input.Clone();
IndexInput clone2 = (IndexInput)outerInstance.outerInstance.input.Clone();
try
{
// Subsample the index terms
clone1.Seek(packedIndexStart);
PackedInt32s.IReaderIterator termsDictOffsetsIter = PackedInt32s.GetReaderIterator(clone1, PackedInt32s.DEFAULT_BUFFER_SIZE);
clone2.Seek(packedOffsetsStart);
PackedInt32s.IReaderIterator termOffsetsIter = PackedInt32s.GetReaderIterator(clone2, PackedInt32s.DEFAULT_BUFFER_SIZE);
// TODO: often we can get by w/ fewer bits per
// value, below.. .but this would be more complex:
// we'd have to try @ fewer bits and then grow
// if we overflowed it.
PackedInt32s.Mutable termsDictOffsetsM = PackedInt32s.GetMutable(this.numIndexTerms, termsDictOffsetsIter.BitsPerValue, PackedInt32s.DEFAULT);
PackedInt32s.Mutable termOffsetsM = PackedInt32s.GetMutable(this.numIndexTerms + 1, termOffsetsIter.BitsPerValue, PackedInt32s.DEFAULT);
termsDictOffsets = termsDictOffsetsM;
termOffsets = termOffsetsM;
int upto = 0;
long termOffsetUpto = 0;
while (upto < this.numIndexTerms)
{
// main file offset copies straight over
termsDictOffsetsM.Set(upto, termsDictOffsetsIter.Next());
termOffsetsM.Set(upto, termOffsetUpto);
long termOffset = termOffsetsIter.Next();
long nextTermOffset = termOffsetsIter.Next();
int numTermBytes = (int)(nextTermOffset - termOffset);
clone.Seek(indexStart + termOffset);
if (Debugging.AssertsEnabled)
{
Debugging.Assert(indexStart + termOffset < clone.Length, "indexStart={0} termOffset={1} len={2}", indexStart, termOffset, clone.Length);
Debugging.Assert(indexStart + termOffset + numTermBytes < clone.Length);
}
outerInstance.outerInstance.termBytes.Copy(clone, numTermBytes);
termOffsetUpto += numTermBytes;
upto++;
if (upto == this.numIndexTerms)
{
break;
}
// skip terms:
termsDictOffsetsIter.Next();
for (int i = 0; i < outerInstance.outerInstance.indexDivisor - 2; i++)
{
termOffsetsIter.Next();
termsDictOffsetsIter.Next();
}
}
termOffsetsM.Set(upto, termOffsetUpto);
}
finally
{
clone1.Dispose();
clone2.Dispose();
clone.Dispose();
}
}
}
/// <summary>Returns approximate RAM bytes used.</summary>
public long RamBytesUsed()
{
return ((termOffsets != null) ? termOffsets.RamBytesUsed() : 0) +
((termsDictOffsets != null) ? termsDictOffsets.RamBytesUsed() : 0);
}
}
}
public override FieldIndexEnum GetFieldEnum(FieldInfo fieldInfo)
{
if (!fields.TryGetValue(fieldInfo, out FieldIndexData fieldData) || fieldData is null || fieldData.coreIndex is null)
{
return null;
}
else
{
return new IndexEnum(this, fieldData.coreIndex);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (input != null && !indexLoaded)
{
input.Dispose();
}
}
}
private void SeekDir(IndexInput input, long dirOffset)
{
if (version >= FixedGapTermsIndexWriter.VERSION_CHECKSUM)
{
input.Seek(input.Length - CodecUtil.FooterLength() - 8);
dirOffset = input.ReadInt64();
}
else if (version >= FixedGapTermsIndexWriter.VERSION_APPEND_ONLY)
{
input.Seek(input.Length - 8);
dirOffset = input.ReadInt64();
}
input.Seek(dirOffset);
}
public override long RamBytesUsed()
{
long sizeInBytes = ((termBytes != null) ? termBytes.RamBytesUsed() : 0) +
((termBytesReader != null) ? termBytesReader.RamBytesUsed() : 0);
foreach (FieldIndexData entry in fields.Values)
{
sizeInBytes += entry.coreIndex.RamBytesUsed();
}
return sizeInBytes;
}
}
}