|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.common.lucene.search; |
| 11 | + |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.search.LeafFieldComparator; |
| 14 | +import org.apache.lucene.search.Pruning; |
| 15 | +import org.apache.lucene.search.comparators.LongComparator; |
| 16 | +import org.apache.lucene.util.NumericUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +// TODO: remove once upgrading to Lucene 10.3, see base class why. |
| 21 | +public class XLongComparator extends XNumericComparator<Long> { |
| 22 | + |
| 23 | + private final long[] values; |
| 24 | + protected long topValue; |
| 25 | + protected long bottom; |
| 26 | + |
| 27 | + public XLongComparator(int numHits, String field, Long missingValue, boolean reverse, Pruning pruning) { |
| 28 | + super(field, missingValue != null ? missingValue : 0L, reverse, pruning, Long.BYTES); |
| 29 | + values = new long[numHits]; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public int compare(int slot1, int slot2) { |
| 34 | + return Long.compare(values[slot1], values[slot2]); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void setTopValue(Long value) { |
| 39 | + super.setTopValue(value); |
| 40 | + topValue = value; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Long value(int slot) { |
| 45 | + return Long.valueOf(values[slot]); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected long missingValueAsComparableLong() { |
| 50 | + return missingValue; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected long sortableBytesToLong(byte[] bytes) { |
| 55 | + return NumericUtils.sortableBytesToLong(bytes, 0); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException { |
| 60 | + return new XLongComparator.LongLeafComparator(context); |
| 61 | + } |
| 62 | + |
| 63 | + /** Leaf comparator for {@link LongComparator} that provides skipping functionality */ |
| 64 | + public class LongLeafComparator extends XNumericComparator<Long>.NumericLeafComparator { |
| 65 | + |
| 66 | + public LongLeafComparator(LeafReaderContext context) throws IOException { |
| 67 | + super(context); |
| 68 | + } |
| 69 | + |
| 70 | + private long getValueForDoc(int doc) throws IOException { |
| 71 | + if (docValues.advanceExact(doc)) { |
| 72 | + return docValues.longValue(); |
| 73 | + } else { |
| 74 | + return missingValue; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void setBottom(int slot) throws IOException { |
| 80 | + bottom = values[slot]; |
| 81 | + super.setBottom(slot); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int compareBottom(int doc) throws IOException { |
| 86 | + return Long.compare(bottom, getValueForDoc(doc)); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int compareTop(int doc) throws IOException { |
| 91 | + return Long.compare(topValue, getValueForDoc(doc)); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void copy(int slot, int doc) throws IOException { |
| 96 | + values[slot] = getValueForDoc(doc); |
| 97 | + super.copy(slot, doc); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected long bottomAsComparableLong() { |
| 102 | + return bottom; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected long topAsComparableLong() { |
| 107 | + return topValue; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments