4646import static org .elasticsearch .index .codec .tsdb .ES87TSDBDocValuesFormat .TERMS_DICT_BLOCK_LZ4_SHIFT ;
4747
4848public class ES87TSDBDocValuesProducer extends DocValuesProducer {
49- private final Map <String , NumericEntry > numerics = new HashMap <>() ;
50- private final Map <String , BinaryEntry > binaries = new HashMap <>() ;
51- private final Map <String , SortedEntry > sorted = new HashMap <>() ;
52- private final Map <String , SortedSetEntry > sortedSets = new HashMap <>() ;
53- private final Map <String , SortedNumericEntry > sortedNumerics = new HashMap <>() ;
49+ private final Map <String , NumericEntry > numerics ;
50+ private final Map <String , BinaryEntry > binaries ;
51+ private final Map <String , SortedEntry > sorted ;
52+ private final Map <String , SortedSetEntry > sortedSets ;
53+ private final Map <String , SortedNumericEntry > sortedNumerics ;
5454 private final IndexInput data ;
5555 private final int maxDoc ;
56+ private final int version ;
57+ private final boolean merging ;
5658
5759 ES87TSDBDocValuesProducer (SegmentReadState state , String dataCodec , String dataExtension , String metaCodec , String metaExtension )
5860 throws IOException {
59- String metaName = IndexFileNames .segmentFileName (state .segmentInfo .name , state .segmentSuffix , metaExtension );
61+ this .numerics = new HashMap <>();
62+ this .binaries = new HashMap <>();
63+ this .sorted = new HashMap <>();
64+ this .sortedSets = new HashMap <>();
65+ this .sortedNumerics = new HashMap <>();
6066 this .maxDoc = state .segmentInfo .maxDoc ();
67+ this .merging = false ;
6168
6269 // read in the entries from the metadata file.
6370 int version = -1 ;
71+ String metaName = IndexFileNames .segmentFileName (state .segmentInfo .name , state .segmentSuffix , metaExtension );
6472 try (ChecksumIndexInput in = state .directory .openChecksumInput (metaName , state .context )) {
6573 Throwable priorE = null ;
6674
@@ -106,13 +114,41 @@ public class ES87TSDBDocValuesProducer extends DocValuesProducer {
106114 CodecUtil .retrieveChecksum (data );
107115
108116 success = true ;
117+ this .version = version ;
109118 } finally {
110119 if (success == false ) {
111120 IOUtils .closeWhileHandlingException (this .data );
112121 }
113122 }
114123 }
115124
125+ private ES87TSDBDocValuesProducer (
126+ Map <String , NumericEntry > numerics ,
127+ Map <String , BinaryEntry > binaries ,
128+ Map <String , SortedEntry > sorted ,
129+ Map <String , SortedSetEntry > sortedSets ,
130+ Map <String , SortedNumericEntry > sortedNumerics ,
131+ IndexInput data ,
132+ int maxDoc ,
133+ int version ,
134+ boolean merging
135+ ) {
136+ this .numerics = numerics ;
137+ this .binaries = binaries ;
138+ this .sorted = sorted ;
139+ this .sortedSets = sortedSets ;
140+ this .sortedNumerics = sortedNumerics ;
141+ this .data = data .clone ();
142+ this .maxDoc = maxDoc ;
143+ this .version = version ;
144+ this .merging = merging ;
145+ }
146+
147+ @ Override
148+ public DocValuesProducer getMergeInstance () {
149+ return new ES87TSDBDocValuesProducer (numerics , binaries , sorted , sortedSets , sortedNumerics , data , maxDoc , version , true );
150+ }
151+
116152 @ Override
117153 public NumericDocValues getNumeric (FieldInfo field ) throws IOException {
118154 NumericEntry entry = numerics .get (field .name );
@@ -146,7 +182,7 @@ public BytesRef binaryValue() throws IOException {
146182 } else {
147183 // variable length
148184 final RandomAccessInput addressesData = this .data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
149- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData );
185+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData , merging );
150186 return new DenseBinaryDocValues (maxDoc ) {
151187 final BytesRef bytes = new BytesRef (new byte [entry .maxLength ], 0 , entry .maxLength );
152188
@@ -186,7 +222,7 @@ public BytesRef binaryValue() throws IOException {
186222 } else {
187223 // variable length
188224 final RandomAccessInput addressesData = this .data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
189- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData );
225+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesData , merging );
190226 return new SparseBinaryDocValues (disi ) {
191227 final BytesRef bytes = new BytesRef (new byte [entry .maxLength ], 0 , entry .maxLength );
192228
@@ -351,19 +387,21 @@ public int lookupTerm(BytesRef key) throws IOException {
351387
352388 @ Override
353389 public TermsEnum termsEnum () throws IOException {
354- return new TermsDict (entry .termsDictEntry , data );
390+ return new TermsDict (entry .termsDictEntry , data , merging );
355391 }
356392 }
357393
358394 private abstract class BaseSortedSetDocValues extends SortedSetDocValues {
359395
360396 final SortedSetEntry entry ;
361397 final IndexInput data ;
398+ final boolean merging ;
362399 final TermsEnum termsEnum ;
363400
364- BaseSortedSetDocValues (SortedSetEntry entry , IndexInput data ) throws IOException {
401+ BaseSortedSetDocValues (SortedSetEntry entry , IndexInput data , boolean merging ) throws IOException {
365402 this .entry = entry ;
366403 this .data = data ;
404+ this .merging = merging ;
367405 this .termsEnum = termsEnum ();
368406 }
369407
@@ -389,7 +427,7 @@ public long lookupTerm(BytesRef key) throws IOException {
389427
390428 @ Override
391429 public TermsEnum termsEnum () throws IOException {
392- return new TermsDict (entry .termsDictEntry , data );
430+ return new TermsDict (entry .termsDictEntry , data , merging );
393431 }
394432 }
395433
@@ -410,17 +448,17 @@ private static class TermsDict extends BaseTermsEnum {
410448 long currentCompressedBlockStart = -1 ;
411449 long currentCompressedBlockEnd = -1 ;
412450
413- TermsDict (TermsDictEntry entry , IndexInput data ) throws IOException {
451+ TermsDict (TermsDictEntry entry , IndexInput data , boolean merging ) throws IOException {
414452 this .entry = entry ;
415453 RandomAccessInput addressesSlice = data .randomAccessSlice (entry .termsAddressesOffset , entry .termsAddressesLength );
416- blockAddresses = DirectMonotonicReader .getInstance (entry .termsAddressesMeta , addressesSlice );
454+ blockAddresses = DirectMonotonicReader .getInstance (entry .termsAddressesMeta , addressesSlice , merging );
417455 bytes = data .slice ("terms" , entry .termsDataOffset , entry .termsDataLength );
418456 blockMask = (1L << TERMS_DICT_BLOCK_LZ4_SHIFT ) - 1 ;
419457 RandomAccessInput indexAddressesSlice = data .randomAccessSlice (
420458 entry .termsIndexAddressesOffset ,
421459 entry .termsIndexAddressesLength
422460 );
423- indexAddresses = DirectMonotonicReader .getInstance (entry .termsIndexAddressesMeta , indexAddressesSlice );
461+ indexAddresses = DirectMonotonicReader .getInstance (entry .termsIndexAddressesMeta , indexAddressesSlice , merging );
424462 indexBytes = data .slice ("terms-index" , entry .termsIndexOffset , entry .termsIndexLength );
425463 term = new BytesRef (entry .maxTermLength );
426464
@@ -646,7 +684,7 @@ public SortedSetDocValues getSortedSet(FieldInfo field) throws IOException {
646684
647685 SortedNumericEntry ordsEntry = entry .ordsEntry ;
648686 final SortedNumericDocValues ords = getSortedNumeric (ordsEntry , entry .termsDictEntry .termsDictSize );
649- return new BaseSortedSetDocValues (entry , data ) {
687+ return new BaseSortedSetDocValues (entry , data , merging ) {
650688
651689 int i = 0 ;
652690 int count = 0 ;
@@ -955,7 +993,7 @@ public long longValue() {
955993 // makes things slower.
956994
957995 final RandomAccessInput indexSlice = data .randomAccessSlice (entry .indexOffset , entry .indexLength );
958- final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice );
996+ final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice , merging );
959997 final IndexInput valuesData = data .slice ("values" , entry .valuesOffset , entry .valuesLength );
960998
961999 final int bitsPerOrd = maxOrd >= 0 ? PackedInts .bitsRequired (maxOrd - 1 ) : -1 ;
@@ -1086,7 +1124,7 @@ public long longValue() throws IOException {
10861124 private NumericValues getValues (NumericEntry entry , final long maxOrd ) throws IOException {
10871125 assert entry .numValues > 0 ;
10881126 final RandomAccessInput indexSlice = data .randomAccessSlice (entry .indexOffset , entry .indexLength );
1089- final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice );
1127+ final DirectMonotonicReader indexReader = DirectMonotonicReader .getInstance (entry .indexMeta , indexSlice , merging );
10901128
10911129 final IndexInput valuesData = data .slice ("values" , entry .valuesOffset , entry .valuesLength );
10921130 final int bitsPerOrd = maxOrd >= 0 ? PackedInts .bitsRequired (maxOrd - 1 ) : -1 ;
@@ -1123,7 +1161,7 @@ private SortedNumericDocValues getSortedNumeric(SortedNumericEntry entry, long m
11231161 }
11241162
11251163 final RandomAccessInput addressesInput = data .randomAccessSlice (entry .addressesOffset , entry .addressesLength );
1126- final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesInput );
1164+ final LongValues addresses = DirectMonotonicReader .getInstance (entry .addressesMeta , addressesInput , merging );
11271165
11281166 final NumericValues values = getValues (entry , maxOrd );
11291167
0 commit comments