Skip to content

Apache Lucene open-source search software

LUCENE-6793: Make LegacyNumericRangeQuery and point queries less subject to hash collisions.

Adrien Grand509c6a0acbea
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 88f92d166f5..3e98a45cff6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -115,6 +115,9 @@ Optimizations
merging to merge sort the already sorted segments instead of
re-indexing (Mike McCandless)
+* LUCENE-6793: LegacyNumericRangeQuery.hashCode() is now less subject to hash
+ collisions. (J.B. Langston via Adrien Grand)
+
Changes in Runtime Behavior
* LUCENE-6789: IndexSearcher's default Similarity is changed to BM25Similarity.
diff --git a/lucene/core/src/java/org/apache/lucene/search/LegacyNumericRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/LegacyNumericRangeQuery.java
index 9b478456df4..5e0506f5e24 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LegacyNumericRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LegacyNumericRangeQuery.java
@@ -348,12 +348,12 @@ public final class LegacyNumericRangeQuery<T extends Number> extends MultiTermQu
@Override
public final int hashCode() {
int hash = super.hashCode();
- hash += precisionStep^0x64365465;
- if (min != null) hash += min.hashCode()^0x14fa55fb;
- if (max != null) hash += max.hashCode()^0x733fa5fe;
- return hash +
- (Boolean.valueOf(minInclusive).hashCode()^0x14fa55fb)+
- (Boolean.valueOf(maxInclusive).hashCode()^0x733fa5fe);
+ hash = 31 * hash + precisionStep;
+ hash = 31 * hash + Objects.hashCode(min);
+ hash = 31 * hash + Objects.hashCode(max);
+ hash = 31 * hash + Objects.hashCode(minInclusive);
+ hash = 31 * hash + Objects.hashCode(maxInclusive);
+ return hash;
}
// members (package private, to be also fast accessible by NumericRangeTermEnum)
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 348f598f27b..e695cf79789 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
@@ -301,9 +301,9 @@ public class PointInSetQuery extends Query {
@Override
public int hashCode() {
int hash = super.hashCode();
- hash += sortedPackedPointsHashCode^0x14fa55fb;
- hash += numDims^0x14fa55fb;
- hash += bytesPerDim^0x14fa55fb;
+ hash = 31 * hash + sortedPackedPointsHashCode;
+ hash = 31 * hash + numDims;
+ hash = 31 * hash + bytesPerDim;
return hash;
}
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 59f8f0fd775..be52e61209b 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java
@@ -287,12 +287,12 @@ public abstract class PointRangeQuery extends Query {
@Override
public int hashCode() {
int hash = super.hashCode();
- hash += Arrays.hashCode(lowerPoint)^0x14fa55fb;
- hash += Arrays.hashCode(upperPoint)^0x733fa5fe;
- hash += Arrays.hashCode(lowerInclusive)^0x14fa55fb;
- hash += Arrays.hashCode(upperInclusive)^0x733fa5fe;
- hash += numDims^0x14fa55fb;
- hash += Objects.hashCode(bytesPerDim);
+ hash = 31 * hash + Arrays.hashCode(lowerPoint);
+ hash = 31 * hash + Arrays.hashCode(upperPoint);
+ hash = 31 * hash + Arrays.hashCode(lowerInclusive);
+ hash = 31 * hash + Arrays.hashCode(upperInclusive);
+ hash = 31 * hash + numDims;
+ hash = 31 * hash + Objects.hashCode(bytesPerDim);
return hash;
}