diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java index 944fadfb2bb..bee864f8966 100644 --- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java @@ -103,7 +103,7 @@ public abstract class PointInSetQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public final Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { // We don't use RandomAccessWeight here: it's no good to approximate with "match all docs". // This is an inverted structure and should be used in the first pass: @@ -161,14 +161,12 @@ public abstract class PointInSetQuery extends Query { private final DocIdSetBuilder result; private TermIterator iterator; private BytesRef nextQueryPoint; - private final byte[] lastMaxPackedValue; private final BytesRef scratch = new BytesRef(); private final PrefixCodedTerms sortedPackedPoints; public MergePointVisitor(PrefixCodedTerms sortedPackedPoints, DocIdSetBuilder result) throws IOException { this.result = result; this.sortedPackedPoints = sortedPackedPoints; - lastMaxPackedValue = new byte[bytesPerDim]; scratch.length = bytesPerDim; this.iterator = sortedPackedPoints.iterator(); nextQueryPoint = iterator.next(); @@ -304,7 +302,7 @@ public abstract class PointInSetQuery extends Query { } @Override - public int hashCode() { + public final int hashCode() { int hash = super.hashCode(); hash = 31 * hash + sortedPackedPointsHashCode; hash = 31 * hash + numDims; @@ -313,7 +311,7 @@ public abstract class PointInSetQuery extends Query { } @Override - public boolean equals(Object other) { + public final boolean equals(Object other) { if (super.equals(other)) { final PointInSetQuery q = (PointInSetQuery) other; return q.numDims == numDims && @@ -326,7 +324,7 @@ public abstract class PointInSetQuery extends Query { } @Override - public String toString(String field) { + public final String toString(String field) { final StringBuilder sb = new StringBuilder(); if (this.field.equals(field) == false) { sb.append(this.field); diff --git a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java index ebbe7e2fbd6..9384d235d3b 100644 --- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java @@ -109,7 +109,7 @@ public abstract class PointRangeQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public final Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { // We don't use RandomAccessWeight here: it's no good to approximate with "match all docs". // This is an inverted structure and should be used in the first pass: @@ -239,7 +239,7 @@ public abstract class PointRangeQuery extends Query { } @Override - public int hashCode() { + public final int hashCode() { int hash = super.hashCode(); hash = 31 * hash + Arrays.hashCode(lowerPoint); hash = 31 * hash + Arrays.hashCode(upperPoint); @@ -249,20 +249,36 @@ public abstract class PointRangeQuery extends Query { } @Override - public boolean equals(Object other) { - if (super.equals(other)) { - final PointRangeQuery q = (PointRangeQuery) other; - return q.numDims == numDims && - q.bytesPerDim == bytesPerDim && - Arrays.equals(lowerPoint, q.lowerPoint) && - Arrays.equals(upperPoint, q.upperPoint); + public final boolean equals(Object other) { + if (super.equals(other) == false) { + return false; } - return false; + final PointRangeQuery q = (PointRangeQuery) other; + if (q.numDims != numDims) { + return false; + } + + if (q.bytesPerDim != bytesPerDim) { + return false; + } + + // Cannot use Arrays.equals here, because it in turn uses byte[].equals + // to compare each value, which only uses "==" + for(int dim=0;dim() { + @Override + public int compare(byte[] a, byte[] b) { + return StringHelper.compare(BYTES, a, 0, b, 0); + } + }); final BytesRef encoded = new BytesRef(new byte[BYTES]); @@ -230,7 +245,7 @@ public class InetAddressPoint extends Field { if (upto == sortedValues.length) { return null; } else { - encoded.bytes = encode(sortedValues[upto]); + encoded.bytes = sortedValues[upto]; assert encoded.bytes.length == encoded.length; upto++; return encoded; diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java index 500c2a32061..8f38bcd1aa4 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java +++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java @@ -21,6 +21,7 @@ import java.math.BigInteger; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -93,4 +94,18 @@ public class TestBigIntegerPoint extends LuceneTestCase { new BigInteger[] {BigInteger.valueOf(17), BigInteger.valueOf(42)}).toString()); assertEquals("field:{1}", BigIntegerPoint.newSetQuery("field", BigInteger.ONE).toString()); } + + public void testQueryEquals() throws Exception { + Query q = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000)); + assertEquals(q, BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000))); + assertFalse(q.equals(BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(1), BigInteger.valueOf(1000)))); + + q = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000)); + assertEquals(q, BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000))); + assertFalse(q.equals(BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1)))); + + q = BigIntegerPoint.newSetQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000), BigInteger.valueOf(17)); + assertEquals(q, BigIntegerPoint.newSetQuery("a", BigInteger.valueOf(17), BigInteger.valueOf(0), BigInteger.valueOf(1000))); + assertFalse(q.equals(BigIntegerPoint.newSetQuery("a", BigInteger.valueOf(1), BigInteger.valueOf(17), BigInteger.valueOf(1000)))); + } } diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java index d4ddb3adbfd..c91b52b252f 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java +++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java @@ -21,6 +21,7 @@ import java.net.InetAddress; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -45,6 +46,7 @@ public class TestInetAddressPoint extends LuceneTestCase { assertEquals(1, searcher.count(InetAddressPoint.newPrefixQuery("field", address, 24))); assertEquals(1, searcher.count(InetAddressPoint.newRangeQuery("field", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5")))); assertEquals(1, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.4")))); + assertEquals(1, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.4"), InetAddress.getByName("1.2.3.5")))); assertEquals(0, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.3")))); assertEquals(0, searcher.count(InetAddressPoint.newSetQuery("field"))); @@ -88,4 +90,23 @@ public class TestInetAddressPoint extends LuceneTestCase { assertEquals("field:[fdc8:57ed:f042:ad1:0:0:0:0 TO fdc8:57ed:f042:ad1:ffff:ffff:ffff:ffff]", InetAddressPoint.newPrefixQuery("field", InetAddress.getByName("fdc8:57ed:f042:0ad1:f66d:4ff:fe90:ce0c"), 64).toString()); assertEquals("field:{fdc8:57ed:f042:ad1:f66d:4ff:fe90:ce0c}", InetAddressPoint.newSetQuery("field", InetAddress.getByName("fdc8:57ed:f042:0ad1:f66d:4ff:fe90:ce0c")).toString()); } + + public void testQueryEquals() throws Exception { + Query q = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5")); + assertEquals(q, InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"))); + assertFalse(q.equals(InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.7")))); + + q = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16); + assertEquals(q, InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16)); + assertFalse(q.equals(InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.1.3.5"), 16))); + assertFalse(q.equals(InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.5"), 24))); + + q = InetAddressPoint.newExactQuery("a", InetAddress.getByName("1.2.3.3")); + assertEquals(q, InetAddressPoint.newExactQuery("a", InetAddress.getByName("1.2.3.3"))); + assertFalse(q.equals(InetAddressPoint.newExactQuery("a", InetAddress.getByName("1.2.3.5")))); + + q = InetAddressPoint.newSetQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5")); + assertEquals(q, InetAddressPoint.newSetQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"))); + assertFalse(q.equals(InetAddressPoint.newSetQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.7")))); + } } diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java index 0ef948d3f6d..61c6754c654 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java +++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java @@ -19,6 +19,7 @@ package org.apache.lucene.document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -170,4 +171,23 @@ public class TestLatLonPoint extends LuceneTestCase { assertEquals(lonEnc, lonEnc2, 0.0); } } + + public void testQueryEquals() throws Exception { + Query q = LatLonPoint.newBoxQuery("field", 50, 70, -40, 20); + assertEquals(q, LatLonPoint.newBoxQuery("field", 50, 70, -40, 20)); + assertFalse(q.equals(LatLonPoint.newBoxQuery("field", 50, 70, -40, 10))); + + q = LatLonPoint.newDistanceQuery("field", 50, 70, 10000); + assertEquals(q, LatLonPoint.newDistanceQuery("field", 50, 70, 10000)); + assertFalse(q.equals(LatLonPoint.newDistanceQuery("field", 50, 70, 11000))); + assertFalse(q.equals(LatLonPoint.newDistanceQuery("field", 50, 60, 10000))); + + + double[] polyLats1 = new double[] {30, 40, 40, 30, 30}; + double[] polyLons1 = new double[] {90, 90, -40, -40, 90}; + double[] polyLats2 = new double[] {20, 40, 40, 20, 20}; + q = LatLonPoint.newPolygonQuery("field", polyLats1, polyLons1); + assertEquals(q, LatLonPoint.newPolygonQuery("field", polyLats1, polyLons1)); + assertFalse(q.equals(LatLonPoint.newPolygonQuery("field", polyLats2, polyLons1))); + } } diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java index 9df8752b135..c9b5e4e635b 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java @@ -43,7 +43,7 @@ import org.apache.lucene.util.NumericUtils; * * @lucene.experimental */ -class PointInGeo3DShapeQuery extends Query { +final class PointInGeo3DShapeQuery extends Query { final String field; final GeoShape shape; @@ -192,7 +192,7 @@ class PointInGeo3DShapeQuery extends Query { } @Override - public final int hashCode() { + public int hashCode() { int result = super.hashCode(); result = 31 * result + shape.hashCode(); return result; diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java index a4d8ed13631..3061b763f85 100644 --- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java +++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java @@ -807,4 +807,18 @@ public class TestGeo3DPoint extends LuceneTestCase { private static Directory getDirectory() { return newDirectory(); } + + public void testEquals() { + GeoShape shape = randomShape(PlanetModel.WGS84); + Query q = Geo3DPoint.newShapeQuery("point", shape); + assertEquals(q, Geo3DPoint.newShapeQuery("point", shape)); + + // make a different random shape: + GeoShape shape2; + do { + shape2 = randomShape(PlanetModel.WGS84); + } while (shape.equals(shape2)); + + assertFalse(q.equals(Geo3DPoint.newShapeQuery("point", shape2))); + } }