From 178b774c103e31b143de25ac61627954130f7c19 Mon Sep 17 00:00:00 2001 From: Josh Elser Date: Fri, 27 Oct 2017 19:27:59 -0400 Subject: [PATCH] HBASE-19111 Add CellUtil#isPut and deprecate methods returning/expecting non public-api data KeyValue.Type, and its corresponding byte value, are not public API. We shouldn't have methods that are expecting them. Added a basic sanity test for isPut and isDelete. --- .../main/java/org/apache/hadoop/hbase/Cell.java | 3 ++ .../java/org/apache/hadoop/hbase/CellUtil.java | 19 ++++++++ .../hadoop/hbase/client/TestFromClientSide.java | 53 ++++++++++++++++++++++ 3 files changed, 75 insertions(+) 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 b2f6304851..a3c6eef50c 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 @@ -133,7 +133,10 @@ public interface Cell { /** * @return The byte representation of the KeyValue.TYPE of this cell: one of Put, Delete, etc + * @deprecated since 2.0, use appropriate {@link CellUtil#isDelete} or + * {@link CellUtil#isPut(Cell)} methods instead. */ + @Deprecated byte getTypeByte(); diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java index f9640a32e6..b3c405089a 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java @@ -607,6 +607,7 @@ public final class CellUtil { } @Override + @SuppressWarnings("deprecation") public byte getTypeByte() { return cell.getTypeByte(); } @@ -790,6 +791,7 @@ public final class CellUtil { } @Override + @SuppressWarnings("deprecation") public byte getTypeByte() { return this.cell.getTypeByte(); } @@ -1423,6 +1425,7 @@ public final class CellUtil { * {KeyValue.Type#DeleteFamily} or a * {@link KeyValue.Type#DeleteColumn} KeyValue type. */ + @SuppressWarnings("deprecation") public static boolean isDelete(final Cell cell) { return isDelete(cell.getTypeByte()); } @@ -1431,7 +1434,9 @@ public final class CellUtil { * @return True if a delete type, a {@link KeyValue.Type#Delete} or a * {KeyValue.Type#DeleteFamily} or a * {@link KeyValue.Type#DeleteColumn} KeyValue type. + * @deprecated since 2.0. Use {@link #isDelete(Cell)} instead. */ + @Deprecated public static boolean isDelete(final byte type) { return Type.Delete.getCode() <= type && type <= Type.DeleteFamily.getCode(); @@ -1440,22 +1445,27 @@ public final class CellUtil { /** * @return True if this cell is a {@link KeyValue.Type#Delete} type. */ + @SuppressWarnings("deprecation") public static boolean isDeleteType(Cell cell) { return cell.getTypeByte() == Type.Delete.getCode(); } + @SuppressWarnings("deprecation") public static boolean isDeleteFamily(final Cell cell) { return cell.getTypeByte() == Type.DeleteFamily.getCode(); } + @SuppressWarnings("deprecation") public static boolean isDeleteFamilyVersion(final Cell cell) { return cell.getTypeByte() == Type.DeleteFamilyVersion.getCode(); } + @SuppressWarnings("deprecation") public static boolean isDeleteColumns(final Cell cell) { return cell.getTypeByte() == Type.DeleteColumn.getCode(); } + @SuppressWarnings("deprecation") public static boolean isDeleteColumnVersion(final Cell cell) { return cell.getTypeByte() == Type.Delete.getCode(); } @@ -1464,11 +1474,20 @@ public final class CellUtil { * * @return True if this cell is a delete family or column type. */ + @SuppressWarnings("deprecation") public static boolean isDeleteColumnOrFamily(Cell cell) { int t = cell.getTypeByte(); return t == Type.DeleteColumn.getCode() || t == Type.DeleteFamily.getCode(); } + /** + * @return True if this cell is a Put. + */ + @SuppressWarnings("deprecation") + public static boolean isPut(Cell cell) { + return cell.getTypeByte() == Type.Put.getCode(); + } + /** * Estimate based on keyvalue's serialization format in the RPC layer. Note that there is an extra * SIZEOF_INT added to the size here that indicates the actual length of the cell for cases where diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java index 85d84de734..e8752ab55f 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java @@ -50,6 +50,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellScanner; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.ClusterStatus.Option; import org.apache.hadoop.hbase.CompareOperator; @@ -6563,4 +6564,56 @@ public class TestFromClientSide { table.close(); admin.close(); } + + @Test + public void testCellUtilTypeMethods() throws IOException { + final TableName tableName = TableName.valueOf(name.getMethodName()); + Table table = TEST_UTIL.createTable(tableName, FAMILY); + + final byte[] row = Bytes.toBytes("p"); + Put p = new Put(row); + p.addColumn(FAMILY, QUALIFIER, VALUE); + table.put(p); + + try (ResultScanner scanner = table.getScanner(new Scan())) { + Result result = scanner.next(); + assertNotNull(result); + CellScanner cs = result.cellScanner(); + assertTrue(cs.advance()); + Cell c = cs.current(); + assertTrue(CellUtil.isPut(c)); + assertFalse(CellUtil.isDelete(c)); + assertFalse(cs.advance()); + assertNull(scanner.next()); + } + + Delete d = new Delete(row); + d.addColumn(FAMILY, QUALIFIER); + table.delete(d); + + Scan scan = new Scan(); + scan.setRaw(true); + try (ResultScanner scanner = table.getScanner(scan)) { + Result result = scanner.next(); + assertNotNull(result); + CellScanner cs = result.cellScanner(); + assertTrue(cs.advance()); + + // First cell should be the delete (masking the Put) + Cell c = cs.current(); + assertTrue(CellUtil.isDelete(c)); + assertFalse(CellUtil.isPut(c)); + + // Second cell should be the original Put + assertTrue(cs.advance()); + assertFalse(CellUtil.isDelete(c)); + assertTrue(CellUtil.isPut(c)); + + // No more cells in this row + assertFalse(cs.advance()); + + // No more results in this scan + assertNull(scanner.next()); + } + } } -- 2.14.1