You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zh...@apache.org on 2018/01/15 07:41:13 UTC

[14/27] hbase git commit: HBASE-19746 Add default impl to Cell#getType

HBASE-19746 Add default impl to Cell#getType


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

Branch: refs/heads/HBASE-19064
Commit: 4bd6ac3e1028c3d9c6c3884b01e875806508ea7a
Parents: 3787c60
Author: Chia-Ping Tsai <ch...@gmail.com>
Authored: Thu Jan 11 15:03:20 2018 -0800
Committer: Michael Stack <st...@apache.org>
Committed: Thu Jan 11 15:03:20 2018 -0800

----------------------------------------------------------------------
 .../hadoop/hbase/ByteBufferKeyOnlyKeyValue.java |  4 ---
 .../main/java/org/apache/hadoop/hbase/Cell.java | 21 +++++++++--
 .../org/apache/hadoop/hbase/ExtendedCell.java   | 11 ------
 .../org/apache/hadoop/hbase/TestCellUtil.java   | 38 ++++++++++++++------
 .../org/apache/hadoop/hbase/TestKeyValue.java   |  5 ---
 .../org/apache/hadoop/hbase/fs/HFileSystem.java |  2 +-
 6 files changed, 47 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
index db2890f..31f71f9 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
@@ -152,10 +152,6 @@ public class ByteBufferKeyOnlyKeyValue extends ByteBufferExtendedCell {
     return ByteBufferUtils.toByte(this.buf, this.offset + this.length - 1);
   }
 
-  public Type getType() {
-    return PrivateCellUtil.toType(getTypeByte());
-  }
-
   @Override
   public void setSequenceId(long seqId) throws IOException {
     throw new IllegalArgumentException("This is a key only Cell");

http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
index f208625..8cdefff 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
@@ -201,10 +201,19 @@ public interface Cell {
   int getTagsLength();
 
   /**
-   * Returns the type of cell in a human readable format using {@link Type}
+   * Returns the type of cell in a human readable format using {@link Type}.
+   * Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
+   * {@link KeyValue.Type#Minimum}
    * @return The data type this cell: one of Put, Delete, etc
    */
-  Type getType();
+  default Type getType() {
+    byte byteType = getTypeByte();
+    Type t = Type.CODE_ARRAY[byteType & 0xff];
+    if (t != null) {
+      return t;
+    }
+    throw new UnsupportedOperationException("Invalid type of cell " + byteType);
+  }
 
   /**
    * The valid types for user to build the cell. Currently, This is subset of {@link KeyValue.Type}.
@@ -229,5 +238,13 @@ public interface Cell {
     public byte getCode() {
       return this.code;
     }
+
+    private static final Type[] CODE_ARRAY = new Type[256];
+
+    static {
+      for (Type t : Type.values()) {
+        CODE_ARRAY[t.code & 0xff] = t;
+      }
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
index be6d96c..07b0e3f 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
@@ -163,17 +163,6 @@ public interface ExtendedCell extends RawCell, HeapSize, Cloneable {
   int getTagsLength();
 
   /**
-   * {@inheritDoc}
-   * <p>
-   * Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
-   * {@link KeyValue.Type#Minimum}
-   */
-  @Override
-  default Type getType() {
-    return PrivateCellUtil.toType(getTypeByte());
-  }
-
-  /**
    * @return The byte representation of the KeyValue.TYPE of this cell: one of Put, Delete, etc
    */
   byte getTypeByte();

http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
index 60a1d55..1f95db9 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
@@ -21,6 +21,7 @@ package org.apache.hadoop.hbase;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -36,6 +37,7 @@ import org.apache.hadoop.hbase.util.Bytes;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.mockito.Mockito;
 
 @Category({MiscTests.class, SmallTests.class})
 public class TestCellUtil {
@@ -195,11 +197,6 @@ public class TestCellUtil {
       // TODO Auto-generated method stub
       return 0;
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 
   /**
@@ -382,7 +379,7 @@ public class TestCellUtil {
         HConstants.EMPTY_BYTE_ARRAY);
     cellToString = CellUtil.getCellKeyAsString(cell);
     assertEquals(kv.toString(), cellToString);
-    
+
   }
 
   @Test
@@ -522,6 +519,30 @@ public class TestCellUtil {
     assertTrue(CellUtil.equals(kv, res));
   }
 
+  @Test
+  public void testGetType() throws IOException {
+    Cell c = Mockito.mock(Cell.class);
+    Mockito.when(c.getType()).thenCallRealMethod();
+    for (Cell.Type type : Cell.Type.values()) {
+      Mockito.when(c.getTypeByte()).thenReturn(type.getCode());
+      assertEquals(type, c.getType());
+    }
+
+    try {
+      Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Maximum.getCode());
+      c.getType();
+      fail("The code of Maximum can't be handled by Cell.Type");
+    } catch(UnsupportedOperationException e) {
+    }
+
+    try {
+      Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Minimum.getCode());
+      c.getType();
+      fail("The code of Maximum can't be handled by Cell.Type");
+    } catch(UnsupportedOperationException e) {
+    }
+  }
+
   private static class NonExtendedCell implements Cell {
     private KeyValue kv;
 
@@ -618,10 +639,5 @@ public class TestCellUtil {
     public int getTagsLength() {
       return this.kv.getTagsLength();
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/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 bd45a09..7c33ff8 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
@@ -737,10 +737,5 @@ public class TestKeyValue extends TestCase {
     public byte[] getTagsArray() {
       return this.kv.getTagsArray();
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/4bd6ac3e/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
index 0723f85..b89470f 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
@@ -86,7 +86,7 @@ public class HFileSystem extends FilterFileSystem {
     this.useHBaseChecksum = useHBaseChecksum;
 
     fs.initialize(getDefaultUri(conf), conf);
-    
+
     // disable checksum verification for local fileSystem, see HBASE-11218
     if (fs instanceof LocalFileSystem) {
       fs.setWriteChecksum(false);