Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
easyice committed Jun 8, 2023
1 parent 4fa7a94 commit 9c9ca15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.lucene.search.CollectionTerminatedException;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.DocIdSetBuilder;

import java.io.IOException;
Expand Down Expand Up @@ -73,9 +74,9 @@ private class Visitor implements PointValues.IntersectVisitor {
final CompositeValuesCollectorQueue queue;
final DocIdSetBuilder builder;
final int maxDoc;
final int bytesPerDim;
final long lowerBucket;
final long upperBucket;
final ArrayUtil.ByteArrayComparator comparator;

DocIdSetBuilder bucketDocsBuilder;
DocIdSetBuilder.BulkAdder adder;
Expand All @@ -98,7 +99,7 @@ private class Visitor implements PointValues.IntersectVisitor {
this.lowerBucket = lowerBucket;
this.upperBucket = upperBucket;
this.bucketDocsBuilder = new DocIdSetBuilder(maxDoc);
this.bytesPerDim = bytesPerDim;
this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
}

@Override
Expand Down Expand Up @@ -142,9 +143,9 @@ public void visit(int docID, byte[] packedValue) throws IOException {

@Override
public PointValues.Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
if ((upperPointQuery != null && Arrays.compareUnsigned(minPackedValue, 0, bytesPerDim, upperPointQuery, 0, bytesPerDim) > 0)
if ((upperPointQuery != null && comparator.compare(minPackedValue, 0, upperPointQuery, 0) > 0)
|| (lowerPointQuery != null
&& Arrays.compareUnsigned(maxPackedValue, 0, bytesPerDim, lowerPointQuery, 0, bytesPerDim) < 0)) {
&& comparator.compare(maxPackedValue, 0, lowerPointQuery, 0) < 0)) {
// does not match the query
return PointValues.Relation.CELL_OUTSIDE_QUERY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.ScorerSupplier;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.ArrayUtil;

import java.io.IOException;
import java.util.Objects;

import static java.util.Arrays.compareUnsigned;

/**
* Query merging two point in range queries.
Expand All @@ -51,12 +51,14 @@ public static Query merge(PointRangeQuery lhs, PointRangeQuery rhs) {
private final String field;
private final Query delegateForMultiValuedSegments;
private final Query delegateForSingleValuedSegments;
private final ArrayUtil.ByteArrayComparator comparator;

private MergedPointRangeQuery(PointRangeQuery lhs, PointRangeQuery rhs) {
field = lhs.getField();
delegateForMultiValuedSegments = new BooleanQuery.Builder().add(lhs, Occur.MUST).add(rhs, Occur.MUST).build();
int numDims = lhs.getNumDims();
int bytesPerDim = lhs.getBytesPerDim();
this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
this.delegateForSingleValuedSegments = pickDelegateForSingleValuedSegments(
mergeBound(lhs.getLowerPoint(), rhs.getLowerPoint(), numDims, bytesPerDim, true),
mergeBound(lhs.getUpperPoint(), rhs.getUpperPoint(), numDims, bytesPerDim, false),
Expand All @@ -69,7 +71,7 @@ private Query pickDelegateForSingleValuedSegments(byte[] lower, byte[] upper, in
// If we ended up with disjoint ranges in any dimension then on single valued segments we can't match any docs.
for (int dim = 0; dim < numDims; dim++) {
int offset = dim * bytesPerDim;
if (compareUnsigned(lower, offset, offset + bytesPerDim, upper, offset, offset + bytesPerDim) > 0) {
if (comparator.compare(lower, offset, upper, offset) > 0) {
return new MatchNoDocsQuery("disjoint ranges");
}
}
Expand Down Expand Up @@ -206,11 +208,11 @@ public int hashCode() {
return Objects.hash(classHash(), delegateForMultiValuedSegments, delegateForSingleValuedSegments);
}

private static byte[] mergeBound(byte[] lhs, byte[] rhs, int numDims, int bytesPerDim, boolean lower) {
private byte[] mergeBound(byte[] lhs, byte[] rhs, int numDims, int bytesPerDim, boolean lower) {
byte[] merged = new byte[lhs.length];
for (int dim = 0; dim < numDims; dim++) {
int offset = dim * bytesPerDim;
boolean cmp = compareUnsigned(lhs, offset, offset + bytesPerDim, rhs, offset, offset + bytesPerDim) <= 0;
boolean cmp = comparator.compare(lhs, offset, rhs, offset) <= 0;
byte[] from = (cmp ^ lower) ? lhs : rhs;
System.arraycopy(from, offset, merged, offset, bytesPerDim);
}
Expand Down

0 comments on commit 9c9ca15

Please sign in to comment.