LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@703565 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2008-10-10 19:45:19 +00:00
parent b0e6bac10d
commit 00f98b1fd7
3 changed files with 79 additions and 2 deletions

View File

@ -9,6 +9,10 @@ API Changes
Bug fixes
1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()
implementation - Leads to Solr Cache misses.
(Todd Feak, Mark Miller via yonik)
New features
Optimizations

View File

@ -311,7 +311,7 @@ public class MultiPhraseQuery extends Query {
MultiPhraseQuery other = (MultiPhraseQuery)o;
return this.getBoost() == other.getBoost()
&& this.slop == other.slop
&& this.termArrays.equals(other.termArrays)
&& termArraysEquals(this.termArrays, other.termArrays)
&& this.positions.equals(other.positions);
}
@ -319,8 +319,52 @@ public class MultiPhraseQuery extends Query {
public int hashCode() {
return Float.floatToIntBits(getBoost())
^ slop
^ termArrays.hashCode()
^ termArraysHashCode()
^ positions.hashCode()
^ 0x4AC65113;
}
// Breakout calculation of the termArrays hashcode
private int termArraysHashCode() {
int hashCode = 1;
Iterator iterator = termArrays.iterator();
while (iterator.hasNext()) {
Term[] termArray = (Term[]) iterator.next();
hashCode = 31 * hashCode
+ (termArray == null ? 0 : arraysHashCode(termArray));
}
return hashCode;
}
private int arraysHashCode(Term[] termArray) {
if (termArray == null)
return 0;
int result = 1;
for (int i = 0; i < termArray.length; i++) {
Term term = termArray[i];
result = 31 * result + (term == null ? 0 : term.hashCode());
}
return result;
}
// Breakout calculation of the termArrays equals
private boolean termArraysEquals(List termArrays1, List termArrays2) {
if (termArrays1.size() != termArrays2.size()) {
return false;
}
ListIterator iterator1 = termArrays1.listIterator();
ListIterator iterator2 = termArrays2.listIterator();
while (iterator1.hasNext()) {
Term[] termArray1 = (Term[]) iterator1.next();
Term[] termArray2 = (Term[]) iterator2.next();
if (!(termArray1 == null ? termArray2 == null : Arrays.equals(termArray1,
termArray2))) {
return false;
}
}
return true;
}
}

View File

@ -191,6 +191,35 @@ public class TestMultiPhraseQuery extends LuceneTestCase
searcher.close();
}
public void testHashCodeAndEquals(){
MultiPhraseQuery query1 = new MultiPhraseQuery();
MultiPhraseQuery query2 = new MultiPhraseQuery();
assertEquals(query1.hashCode(), query2.hashCode());
assertEquals(query1,query2);
Term term1= new Term("someField","someText");
query1.add(term1);
query2.add(term1);
assertEquals(query1.hashCode(), query2.hashCode());
assertEquals(query1,query2);
Term term2= new Term("someField","someMoreText");
query1.add(term2);
assertFalse(query1.hashCode()==query2.hashCode());
assertFalse(query1.equals(query2));
query2.add(term2);
assertEquals(query1.hashCode(), query2.hashCode());
assertEquals(query1,query2);
}
private void add(String s, String type, IndexWriter writer) throws IOException {
Document doc = new Document();
doc.add(new Field("body", s, Field.Store.YES, Field.Index.ANALYZED));