From c4f79b7c801f10de6f1a5b52880fd4018165ebc8 Mon Sep 17 00:00:00 2001 From: Balazs Meszaros Date: Wed, 11 Jul 2018 15:23:15 +0200 Subject: [PATCH] HBASE-20853 Polish "Add defaults to Table Interface so Implementors don't have to" --- .../org/apache/hadoop/hbase/client/Table.java | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 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 191d359c82..51d295373c 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 @@ -20,12 +20,14 @@ package org.apache.hadoop.hbase.client; import java.io.Closeable; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.NotImplementedException; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; @@ -34,7 +36,7 @@ import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.client.coprocessor.Batch; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel; - +import org.apache.hadoop.hbase.util.Bytes; import com.google.protobuf.Descriptors; import com.google.protobuf.Message; import com.google.protobuf.Service; @@ -73,7 +75,9 @@ public interface Table extends Closeable { * use {@link #getDescriptor()} */ @Deprecated - HTableDescriptor getTableDescriptor() throws IOException; + default HTableDescriptor getTableDescriptor() throws IOException { + return (HTableDescriptor)getDescriptor(); + } /** * Gets the {@link org.apache.hadoop.hbase.client.TableDescriptor table descriptor} for this table. @@ -96,7 +100,7 @@ public interface Table extends Closeable { * @throws IOException e */ default boolean exists(Get get) throws IOException { - throw new NotImplementedException("Add an implementation!"); + return exists(Collections.singletonList(get))[0]; } /** @@ -176,7 +180,7 @@ public interface Table extends Closeable { * @since 0.20.0 */ default Result get(Get get) throws IOException { - throw new NotImplementedException("Add an implementation!"); + return get(Collections.singletonList(get))[0]; } /** @@ -240,7 +244,6 @@ public interface Table extends Closeable { throw new NotImplementedException("Add an implementation!"); } - /** * Puts some data in the table. * @@ -249,7 +252,7 @@ public interface Table extends Closeable { * @since 0.20.0 */ default void put(Put put) throws IOException { - throw new NotImplementedException("Add an implementation!"); + put(Collections.singletonList(put)); } /** @@ -289,7 +292,7 @@ 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!"); + return checkAndPut(row, family, qualifier, CompareOperator.EQUAL, value, put); } /** @@ -315,7 +318,10 @@ 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!"); + RowMutations mutations = new RowMutations(put.getRow(), 1); + mutations.add(put); + + return checkAndMutate(row, family, qualifier, compareOp, value, mutations); } /** @@ -341,7 +347,10 @@ 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!"); + RowMutations mutations = new RowMutations(put.getRow(), 1); + mutations.add(put); + + return checkAndMutate(row, family, qualifier, op, value, mutations); } /** @@ -352,7 +361,7 @@ public interface Table extends Closeable { * @since 0.20.0 */ default void delete(Delete delete) throws IOException { - throw new NotImplementedException("Add an implementation!"); + delete(Collections.singletonList(delete)); } /** @@ -399,7 +408,7 @@ 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!"); + return checkAndDelete(row, family, qualifier, CompareOperator.EQUAL, value, delete); } /** @@ -425,7 +434,10 @@ 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!"); + RowMutations mutations = new RowMutations(delete.getRow(), 1); + mutations.add(delete); + + return checkAndMutate(row, family, qualifier, compareOp, value, mutations); } /** @@ -451,7 +463,10 @@ 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!"); + RowMutations mutations = new RowMutations(delete.getRow(), 1); + mutations.add(delete); + + return checkAndMutate(row, family, qualifier, op, value, mutations); } /** @@ -580,7 +595,9 @@ public interface Table extends Closeable { */ default long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) throws IOException { - throw new NotImplementedException("Add an implementation!"); + Increment increment = new Increment(row).addColumn(family, qualifier, amount); + Cell cell = increment(increment).getColumnLatestCell(family, qualifier); + return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); } /** @@ -602,7 +619,11 @@ public interface Table extends Closeable { */ default long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { - throw new NotImplementedException("Add an implementation!"); + Increment increment = new Increment(row) + .addColumn(family, qualifier, amount) + .setDurability(durability); + Cell cell = increment(increment).getColumnLatestCell(family, qualifier); + return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); } /** @@ -833,7 +854,7 @@ public interface Table extends Closeable { */ @Deprecated default int getRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -850,7 +871,8 @@ public interface Table extends Closeable { */ @Deprecated default void setRpcTimeout(int rpcTimeout) { - throw new NotImplementedException("Add an implementation!"); + setReadRpcTimeout(rpcTimeout); + setWriteRpcTimeout(rpcTimeout); } /** @@ -869,7 +891,7 @@ public interface Table extends Closeable { */ @Deprecated default int getReadRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getReadRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -902,7 +924,7 @@ public interface Table extends Closeable { */ @Deprecated default int getWriteRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getWriteRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -935,7 +957,7 @@ public interface Table extends Closeable { */ @Deprecated default int getOperationTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getOperationTimeout(TimeUnit.MILLISECONDS); } /** -- 2.17.0