|
17 | 17 | import org.elasticsearch.common.util.BitArray; |
18 | 18 | import org.elasticsearch.common.util.DoubleArray; |
19 | 19 | import org.elasticsearch.common.util.FloatArray; |
| 20 | +import org.elasticsearch.common.util.IntArray; |
20 | 21 | import org.elasticsearch.common.util.LongArray; |
21 | 22 | import org.elasticsearch.core.Releasable; |
22 | 23 | import org.elasticsearch.core.Releasables; |
@@ -714,4 +715,93 @@ protected final boolean docBetterThan(long index) { |
714 | 715 | } |
715 | 716 | } |
716 | 717 | } |
| 718 | + |
| 719 | + public abstract static class ForInts extends BucketedSort { |
| 720 | + private IntArray values; |
| 721 | + |
| 722 | + @SuppressWarnings("this-escape") |
| 723 | + public ForInts(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, ExtraData extra) { |
| 724 | + super(bigArrays, sortOrder, format, bucketSize, extra); |
| 725 | + boolean success = false; |
| 726 | + try { |
| 727 | + values = bigArrays.newIntArray(1, false); |
| 728 | + success = true; |
| 729 | + } finally { |
| 730 | + if (success == false) { |
| 731 | + close(); |
| 732 | + } |
| 733 | + } |
| 734 | + initGatherOffsets(); |
| 735 | + } |
| 736 | + |
| 737 | + @Override |
| 738 | + public final boolean needsScores() { |
| 739 | + return false; |
| 740 | + } |
| 741 | + |
| 742 | + @Override |
| 743 | + protected final BigArray values() { |
| 744 | + return values; |
| 745 | + } |
| 746 | + |
| 747 | + @Override |
| 748 | + protected final void growValues(long minSize) { |
| 749 | + values = bigArrays.grow(values, minSize); |
| 750 | + } |
| 751 | + |
| 752 | + @Override |
| 753 | + protected final int getNextGatherOffset(long rootIndex) { |
| 754 | + return values.get(rootIndex); |
| 755 | + } |
| 756 | + |
| 757 | + @Override |
| 758 | + protected final void setNextGatherOffset(long rootIndex, int offset) { |
| 759 | + values.set(rootIndex, offset); |
| 760 | + } |
| 761 | + |
| 762 | + @Override |
| 763 | + protected final SortValue getValue(long index) { |
| 764 | + return SortValue.from(values.get(index)); |
| 765 | + } |
| 766 | + |
| 767 | + @Override |
| 768 | + protected final boolean betterThan(long lhs, long rhs) { |
| 769 | + return getOrder().reverseMul() * Integer.compare(values.get(lhs), values.get(rhs)) < 0; |
| 770 | + } |
| 771 | + |
| 772 | + @Override |
| 773 | + protected final void swap(long lhs, long rhs) { |
| 774 | + int tmp = values.get(lhs); |
| 775 | + values.set(lhs, values.get(rhs)); |
| 776 | + values.set(rhs, tmp); |
| 777 | + } |
| 778 | + |
| 779 | + protected abstract class Leaf extends BucketedSort.Leaf { |
| 780 | + protected Leaf(LeafReaderContext ctx) { |
| 781 | + super(ctx); |
| 782 | + } |
| 783 | + |
| 784 | + /** |
| 785 | + * Return the value for of this sort for the document to which |
| 786 | + * we just {@link #advanceExact(int) moved}. This should be fast |
| 787 | + * because it is called twice per competitive hit when in heap |
| 788 | + * mode, once for {@link #docBetterThan(long)} and once |
| 789 | + * for {@link #setIndexToDocValue(long)}. |
| 790 | + */ |
| 791 | + protected abstract int docValue(); |
| 792 | + |
| 793 | + @Override |
| 794 | + public final void setScorer(Scorable scorer) {} |
| 795 | + |
| 796 | + @Override |
| 797 | + protected final void setIndexToDocValue(long index) { |
| 798 | + values.set(index, docValue()); |
| 799 | + } |
| 800 | + |
| 801 | + @Override |
| 802 | + protected final boolean docBetterThan(long index) { |
| 803 | + return getOrder().reverseMul() * Integer.compare(docValue(), values.get(index)) < 0; |
| 804 | + } |
| 805 | + } |
| 806 | + } |
717 | 807 | } |
0 commit comments