From 1f33c7a0e4c9a30d32ee3fbef606297e19f263c4 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 | 68 +++++++++++++------ 1 file changed, 48 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..3edd0b77a6 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,15 @@ public interface Table extends Closeable { * use {@link #getDescriptor()} */ @Deprecated - HTableDescriptor getTableDescriptor() throws IOException; + default HTableDescriptor getTableDescriptor() throws IOException { + TableDescriptor descriptor = getDescriptor(); + + if (descriptor instanceof HTableDescriptor) { + return (HTableDescriptor)descriptor; + } else { + return new HTableDescriptor(descriptor); + } + } /** * Gets the {@link org.apache.hadoop.hbase.client.TableDescriptor table descriptor} for this table. @@ -96,7 +106,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 +186,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 +250,6 @@ public interface Table extends Closeable { throw new NotImplementedException("Add an implementation!"); } - /** * Puts some data in the table. * @@ -249,7 +258,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 +298,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 +324,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 +353,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 +367,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 +414,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 +440,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 +469,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 +601,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 +625,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 +860,7 @@ public interface Table extends Closeable { */ @Deprecated default int getRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -850,7 +877,8 @@ public interface Table extends Closeable { */ @Deprecated default void setRpcTimeout(int rpcTimeout) { - throw new NotImplementedException("Add an implementation!"); + setReadRpcTimeout(rpcTimeout); + setWriteRpcTimeout(rpcTimeout); } /** @@ -869,7 +897,7 @@ public interface Table extends Closeable { */ @Deprecated default int getReadRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getReadRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -902,7 +930,7 @@ public interface Table extends Closeable { */ @Deprecated default int getWriteRpcTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getWriteRpcTimeout(TimeUnit.MILLISECONDS); } /** @@ -935,7 +963,7 @@ public interface Table extends Closeable { */ @Deprecated default int getOperationTimeout() { - throw new NotImplementedException("Add an implementation!"); + return (int)getOperationTimeout(TimeUnit.MILLISECONDS); } /** -- 2.17.0