Skip to content

Commit

Permalink
Fix equals implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Jul 22, 2024
1 parent 85ea5f3 commit 2f6ffbb
Showing 1 changed file with 31 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.opensearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Predicate;

Expand All @@ -36,8 +38,6 @@
* after {@code size} is hit
*/
public abstract class ApproximatePointRangeQuery extends Query {
final int bytesPerDim;

private int size;

private SortOrder sortOrder;
Expand All @@ -60,35 +60,9 @@ protected ApproximatePointRangeQuery(String field, byte[] lowerPoint, byte[] upp
this.pointRangeQuery = new PointRangeQuery(field, lowerPoint, upperPoint, numDims) {
@Override
protected String toString(int dimension, byte[] value) {
final StringBuilder sb = new StringBuilder();
if (pointRangeQuery.getField().equals(field) == false) {
sb.append(pointRangeQuery.getField());
sb.append(':');
}

// print ourselves as "range per dimension"
for (int i = 0; i < pointRangeQuery.getNumDims(); i++) {
if (i > 0) {
sb.append(',');
}

int startOffset = bytesPerDim * i;

sb.append('[');
sb.append(
toString(i, ArrayUtil.copyOfSubArray(pointRangeQuery.getLowerPoint(), startOffset, startOffset + bytesPerDim))
);
sb.append(" TO ");
sb.append(
toString(i, ArrayUtil.copyOfSubArray(pointRangeQuery.getUpperPoint(), startOffset, startOffset + bytesPerDim))
);
sb.append(']');
}

return sb.toString();
return super.toString(field);
}
};
this.bytesPerDim = pointRangeQuery.getLowerPoint().length / pointRangeQuery.getNumDims();
}

public int getSize() {
Expand Down Expand Up @@ -121,7 +95,7 @@ public final Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, fl

return new ConstantScoreWeight(this, boost) {

private final ArrayUtil.ByteArrayComparator comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
private final ArrayUtil.ByteArrayComparator comparator = ArrayUtil.getUnsignedComparator(pointRangeQuery.getBytesPerDim());

private boolean matches(byte[] packedValue) {
return relate(packedValue, packedValue) != PointValues.Relation.CELL_OUTSIDE_QUERY;
Expand All @@ -132,7 +106,7 @@ private PointValues.Relation relate(byte[] minPackedValue, byte[] maxPackedValue
boolean crosses = false;

for (int dim = 0; dim < pointRangeQuery.getNumDims(); dim++) {
int offset = dim * bytesPerDim;
int offset = dim * pointRangeQuery.getBytesPerDim();

if (comparator.compare(minPackedValue, offset, pointRangeQuery.getUpperPoint(), offset) > 0
|| comparator.compare(maxPackedValue, offset, pointRangeQuery.getLowerPoint(), offset) < 0) {
Expand Down Expand Up @@ -217,14 +191,14 @@ private boolean checkValidPointValues(PointValues values) throws IOException {
+ pointRangeQuery.getNumDims()
);
}
if (bytesPerDim != values.getBytesPerDimension()) {
if (pointRangeQuery.getBytesPerDim() != values.getBytesPerDimension()) {
throw new IllegalArgumentException(
"field=\""
+ pointRangeQuery.getField()
+ "\" was indexed with bytesPerDim="
+ values.getBytesPerDimension()
+ " but this query has bytesPerDim="
+ bytesPerDim
+ pointRangeQuery.getBytesPerDim()
);
}
return true;
Expand Down Expand Up @@ -361,7 +335,7 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
final byte[] fieldPackedLower = values.getMinPackedValue();
final byte[] fieldPackedUpper = values.getMaxPackedValue();
for (int i = 0; i < pointRangeQuery.getNumDims(); ++i) {
int offset = i * bytesPerDim;
int offset = i * pointRangeQuery.getBytesPerDim();
if (comparator.compare(pointRangeQuery.getLowerPoint(), offset, fieldPackedUpper, offset) > 0
|| comparator.compare(pointRangeQuery.getUpperPoint(), offset, fieldPackedLower, offset) < 0) {
// If this query is a required clause of a boolean query, then returning null here
Expand All @@ -379,7 +353,7 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
final byte[] fieldPackedUpper = values.getMaxPackedValue();
allDocsMatch = true;
for (int i = 0; i < pointRangeQuery.getNumDims(); ++i) {
int offset = i * bytesPerDim;
int offset = i * pointRangeQuery.getBytesPerDim();
if (comparator.compare(pointRangeQuery.getLowerPoint(), offset, fieldPackedLower, offset) > 0
|| comparator.compare(pointRangeQuery.getUpperPoint(), offset, fieldPackedUpper, offset) < 0) {
allDocsMatch = false;
Expand Down Expand Up @@ -558,7 +532,15 @@ public final int hashCode() {

@Override
public final boolean equals(Object o) {
return pointRangeQuery.equals(o);
return sameClassAs(o) && equalsTo(getClass().cast(o));
}

private boolean equalsTo(ApproximatePointRangeQuery other) {
return Objects.equals(pointRangeQuery.getField(), other.pointRangeQuery.getField())
&& pointRangeQuery.getNumDims() == other.pointRangeQuery.getNumDims()
&& pointRangeQuery.getBytesPerDim() == other.pointRangeQuery.getBytesPerDim()
&& Arrays.equals(pointRangeQuery.getLowerPoint(), other.pointRangeQuery.getLowerPoint())
&& Arrays.equals(pointRangeQuery.getUpperPoint(), other.pointRangeQuery.getUpperPoint());
}

@Override
Expand All @@ -575,12 +557,22 @@ public final String toString(String field) {
sb.append(',');
}

int startOffset = bytesPerDim * i;
int startOffset = pointRangeQuery.getBytesPerDim() * i;

sb.append('[');
sb.append(toString(i, ArrayUtil.copyOfSubArray(pointRangeQuery.getLowerPoint(), startOffset, startOffset + bytesPerDim)));
sb.append(
toString(
i,
ArrayUtil.copyOfSubArray(pointRangeQuery.getLowerPoint(), startOffset, startOffset + pointRangeQuery.getBytesPerDim())
)
);
sb.append(" TO ");
sb.append(toString(i, ArrayUtil.copyOfSubArray(pointRangeQuery.getUpperPoint(), startOffset, startOffset + bytesPerDim)));
sb.append(
toString(
i,
ArrayUtil.copyOfSubArray(pointRangeQuery.getUpperPoint(), startOffset, startOffset + pointRangeQuery.getBytesPerDim())
)
);
sb.append(']');
}

Expand Down

0 comments on commit 2f6ffbb

Please sign in to comment.