|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.aggregations.bucket.composite; |
| 10 | + |
| 11 | +import org.apache.lucene.index.IndexReader; |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.index.SortedSetDocValues; |
| 14 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 15 | +import org.apache.lucene.search.Query; |
| 16 | +import org.apache.lucene.util.BytesRef; |
| 17 | +import org.elasticsearch.common.util.BigArrays; |
| 18 | +import org.elasticsearch.common.util.LongArray; |
| 19 | +import org.elasticsearch.core.CheckedFunction; |
| 20 | +import org.elasticsearch.core.Releasables; |
| 21 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 22 | +import org.elasticsearch.index.mapper.StringFieldType; |
| 23 | +import org.elasticsearch.search.DocValueFormat; |
| 24 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +import static org.apache.lucene.index.SortedSetDocValues.NO_MORE_ORDS; |
| 29 | + |
| 30 | +/** |
| 31 | + * A {@link SingleDimensionValuesSource} for global ordinals. |
| 32 | + */ |
| 33 | +class GlobalOrdinalValuesSource extends SingleDimensionValuesSource<BytesRef> { |
| 34 | + private final CheckedFunction<LeafReaderContext, SortedSetDocValues, IOException> docValuesFunc; |
| 35 | + private LongArray values; |
| 36 | + private SortedSetDocValues lookup; |
| 37 | + private long currentValue; |
| 38 | + private Long afterValueGlobalOrd; |
| 39 | + private boolean isTopValueInsertionPoint; |
| 40 | + |
| 41 | + private long lastLookupOrd = -1; |
| 42 | + private BytesRef lastLookupValue; |
| 43 | + |
| 44 | + GlobalOrdinalValuesSource( |
| 45 | + BigArrays bigArrays, |
| 46 | + MappedFieldType type, |
| 47 | + CheckedFunction<LeafReaderContext, SortedSetDocValues, IOException> docValuesFunc, |
| 48 | + DocValueFormat format, |
| 49 | + boolean missingBucket, |
| 50 | + int size, |
| 51 | + int reverseMul |
| 52 | + ) { |
| 53 | + super(bigArrays, format, type, missingBucket, size, reverseMul); |
| 54 | + this.docValuesFunc = docValuesFunc; |
| 55 | + this.values = bigArrays.newLongArray(Math.min(size, 100), false); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + void copyCurrent(int slot) { |
| 60 | + values = bigArrays.grow(values, slot + 1); |
| 61 | + values.set(slot, currentValue); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + int compare(int from, int to) { |
| 66 | + return Long.compare(values.get(from), values.get(to)) * reverseMul; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + int compareCurrent(int slot) { |
| 71 | + return Long.compare(currentValue, values.get(slot)) * reverseMul; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + int compareCurrentWithAfter() { |
| 76 | + int cmp = Long.compare(currentValue, afterValueGlobalOrd); |
| 77 | + if (cmp == 0 && isTopValueInsertionPoint) { |
| 78 | + // the top value is missing in this shard, the comparison is against |
| 79 | + // the insertion point of the top value so equality means that the value |
| 80 | + // is "after" the insertion point. |
| 81 | + return reverseMul; |
| 82 | + } |
| 83 | + return cmp * reverseMul; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + int hashCode(int slot) { |
| 88 | + return Long.hashCode(values.get(slot)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + int hashCodeCurrent() { |
| 93 | + return Long.hashCode(currentValue); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + void setAfter(Comparable<?> value) { |
| 98 | + if (missingBucket && value == null) { |
| 99 | + afterValue = null; |
| 100 | + afterValueGlobalOrd = -1L; |
| 101 | + } else if (value.getClass() == String.class || (missingBucket && fieldType == null)) { |
| 102 | + // the value might be not string if this field is missing in this shard but present in other shards |
| 103 | + // and doesn't have a string type |
| 104 | + afterValue = format.parseBytesRef(value.toString()); |
| 105 | + } else { |
| 106 | + throw new IllegalArgumentException("invalid value, expected string, got " + value.getClass().getSimpleName()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + BytesRef toComparable(int slot) throws IOException { |
| 112 | + long globalOrd = values.get(slot); |
| 113 | + if (missingBucket && globalOrd == -1) { |
| 114 | + return null; |
| 115 | + } else if (globalOrd == lastLookupOrd) { |
| 116 | + return lastLookupValue; |
| 117 | + } else { |
| 118 | + lastLookupOrd = globalOrd; |
| 119 | + lastLookupValue = BytesRef.deepCopyOf(lookup.lookupOrd(values.get(slot))); |
| 120 | + return lastLookupValue; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + LeafBucketCollector getLeafCollector(LeafReaderContext context, LeafBucketCollector next) throws IOException { |
| 126 | + final SortedSetDocValues dvs = docValuesFunc.apply(context); |
| 127 | + if (lookup == null) { |
| 128 | + initLookup(dvs); |
| 129 | + } |
| 130 | + return new LeafBucketCollector() { |
| 131 | + @Override |
| 132 | + public void collect(int doc, long bucket) throws IOException { |
| 133 | + if (dvs.advanceExact(doc)) { |
| 134 | + long ord; |
| 135 | + while ((ord = dvs.nextOrd()) != NO_MORE_ORDS) { |
| 136 | + currentValue = ord; |
| 137 | + next.collect(doc, bucket); |
| 138 | + } |
| 139 | + } else if (missingBucket) { |
| 140 | + currentValue = -1; |
| 141 | + next.collect(doc, bucket); |
| 142 | + } |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + LeafBucketCollector getLeafCollector(Comparable<BytesRef> value, LeafReaderContext context, LeafBucketCollector next) |
| 149 | + throws IOException { |
| 150 | + if (value.getClass() != BytesRef.class) { |
| 151 | + throw new IllegalArgumentException("Expected BytesRef, got " + value.getClass()); |
| 152 | + } |
| 153 | + BytesRef term = (BytesRef) value; |
| 154 | + final SortedSetDocValues dvs = docValuesFunc.apply(context); |
| 155 | + if (lookup == null) { |
| 156 | + initLookup(dvs); |
| 157 | + } |
| 158 | + return new LeafBucketCollector() { |
| 159 | + boolean currentValueIsSet = false; |
| 160 | + |
| 161 | + @Override |
| 162 | + public void collect(int doc, long bucket) throws IOException { |
| 163 | + if (currentValueIsSet == false) { |
| 164 | + if (dvs.advanceExact(doc)) { |
| 165 | + long ord; |
| 166 | + while ((ord = dvs.nextOrd()) != NO_MORE_ORDS) { |
| 167 | + if (term.equals(lookup.lookupOrd(ord))) { |
| 168 | + currentValueIsSet = true; |
| 169 | + currentValue = ord; |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + assert currentValueIsSet; |
| 176 | + next.collect(doc, bucket); |
| 177 | + } |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + SortedDocsProducer createSortedDocsProducerOrNull(IndexReader reader, Query query) { |
| 183 | + if (checkIfSortedDocsIsApplicable(reader, fieldType) == false |
| 184 | + || fieldType instanceof StringFieldType == false |
| 185 | + || (query != null && query.getClass() != MatchAllDocsQuery.class)) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + return new TermsSortedDocsProducer(fieldType.name()); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void close() { |
| 193 | + Releasables.close(values); |
| 194 | + } |
| 195 | + |
| 196 | + private void initLookup(SortedSetDocValues dvs) throws IOException { |
| 197 | + lookup = dvs; |
| 198 | + if (afterValue != null && afterValueGlobalOrd == null) { |
| 199 | + afterValueGlobalOrd = lookup.lookupTerm(afterValue); |
| 200 | + if (afterValueGlobalOrd < 0) { |
| 201 | + // convert negative insert position |
| 202 | + afterValueGlobalOrd = -afterValueGlobalOrd - 1; |
| 203 | + isTopValueInsertionPoint = true; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments