hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurt...@apache.org
Subject [4/4] hbase git commit: HBASE-12413 Mismatch in the equals and hashcode methods of KeyValue (Jingcheng Du and Gariel Reid)
Date Sat, 02 May 2015 01:43:51 GMT
HBASE-12413 Mismatch in the equals and hashcode methods of KeyValue (Jingcheng Du and Gariel
Reid)

Conflicts:
	hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java
	hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/fdfebefa
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/fdfebefa
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/fdfebefa

Branch: refs/heads/branch-1.0
Commit: fdfebefa5f74707e3b58dc5a68f0b56689d7a510
Parents: de5b226
Author: tedyu <yuzhihong@gmail.com>
Authored: Fri May 1 18:31:19 2015 -0700
Committer: Andrew Purtell <apurtell@apache.org>
Committed: Fri May 1 18:31:19 2015 -0700

----------------------------------------------------------------------
 .../org/apache/hadoop/hbase/CellComparator.java |  6 ++----
 .../java/org/apache/hadoop/hbase/KeyValue.java  | 11 ++++-------
 .../org/apache/hadoop/hbase/TestKeyValue.java   | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/fdfebefa/hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java
index 46c0eaf..2c1cc09 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellComparator.java
@@ -244,7 +244,6 @@ public class CellComparator implements Comparator<Cell>, Serializable
{
 
   /**
    * Returns a hash code that is always the same for two Cells having a matching equals(..)
result.
-   * Currently does not guard against nulls, but it could if necessary.
    */
   public static int hashCode(Cell cell){
     if (cell == null) {// return 0 for empty Cell
@@ -258,9 +257,8 @@ public class CellComparator implements Comparator<Cell>, Serializable
{
 
   /**
    * Returns a hash code that is always the same for two Cells having a matching
-   * equals(..) result. Currently does not guard against nulls, but it could if
-   * necessary. Note : Ignore mvcc while calculating the hashcode
-   *
+   * equals(..) result. Note : Ignore mvcc while calculating the hashcode
+   * 
    * @param cell
    * @return hashCode
    */

http://git-wip-us.apache.org/repos/asf/hbase/blob/fdfebefa/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
index fb4ec2c..77b72bf 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
@@ -1122,15 +1122,12 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId,
     return CellComparator.equals(this, (Cell)other);
   }
 
+  /**
+   * In line with {@link #equals(Object)}, only uses the key portion, not the value.
+   */
   @Override
   public int hashCode() {
-    byte[] b = getBuffer();
-    int start = getOffset(), end = getOffset() + getLength();
-    int h = b[start++];
-    for (int i = start; i < end; i++) {
-      h = (h * 13) ^ b[i];
-    }
-    return h;
+    return CellComparator.hashCodeIgnoreMvcc(this);
   }
 
   //---------------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/hbase/blob/fdfebefa/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
index b41f692..c6a31b5 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
@@ -39,6 +39,8 @@ import org.apache.hadoop.hbase.KeyValue.MetaComparator;
 import org.apache.hadoop.hbase.KeyValue.Type;
 import org.apache.hadoop.hbase.util.Bytes;
 
+import static org.junit.Assert.assertNotEquals;
+
 public class TestKeyValue extends TestCase {
   private final Log LOG = LogFactory.getLog(this.getClass().getName());
 
@@ -829,4 +831,22 @@ public class TestKeyValue extends TestCase {
       return this.kv.getTagsArray();
     }
   }
+
+  public void testEqualsAndHashCode() throws Exception {
+    KeyValue kvA1 = new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"),
+        Bytes.toBytes("qualA"), Bytes.toBytes("1"));
+    KeyValue kvA2 = new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"),
+        Bytes.toBytes("qualA"), Bytes.toBytes("2"));
+    // We set a different sequence id on kvA2 to demonstrate that the equals and hashCode
also
+    // don't take this into account.
+    kvA2.setSequenceId(2);
+    KeyValue kvB = new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"),
+        Bytes.toBytes("qualB"), Bytes.toBytes("1"));
+
+    assertEquals(kvA1, kvA2);
+    assertNotEquals(kvA1, kvB);
+    assertEquals(kvA1.hashCode(), kvA2.hashCode());
+    assertNotEquals(kvA1.hashCode(), kvB.hashCode());
+  }
+
 }


Mime
View raw message