From cf186a2f74e7fbdb72bd08b0204f8f4b221ab887 Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Sat, 30 Jun 2018 22:30:54 -0400 Subject: [PATCH] HBASE-20812 Add defaults to Table Interface so implementors don't have to; ADDENDUM adding comments and defaults. --- .../java/org/apache/hadoop/hbase/client/Table.java | 41 ++++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Table.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Table.java index 41502cc004..3a8a4d9846 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Table.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Table.java @@ -51,12 +51,22 @@ import com.google.protobuf.ServiceException; * @see RegionLocator * @since 0.99.0 */ +// NOTE TO DEVS: If you add methods here, add them with defaults even if the implementation does +// nothing but throw NotImplementedException("Add an implementation!"). It will make it easier on +// Table implementers, those who are only interested in implementing a method or two of the below; +// e.g. the hbase-indexer writes to its search index via a Table implementation that takes batches. @InterfaceAudience.Public public interface Table extends Closeable { /** * Gets the fully qualified table name instance of this table. */ - TableName getName(); + default TableName getName() { + try { + return getDescriptor().getTableName(); + } catch(IOException ioe) { + throw new RuntimeException(ioe); + } + } /** * Returns the {@link org.apache.hadoop.conf.Configuration} object used by this instance. @@ -73,7 +83,9 @@ public interface Table extends Closeable { * use {@link #getDescriptor()} */ @Deprecated - HTableDescriptor getTableDescriptor() throws IOException; + default HTableDescriptor getTableDescriptor() throws IOException { + return new HTableDescriptor(getDescriptor()); + } /** * Gets the {@link org.apache.hadoop.hbase.client.TableDescriptor table descriptor} for this table. @@ -240,7 +252,6 @@ public interface Table extends Closeable { throw new NotImplementedException("Add an implementation!"); } - /** * Puts some data in the table. * @@ -289,7 +300,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException { - throw new NotImplementedException("Add an implementation!"); + CheckAndMutateBuilder builder = checkAndMutate(row, family); + return builder.qualifier(qualifier).ifEquals(value).thenPut(put); } /** @@ -315,7 +327,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, Put put) throws IOException { - throw new NotImplementedException("Add an implementation!"); + return checkAndPut(row, family, qualifier, CompareOperator.valueOf(compareOp.name()), value, + put); } /** @@ -341,7 +354,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareOperator op, byte[] value, Put put) throws IOException { - throw new NotImplementedException("Add an implementation!"); + CheckAndMutateBuilder builder = checkAndMutate(row, family); + return builder.qualifier(qualifier).ifMatches(op, value).thenPut(put); } /** @@ -399,7 +413,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value, Delete delete) throws IOException { - throw new NotImplementedException("Add an implementation!"); + CheckAndMutateBuilder builder = checkAndMutate(row, family); + return builder.qualifier(qualifier).ifEquals(value).thenDelete(delete); } /** @@ -425,7 +440,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, Delete delete) throws IOException { - throw new NotImplementedException("Add an implementation!"); + return checkAndDelete(row, family, qualifier, CompareOperator.valueOf(compareOp.name()), value, + delete); } /** @@ -451,7 +467,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, CompareOperator op, byte[] value, Delete delete) throws IOException { - throw new NotImplementedException("Add an implementation!"); + CheckAndMutateBuilder builder = checkAndMutate(row, family); + return builder.qualifier(qualifier).ifMatches(op, value).thenDelete(delete); } /** @@ -784,7 +801,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, RowMutations mutation) throws IOException { - throw new NotImplementedException("Add an implementation!"); + return checkAndMutate(row, family, qualifier, CompareOperator.valueOf(compareOp.name()), value, + mutation); } /** @@ -810,7 +828,8 @@ public interface Table extends Closeable { @Deprecated default boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOperator op, byte[] value, RowMutations mutation) throws IOException { - throw new NotImplementedException("Add an implementation!"); + CheckAndMutateBuilder builder = checkAndMutate(row, family); + return builder.qualifier(qualifier).ifMatches(op, value).thenMutate(mutation); } /** -- 2.16.3