You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/05/07 00:08:30 UTC
svn commit: r1678104 - in /lucene/dev/branches/branch_5x: ./ lucene/
lucene/core/ lucene/core/src/java/org/apache/lucene/search/payloads/
lucene/core/src/java/org/apache/lucene/search/spans/
Author: jpountz
Date: Wed May 6 22:08:30 2015
New Revision: 1678104
URL: http://svn.apache.org/r1678104
Log:
LUCENE-6372: Simplified and improved equals/hashcode of span queries.
Modified:
lucene/dev/branches/branch_5x/ (props changed)
lucene/dev/branches/branch_5x/lucene/ (props changed)
lucene/dev/branches/branch_5x/lucene/CHANGES.txt (contents, props changed)
lucene/dev/branches/branch_5x/lucene/core/ (props changed)
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java
lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java
Modified: lucene/dev/branches/branch_5x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/CHANGES.txt?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/lucene/CHANGES.txt Wed May 6 22:08:30 2015
@@ -163,6 +163,9 @@ Other
* LUCENE-6382: Lucene now enforces that positions never exceed the
maximum value IndexWriter.MAX_POSITION. (Robert Muir, Mike McCandless)
+* LUCENE-6372: Simplified and improved equals/hashcode of span queries.
+ (Paul Elschot via Adrien Grand)
+
Build
* LUCENE-6420: Update forbiddenapis to v1.8 (Uwe Schindler)
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java Wed May 6 22:08:30 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.search.payload
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
+import java.util.Objects;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Explanation;
@@ -64,8 +65,8 @@ public class PayloadNearQuery extends Sp
public PayloadNearQuery(SpanQuery[] clauses, int slop, boolean inOrder,
PayloadFunction function) {
super(clauses, slop, inOrder);
- fieldName = clauses[0].getField(); // all clauses must have same field
- this.function = function;
+ this.fieldName = Objects.requireNonNull(clauses[0].getField(), "all clauses must have same non null field");
+ this.function = Objects.requireNonNull(function);
}
@Override
@@ -111,32 +112,20 @@ public class PayloadNearQuery extends Sp
@Override
public int hashCode() {
final int prime = 31;
- int result = super.hashCode() ^ getClass().hashCode();
- result = prime * result + ((fieldName == null) ? 0 : fieldName.hashCode());
- result = prime * result + ((function == null) ? 0 : function.hashCode());
+ int result = super.hashCode();
+ result = prime * result + fieldName.hashCode();
+ result = prime * result + function.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
+ if (! super.equals(obj)) {
return false;
+ }
PayloadNearQuery other = (PayloadNearQuery) obj;
- if (fieldName == null) {
- if (other.fieldName != null)
- return false;
- } else if (!fieldName.equals(other.fieldName))
- return false;
- if (function == null) {
- if (other.function != null)
- return false;
- } else if (!function.equals(other.function))
- return false;
- return true;
+ return fieldName.equals(other.fieldName)
+ && function.equals(other.function);
}
public class PayloadNearSpanWeight extends SpanWeight {
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java Wed May 6 22:08:30 2015
@@ -217,16 +217,12 @@ public class PayloadTermQuery extends Sp
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
+ if (!super.equals(obj)) {
return false;
+ }
PayloadTermQuery other = (PayloadTermQuery) obj;
- if (includeSpanScore != other.includeSpanScore)
- return false;
- return function.equals(other.function);
+ return (includeSpanScore == other.includeSpanScore)
+ && function.equals(other.function);
}
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java Wed May 6 22:08:30 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.search.spans;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
+import java.util.Objects;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.IndexReader;
@@ -79,8 +80,8 @@ public class FieldMaskingSpanQuery exten
private String field;
public FieldMaskingSpanQuery(SpanQuery maskedQuery, String maskedField) {
- this.maskedQuery = maskedQuery;
- this.field = maskedField;
+ this.maskedQuery = Objects.requireNonNull(maskedQuery);
+ this.field = Objects.requireNonNull(maskedField);
}
@Override
@@ -141,19 +142,19 @@ public class FieldMaskingSpanQuery exten
@Override
public boolean equals(Object o) {
- if (!(o instanceof FieldMaskingSpanQuery))
+ if (! super.equals(o)) {
return false;
+ }
FieldMaskingSpanQuery other = (FieldMaskingSpanQuery) o;
return (this.getField().equals(other.getField())
- && (this.getBoost() == other.getBoost())
&& this.getMaskedQuery().equals(other.getMaskedQuery()));
}
@Override
public int hashCode() {
- return getMaskedQuery().hashCode()
- ^ getField().hashCode()
- ^ Float.floatToRawIntBits(getBoost());
+ return super.hashCode()
+ ^ getMaskedQuery().hashCode()
+ ^ getField().hashCode();
}
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java Wed May 6 22:08:30 2015
@@ -67,24 +67,4 @@ public class SpanFirstQuery extends Span
return spanFirstQuery;
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SpanFirstQuery)) return false;
-
- SpanFirstQuery other = (SpanFirstQuery)o;
- return this.end == other.end
- && this.match.equals(other.match)
- && this.getBoost() == other.getBoost();
- }
-
- @Override
- public int hashCode() {
- int h = match.hashCode();
- h ^= (h << 8) | (h >>> 25); // reversible
- h ^= Float.floatToRawIntBits(getBoost()) ^ end;
- return h;
- }
-
-
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java Wed May 6 22:08:30 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.search.spans;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
+import java.util.Objects;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.IndexReader;
@@ -63,7 +64,7 @@ public class SpanMultiTermQueryWrapper<Q
*/
@SuppressWarnings({"rawtypes","unchecked"})
public SpanMultiTermQueryWrapper(Q query) {
- this.query = query;
+ this.query = Objects.requireNonNull(query);
MultiTermQuery.RewriteMethod method = query.getRewriteMethod();
if (method instanceof TopTermsRewrite) {
@@ -144,12 +145,11 @@ public class SpanMultiTermQueryWrapper<Q
@Override
public boolean equals(Object obj) {
- if (this == obj) return true;
- if (!super.equals(obj)) return false;
- if (getClass() != obj.getClass()) return false;
+ if (! super.equals(obj)) {
+ return false;
+ }
SpanMultiTermQueryWrapper<?> other = (SpanMultiTermQueryWrapper<?>) obj;
- if (!query.equals(other.query)) return false;
- return true;
+ return query.equals(other.query);
}
/** Abstract class that defines how the query is rewritten. */
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java Wed May 6 22:08:30 2015
@@ -22,6 +22,7 @@ import org.apache.lucene.util.ToStringUt
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Objects;
/**
@@ -37,7 +38,7 @@ public class SpanNearPayloadCheckQuery e
*/
public SpanNearPayloadCheckQuery(SpanNearQuery match, Collection<byte[]> payloadToMatch) {
super(match);
- this.payloadToMatch = payloadToMatch;
+ this.payloadToMatch = Objects.requireNonNull(payloadToMatch);
}
@Override
@@ -95,22 +96,17 @@ public class SpanNearPayloadCheckQuery e
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SpanNearPayloadCheckQuery)) return false;
-
+ if (! super.equals(o)) {
+ return false;
+ }
SpanNearPayloadCheckQuery other = (SpanNearPayloadCheckQuery) o;
- return this.payloadToMatch.equals(other.payloadToMatch)
- && this.match.equals(other.match)
- && this.getBoost() == other.getBoost();
+ return this.payloadToMatch.equals(other.payloadToMatch);
}
@Override
public int hashCode() {
- int h = match.hashCode() ^ getClass().hashCode();
- h ^= (h << 8) | (h >>> 25); // reversible
- //TODO: is this right?
- h ^= payloadToMatch.hashCode();
- h ^= Float.floatToRawIntBits(getBoost());
+ int h = super.hashCode();
+ h = (h * 15) ^ payloadToMatch.hashCode();
return h;
}
}
\ No newline at end of file
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java Wed May 6 22:08:30 2015
@@ -176,29 +176,23 @@ public class SpanNearQuery extends SpanQ
/** Returns true iff <code>o</code> is equal to this. */
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SpanNearQuery)) return false;
-
+ if (! super.equals(o)) {
+ return false;
+ }
final SpanNearQuery spanNearQuery = (SpanNearQuery) o;
- if (inOrder != spanNearQuery.inOrder) return false;
- if (slop != spanNearQuery.slop) return false;
- if (!clauses.equals(spanNearQuery.clauses)) return false;
-
- return getBoost() == spanNearQuery.getBoost();
+ return (inOrder == spanNearQuery.inOrder)
+ && (slop == spanNearQuery.slop)
+ && (collectPayloads == spanNearQuery.collectPayloads)
+ && clauses.equals(spanNearQuery.clauses);
}
@Override
public int hashCode() {
- int result;
- result = clauses.hashCode();
- // Mix bits before folding in things like boost, since it could cancel the
- // last element of clauses. This particular mix also serves to
- // differentiate SpanNearQuery hashcodes from others.
- result ^= (result << 14) | (result >>> 19); // reversible
- result += Float.floatToRawIntBits(getBoost());
+ int result = super.hashCode();
+ result ^= clauses.hashCode();
result += slop;
- result ^= (inOrder ? 0x99AFD3BD : 0);
- return result;
+ int fac = 1 + (inOrder ? 8 : 4) + (collectPayloads ? 2 : 0);
+ return fac * result;
}
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java Wed May 6 22:08:30 2015
@@ -131,21 +131,17 @@ public class SpanOrQuery extends SpanQue
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
+ if (! super.equals(o)) {
+ return false;
+ }
final SpanOrQuery that = (SpanOrQuery) o;
-
- if (!clauses.equals(that.clauses)) return false;
-
- return getBoost() == that.getBoost();
+ return clauses.equals(that.clauses);
}
@Override
public int hashCode() {
- int h = clauses.hashCode();
- h ^= (h << 10) | (h >>> 23);
- h ^= Float.floatToRawIntBits(getBoost());
+ int h = super.hashCode();
+ h = (h * 7) ^ clauses.hashCode();
return h;
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java Wed May 6 22:08:30 2015
@@ -97,22 +97,17 @@ public class SpanPayloadCheckQuery exten
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SpanPayloadCheckQuery)) return false;
-
+ if (! super.equals(o)) {
+ return false;
+ }
SpanPayloadCheckQuery other = (SpanPayloadCheckQuery)o;
- return this.payloadToMatch.equals(other.payloadToMatch)
- && this.match.equals(other.match)
- && this.getBoost() == other.getBoost();
+ return this.payloadToMatch.equals(other.payloadToMatch);
}
@Override
public int hashCode() {
- int h = match.hashCode() ^ getClass().hashCode();
- h ^= (h << 8) | (h >>> 25); // reversible
- //TODO: is this right?
- h ^= payloadToMatch.hashCode();
- h ^= Float.floatToRawIntBits(getBoost()) ;
+ int h = super.hashCode();
+ h = (h * 63) ^ payloadToMatch.hashCode();
return h;
}
}
\ No newline at end of file
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java Wed May 6 22:08:30 2015
@@ -107,15 +107,15 @@ public abstract class SpanPositionCheckQ
/** Returns true iff <code>o</code> is equal to this. */
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null) return false;
- if (getClass() != o.getClass()) return false;
- final SpanPositionCheckQuery spcq = (SpanPositionCheckQuery) o;
+ if (! super.equals(o)) {
+ return false;
+ }
+ SpanPositionCheckQuery spcq = (SpanPositionCheckQuery) o;
return match.equals(spcq.match);
}
@Override
public int hashCode() {
- return match.hashCode() ^ getClass().hashCode();
+ return match.hashCode() ^ super.hashCode();
}
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java Wed May 6 22:08:30 2015
@@ -85,20 +85,17 @@ public class SpanPositionRangeQuery exte
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SpanPositionRangeQuery)) return false;
-
+ if (! super.equals(o)) {
+ return false;
+ }
SpanPositionRangeQuery other = (SpanPositionRangeQuery)o;
- return this.end == other.end && this.start == other.start
- && this.match.equals(other.match)
- && this.getBoost() == other.getBoost();
+ return this.end == other.end && this.start == other.start;
}
@Override
public int hashCode() {
- int h = match.hashCode() ^ getClass().hashCode();
- h ^= (h << 8) | (h >>> 25); // reversible
- h ^= Float.floatToRawIntBits(getBoost()) ^ end ^ start;
+ int h = super.hashCode() ^ end;
+ h = (h * 127) ^ start;
return h;
}
Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java?rev=1678104&r1=1678103&r2=1678104&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java Wed May 6 22:08:30 2015
@@ -75,12 +75,9 @@ public class SpanTermQuery extends SpanQ
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (getClass() != obj.getClass())
+ if (! super.equals(obj)) {
return false;
+ }
SpanTermQuery other = (SpanTermQuery) obj;
return term.equals(other.term);
}