From 50b20a8bab25ca51e772be65e70f353c79e80741 Mon Sep 17 00:00:00 2001 From: Finn Carroll Date: Fri, 2 Aug 2024 15:04:11 -0700 Subject: [PATCH] Spotless apply Signed-off-by: Finn Carroll --- .../filterrewrite/AggregatorBridge.java | 3 +- .../CompositeAggregatorBridge.java | 46 +++++++++++------- .../DateHistogramAggregatorBridge.java | 48 +++++++++++-------- .../filterrewrite/OptimizationContext.java | 8 +++- .../filterrewrite/RangeAggregatorBridge.java | 39 +++++++++------ .../optimization/filterrewrite/Ranges.java | 29 ++++++++--- .../filterrewrite/TreeTraversal.java | 10 ++-- 7 files changed, 118 insertions(+), 65 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/AggregatorBridge.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/AggregatorBridge.java index 17b8c9db9a782..f57dab3e5313d 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/AggregatorBridge.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/AggregatorBridge.java @@ -73,5 +73,6 @@ void setOptimizationContext(OptimizationContext context) { * @param values the point values (index structure for numeric values) for a segment * @param incrementDocCount a consumer to increment the document count for a range bucket. The First parameter is document count, the second is the key of the bucket */ - public abstract void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) throws IOException; + public abstract void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) + throws IOException; } diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/CompositeAggregatorBridge.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/CompositeAggregatorBridge.java index 74fe30720b85d..b84897f793ea4 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/CompositeAggregatorBridge.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/CompositeAggregatorBridge.java @@ -17,7 +17,6 @@ import org.opensearch.search.aggregations.bucket.composite.RoundingValuesSource; import java.io.IOException; -import java.util.List; import java.util.function.BiConsumer; import static org.opensearch.search.optimization.filterrewrite.TreeTraversal.multiRangesTraverse; @@ -44,29 +43,40 @@ private boolean canOptimize(boolean missing, boolean hasScript, MappedFieldType } @Override - public final void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) throws IOException { + public final void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) + throws IOException { DateFieldMapper.DateFieldType fieldType = getFieldType(); TreeTraversal.RangeAwareIntersectVisitor treeVisitor; if (sub != null) { - treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), getSize(), (activeIndex, docID) -> { - long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); - rangeStart = fieldType.convertNanosToMillis(rangeStart); - long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); + treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + getSize(), + (activeIndex, docID) -> { + long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); + rangeStart = fieldType.convertNanosToMillis(rangeStart); + long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); - try { - incrementDocCount.accept(ord, (long) 1); - sub.collect(docID, ord); - } catch ( IOException ioe) { - throw new RuntimeException(ioe); + try { + incrementDocCount.accept(ord, (long) 1); + sub.collect(docID, ord); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } } - }); + ); } else { - treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), getSize(), (activeIndex, docCount) -> { - long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); - rangeStart = fieldType.convertNanosToMillis(rangeStart); - long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); - incrementDocCount.accept(ord, (long) docCount); - }); + treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + getSize(), + (activeIndex, docCount) -> { + long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); + rangeStart = fieldType.convertNanosToMillis(rangeStart); + long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); + incrementDocCount.accept(ord, (long) docCount); + } + ); } optimizationContext.consumeDebugInfo(multiRangesTraverse(treeVisitor)); diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/DateHistogramAggregatorBridge.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/DateHistogramAggregatorBridge.java index 06d0e251a4105..164e6a2c9aefd 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/DateHistogramAggregatorBridge.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/DateHistogramAggregatorBridge.java @@ -22,7 +22,6 @@ import org.opensearch.search.internal.SearchContext; import java.io.IOException; -import java.util.List; import java.util.OptionalLong; import java.util.function.BiConsumer; import java.util.function.Function; @@ -125,29 +124,40 @@ protected int getSize() { } @Override - public void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) throws IOException { + public void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) + throws IOException { DateFieldMapper.DateFieldType fieldType = getFieldType(); TreeTraversal.RangeAwareIntersectVisitor treeVisitor; if (sub != null) { - treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), getSize(), (activeIndex, docID) -> { - long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); - rangeStart = fieldType.convertNanosToMillis(rangeStart); - long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); - - try { - incrementDocCount.accept(ord, (long) 1); - sub.collect(docID, ord); - } catch ( IOException ioe) { - throw new RuntimeException(ioe); + treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + getSize(), + (activeIndex, docID) -> { + long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); + rangeStart = fieldType.convertNanosToMillis(rangeStart); + long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); + + try { + incrementDocCount.accept(ord, (long) 1); + sub.collect(docID, ord); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } } - }); + ); } else { - treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), getSize(), (activeIndex, docCount) -> { - long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); - rangeStart = fieldType.convertNanosToMillis(rangeStart); - long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); - incrementDocCount.accept(ord, (long) docCount); - }); + treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + getSize(), + (activeIndex, docCount) -> { + long rangeStart = LongPoint.decodeDimension(optimizationContext.getRanges().lowers[activeIndex], 0); + rangeStart = fieldType.convertNanosToMillis(rangeStart); + long ord = getBucketOrd(bucketOrdProducer().apply(rangeStart)); + incrementDocCount.accept(ord, (long) docCount); + } + ); } optimizationContext.consumeDebugInfo(multiRangesTraverse(treeVisitor)); diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/OptimizationContext.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/OptimizationContext.java index 896867ebafd53..98c42f3aea049 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/OptimizationContext.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/OptimizationContext.java @@ -102,8 +102,12 @@ Ranges getRanges() { * @param incrementDocCount consume the doc_count results for certain ordinal * @param segmentMatchAll if your optimization can prepareFromSegment, you should pass in this flag to decide whether to prepareFromSegment */ - public boolean tryOptimize(final LeafReaderContext leafCtx, LeafBucketCollector sub, final BiConsumer incrementDocCount, boolean segmentMatchAll) - throws IOException { + public boolean tryOptimize( + final LeafReaderContext leafCtx, + LeafBucketCollector sub, + final BiConsumer incrementDocCount, + boolean segmentMatchAll + ) throws IOException { segments++; if (!canOptimize) { return false; diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/RangeAggregatorBridge.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/RangeAggregatorBridge.java index 0c276891f345d..6fda3b4ea6951 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/RangeAggregatorBridge.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/RangeAggregatorBridge.java @@ -75,27 +75,38 @@ public void prepareFromSegment(LeafReaderContext leaf) { } @Override - public final void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) throws IOException { + public final void tryOptimize(PointValues values, BiConsumer incrementDocCount, final LeafBucketCollector sub) + throws IOException { TreeTraversal.RangeAwareIntersectVisitor treeVisitor; if (sub != null) { - treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), Integer.MAX_VALUE, (activeIndex, docID) -> { - long ord = bucketOrdProducer().apply(activeIndex); + treeVisitor = new TreeTraversal.DocCollectRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + Integer.MAX_VALUE, + (activeIndex, docID) -> { + long ord = bucketOrdProducer().apply(activeIndex); - try { - incrementDocCount.accept(ord, (long) 1); - sub.collect(docID, ord); - } catch ( IOException ioe) { - throw new RuntimeException(ioe); + try { + incrementDocCount.accept(ord, (long) 1); + sub.collect(docID, ord); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } } - }); + ); } else { - treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor(values.getPointTree(), optimizationContext.getRanges(), Integer.MAX_VALUE, (activeIndex, docCount) -> { - long ord = bucketOrdProducer().apply(activeIndex); - incrementDocCount.accept(ord, (long) docCount); - }); + treeVisitor = new TreeTraversal.DocCountRangeAwareIntersectVisitor( + values.getPointTree(), + optimizationContext.getRanges(), + Integer.MAX_VALUE, + (activeIndex, docCount) -> { + long ord = bucketOrdProducer().apply(activeIndex); + incrementDocCount.accept(ord, (long) docCount); + } + ); } - optimizationContext.consumeDebugInfo(multiRangesTraverse(treeVisitor)); + optimizationContext.consumeDebugInfo(multiRangesTraverse(treeVisitor)); } /** diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/Ranges.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/Ranges.java index 18c88a4af2e67..c9aaf0b39f9c9 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/Ranges.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/Ranges.java @@ -29,12 +29,29 @@ public Ranges(byte[][] lowers, byte[][] uppers) { comparator = ArrayUtil.getUnsignedComparator(byteLen); } - public static int compareByteValue(byte[] value1, byte[] value2) { return comparator.compare(value1, 0, value2, 0); } - public static boolean withinLowerBound(byte[] value, byte[] lowerBound) { return compareByteValue(value, lowerBound) >= 0; } - public static boolean withinUpperBound(byte[] value, byte[] upperBound) { return compareByteValue(value, upperBound) < 0; } - public boolean withinLowerBound(byte[] value, int idx) { return Ranges.withinLowerBound(value, lowers[idx]); } - public boolean withinUpperBound(byte[] value, int idx) { return Ranges.withinUpperBound(value, uppers[idx]); } - public boolean withinRange(byte[] value, int idx) { return withinLowerBound(value, idx) && withinUpperBound(value, idx); } + public static int compareByteValue(byte[] value1, byte[] value2) { + return comparator.compare(value1, 0, value2, 0); + } + + public static boolean withinLowerBound(byte[] value, byte[] lowerBound) { + return compareByteValue(value, lowerBound) >= 0; + } + + public static boolean withinUpperBound(byte[] value, byte[] upperBound) { + return compareByteValue(value, upperBound) < 0; + } + + public boolean withinLowerBound(byte[] value, int idx) { + return Ranges.withinLowerBound(value, lowers[idx]); + } + + public boolean withinUpperBound(byte[] value, int idx) { + return Ranges.withinUpperBound(value, uppers[idx]); + } + + public boolean withinRange(byte[] value, int idx) { + return withinLowerBound(value, idx) && withinUpperBound(value, idx); + } public int firstRangeIndex(byte[] globalMin, byte[] globalMax) { if (compareByteValue(lowers[0], globalMax) > 0) { diff --git a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/TreeTraversal.java b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/TreeTraversal.java index 8ff3821f45dc8..65a72bbbc89fd 100644 --- a/server/src/main/java/org/opensearch/search/optimization/filterrewrite/TreeTraversal.java +++ b/server/src/main/java/org/opensearch/search/optimization/filterrewrite/TreeTraversal.java @@ -65,11 +65,7 @@ public static abstract class RangeAwareIntersectVisitor implements PointValues.I protected int visitedRange = 0; protected int activeIndex; - public RangeAwareIntersectVisitor( - PointValues.PointTree pointTree, - Ranges ranges, - int maxNumNonZeroRange - ) { + public RangeAwareIntersectVisitor(PointValues.PointTree pointTree, Ranges ranges, int maxNumNonZeroRange) { this.ranges = ranges; this.pointTree = pointTree; this.maxNumNonZeroRange = maxNumNonZeroRange; @@ -81,9 +77,13 @@ public int getActiveIndex() { } public abstract void visit(int docID); + public abstract void visit(int docID, byte[] packedValue); + public abstract void visit(DocIdSetIterator iterator, byte[] packedValue) throws IOException; + protected abstract void consumeContainedNode(PointValues.PointTree pointTree) throws IOException; + protected abstract void consumeCrossedNode(PointValues.PointTree pointTree) throws IOException; public void traverse(OptimizationContext.DebugInfo debug) throws IOException {