You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by yo...@apache.org on 2008/10/10 21:45:20 UTC

svn commit: r703565 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/MultiPhraseQuery.java src/test/org/apache/lucene/search/TestMultiPhraseQuery.java

Author: yonik
Date: Fri Oct 10 12:45:19 2008
New Revision: 703565

URL: http://svn.apache.org/viewvc?rev=703565&view=rev
Log:
LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=703565&r1=703564&r2=703565&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Fri Oct 10 12:45:19 2008
@@ -9,6 +9,10 @@
 
 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

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java?rev=703565&r1=703564&r2=703565&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java Fri Oct 10 12:45:19 2008
@@ -311,7 +311,7 @@
     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 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;
+  }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java?rev=703565&r1=703564&r2=703565&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java Fri Oct 10 12:45:19 2008
@@ -191,6 +191,35 @@
     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));