5151import static org .elasticsearch .index .codec .tsdb .ES87TSDBDocValuesFormat .TERMS_DICT_BLOCK_LZ4_SHIFT ;
5252
5353public class ES87TSDBDocValuesProducer extends DocValuesProducer {
54- private final Map <String , NumericEntry > numerics = new HashMap <>() ;
55- private final Map <String , BinaryEntry > binaries = new HashMap <>() ;
56- private final Map <String , SortedEntry > sorted = new HashMap <>() ;
57- private final Map <String , SortedSetEntry > sortedSets = new HashMap <>() ;
58- private final Map <String , SortedNumericEntry > sortedNumerics = new HashMap <>() ;
59- private final Map <String , DocValuesSkipperEntry > skippers = new HashMap <>() ;
54+ private final Map <String , NumericEntry > numerics ;
55+ private final Map <String , BinaryEntry > binaries ;
56+ private final Map <String , SortedEntry > sorted ;
57+ private final Map <String , SortedSetEntry > sortedSets ;
58+ private final Map <String , SortedNumericEntry > sortedNumerics ;
59+ private final Map <String , DocValuesSkipperEntry > skippers ;
6060 private final IndexInput data ;
6161 private final int maxDoc ;
62+ private final int version ;
63+ private final boolean merging ;
6264
6365 ES87TSDBDocValuesProducer (SegmentReadState state , String dataCodec , String dataExtension , String metaCodec , String metaExtension )
6466 throws IOException {
65- String metaName = IndexFileNames .segmentFileName (state .segmentInfo .name , state .segmentSuffix , metaExtension );
67+ this .numerics = new HashMap <>();
68+ this .binaries = new HashMap <>();
69+ this .sorted = new HashMap <>();
70+ this .sortedSets = new HashMap <>();
71+ this .sortedNumerics = new HashMap <>();
72+ this .skippers = new HashMap <>();
6673 this .maxDoc = state .segmentInfo .maxDoc ();
74+ this .merging = false ;
6775
6876 // read in the entries from the metadata file.
6977 int version = -1 ;
78+ String metaName = IndexFileNames .segmentFileName (state .segmentInfo .name , state .segmentSuffix , metaExtension );
7079 try (ChecksumIndexInput in = state .directory .openChecksumInput (metaName )) {
7180 Throwable priorE = null ;
7281
@@ -112,13 +121,43 @@ public class ES87TSDBDocValuesProducer extends DocValuesProducer {
112121 CodecUtil .retrieveChecksum (data );
113122
114123 success = true ;
124+ this .version = version ;
115125 } finally {
116126 if (success == false ) {
117127 IOUtils .closeWhileHandlingException (this .data );
118128 }
119129 }
120130 }
121131
132+ private ES87TSDBDocValuesProducer (
133+ Map <String , NumericEntry > numerics ,
134+ Map <String , BinaryEntry > binaries ,
135+ Map <String , SortedEntry > sorted ,
136+ Map <String , SortedSetEntry > sortedSets ,
137+ Map <String , SortedNumericEntry > sortedNumerics ,
138+ Map <String , DocValuesSkipperEntry > skippers ,
139+ IndexInput data ,
140+ int maxDoc ,
141+ int version ,
142+ boolean merging
143+ ) {
144+ this .numerics = numerics ;
145+ this .binaries = binaries ;
146+ this .sorted = sorted ;
147+ this .sortedSets = sortedSets ;
148+ this .sortedNumerics = sortedNumerics ;
149+ this .skippers = skippers ;
150+ this .data = data .clone ();
151+ this .maxDoc = maxDoc ;
152+ this .version = version ;
153+ this .merging = merging ;
154+ }
155+
156+ @ Override
157+ public DocValuesProducer getMergeInstance () {
158+ return new ES87TSDBDocValuesProducer (numerics , binaries , sorted , sortedSets , sortedNumerics , skippers , data , maxDoc , version , true );
159+ }
160+
122161 @ Override
123162 public NumericDocValues getNumeric (FieldInfo field ) throws IOException {
124163 NumericEntry entry = numerics .get (field .name );
@@ -151,7 +190,7 @@ public BytesRef binaryValue() throws IOException {
151190 } else {
152191 // variable length
153192 final RandomAccessInput addressesData = this .data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
154- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData );
193+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData , merging );
155194 return new DenseBinaryDocValues (maxDoc ) {
156195 final BytesRef bytes = new BytesRef (new byte [entry .maxLength ], 0 , entry .maxLength );
157196
@@ -189,7 +228,7 @@ public BytesRef binaryValue() throws IOException {
189228 } else {
190229 // variable length
191230 final RandomAccessInput addressesData = this .data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
192- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData );
231+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData , merging );
193232 return new SparseBinaryDocValues (disi ) {
194233 final BytesRef bytes = new BytesRef (new byte [entry .maxLength ], 0 , entry .maxLength );
195234
@@ -353,19 +392,21 @@ public int lookupTerm(BytesRef key) throws IOException {
353392
354393 @ Override
355394 public TermsEnum termsEnum () throws IOException {
356- return new TermsDict (entry .termsDictEntry , data );
395+ return new TermsDict (entry .termsDictEntry , data , merging );
357396 }
358397 }
359398
360399 private abstract static class BaseSortedSetDocValues extends SortedSetDocValues {
361400
362401 final SortedSetEntry entry ;
363402 final IndexInput data ;
403+ final boolean merging ;
364404 final TermsEnum termsEnum ;
365405
366- BaseSortedSetDocValues (SortedSetEntry entry , IndexInput data ) throws IOException {
406+ BaseSortedSetDocValues (SortedSetEntry entry , IndexInput data , boolean merging ) throws IOException {
367407 this .entry = entry ;
368408 this .data = data ;
409+ this .merging = merging ;
369410 this .termsEnum = termsEnum ();
370411 }
371412
@@ -391,7 +432,7 @@ public long lookupTerm(BytesRef key) throws IOException {
391432
392433 @ Override
393434 public TermsEnum termsEnum () throws IOException {
394- return new TermsDict (entry .termsDictEntry , data );
435+ return new TermsDict (entry .termsDictEntry , data , merging );
395436 }
396437 }
397438
@@ -412,17 +453,17 @@ private static class TermsDict extends BaseTermsEnum {
412453 long currentCompressedBlockStart = -1 ;
413454 long currentCompressedBlockEnd = -1 ;
414455
415- TermsDict (TermsDictEntry entry , IndexInput data ) throws IOException {
456+ TermsDict (TermsDictEntry entry , IndexInput data , boolean merging ) throws IOException {
416457 this .entry = entry ;
417458 RandomAccessInput addressesSlice = data .randomAccessSlice (entry .termsAddressesOffset , entry .termsAddressesLength );
418- blockAddresses = DirectMonotonicReader .getInstance (entry .termsAddressesMeta , addressesSlice );
459+ blockAddresses = DirectMonotonicReader .getInstance (entry .termsAddressesMeta , addressesSlice , merging );
419460 bytes = data .slice ("terms" , entry .termsDataOffset , entry .termsDataLength );
420461 blockMask = (1L << TERMS_DICT_BLOCK_LZ4_SHIFT ) - 1 ;
421462 RandomAccessInput indexAddressesSlice = data .randomAccessSlice (
422463 entry .termsIndexAddressesOffset ,
423464 entry .termsIndexAddressesLength
424465 );
425- indexAddresses = DirectMonotonicReader .getInstance (entry .termsIndexAddressesMeta , indexAddressesSlice );
466+ indexAddresses = DirectMonotonicReader .getInstance (entry .termsIndexAddressesMeta , indexAddressesSlice , merging );
426467 indexBytes = data .randomAccessSlice (entry .termsIndexOffset , entry .termsIndexLength );
427468 term = new BytesRef (entry .maxTermLength );
428469
@@ -647,7 +688,7 @@ public SortedSetDocValues getSortedSet(FieldInfo field) throws IOException {
647688
648689 SortedNumericEntry ordsEntry = entry .ordsEntry ;
649690 final SortedNumericDocValues ords = getSortedNumeric (ordsEntry , entry .termsDictEntry .termsDictSize );
650- return new BaseSortedSetDocValues (entry , data ) {
691+ return new BaseSortedSetDocValues (entry , data , merging ) {
651692
652693 int i = 0 ;
653694 int count = 0 ;
@@ -1079,7 +1120,7 @@ public long longValue() {
10791120 // makes things slower.
10801121
10811122 final RandomAccessInput indexSlice = data .randomAccessSlice (entry .indexOffset , entry .indexLength );
1082- final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice );
1123+ final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice , merging );
10831124 final IndexInput valuesData = data .slice ("values" , entry .valuesOffset , entry .valuesLength );
10841125
10851126 final int bitsPerOrd = maxOrd >= 0 ? PackedInts .bitsRequired (maxOrd - 1 ) : -1 ;
@@ -1210,7 +1251,7 @@ public long longValue() throws IOException {
12101251 private NumericValues getValues (NumericEntry entry , final long maxOrd ) throws IOException {
12111252 assert entry .numValues > 0 ;
12121253 final RandomAccessInput indexSlice = data .randomAccessSlice (entry .indexOffset , entry .indexLength );
1213- final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice );
1254+ final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice , merging );
12141255
12151256 final IndexInput valuesData = data .slice ("values" , entry .valuesOffset , entry .valuesLength );
12161257 final int bitsPerOrd = maxOrd >= 0 ? PackedInts .bitsRequired (maxOrd - 1 ) : -1 ;
@@ -1247,7 +1288,7 @@ private SortedNumericDocValues getSortedNumeric(SortedNumericEntry entry, long m
12471288 }
12481289
12491290 final RandomAccessInput addressesInput = data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
1250- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesInput );
1291+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesInput , merging );
12511292
12521293 final NumericValues values = getValues (entry , maxOrd );
12531294
0 commit comments