HBASE-6907 KeyValue equals and compareTo methods should match (Ted Yu)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1436434 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2013-01-21 15:59:06 +00:00
parent 6298623a2f
commit 7e1a05c200
5 changed files with 37 additions and 18 deletions

View File

@ -842,16 +842,13 @@ public class KeyValue implements Cell, HeapSize {
/**
* Needed doing 'contains' on List. Only compares the key portion, not the value.
*
* For temporary backwards compatibility with the original KeyValue.equals method, we ignore the
* mvccVersion.
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof Cell)) {
return false;
}
return CellComparator.equalsIgnoreMvccVersion(this, (Cell)other);
return CellComparator.equals(this, (Cell)other);
}
@Override

View File

@ -23,10 +23,10 @@ import java.util.Collection;
import java.util.List;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.IterableUtils;
import org.apache.hadoop.hbase.util.Strings;
import org.apache.hbase.cell.CellComparator;
import com.google.common.collect.Lists;
@ -61,7 +61,6 @@ public class KeyValueTestUtil {
);
}
public static ByteBuffer toByteBufferAndRewind(final Iterable<? extends KeyValue> kvs,
boolean includeMemstoreTS) {
int totalBytes = KeyValueTool.totalLengthWithMvccVersion(kvs, includeMemstoreTS);
@ -73,6 +72,27 @@ public class KeyValueTestUtil {
return bb;
}
/**
* Checks whether KeyValues from kvCollection2 are contained in kvCollection1.
*
* The comparison is made without distinguishing MVCC version of the KeyValues
*
* @param kvCollection1
* @param kvCollection2
* @return true if KeyValues from kvCollection2 are contained in kvCollection1
*/
public static boolean containsIgnoreMvccVersion(Collection<KeyValue> kvCollection1,
Collection<KeyValue> kvCollection2) {
for (KeyValue kv1 : kvCollection1) {
boolean found = false;
for (KeyValue kv2 : kvCollection2) {
if (CellComparator.equalsIgnoreMvccVersion(kv1, kv2)) found = true;
}
if (!found) return false;
}
return true;
}
public static List<KeyValue> rewindThenToList(final ByteBuffer bb,
final boolean includesMemstoreTS) {
bb.rewind();

View File

@ -35,6 +35,7 @@ import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hbase.cell.CellComparator;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@ -149,7 +150,7 @@ public class TestColumnSeeking {
while (scanner.next(results))
;
assertEquals(kvSet.size(), results.size());
assertTrue(results.containsAll(kvSet));
assertTrue(KeyValueTestUtil.containsIgnoreMvccVersion(results, kvSet));
}
} finally {
HRegion.closeHRegion(region);
@ -260,7 +261,7 @@ public class TestColumnSeeking {
while (scanner.next(results))
;
assertEquals(kvSet.size(), results.size());
assertTrue(results.containsAll(kvSet));
assertTrue(KeyValueTestUtil.containsIgnoreMvccVersion(results, kvSet));
}
HRegion.closeHRegion(region);

View File

@ -90,6 +90,7 @@ import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.PairOfSameType;
import org.apache.hadoop.hbase.util.Threads;
import org.apache.hbase.cell.CellComparator;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@ -1910,8 +1911,8 @@ public class TestHRegion extends HBaseTestCase {
res = new ArrayList<KeyValue>();
is.next(res);
for(int i=0; i<res.size(); i++) {
assertEquals(expected1.get(i), res.get(i));
for (int i = 0; i < res.size(); i++) {
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected1.get(i), res.get(i)));
}
//Result 2
@ -1922,7 +1923,7 @@ public class TestHRegion extends HBaseTestCase {
res = new ArrayList<KeyValue>();
is.next(res);
for(int i=0; i<res.size(); i++) {
assertEquals(expected2.get(i), res.get(i));
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected2.get(i), res.get(i)));
}
} finally {
HRegion.closeHRegion(this.region);
@ -2046,7 +2047,7 @@ public class TestHRegion extends HBaseTestCase {
//Verify result
for(int i=0; i<expected.size(); i++) {
assertEquals(expected.get(i), actual.get(i));
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected.get(i), actual.get(i)));
}
} finally {
HRegion.closeHRegion(this.region);
@ -2129,7 +2130,7 @@ public class TestHRegion extends HBaseTestCase {
//Verify result
for(int i=0; i<expected.size(); i++) {
assertEquals(expected.get(i), actual.get(i));
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected.get(i), actual.get(i)));
}
} finally {
HRegion.closeHRegion(this.region);
@ -2253,7 +2254,7 @@ public class TestHRegion extends HBaseTestCase {
//Verify result
for(int i=0; i<expected.size(); i++) {
assertEquals(expected.get(i), actual.get(i));
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected.get(i), actual.get(i)));
}
} finally {
HRegion.closeHRegion(this.region);
@ -2412,7 +2413,7 @@ public class TestHRegion extends HBaseTestCase {
//Verify result
for(int i=0; i<expected.size(); i++) {
assertEquals(expected.get(i), actual.get(i));
assertTrue(CellComparator.equalsIgnoreMvccVersion(expected.get(i), actual.get(i)));
}
} finally {
HRegion.closeHRegion(this.region);

View File

@ -48,7 +48,7 @@ import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.apache.hbase.cell.CellComparator;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
@ -262,8 +262,8 @@ public class TestMultiColumnScanner {
}
assertTrue("Scanner returned additional key/value: " + kv + ", "
+ queryInfo + deleteInfo + ";", kvPos < kvs.size());
assertEquals("Scanner returned wrong key/value; " + queryInfo
+ deleteInfo + ";", kvs.get(kvPos), kv);
assertTrue("Scanner returned wrong key/value; " + queryInfo
+ deleteInfo + ";", CellComparator.equalsIgnoreMvccVersion(kvs.get(kvPos), (kv)));
++kvPos;
++numResults;
}