-
Notifications
You must be signed in to change notification settings - Fork 624
/
Lucene41PostingsReader.cs
1717 lines (1538 loc) · 71.3 KB
/
Lucene41PostingsReader.cs
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using J2N.Numerics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Runtime.CompilerServices;
namespace Lucene.Net.Codecs.Lucene41
{
/*
* 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>
/// Concrete class that reads docId(maybe frq,pos,offset,payloads) list
/// with postings format.
/// <para/>
/// @lucene.experimental
/// </summary>
/// <seealso cref="Lucene41SkipReader"/>
public sealed class Lucene41PostingsReader : PostingsReaderBase
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private readonly IndexInput docIn;
private readonly IndexInput posIn;
private readonly IndexInput payIn;
#pragma warning restore CA2213 // Disposable fields should be disposed
private readonly ForUtil forUtil;
private readonly int version; // LUCENENET: marked readonly
// public static boolean DEBUG = false;
/// <summary>
/// Sole constructor. </summary>
public Lucene41PostingsReader(Directory dir, FieldInfos fieldInfos, SegmentInfo segmentInfo, IOContext ioContext, string segmentSuffix)
{
bool success = false;
IndexInput docIn = null;
IndexInput posIn = null;
IndexInput payIn = null;
try
{
docIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.DOC_EXTENSION), ioContext);
version = CodecUtil.CheckHeader(docIn, Lucene41PostingsWriter.DOC_CODEC, Lucene41PostingsWriter.VERSION_START, Lucene41PostingsWriter.VERSION_CURRENT);
forUtil = new ForUtil(docIn);
if (fieldInfos.HasProx)
{
posIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.POS_EXTENSION), ioContext);
CodecUtil.CheckHeader(posIn, Lucene41PostingsWriter.POS_CODEC, version, version);
if (fieldInfos.HasPayloads || fieldInfos.HasOffsets)
{
payIn = dir.OpenInput(IndexFileNames.SegmentFileName(segmentInfo.Name, segmentSuffix, Lucene41PostingsFormat.PAY_EXTENSION), ioContext);
CodecUtil.CheckHeader(payIn, Lucene41PostingsWriter.PAY_CODEC, version, version);
}
}
this.docIn = docIn;
this.posIn = posIn;
this.payIn = payIn;
success = true;
}
finally
{
if (!success)
{
IOUtils.DisposeWhileHandlingException(docIn, posIn, payIn);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init(IndexInput termsIn)
{
// Make sure we are talking to the matching postings writer
CodecUtil.CheckHeader(termsIn, Lucene41PostingsWriter.TERMS_CODEC, Lucene41PostingsWriter.VERSION_START, Lucene41PostingsWriter.VERSION_CURRENT);
int indexBlockSize = termsIn.ReadVInt32();
if (indexBlockSize != Lucene41PostingsFormat.BLOCK_SIZE)
{
throw IllegalStateException.Create("index-time BLOCK_SIZE (" + indexBlockSize + ") != read-time BLOCK_SIZE (" + Lucene41PostingsFormat.BLOCK_SIZE + ")");
}
}
/// <summary>
/// Read values that have been written using variable-length encoding instead of bit-packing.
/// <para/>
/// NOTE: This was readVIntBlock() in Lucene.
/// </summary>
internal static void ReadVInt32Block(IndexInput docIn, int[] docBuffer, int[] freqBuffer, int num, bool indexHasFreq)
{
if (indexHasFreq)
{
for (int i = 0; i < num; i++)
{
int code = docIn.ReadVInt32();
docBuffer[i] = code.TripleShift(1);
if ((code & 1) != 0)
{
freqBuffer[i] = 1;
}
else
{
freqBuffer[i] = docIn.ReadVInt32();
}
}
}
else
{
for (int i = 0; i < num; i++)
{
docBuffer[i] = docIn.ReadVInt32();
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BlockTermState NewTermState()
{
return new Lucene41PostingsWriter.Int32BlockTermState();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void Dispose(bool disposing)
{
if (disposing)
IOUtils.Dispose(docIn, posIn, payIn);
}
public override void DecodeTerm(long[] longs, DataInput @in, FieldInfo fieldInfo, BlockTermState termState, bool absolute)
{
Lucene41PostingsWriter.Int32BlockTermState termState2 = (Lucene41PostingsWriter.Int32BlockTermState)termState;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
bool fieldHasPositions = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
bool fieldHasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
bool fieldHasPayloads = fieldInfo.HasPayloads;
if (absolute)
{
termState2.docStartFP = 0;
termState2.posStartFP = 0;
termState2.payStartFP = 0;
}
if (version < Lucene41PostingsWriter.VERSION_META_ARRAY) // backward compatibility
{
DecodeTerm(@in, fieldInfo, termState2);
return;
}
termState2.docStartFP += longs[0];
if (fieldHasPositions)
{
termState2.posStartFP += longs[1];
if (fieldHasOffsets || fieldHasPayloads)
{
termState2.payStartFP += longs[2];
}
}
if (termState2.DocFreq == 1)
{
termState2.singletonDocID = @in.ReadVInt32();
}
else
{
termState2.singletonDocID = -1;
}
if (fieldHasPositions)
{
if (termState2.TotalTermFreq > Lucene41PostingsFormat.BLOCK_SIZE)
{
termState2.lastPosBlockOffset = @in.ReadVInt64();
}
else
{
termState2.lastPosBlockOffset = -1;
}
}
if (termState2.DocFreq > Lucene41PostingsFormat.BLOCK_SIZE)
{
termState2.skipOffset = @in.ReadVInt64();
}
else
{
termState2.skipOffset = -1;
}
}
private void DecodeTerm(DataInput @in, FieldInfo fieldInfo, Lucene41PostingsWriter.Int32BlockTermState termState)
{
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
bool fieldHasPositions = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
bool fieldHasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
bool fieldHasPayloads = fieldInfo.HasPayloads;
if (termState.DocFreq == 1)
{
termState.singletonDocID = @in.ReadVInt32();
}
else
{
termState.singletonDocID = -1;
termState.docStartFP += @in.ReadVInt64();
}
if (fieldHasPositions)
{
termState.posStartFP += @in.ReadVInt64();
if (termState.TotalTermFreq > Lucene41PostingsFormat.BLOCK_SIZE)
{
termState.lastPosBlockOffset = @in.ReadVInt64();
}
else
{
termState.lastPosBlockOffset = -1;
}
if ((fieldHasPayloads || fieldHasOffsets) && termState.TotalTermFreq >= Lucene41PostingsFormat.BLOCK_SIZE)
{
termState.payStartFP += @in.ReadVInt64();
}
}
if (termState.DocFreq > Lucene41PostingsFormat.BLOCK_SIZE)
{
termState.skipOffset = @in.ReadVInt64();
}
else
{
termState.skipOffset = -1;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override DocsEnum Docs(FieldInfo fieldInfo, BlockTermState termState, IBits liveDocs, DocsEnum reuse, DocsFlags flags)
{
if (reuse is null || !(reuse is BlockDocsEnum docsEnum) || !docsEnum.CanReuse(docIn, fieldInfo))
docsEnum = new BlockDocsEnum(this, fieldInfo);
return docsEnum.Reset(liveDocs, (Lucene41PostingsWriter.Int32BlockTermState)termState, flags);
}
// TODO: specialize to liveDocs vs not
public override DocsAndPositionsEnum DocsAndPositions(FieldInfo fieldInfo, BlockTermState termState, IBits liveDocs, DocsAndPositionsEnum reuse, DocsAndPositionsFlags flags)
{
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
bool indexHasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
bool indexHasPayloads = fieldInfo.HasPayloads;
if ((!indexHasOffsets || (flags & DocsAndPositionsFlags.OFFSETS) == 0) && (!indexHasPayloads || (flags & DocsAndPositionsFlags.PAYLOADS) == 0))
{
if (reuse is null || !(reuse is BlockDocsAndPositionsEnum docsAndPositionsEnum) || !docsAndPositionsEnum.CanReuse(docIn, fieldInfo))
docsAndPositionsEnum = new BlockDocsAndPositionsEnum(this, fieldInfo);
return docsAndPositionsEnum.Reset(liveDocs, (Lucene41PostingsWriter.Int32BlockTermState)termState);
}
else
{
if (reuse is null || !(reuse is EverythingEnum everythingEnum) || !everythingEnum.CanReuse(docIn, fieldInfo))
everythingEnum = new EverythingEnum(this, fieldInfo);
return everythingEnum.Reset(liveDocs, (Lucene41PostingsWriter.Int32BlockTermState)termState, flags);
}
}
internal sealed class BlockDocsEnum : DocsEnum
{
private readonly Lucene41PostingsReader outerInstance;
private readonly byte[] encoded;
private readonly int[] docDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE];
private readonly int[] freqBuffer = new int[ForUtil.MAX_DATA_SIZE];
private int docBufferUpto;
private Lucene41SkipReader skipper;
private bool skipped;
internal readonly IndexInput startDocIn;
internal IndexInput docIn;
internal readonly bool indexHasFreq;
internal readonly bool indexHasPos;
internal readonly bool indexHasOffsets;
internal readonly bool indexHasPayloads;
private int docFreq; // number of docs in this posting list
private long totalTermFreq; // sum of freqs in this posting list (or DocFreq when omitted)
private int docUpto; // how many docs we've read
private int doc; // doc we last read
private int accum; // accumulator for doc deltas
private int freq; // freq we last read
// Where this term's postings start in the .doc file:
private long docTermStartFP;
// Where this term's skip data starts (after
// docTermStartFP) in the .doc file (or -1 if there is
// no skip data for this term):
private long skipOffset;
// docID for next skip point, we won't use skipper if
// target docID is not larger than this
private int nextSkipDoc;
private IBits liveDocs;
private bool needsFreq; // true if the caller actually needs frequencies
private int singletonDocID; // docid when there is a single pulsed posting, otherwise -1
public BlockDocsEnum(Lucene41PostingsReader outerInstance, FieldInfo fieldInfo)
{
this.outerInstance = outerInstance;
this.startDocIn = outerInstance.docIn;
this.docIn = null;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
indexHasFreq = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;
indexHasPos = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
indexHasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
indexHasPayloads = fieldInfo.HasPayloads;
encoded = new byte[ForUtil.MAX_ENCODED_SIZE];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanReuse(IndexInput docIn, FieldInfo fieldInfo)
{
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
return docIn == startDocIn &&
indexHasFreq == (IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0) &&
indexHasPos == (IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) &&
indexHasPayloads == fieldInfo.HasPayloads;
}
public DocsEnum Reset(IBits liveDocs, Lucene41PostingsWriter.Int32BlockTermState termState, DocsFlags flags)
{
this.liveDocs = liveDocs;
// if (DEBUG) {
// System.out.println(" FPR.reset: termState=" + termState);
// }
docFreq = termState.DocFreq;
totalTermFreq = indexHasFreq ? termState.TotalTermFreq : docFreq;
docTermStartFP = termState.docStartFP;
skipOffset = termState.skipOffset;
singletonDocID = termState.singletonDocID;
if (docFreq > 1)
{
if (docIn is null)
{
// lazy init
docIn = (IndexInput)startDocIn.Clone();
}
docIn.Seek(docTermStartFP);
}
doc = -1;
this.needsFreq = (flags & DocsFlags.FREQS) != 0;
if (!indexHasFreq)
{
Arrays.Fill(freqBuffer, 1);
}
accum = 0;
docUpto = 0;
nextSkipDoc = Lucene41PostingsFormat.BLOCK_SIZE - 1; // we won't skip if target is found in first block
docBufferUpto = Lucene41PostingsFormat.BLOCK_SIZE;
skipped = false;
return this;
}
public override int Freq => freq;
public override int DocID => doc;
private void RefillDocs()
{
int left = docFreq - docUpto;
if (Debugging.AssertsEnabled) Debugging.Assert(left > 0);
if (left >= Lucene41PostingsFormat.BLOCK_SIZE)
{
// if (DEBUG) {
// System.out.println(" fill doc block from fp=" + docIn.getFilePointer());
// }
outerInstance.forUtil.ReadBlock(docIn, encoded, docDeltaBuffer);
if (indexHasFreq)
{
// if (DEBUG) {
// System.out.println(" fill freq block from fp=" + docIn.getFilePointer());
// }
if (needsFreq)
{
outerInstance.forUtil.ReadBlock(docIn, encoded, freqBuffer);
}
else
{
outerInstance.forUtil.SkipBlock(docIn); // skip over freqs
}
}
}
else if (docFreq == 1)
{
docDeltaBuffer[0] = singletonDocID;
freqBuffer[0] = (int)totalTermFreq;
}
else
{
// Read vInts:
// if (DEBUG) {
// System.out.println(" fill last vInt block from fp=" + docIn.getFilePointer());
// }
ReadVInt32Block(docIn, docDeltaBuffer, freqBuffer, left, indexHasFreq);
}
docBufferUpto = 0;
}
public override int NextDoc()
{
// if (DEBUG) {
// System.out.println("\nFPR.nextDoc");
// }
while (true)
{
// if (DEBUG) {
// System.out.println(" docUpto=" + docUpto + " (of df=" + DocFreq + ") docBufferUpto=" + docBufferUpto);
// }
if (docUpto == docFreq)
{
// if (DEBUG) {
// System.out.println(" return doc=END");
// }
return doc = NO_MORE_DOCS;
}
if (docBufferUpto == Lucene41PostingsFormat.BLOCK_SIZE)
{
RefillDocs();
}
// if (DEBUG) {
// System.out.println(" accum=" + accum + " docDeltaBuffer[" + docBufferUpto + "]=" + docDeltaBuffer[docBufferUpto]);
// }
accum += docDeltaBuffer[docBufferUpto];
docUpto++;
if (liveDocs is null || liveDocs.Get(accum))
{
doc = accum;
freq = freqBuffer[docBufferUpto];
docBufferUpto++;
// if (DEBUG) {
// System.out.println(" return doc=" + doc + " freq=" + freq);
// }
return doc;
}
// if (DEBUG) {
// System.out.println(" doc=" + accum + " is deleted; try next doc");
// }
docBufferUpto++;
}
}
public override int Advance(int target)
{
// TODO: make frq block load lazy/skippable
// if (DEBUG) {
// System.out.println(" FPR.advance target=" + target);
// }
// current skip docID < docIDs generated from current buffer <= next skip docID
// we don't need to skip if target is buffered already
if (docFreq > Lucene41PostingsFormat.BLOCK_SIZE && target > nextSkipDoc)
{
// if (DEBUG) {
// System.out.println("load skipper");
// }
if (skipper is null)
{
// Lazy init: first time this enum has ever been used for skipping
skipper = new Lucene41SkipReader((IndexInput)docIn.Clone(), Lucene41PostingsWriter.maxSkipLevels, Lucene41PostingsFormat.BLOCK_SIZE, indexHasPos, indexHasOffsets, indexHasPayloads);
}
if (!skipped)
{
if (Debugging.AssertsEnabled) Debugging.Assert(skipOffset != -1);
// this is the first time this enum has skipped
// since reset() was called; load the skip data:
skipper.Init(docTermStartFP + skipOffset, docTermStartFP, 0, 0, docFreq);
skipped = true;
}
// always plus one to fix the result, since skip position in Lucene41SkipReader
// is a little different from MultiLevelSkipListReader
int newDocUpto = skipper.SkipTo(target) + 1;
if (newDocUpto > docUpto)
{
// Skipper moved
// if (DEBUG) {
// System.out.println("skipper moved to docUpto=" + newDocUpto + " vs current=" + docUpto + "; docID=" + skipper.getDoc() + " fp=" + skipper.getDocPointer());
// }
if (Debugging.AssertsEnabled) Debugging.Assert(newDocUpto % Lucene41PostingsFormat.BLOCK_SIZE == 0,"got {0}", newDocUpto);
docUpto = newDocUpto;
// Force to read next block
docBufferUpto = Lucene41PostingsFormat.BLOCK_SIZE;
accum = skipper.Doc; // actually, this is just lastSkipEntry
docIn.Seek(skipper.DocPointer); // now point to the block we want to search
}
// next time we call advance, this is used to
// foresee whether skipper is necessary.
nextSkipDoc = skipper.NextSkipDoc;
}
if (docUpto == docFreq)
{
return doc = NO_MORE_DOCS;
}
if (docBufferUpto == Lucene41PostingsFormat.BLOCK_SIZE)
{
RefillDocs();
}
// Now scan... this is an inlined/pared down version
// of nextDoc():
while (true)
{
// if (DEBUG) {
// System.out.println(" scan doc=" + accum + " docBufferUpto=" + docBufferUpto);
// }
accum += docDeltaBuffer[docBufferUpto];
docUpto++;
if (accum >= target)
{
break;
}
docBufferUpto++;
if (docUpto == docFreq)
{
return doc = NO_MORE_DOCS;
}
}
if (liveDocs is null || liveDocs.Get(accum))
{
// if (DEBUG) {
// System.out.println(" return doc=" + accum);
// }
freq = freqBuffer[docBufferUpto];
docBufferUpto++;
return doc = accum;
}
else
{
// if (DEBUG) {
// System.out.println(" now do nextDoc()");
// }
docBufferUpto++;
return NextDoc();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetCost()
{
return docFreq;
}
}
internal sealed class BlockDocsAndPositionsEnum : DocsAndPositionsEnum
{
private readonly Lucene41PostingsReader outerInstance;
private readonly byte[] encoded;
private readonly int[] docDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE];
private readonly int[] freqBuffer = new int[ForUtil.MAX_DATA_SIZE];
private readonly int[] posDeltaBuffer = new int[ForUtil.MAX_DATA_SIZE];
private int docBufferUpto;
private int posBufferUpto;
private Lucene41SkipReader skipper;
private bool skipped;
internal readonly IndexInput startDocIn;
internal IndexInput docIn;
internal readonly IndexInput posIn;
internal readonly bool indexHasOffsets;
internal readonly bool indexHasPayloads;
private int docFreq; // number of docs in this posting list
private long totalTermFreq; // number of positions in this posting list
private int docUpto; // how many docs we've read
private int doc; // doc we last read
private int accum; // accumulator for doc deltas
private int freq; // freq we last read
private int position; // current position
// how many positions "behind" we are; nextPosition must
// skip these to "catch up":
private int posPendingCount;
// Lazy pos seek: if != -1 then we must seek to this FP
// before reading positions:
private long posPendingFP;
// Where this term's postings start in the .doc file:
private long docTermStartFP;
// Where this term's postings start in the .pos file:
private long posTermStartFP;
// Where this term's payloads/offsets start in the .pay
// file:
private long payTermStartFP;
// File pointer where the last (vInt encoded) pos delta
// block is. We need this to know whether to bulk
// decode vs vInt decode the block:
private long lastPosBlockFP;
// Where this term's skip data starts (after
// docTermStartFP) in the .doc file (or -1 if there is
// no skip data for this term):
private long skipOffset;
private int nextSkipDoc;
private IBits liveDocs;
private int singletonDocID; // docid when there is a single pulsed posting, otherwise -1
public BlockDocsAndPositionsEnum(Lucene41PostingsReader outerInstance, FieldInfo fieldInfo)
{
this.outerInstance = outerInstance;
this.startDocIn = outerInstance.docIn;
this.docIn = null;
this.posIn = (IndexInput)outerInstance.posIn.Clone();
encoded = new byte[ForUtil.MAX_ENCODED_SIZE];
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
indexHasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
indexHasPayloads = fieldInfo.HasPayloads;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanReuse(IndexInput docIn, FieldInfo fieldInfo)
{
return docIn == startDocIn &&
indexHasOffsets == (IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
&& indexHasPayloads == fieldInfo.HasPayloads;
}
public DocsAndPositionsEnum Reset(IBits liveDocs, Lucene41PostingsWriter.Int32BlockTermState termState)
{
this.liveDocs = liveDocs;
// if (DEBUG) {
// System.out.println(" FPR.reset: termState=" + termState);
// }
docFreq = termState.DocFreq;
docTermStartFP = termState.docStartFP;
posTermStartFP = termState.posStartFP;
payTermStartFP = termState.payStartFP;
skipOffset = termState.skipOffset;
totalTermFreq = termState.TotalTermFreq;
singletonDocID = termState.singletonDocID;
if (docFreq > 1)
{
if (docIn is null)
{
// lazy init
docIn = (IndexInput)startDocIn.Clone();
}
docIn.Seek(docTermStartFP);
}
posPendingFP = posTermStartFP;
posPendingCount = 0;
if (termState.TotalTermFreq < Lucene41PostingsFormat.BLOCK_SIZE)
{
lastPosBlockFP = posTermStartFP;
}
else if (termState.TotalTermFreq == Lucene41PostingsFormat.BLOCK_SIZE)
{
lastPosBlockFP = -1;
}
else
{
lastPosBlockFP = posTermStartFP + termState.lastPosBlockOffset;
}
doc = -1;
accum = 0;
docUpto = 0;
nextSkipDoc = Lucene41PostingsFormat.BLOCK_SIZE - 1;
docBufferUpto = Lucene41PostingsFormat.BLOCK_SIZE;
skipped = false;
return this;
}
public override int Freq => freq;
public override int DocID => doc;
private void RefillDocs()
{
int left = docFreq - docUpto;
if (Debugging.AssertsEnabled) Debugging.Assert(left > 0);
if (left >= Lucene41PostingsFormat.BLOCK_SIZE)
{
// if (DEBUG) {
// System.out.println(" fill doc block from fp=" + docIn.getFilePointer());
// }
outerInstance.forUtil.ReadBlock(docIn, encoded, docDeltaBuffer);
// if (DEBUG) {
// System.out.println(" fill freq block from fp=" + docIn.getFilePointer());
// }
outerInstance.forUtil.ReadBlock(docIn, encoded, freqBuffer);
}
else if (docFreq == 1)
{
docDeltaBuffer[0] = singletonDocID;
freqBuffer[0] = (int)totalTermFreq;
}
else
{
// Read vInts:
// if (DEBUG) {
// System.out.println(" fill last vInt doc block from fp=" + docIn.getFilePointer());
// }
ReadVInt32Block(docIn, docDeltaBuffer, freqBuffer, left, true);
}
docBufferUpto = 0;
}
private void RefillPositions()
{
// if (DEBUG) {
// System.out.println(" refillPositions");
// }
if (posIn.Position == lastPosBlockFP) // LUCENENET specific: Renamed from getFilePointer() to match FileStream
{
// if (DEBUG) {
// System.out.println(" vInt pos block @ fp=" + posIn.getFilePointer() + " hasPayloads=" + indexHasPayloads + " hasOffsets=" + indexHasOffsets);
// }
int count = (int)(totalTermFreq % Lucene41PostingsFormat.BLOCK_SIZE);
int payloadLength = 0;
for (int i = 0; i < count; i++)
{
int code = posIn.ReadVInt32();
if (indexHasPayloads)
{
if ((code & 1) != 0)
{
payloadLength = posIn.ReadVInt32();
}
posDeltaBuffer[i] = code.TripleShift(1);
if (payloadLength != 0)
{
posIn.Seek(posIn.Position + payloadLength); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
}
}
else
{
posDeltaBuffer[i] = code;
}
if (indexHasOffsets)
{
if ((posIn.ReadVInt32() & 1) != 0)
{
// offset length changed
posIn.ReadVInt32();
}
}
}
}
else
{
// if (DEBUG) {
// System.out.println(" bulk pos block @ fp=" + posIn.getFilePointer());
// }
outerInstance.forUtil.ReadBlock(posIn, encoded, posDeltaBuffer);
}
}
public override int NextDoc()
{
// if (DEBUG) {
// System.out.println(" FPR.nextDoc");
// }
while (true)
{
// if (DEBUG) {
// System.out.println(" docUpto=" + docUpto + " (of df=" + DocFreq + ") docBufferUpto=" + docBufferUpto);
// }
if (docUpto == docFreq)
{
return doc = NO_MORE_DOCS;
}
if (docBufferUpto == Lucene41PostingsFormat.BLOCK_SIZE)
{
RefillDocs();
}
// if (DEBUG) {
// System.out.println(" accum=" + accum + " docDeltaBuffer[" + docBufferUpto + "]=" + docDeltaBuffer[docBufferUpto]);
// }
accum += docDeltaBuffer[docBufferUpto];
freq = freqBuffer[docBufferUpto];
posPendingCount += freq;
docBufferUpto++;
docUpto++;
if (liveDocs is null || liveDocs.Get(accum))
{
doc = accum;
position = 0;
// if (DEBUG) {
// System.out.println(" return doc=" + doc + " freq=" + freq + " posPendingCount=" + posPendingCount);
// }
return doc;
}
// if (DEBUG) {
// System.out.println(" doc=" + accum + " is deleted; try next doc");
// }
}
}
public override int Advance(int target)
{
// TODO: make frq block load lazy/skippable
// if (DEBUG) {
// System.out.println(" FPR.advance target=" + target);
// }
if (docFreq > Lucene41PostingsFormat.BLOCK_SIZE && target > nextSkipDoc)
{
// if (DEBUG) {
// System.out.println(" try skipper");
// }
if (skipper is null)
{
// Lazy init: first time this enum has ever been used for skipping
// if (DEBUG) {
// System.out.println(" create skipper");
// }
skipper = new Lucene41SkipReader((IndexInput)docIn.Clone(), Lucene41PostingsWriter.maxSkipLevels, Lucene41PostingsFormat.BLOCK_SIZE, true, indexHasOffsets, indexHasPayloads);
}
if (!skipped)
{
if (Debugging.AssertsEnabled) Debugging.Assert(skipOffset != -1);
// this is the first time this enum has skipped
// since reset() was called; load the skip data:
// if (DEBUG) {
// System.out.println(" init skipper");
// }
skipper.Init(docTermStartFP + skipOffset, docTermStartFP, posTermStartFP, payTermStartFP, docFreq);
skipped = true;
}
int newDocUpto = skipper.SkipTo(target) + 1;
if (newDocUpto > docUpto)
{
// Skipper moved
// if (DEBUG) {
// System.out.println(" skipper moved to docUpto=" + newDocUpto + " vs current=" + docUpto + "; docID=" + skipper.getDoc() + " fp=" + skipper.getDocPointer() + " pos.fp=" + skipper.getPosPointer() + " pos.bufferUpto=" + skipper.getPosBufferUpto());
// }
if (Debugging.AssertsEnabled) Debugging.Assert(newDocUpto % Lucene41PostingsFormat.BLOCK_SIZE == 0,"got {0}", newDocUpto);
docUpto = newDocUpto;
// Force to read next block
docBufferUpto = Lucene41PostingsFormat.BLOCK_SIZE;
accum = skipper.Doc;
docIn.Seek(skipper.DocPointer);
posPendingFP = skipper.PosPointer;
posPendingCount = skipper.PosBufferUpto;
}
nextSkipDoc = skipper.NextSkipDoc;
}
if (docUpto == docFreq)
{
return doc = NO_MORE_DOCS;
}
if (docBufferUpto == Lucene41PostingsFormat.BLOCK_SIZE)
{
RefillDocs();
}
// Now scan... this is an inlined/pared down version
// of nextDoc():
while (true)
{
// if (DEBUG) {
// System.out.println(" scan doc=" + accum + " docBufferUpto=" + docBufferUpto);
// }
accum += docDeltaBuffer[docBufferUpto];
freq = freqBuffer[docBufferUpto];
posPendingCount += freq;
docBufferUpto++;
docUpto++;
if (accum >= target)
{
break;
}
if (docUpto == docFreq)
{
return doc = NO_MORE_DOCS;
}
}
if (liveDocs is null || liveDocs.Get(accum))
{
// if (DEBUG) {
// System.out.println(" return doc=" + accum);
// }
position = 0;
return doc = accum;
}
else
{
// if (DEBUG) {
// System.out.println(" now do nextDoc()");
// }
return NextDoc();
}
}
// TODO: in theory we could avoid loading frq block
// when not needed, ie, use skip data to load how far to
// seek the pos pointer ... instead of having to load frq
// blocks only to sum up how many positions to skip
private void SkipPositions()
{
// Skip positions now:
int toSkip = posPendingCount - freq;
// if (DEBUG) {
// System.out.println(" FPR.skipPositions: toSkip=" + toSkip);
// }
int leftInBlock = Lucene41PostingsFormat.BLOCK_SIZE - posBufferUpto;
if (toSkip < leftInBlock)
{
posBufferUpto += toSkip;
// if (DEBUG) {
// System.out.println(" skip w/in block to posBufferUpto=" + posBufferUpto);
// }
}
else
{
toSkip -= leftInBlock;
while (toSkip >= Lucene41PostingsFormat.BLOCK_SIZE)
{
// if (DEBUG) {
// System.out.println(" skip whole block @ fp=" + posIn.getFilePointer());
// }
if (Debugging.AssertsEnabled) Debugging.Assert(posIn.Position != lastPosBlockFP); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
outerInstance.forUtil.SkipBlock(posIn);
toSkip -= Lucene41PostingsFormat.BLOCK_SIZE;
}
RefillPositions();
posBufferUpto = toSkip;
// if (DEBUG) {
// System.out.println(" skip w/in block to posBufferUpto=" + posBufferUpto);
// }
}
position = 0;
}
public override int NextPosition()
{
// if (DEBUG) {
// System.out.println(" FPR.nextPosition posPendingCount=" + posPendingCount + " posBufferUpto=" + posBufferUpto);
// }
if (posPendingFP != -1)
{
// if (DEBUG) {
// System.out.println(" seek to pendingFP=" + posPendingFP);
// }
posIn.Seek(posPendingFP);
posPendingFP = -1;
// Force buffer refill:
posBufferUpto = Lucene41PostingsFormat.BLOCK_SIZE;