hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurt...@apache.org
Subject [1/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:48 GMT
Repository: hbase
Updated Branches:
  refs/heads/0.98 4a0516deb -> 38288ebca
  refs/heads/branch-1 30d1fe219 -> 9a4ea44d1
  refs/heads/branch-1.0 de5b226b0 -> fdfebefa5
  refs/heads/branch-1.1 7cc85d367 -> 9acaba276


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


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

Branch: refs/heads/0.98
Commit: 38288ebcac2db06d3241ffa7bbb6c5c8e1b0a23f
Parents: 4a0516d
Author: tedyu <yuzhihong@gmail.com>
Authored: Fri May 1 18:31:14 2015 -0700
Committer: Andrew Purtell <apurtell@apache.org>
Committed: Fri May 1 18:31:14 2015 -0700

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


http://git-wip-us.apache.org/repos/asf/hbase/blob/38288ebc/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 992079d..245b181 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
@@ -140,13 +140,32 @@ 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
       return 0;
     }
+    int hash = calculateHashForKeyValue(cell);
+    hash = 31 * hash + (int)cell.getMvccVersion();
+    return hash;
+  }
+
+  /**
+   * Returns a hash code that is always the same for two Cells having a matching
+   * equals(..) result. Note : Ignore mvcc while calculating the hashcode
+   * 
+   * @param cell
+   * @return hashCode
+   */
+  public static int hashCodeIgnoreMvcc(Cell cell) {
+    if (cell == null) {// return 0 for empty Cell
+      return 0;
+    }
+    int hash = calculateHashForKeyValue(cell);
+    return hash;
+  }
 
+  private static int calculateHashForKeyValue(Cell cell) {
     //pre-calculate the 3 hashes made of byte ranges
     int rowHash = Bytes.hashCode(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
     int familyHash =
@@ -159,7 +178,6 @@ public class CellComparator implements Comparator<Cell>, Serializable{
     hash = 31 * hash + qualifierHash;
     hash = 31 * hash + (int)cell.getTimestamp();
     hash = 31 * hash + cell.getTypeByte();
-    hash = 31 * hash + (int)cell.getMvccVersion();
     return hash;
   }
 

http://git-wip-us.apache.org/repos/asf/hbase/blob/38288ebc/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 924da95..66f18d0 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
@@ -1053,15 +1053,12 @@ public class KeyValue implements Cell, HeapSize, Cloneable {
     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/38288ebc/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 45c2b39..e6d000c 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
@@ -35,6 +35,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());
 
@@ -614,4 +616,22 @@ public class TestKeyValue extends TestCase {
     b = new KeyValue(Bytes.toBytes("table,111,222,bbb"), now);
     assertTrue(c.compare(a, b) < 0);
   }
+
+  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.setMvccVersion(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