-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add ordinal range encode for tsid #133018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a5e4ee0
Add ordinal range encode for tsid
dnhatn 08813b6
Increase the threshold
dnhatn f3df32b
Merge remote-tracking branch 'elastic/main' into sorted-dv-codec
dnhatn 40c30da
blockshift
dnhatn 1480945
enable merging
dnhatn f1824d9
renaming
dnhatn d9bdf5e
primarySortFieldNumber
dnhatn 511e744
Merge remote-tracking branch 'elastic/main' into sorted-dv-codec
dnhatn 10c91f9
javadoc
dnhatn 90a6567
naming
dnhatn 3a0148a
oops
dnhatn 6bdfd69
Add tests
dnhatn 8dc9641
remove toIntExact
dnhatn 2023dc0
deleted docs
dnhatn 0301968
Update docs/changelog/133018.yaml
dnhatn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,11 +62,14 @@ final class ES819TSDBDocValuesConsumer extends XDocValuesConsumer { | |
| final int maxDoc; | ||
| private byte[] termsDictBuffer; | ||
| private final int skipIndexIntervalSize; | ||
| private final int minDocsPerOrdinalForOrdinalRangeEncoding; | ||
| final boolean enableOptimizedMerge; | ||
| private int primarySortField = -1; | ||
|
|
||
| ES819TSDBDocValuesConsumer( | ||
| SegmentWriteState state, | ||
| int skipIndexIntervalSize, | ||
| int minDocsPerOrdinalForOrdinalRangeEncoding, | ||
| boolean enableOptimizedMerge, | ||
| String dataCodec, | ||
| String dataExtension, | ||
|
|
@@ -75,6 +78,7 @@ final class ES819TSDBDocValuesConsumer extends XDocValuesConsumer { | |
| ) throws IOException { | ||
| this.termsDictBuffer = new byte[1 << 14]; | ||
| this.dir = state.directory; | ||
| this.minDocsPerOrdinalForOrdinalRangeEncoding = minDocsPerOrdinalForOrdinalRangeEncoding; | ||
| this.context = state.context; | ||
| boolean success = false; | ||
| try { | ||
|
|
@@ -99,6 +103,13 @@ final class ES819TSDBDocValuesConsumer extends XDocValuesConsumer { | |
| maxDoc = state.segmentInfo.maxDoc(); | ||
| this.skipIndexIntervalSize = skipIndexIntervalSize; | ||
| this.enableOptimizedMerge = enableOptimizedMerge; | ||
| final var indexSort = state.segmentInfo.getIndexSort(); | ||
| if (indexSort != null && indexSort.getSort().length > 0 && indexSort.getSort()[0].getReverse() == false) { | ||
| var sortField = state.fieldInfos.fieldInfo(indexSort.getSort()[0].getField()); | ||
| if (sortField != null) { | ||
| primarySortField = sortField.number; | ||
| } | ||
| } | ||
| success = true; | ||
| } finally { | ||
| if (success == false) { | ||
|
|
@@ -124,6 +135,10 @@ public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOExcepti | |
| writeField(field, producer, -1, null); | ||
| } | ||
|
|
||
| private boolean shouldEncodeOrdinalRange(FieldInfo field, long maxOrd, int numDocsWithValue) { | ||
| return maxDoc > 1 && field.number == primarySortField && (numDocsWithValue / maxOrd) >= minDocsPerOrdinalForOrdinalRangeEncoding; | ||
| } | ||
|
|
||
| private long[] writeField(FieldInfo field, TsdbDocValuesProducer valuesProducer, long maxOrd, OffsetsAccumulator offsetsAccumulator) | ||
| throws IOException { | ||
| int numDocsWithValue = 0; | ||
|
|
@@ -149,19 +164,52 @@ private long[] writeField(FieldInfo field, TsdbDocValuesProducer valuesProducer, | |
| try { | ||
| if (numValues > 0) { | ||
| assert numDocsWithValue > 0; | ||
| // Special case for maxOrd of 1, signal -1 that no blocks will be written | ||
| meta.writeInt(maxOrd != 1 ? ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT : -1); | ||
| final ByteBuffersDataOutput indexOut = new ByteBuffersDataOutput(); | ||
| final DirectMonotonicWriter indexWriter = DirectMonotonicWriter.getInstance( | ||
| meta, | ||
| new ByteBuffersIndexOutput(indexOut, "temp-dv-index", "temp-dv-index"), | ||
| 1L + ((numValues - 1) >>> ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SHIFT), | ||
| ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT | ||
| ); | ||
| DirectMonotonicWriter indexWriter = null; | ||
|
|
||
| final long valuesDataOffset = data.getFilePointer(); | ||
| // Special case for maxOrd of 1, skip writing the blocks | ||
| if (maxOrd != 1) { | ||
| if (maxOrd == 1) { | ||
| // Special case for maxOrd of 1, signal -1 that no blocks will be written | ||
| meta.writeInt(-1); | ||
| } else if (shouldEncodeOrdinalRange(field, maxOrd, numDocsWithValue)) { | ||
| // When a field is sorted, use ordinal range encode for long runs of the same ordinal. | ||
| meta.writeInt(-2); | ||
| meta.writeVInt(Math.toIntExact(maxOrd)); | ||
| values = valuesProducer.getSortedNumeric(field); | ||
| if (enableOptimizedMerge && numDocsWithValue < maxDoc) { | ||
| disiAccumulator = new DISIAccumulator(dir, context, data, IndexedDISI.DEFAULT_DENSE_RANK_POWER); | ||
| } | ||
| DirectMonotonicWriter startDocs = DirectMonotonicWriter.getInstance( | ||
| meta, | ||
| data, | ||
| maxOrd + 1, | ||
| ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT | ||
| ); | ||
| long lastOrd = 0; | ||
| startDocs.add(0); | ||
| for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) { | ||
| if (disiAccumulator != null) { | ||
| disiAccumulator.addDocId(doc); | ||
| } | ||
| if (offsetsAccumulator != null) { | ||
| offsetsAccumulator.addDoc(1); | ||
| } | ||
| final long nextOrd = values.nextValue(); | ||
| if (nextOrd != lastOrd) { | ||
| lastOrd = nextOrd; | ||
| startDocs.add(doc); | ||
| } | ||
| } | ||
| startDocs.add(maxDoc); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that we've already inserted maxDoc in the loop above?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not, because startDocs should be at most |
||
| startDocs.finish(); | ||
| } else { | ||
| indexWriter = DirectMonotonicWriter.getInstance( | ||
| meta, | ||
| new ByteBuffersIndexOutput(indexOut, "temp-dv-index", "temp-dv-index"), | ||
| 1L + ((numValues - 1) >>> ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SHIFT), | ||
| ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT | ||
| ); | ||
| meta.writeInt(DIRECT_MONOTONIC_BLOCK_SHIFT); | ||
| final long[] buffer = new long[ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE]; | ||
| int bufferSize = 0; | ||
| final TSDBDocValuesEncoder encoder = new TSDBDocValuesEncoder(ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE); | ||
|
|
@@ -204,8 +252,7 @@ private long[] writeField(FieldInfo field, TsdbDocValuesProducer valuesProducer, | |
| } | ||
|
|
||
| final long valuesDataLength = data.getFilePointer() - valuesDataOffset; | ||
| if (maxOrd != 1) { | ||
| // Special case for maxOrd of 1, indexWriter isn't really used, so no need to invoke finish() method. | ||
| if (indexWriter != null) { | ||
| indexWriter.finish(); | ||
| } | ||
| final long indexDataOffset = data.getFilePointer(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.