-
Notifications
You must be signed in to change notification settings - Fork 620
/
MultiDocValues.cs
689 lines (624 loc) · 25.7 KB
/
MultiDocValues.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
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace Lucene.Net.Index
{
/*
* 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.
*/
using AppendingPackedInt64Buffer = Lucene.Net.Util.Packed.AppendingPackedInt64Buffer;
using BytesRef = Lucene.Net.Util.BytesRef;
using IBits = Lucene.Net.Util.IBits;
using MonotonicAppendingInt64Buffer = Lucene.Net.Util.Packed.MonotonicAppendingInt64Buffer;
using PackedInt32s = Lucene.Net.Util.Packed.PackedInt32s;
using TermsEnumIndex = Lucene.Net.Index.MultiTermsEnum.TermsEnumIndex;
using TermsEnumWithSlice = Lucene.Net.Index.MultiTermsEnum.TermsEnumWithSlice;
/// <summary>
/// A wrapper for <see cref="CompositeReader"/> providing access to <see cref="DocValues"/>.
///
/// <para/><b>NOTE</b>: for multi readers, you'll get better
/// performance by gathering the sub readers using
/// <see cref="IndexReader.Context"/> to get the
/// atomic leaves and then operate per-AtomicReader,
/// instead of using this class.
///
/// <para/><b>NOTE</b>: this is very costly.
///
/// <para/>
/// @lucene.experimental
/// @lucene.internal
/// </summary>
public static class MultiDocValues // LUCENENET specific: CA1052 Static holder types should be Static or NotInheritable
{
/// <summary>
/// Returns a <see cref="NumericDocValues"/> for a reader's norms (potentially merging on-the-fly).
/// <para>
/// This is a slow way to access normalization values. Instead, access them per-segment
/// with <seealso cref="AtomicReader.GetNormValues(string)"/>
/// </para>
/// </summary>
public static NumericDocValues GetNormValues(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetNormValues(field);
}
FieldInfo fi = MultiFields.GetMergedFieldInfos(r).FieldInfo(field);
if (fi is null || fi.HasNorms == false)
{
return null;
}
bool anyReal = false;
NumericDocValues[] values = new NumericDocValues[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
NumericDocValues v = context.AtomicReader.GetNormValues(field);
if (v is null)
{
v = DocValues.EMPTY_NUMERIC;
}
else
{
anyReal = true;
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (Debugging.AssertsEnabled) Debugging.Assert(anyReal);
return new NumericDocValuesAnonymousClass(values, starts);
}
private class NumericDocValuesAnonymousClass : NumericDocValues
{
private readonly NumericDocValues[] values;
private readonly int[] starts;
public NumericDocValuesAnonymousClass(NumericDocValues[] values, int[] starts)
{
this.values = values;
this.starts = starts;
}
public override long Get(int docID)
{
int subIndex = ReaderUtil.SubIndex(docID, starts);
return values[subIndex].Get(docID - starts[subIndex]);
}
}
/// <summary>
/// Returns a <see cref="NumericDocValues"/> for a reader's docvalues (potentially merging on-the-fly)
/// <para>
/// This is a slow way to access numeric values. Instead, access them per-segment
/// with <see cref="AtomicReader.GetNumericDocValues(string)"/>
/// </para>
/// </summary>
public static NumericDocValues GetNumericValues(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetNumericDocValues(field);
}
bool anyReal = false;
NumericDocValues[] values = new NumericDocValues[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
NumericDocValues v = context.AtomicReader.GetNumericDocValues(field);
if (v is null)
{
v = DocValues.EMPTY_NUMERIC;
}
else
{
anyReal = true;
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (!anyReal)
{
return null;
}
else
{
return new NumericDocValuesAnonymousClass2(values, starts);
}
}
private class NumericDocValuesAnonymousClass2 : NumericDocValues
{
private readonly NumericDocValues[] values;
private readonly int[] starts;
public NumericDocValuesAnonymousClass2(NumericDocValues[] values, int[] starts)
{
this.values = values;
this.starts = starts;
}
public override long Get(int docID)
{
int subIndex = ReaderUtil.SubIndex(docID, starts);
return values[subIndex].Get(docID - starts[subIndex]);
}
}
/// <summary>
/// Returns a <see cref="IBits"/> for a reader's docsWithField (potentially merging on-the-fly)
/// <para>
/// This is a slow way to access this bitset. Instead, access them per-segment
/// with <see cref="AtomicReader.GetDocsWithField(string)"/>
/// </para>
/// </summary>
public static IBits GetDocsWithField(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetDocsWithField(field);
}
bool anyReal = false;
bool anyMissing = false;
IBits[] values = new IBits[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
IBits v = context.AtomicReader.GetDocsWithField(field);
if (v is null)
{
v = new Lucene.Net.Util.Bits.MatchNoBits(context.Reader.MaxDoc);
anyMissing = true;
}
else
{
anyReal = true;
if (v is Lucene.Net.Util.Bits.MatchAllBits == false)
{
anyMissing = true;
}
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (!anyReal)
{
return null;
}
else if (!anyMissing)
{
return new Lucene.Net.Util.Bits.MatchAllBits(r.MaxDoc);
}
else
{
return new MultiBits(values, starts, false);
}
}
/// <summary>
/// Returns a <see cref="BinaryDocValues"/> for a reader's docvalues (potentially merging on-the-fly)
/// <para>
/// This is a slow way to access binary values. Instead, access them per-segment
/// with <see cref="AtomicReader.GetBinaryDocValues(string)"/>
/// </para>
/// </summary>
public static BinaryDocValues GetBinaryValues(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetBinaryDocValues(field);
}
bool anyReal = false;
BinaryDocValues[] values = new BinaryDocValues[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
BinaryDocValues v = context.AtomicReader.GetBinaryDocValues(field);
if (v is null)
{
v = DocValues.EMPTY_BINARY;
}
else
{
anyReal = true;
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (!anyReal)
{
return null;
}
else
{
return new BinaryDocValuesAnonymousClass(values, starts);
}
}
private class BinaryDocValuesAnonymousClass : BinaryDocValues
{
private readonly BinaryDocValues[] values;
private readonly int[] starts;
public BinaryDocValuesAnonymousClass(BinaryDocValues[] values, int[] starts)
{
this.values = values;
this.starts = starts;
}
public override void Get(int docID, BytesRef result)
{
int subIndex = ReaderUtil.SubIndex(docID, starts);
values[subIndex].Get(docID - starts[subIndex], result);
}
}
/// <summary>
/// Returns a <see cref="SortedDocValues"/> for a reader's docvalues (potentially doing extremely slow things).
/// <para>
/// this is an extremely slow way to access sorted values. Instead, access them per-segment
/// with <see cref="AtomicReader.GetSortedDocValues(string)"/>
/// </para>
/// </summary>
public static SortedDocValues GetSortedValues(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetSortedDocValues(field);
}
bool anyReal = false;
var values = new SortedDocValues[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
SortedDocValues v = context.AtomicReader.GetSortedDocValues(field);
if (v is null)
{
v = DocValues.EMPTY_SORTED;
}
else
{
anyReal = true;
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (!anyReal)
{
return null;
}
else
{
TermsEnum[] enums = new TermsEnum[values.Length];
for (int i = 0; i < values.Length; i++)
{
enums[i] = values[i].GetTermsEnum();
}
OrdinalMap mapping = new OrdinalMap(r.CoreCacheKey, enums);
return new MultiSortedDocValues(values, starts, mapping);
}
}
/// <summary>
/// Returns a <see cref="SortedSetDocValues"/> for a reader's docvalues (potentially doing extremely slow things).
/// <para>
/// This is an extremely slow way to access sorted values. Instead, access them per-segment
/// with <see cref="AtomicReader.GetSortedSetDocValues(string)"/>
/// </para>
/// </summary>
public static SortedSetDocValues GetSortedSetValues(IndexReader r, string field)
{
IList<AtomicReaderContext> leaves = r.Leaves;
int size = leaves.Count;
if (size == 0)
{
return null;
}
else if (size == 1)
{
return leaves[0].AtomicReader.GetSortedSetDocValues(field);
}
bool anyReal = false;
SortedSetDocValues[] values = new SortedSetDocValues[size];
int[] starts = new int[size + 1];
for (int i = 0; i < size; i++)
{
AtomicReaderContext context = leaves[i];
SortedSetDocValues v = context.AtomicReader.GetSortedSetDocValues(field);
if (v is null)
{
v = DocValues.EMPTY_SORTED_SET;
}
else
{
anyReal = true;
}
values[i] = v;
starts[i] = context.DocBase;
}
starts[size] = r.MaxDoc;
if (!anyReal)
{
return null;
}
else
{
TermsEnum[] enums = new TermsEnum[values.Length];
for (int i = 0; i < values.Length; i++)
{
enums[i] = values[i].GetTermsEnum();
}
OrdinalMap mapping = new OrdinalMap(r.CoreCacheKey, enums);
return new MultiSortedSetDocValues(values, starts, mapping);
}
}
/// <summary>
/// maps per-segment ordinals to/from global ordinal space </summary>
// TODO: use more efficient packed ints structures?
// TODO: pull this out? its pretty generic (maps between N ord()-enabled TermsEnums)
public class OrdinalMap
{
// cache key of whoever asked for this awful thing
internal readonly object owner;
// globalOrd -> (globalOrd - segmentOrd) where segmentOrd is the the ordinal in the first segment that contains this term
internal readonly MonotonicAppendingInt64Buffer globalOrdDeltas;
// globalOrd -> first segment container
internal readonly AppendingPackedInt64Buffer firstSegments;
// for every segment, segmentOrd -> (globalOrd - segmentOrd)
internal readonly MonotonicAppendingInt64Buffer[] ordDeltas;
/// <summary>
/// Creates an ordinal map that allows mapping ords to/from a merged
/// space from <c>subs</c>. </summary>
/// <param name="owner"> a cache key </param>
/// <param name="subs"> <see cref="TermsEnum"/>s that support <see cref="TermsEnum.Ord"/>. They need
/// not be dense (e.g. can be FilteredTermsEnums). </param>
/// <exception cref="IOException"> if an I/O error occurred. </exception>
public OrdinalMap(object owner, TermsEnum[] subs)
{
// create the ordinal mappings by pulling a termsenum over each sub's
// unique terms, and walking a multitermsenum over those
this.owner = owner;
globalOrdDeltas = new MonotonicAppendingInt64Buffer(PackedInt32s.COMPACT);
firstSegments = new AppendingPackedInt64Buffer(PackedInt32s.COMPACT);
ordDeltas = new MonotonicAppendingInt64Buffer[subs.Length];
for (int i = 0; i < ordDeltas.Length; i++)
{
ordDeltas[i] = new MonotonicAppendingInt64Buffer();
}
long[] segmentOrds = new long[subs.Length];
ReaderSlice[] slices = new ReaderSlice[subs.Length];
TermsEnumIndex[] indexes = new TermsEnumIndex[slices.Length];
for (int i = 0; i < slices.Length; i++)
{
slices[i] = new ReaderSlice(0, 0, i);
indexes[i] = new TermsEnumIndex(subs[i], i);
}
MultiTermsEnum mte = new MultiTermsEnum(slices);
mte.Reset(indexes);
long globalOrd = 0;
while (mte.MoveNext())
{
TermsEnumWithSlice[] matches = mte.MatchArray;
for (int i = 0; i < mte.MatchCount; i++)
{
int segmentIndex = matches[i].Index;
long segmentOrd = matches[i].Terms.Ord;
long delta = globalOrd - segmentOrd;
// for each unique term, just mark the first segment index/delta where it occurs
if (i == 0)
{
firstSegments.Add(segmentIndex);
globalOrdDeltas.Add(delta);
}
// for each per-segment ord, map it back to the global term.
while (segmentOrds[segmentIndex] <= segmentOrd)
{
ordDeltas[segmentIndex].Add(delta);
segmentOrds[segmentIndex]++;
}
}
globalOrd++;
}
firstSegments.Freeze();
globalOrdDeltas.Freeze();
for (int i = 0; i < ordDeltas.Length; ++i)
{
ordDeltas[i].Freeze();
}
}
/// <summary>
/// Given a segment number and segment ordinal, returns
/// the corresponding global ordinal.
/// </summary>
public virtual long GetGlobalOrd(int segmentIndex, long segmentOrd)
{
return segmentOrd + ordDeltas[segmentIndex].Get(segmentOrd);
}
/// <summary>
/// Given global ordinal, returns the ordinal of the first segment which contains
/// this ordinal (the corresponding to the segment return <see cref="GetFirstSegmentNumber(long)"/>).
/// </summary>
public virtual long GetFirstSegmentOrd(long globalOrd)
{
return globalOrd - globalOrdDeltas.Get(globalOrd);
}
/// <summary>
/// Given a global ordinal, returns the index of the first
/// segment that contains this term.
/// </summary>
public virtual int GetFirstSegmentNumber(long globalOrd)
{
return (int)firstSegments.Get(globalOrd);
}
/// <summary>
/// Returns the total number of unique terms in global ord space.
/// </summary>
public virtual long ValueCount => globalOrdDeltas.Count;
/// <summary>
/// Returns total byte size used by this ordinal map.
/// </summary>
public virtual long RamBytesUsed()
{
long size = globalOrdDeltas.RamBytesUsed() + firstSegments.RamBytesUsed();
for (int i = 0; i < ordDeltas.Length; i++)
{
size += ordDeltas[i].RamBytesUsed();
}
return size;
}
}
/// <summary>
/// Implements <see cref="SortedDocValues"/> over n subs, using an <see cref="OrdinalMap"/>
/// <para/>
/// @lucene.internal
/// </summary>
public class MultiSortedDocValues : SortedDocValues
{
/// <summary>
/// docbase for each leaf: parallel with <see cref="Values"/> </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public int[] DocStarts => docStarts;
private readonly int[] docStarts;
/// <summary>
/// leaf values </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public SortedDocValues[] Values => values;
private readonly SortedDocValues[] values;
/// <summary>
/// ordinal map mapping ords from <c>values</c> to global ord space </summary>
public OrdinalMap Mapping => mapping;
private readonly OrdinalMap mapping;
/// <summary>
/// Creates a new <see cref="MultiSortedDocValues"/> over <paramref name="values"/> </summary>
internal MultiSortedDocValues(SortedDocValues[] values, int[] docStarts, OrdinalMap mapping)
{
if (Debugging.AssertsEnabled)
{
Debugging.Assert(values.Length == mapping.ordDeltas.Length);
Debugging.Assert(docStarts.Length == values.Length + 1);
}
this.values = values;
this.docStarts = docStarts;
this.mapping = mapping;
}
public override int GetOrd(int docID)
{
int subIndex = ReaderUtil.SubIndex(docID, docStarts);
int segmentOrd = values[subIndex].GetOrd(docID - docStarts[subIndex]);
return segmentOrd == -1 ? segmentOrd : (int)mapping.GetGlobalOrd(subIndex, segmentOrd);
}
public override void LookupOrd(int ord, BytesRef result)
{
int subIndex = mapping.GetFirstSegmentNumber(ord);
int segmentOrd = (int)mapping.GetFirstSegmentOrd(ord);
values[subIndex].LookupOrd(segmentOrd, result);
}
public override int ValueCount => (int)mapping.ValueCount;
}
/// <summary>
/// Implements <see cref="MultiSortedSetDocValues"/> over n subs, using an <see cref="OrdinalMap"/>
/// <para/>
/// @lucene.internal
/// </summary>
public class MultiSortedSetDocValues : SortedSetDocValues
{
/// <summary>
/// docbase for each leaf: parallel with <see cref="Values"/> </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public int[] DocStarts => docStarts;
private readonly int[] docStarts;
/// <summary>
/// leaf values </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public SortedSetDocValues[] Values => values;
private readonly SortedSetDocValues[] values;
/// <summary>
/// ordinal map mapping ords from <c>values</c> to global ord space </summary>
public OrdinalMap Mapping => mapping;
private readonly OrdinalMap mapping;
internal int currentSubIndex;
/// <summary>
/// Creates a new <see cref="MultiSortedSetDocValues"/> over <paramref name="values"/> </summary>
internal MultiSortedSetDocValues(SortedSetDocValues[] values, int[] docStarts, OrdinalMap mapping)
{
if (Debugging.AssertsEnabled)
{
Debugging.Assert(values.Length == mapping.ordDeltas.Length);
Debugging.Assert(docStarts.Length == values.Length + 1);
}
this.values = values;
this.docStarts = docStarts;
this.mapping = mapping;
}
public override long NextOrd()
{
long segmentOrd = values[currentSubIndex].NextOrd();
if (segmentOrd == NO_MORE_ORDS)
{
return segmentOrd;
}
else
{
return mapping.GetGlobalOrd(currentSubIndex, segmentOrd);
}
}
public override void SetDocument(int docID)
{
currentSubIndex = ReaderUtil.SubIndex(docID, docStarts);
values[currentSubIndex].SetDocument(docID - docStarts[currentSubIndex]);
}
public override void LookupOrd(long ord, BytesRef result)
{
int subIndex = mapping.GetFirstSegmentNumber(ord);
long segmentOrd = mapping.GetFirstSegmentOrd(ord);
values[subIndex].LookupOrd(segmentOrd, result);
}
public override long ValueCount => mapping.ValueCount;
}
}
}