LUCENE-5502: Fix TermsFilter.equals.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1576223 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2014-03-11 08:19:48 +00:00
parent 54e1003047
commit ffca357397
3 changed files with 19 additions and 19 deletions

View File

@ -156,6 +156,9 @@ Bug fixes
* LUCENE-5507: Fix HunspellStemFilter loading of dictionaries with large amounts of aliases * LUCENE-5507: Fix HunspellStemFilter loading of dictionaries with large amounts of aliases
etc before the encoding declaration. (Robert Muir) etc before the encoding declaration. (Robert Muir)
* LUCENE-5502: Fixed TermsFilter.equals that could return true for different
filters. (Igor Motov via Adrien Grand)
Test Framework Test Framework
* LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _. * LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _.

View File

@ -222,25 +222,15 @@ public final class TermsFilter extends Filter {
} }
TermsFilter test = (TermsFilter) obj; TermsFilter test = (TermsFilter) obj;
if (test.hashCode == hashCode && this.termsAndFields.length == test.termsAndFields.length) {
// first check the fields before even comparing the bytes // first check the fields before even comparing the bytes
for (int i = 0; i < termsAndFields.length; i++) { if (test.hashCode == hashCode && Arrays.equals(termsAndFields, test.termsAndFields)) {
TermsAndField current = termsAndFields[i]; int lastOffset = termsAndFields[termsAndFields.length - 1].end;
if (!current.equals(test.termsAndFields[i])) { // compare offsets since we sort they must be identical
return false; if (ArrayUtil.equals(offsets, 0, test.offsets, 0, lastOffset + 1)) {
}
}
// straight byte comparison since we sort they must be identical // straight byte comparison since we sort they must be identical
int end = offsets[termsAndFields.length]; return ArrayUtil.equals(termsBytes, 0, test.termsBytes, 0, offsets[lastOffset]);
byte[] left = this.termsBytes;
byte[] right = test.termsBytes;
for(int i=0;i < end;i++) {
if (left[i] != right[i]) {
return false;
} }
} }
return true;
}
return false; return false;
} }

View File

@ -50,7 +50,6 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.TestUtil;
public class TermsFilterTest extends LuceneTestCase { public class TermsFilterTest extends LuceneTestCase {
@ -298,6 +297,14 @@ public class TermsFilterTest extends LuceneTestCase {
} }
} }
public void testSingleFieldEquals() {
// Two terms with the same hash code
assertEquals("AaAaBB".hashCode(), "BBBBBB".hashCode());
TermsFilter left = termsFilter(true, new Term("id", "AaAaAa"), new Term("id", "AaAaBB"));
TermsFilter right = termsFilter(true, new Term("id", "AaAaAa"), new Term("id", "BBBBBB"));
assertFalse(left.equals(right));
}
public void testNoTerms() { public void testNoTerms() {
List<Term> emptyTerms = Collections.emptyList(); List<Term> emptyTerms = Collections.emptyList();
List<BytesRef> emptyBytesRef = Collections.emptyList(); List<BytesRef> emptyBytesRef = Collections.emptyList();