Skip to content

Commit

Permalink
Spotless apply
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <carrofin@amazon.com>
  • Loading branch information
finnegancarroll committed Aug 2, 2024
1 parent d7772ac commit 50b20a8
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, Long> incrementDocCount, final LeafBucketCollector sub) throws IOException;
public abstract void tryOptimize(PointValues values, BiConsumer<Long, Long> incrementDocCount, final LeafBucketCollector sub)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,29 +43,40 @@ private boolean canOptimize(boolean missing, boolean hasScript, MappedFieldType
}

@Override
public final void tryOptimize(PointValues values, BiConsumer<Long, Long> incrementDocCount, final LeafBucketCollector sub) throws IOException {
public final void tryOptimize(PointValues values, BiConsumer<Long, Long> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,29 +124,40 @@ protected int getSize() {
}

@Override
public void tryOptimize(PointValues values, BiConsumer<Long, Long> incrementDocCount, final LeafBucketCollector sub) throws IOException {
public void tryOptimize(PointValues values, BiConsumer<Long, Long> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, Long> incrementDocCount, boolean segmentMatchAll)
throws IOException {
public boolean tryOptimize(
final LeafReaderContext leafCtx,
LeafBucketCollector sub,
final BiConsumer<Long, Long> incrementDocCount,
boolean segmentMatchAll
) throws IOException {
segments++;
if (!canOptimize) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,38 @@ public void prepareFromSegment(LeafReaderContext leaf) {
}

@Override
public final void tryOptimize(PointValues values, BiConsumer<Long, Long> incrementDocCount, final LeafBucketCollector sub) throws IOException {
public final void tryOptimize(PointValues values, BiConsumer<Long, Long> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 50b20a8

Please sign in to comment.