|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.search.sort; |
| 10 | + |
| 11 | +import org.apache.lucene.index.LeafReaderContext; |
| 12 | +import org.apache.lucene.search.FieldComparator; |
| 13 | +import org.apache.lucene.search.LeafFieldComparator; |
| 14 | +import org.apache.lucene.search.Pruning; |
| 15 | +import org.apache.lucene.search.Scorable; |
| 16 | +import org.apache.lucene.search.SortField; |
| 17 | +import org.opensearch.common.util.BigArrays; |
| 18 | +import org.opensearch.index.fielddata.IndexFieldData; |
| 19 | +import org.opensearch.search.DocValueFormat; |
| 20 | +import org.opensearch.search.MultiValueMode; |
| 21 | +import org.opensearch.search.sort.BucketedSort.ExtraData; |
| 22 | + |
| 23 | +/** |
| 24 | + * A pseudo‑field (_shard_doc) comparator that tiebreaks by {@code (shardOrd << 32) | globalDocId} |
| 25 | + */ |
| 26 | +public class ShardDocFieldComparatorSource extends IndexFieldData.XFieldComparatorSource { |
| 27 | + public static final String NAME = "_shard_doc"; |
| 28 | + |
| 29 | + private final int shardId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @param shardId the ordinal of this shard within the coordinating node’s shard list |
| 33 | + */ |
| 34 | + public ShardDocFieldComparatorSource(int shardId) { |
| 35 | + super(null, MultiValueMode.MIN, null); |
| 36 | + this.shardId = shardId; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public SortField.Type reducedType() { |
| 41 | + return SortField.Type.LONG; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public BucketedSort newBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, ExtraData extra) { |
| 46 | + throw new UnsupportedOperationException("bucketed sort not supported for " + NAME); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public FieldComparator<Long> newComparator(String fieldname, int numHits, Pruning pruning, boolean reversed) { |
| 51 | + return new FieldComparator<Long>() { |
| 52 | + private final long[] values = new long[numHits]; |
| 53 | + private long bottom; |
| 54 | + private long topValue; |
| 55 | + |
| 56 | + @Override |
| 57 | + public LeafFieldComparator getLeafComparator(LeafReaderContext context) { |
| 58 | + // derive a stable shard ordinal per-segment |
| 59 | + long shardOrd = shardId; |
| 60 | + final int docBase = context.docBase; |
| 61 | + |
| 62 | + return new LeafFieldComparator() { |
| 63 | + Scorable scorer; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void setScorer(Scorable scorer) { |
| 67 | + this.scorer = scorer; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void setBottom(int slot) { |
| 72 | + bottom = values[slot]; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int compareBottom(int doc) { |
| 77 | + long key = ((long) shardId << 32) | (docBase + doc); |
| 78 | + return Long.compare(bottom, key); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void copy(int slot, int doc) { |
| 83 | + long key = ((long) shardId << 32) | (docBase + doc); |
| 84 | + values[slot] = key; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int compareTop(int doc) { |
| 89 | + long key = ((long) shardId << 32) | (docBase + doc); |
| 90 | + return Long.compare(topValue, key); |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int compare(int slot1, int slot2) { |
| 97 | + return Long.compare(values[slot1], values[slot2]); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Long value(int slot) { |
| 102 | + return values[slot]; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void setTopValue(Long value) { |
| 107 | + this.topValue = value; |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | +} |
0 commit comments