|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.aggregatemetric.mapper; |
| 9 | + |
| 10 | +import org.apache.lucene.index.DocValues; |
| 11 | +import org.apache.lucene.index.LeafReader; |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.index.NumericDocValues; |
| 14 | +import org.apache.lucene.util.NumericUtils; |
| 15 | +import org.elasticsearch.core.Releasables; |
| 16 | +import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 17 | +import org.elasticsearch.index.mapper.blockloader.docvalues.BlockDocValuesReader; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.EnumMap; |
| 21 | + |
| 22 | +public class AggregateMetricDoubleBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader { |
| 23 | + final NumberFieldMapper.NumberFieldType minFieldType; |
| 24 | + final NumberFieldMapper.NumberFieldType maxFieldType; |
| 25 | + final NumberFieldMapper.NumberFieldType sumFieldType; |
| 26 | + final NumberFieldMapper.NumberFieldType countFieldType; |
| 27 | + |
| 28 | + AggregateMetricDoubleBlockLoader(EnumMap<AggregateMetricDoubleFieldMapper.Metric, NumberFieldMapper.NumberFieldType> metricsRequested) { |
| 29 | + minFieldType = metricsRequested.getOrDefault(AggregateMetricDoubleFieldMapper.Metric.min, null); |
| 30 | + maxFieldType = metricsRequested.getOrDefault(AggregateMetricDoubleFieldMapper.Metric.max, null); |
| 31 | + sumFieldType = metricsRequested.getOrDefault(AggregateMetricDoubleFieldMapper.Metric.sum, null); |
| 32 | + countFieldType = metricsRequested.getOrDefault(AggregateMetricDoubleFieldMapper.Metric.value_count, null); |
| 33 | + } |
| 34 | + |
| 35 | + private static NumericDocValues getNumericDocValues(NumberFieldMapper.NumberFieldType field, LeafReader leafReader) throws IOException { |
| 36 | + if (field == null) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + String fieldName = field.name(); |
| 40 | + var values = leafReader.getNumericDocValues(fieldName); |
| 41 | + if (values != null) { |
| 42 | + return values; |
| 43 | + } |
| 44 | + |
| 45 | + var sortedValues = leafReader.getSortedNumericDocValues(fieldName); |
| 46 | + return DocValues.unwrapSingleton(sortedValues); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public AllReader reader(LeafReaderContext context) throws IOException { |
| 51 | + NumericDocValues minValues = getNumericDocValues(minFieldType, context.reader()); |
| 52 | + NumericDocValues maxValues = getNumericDocValues(maxFieldType, context.reader()); |
| 53 | + NumericDocValues sumValues = getNumericDocValues(sumFieldType, context.reader()); |
| 54 | + NumericDocValues valueCountValues = getNumericDocValues(countFieldType, context.reader()); |
| 55 | + |
| 56 | + return new BlockDocValuesReader() { |
| 57 | + |
| 58 | + private int docID = -1; |
| 59 | + |
| 60 | + @Override |
| 61 | + protected int docId() { |
| 62 | + return docID; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + return "BlockDocValuesReader.AggregateMetricDouble"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Block read(BlockFactory factory, Docs docs, int offset, boolean nullsFiltered) throws IOException { |
| 72 | + boolean success = false; |
| 73 | + Block minBlock = null; |
| 74 | + Block maxBlock = null; |
| 75 | + Block sumBlock = null; |
| 76 | + Block countBlock = null; |
| 77 | + try { |
| 78 | + minBlock = readDoubleSubblock(factory, docs, offset, minValues); |
| 79 | + maxBlock = readDoubleSubblock(factory, docs, offset, maxValues); |
| 80 | + sumBlock = readDoubleSubblock(factory, docs, offset, sumValues); |
| 81 | + countBlock = readIntSubblock(factory, docs, offset, valueCountValues); |
| 82 | + Block block = factory.buildAggregateMetricDoubleDirect(minBlock, maxBlock, sumBlock, countBlock); |
| 83 | + success = true; |
| 84 | + return block; |
| 85 | + } finally { |
| 86 | + if (success == false) { |
| 87 | + Releasables.closeExpectNoException(minBlock, maxBlock, sumBlock, countBlock); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private Block readDoubleSubblock(BlockFactory factory, Docs docs, int offset, NumericDocValues values) throws IOException { |
| 93 | + int count = docs.count() - offset; |
| 94 | + if (values == null) { |
| 95 | + return factory.constantNulls(count); |
| 96 | + } |
| 97 | + try (DoubleBuilder builder = factory.doubles(count)) { |
| 98 | + copyDoubleValuesToBuilder(docs, offset, builder, values); |
| 99 | + return builder.build(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private Block readIntSubblock(BlockFactory factory, Docs docs, int offset, NumericDocValues values) throws IOException { |
| 104 | + int count = docs.count() - offset; |
| 105 | + if (values == null) { |
| 106 | + return factory.constantNulls(count); |
| 107 | + } |
| 108 | + try (IntBuilder builder = factory.ints(count)) { |
| 109 | + copyIntValuesToBuilder(docs, offset, builder, values); |
| 110 | + return builder.build(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void copyDoubleValuesToBuilder(Docs docs, int offset, DoubleBuilder builder, NumericDocValues values) |
| 115 | + throws IOException { |
| 116 | + int lastDoc = -1; |
| 117 | + for (int i = offset; i < docs.count(); i++) { |
| 118 | + int doc = docs.get(i); |
| 119 | + if (doc < lastDoc) { |
| 120 | + throw new IllegalStateException("docs within same block must be in order"); |
| 121 | + } |
| 122 | + if (values == null || values.advanceExact(doc) == false) { |
| 123 | + builder.appendNull(); |
| 124 | + } else { |
| 125 | + double value = NumericUtils.sortableLongToDouble(values.longValue()); |
| 126 | + lastDoc = doc; |
| 127 | + this.docID = doc; |
| 128 | + builder.appendDouble(value); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void copyIntValuesToBuilder(Docs docs, int offset, IntBuilder builder, NumericDocValues values) throws IOException { |
| 134 | + int lastDoc = -1; |
| 135 | + for (int i = offset; i < docs.count(); i++) { |
| 136 | + int doc = docs.get(i); |
| 137 | + if (doc < lastDoc) { |
| 138 | + throw new IllegalStateException("docs within same block must be in order"); |
| 139 | + } |
| 140 | + if (values == null || values.advanceExact(doc) == false) { |
| 141 | + builder.appendNull(); |
| 142 | + } else { |
| 143 | + int value = Math.toIntExact(values.longValue()); |
| 144 | + lastDoc = doc; |
| 145 | + this.docID = doc; |
| 146 | + builder.appendInt(value); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void read(int docId, StoredFields storedFields, Builder builder) throws IOException { |
| 153 | + var blockBuilder = (AggregateMetricDoubleBuilder) builder; |
| 154 | + this.docID = docId; |
| 155 | + readSingleRow(docId, blockBuilder); |
| 156 | + } |
| 157 | + |
| 158 | + private void readSingleRow(int docId, AggregateMetricDoubleBuilder builder) throws IOException { |
| 159 | + if (minValues != null && minValues.advanceExact(docId)) { |
| 160 | + builder.min().appendDouble(NumericUtils.sortableLongToDouble(minValues.longValue())); |
| 161 | + } else { |
| 162 | + builder.min().appendNull(); |
| 163 | + } |
| 164 | + if (maxValues != null && maxValues.advanceExact(docId)) { |
| 165 | + builder.max().appendDouble(NumericUtils.sortableLongToDouble(maxValues.longValue())); |
| 166 | + } else { |
| 167 | + builder.max().appendNull(); |
| 168 | + } |
| 169 | + if (sumValues != null && sumValues.advanceExact(docId)) { |
| 170 | + builder.sum().appendDouble(NumericUtils.sortableLongToDouble(sumValues.longValue())); |
| 171 | + } else { |
| 172 | + builder.sum().appendNull(); |
| 173 | + } |
| 174 | + if (valueCountValues != null && valueCountValues.advanceExact(docId)) { |
| 175 | + builder.count().appendInt(Math.toIntExact(valueCountValues.longValue())); |
| 176 | + } else { |
| 177 | + builder.count().appendNull(); |
| 178 | + } |
| 179 | + } |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Builder builder(BlockFactory factory, int expectedCount) { |
| 185 | + return factory.aggregateMetricDoubleBuilder(expectedCount); |
| 186 | + } |
| 187 | +} |
0 commit comments