hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhang...@apache.org
Subject [23/41] hbase git commit: HBASE-19897 RowMutations should follow the fluent pattern
Date Fri, 02 Feb 2018 08:53:30 GMT
HBASE-19897 RowMutations should follow the fluent pattern


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

Branch: refs/heads/HBASE-19064
Commit: adccbb7edf3986701d54b9ca93fcf5a8e99548fb
Parents: d472422
Author: Chia-Ping Tsai <chia7712@gmail.com>
Authored: Thu Feb 1 13:58:54 2018 +0800
Committer: Chia-Ping Tsai <chia7712@gmail.com>
Committed: Fri Feb 2 05:33:08 2018 +0800

----------------------------------------------------------------------
 .../hadoop/hbase/client/RowMutations.java       | 66 +++++++++++++++-----
 .../hbase/client/TestFromClientSide3.java       | 24 +++----
 2 files changed, 61 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/adccbb7e/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RowMutations.java
----------------------------------------------------------------------
diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RowMutations.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RowMutations.java
index 4ff9eb1..1eb3151 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RowMutations.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RowMutations.java
@@ -22,9 +22,9 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
-
-import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.CollectionUtils;
+import org.apache.yetus.audience.InterfaceAudience;
 
 /**
  * Performs multiple mutations atomically on a single row.
@@ -38,6 +38,21 @@ import org.apache.hadoop.hbase.util.Bytes;
  */
 @InterfaceAudience.Public
 public class RowMutations implements Row {
+
+  /**
+   * Create a {@link RowMutations} with the specified mutations.
+   * @param mutations the mutations to send
+   * @return RowMutations
+   * @throws IOException if any row in mutations is different to another
+   */
+  public static RowMutations of(List<? extends Mutation> mutations) throws IOException
{
+    if (CollectionUtils.isEmpty(mutations)) {
+      throw new IllegalArgumentException("Can't instantiate a RowMutations by empty list");
+    }
+    return new RowMutations(mutations.get(0).getRow(), mutations.size())
+        .add(mutations);
+  }
+
   private final List<Mutation> mutations;
   private final byte [] row;
 
@@ -50,8 +65,7 @@ public class RowMutations implements Row {
    * @param initialCapacity the initial capacity of the RowMutations
    */
   public RowMutations(byte [] row, int initialCapacity) {
-    Mutation.checkRow(row);
-    this.row = Bytes.copy(row);
+    this.row = Bytes.copy(Mutation.checkRow(row));
     if (initialCapacity <= 0) {
       this.mutations = new ArrayList<>();
     } else {
@@ -62,29 +76,53 @@ public class RowMutations implements Row {
   /**
    * Add a {@link Put} operation to the list of mutations
    * @param p The {@link Put} to add
-   * @throws IOException
+   * @throws IOException if the row of added mutation doesn't match the original row
+   * @deprecated since 2.0 version and will be removed in 3.0 version.
+   *             use {@link #add(Mutation)}
    */
+  @Deprecated
   public void add(Put p) throws IOException {
-    internalAdd(p);
+    add((Mutation) p);
   }
 
   /**
    * Add a {@link Delete} operation to the list of mutations
    * @param d The {@link Delete} to add
-   * @throws IOException
+   * @throws IOException if the row of added mutation doesn't match the original row
+   * @deprecated since 2.0 version and will be removed in 3.0 version.
+   *             use {@link #add(Mutation)}
    */
+  @Deprecated
   public void add(Delete d) throws IOException {
-    internalAdd(d);
+    add((Mutation) d);
   }
 
-  private void internalAdd(Mutation m) throws IOException {
-    int res = Bytes.compareTo(this.row, m.getRow());
-    if (res != 0) {
-      throw new WrongRowIOException("The row in the recently added Put/Delete <" +
-          Bytes.toStringBinary(m.getRow()) + "> doesn't match the original one <" +
+  /**
+   * Currently only supports {@link Put} and {@link Delete} mutations.
+   *
+   * @param mutation The data to send.
+   * @throws IOException if the row of added mutation doesn't match the original row
+   */
+  public RowMutations add(Mutation mutation) throws IOException {
+    return add(Collections.singletonList(mutation));
+  }
+
+  /**
+   * Currently only supports {@link Put} and {@link Delete} mutations.
+   *
+   * @param mutations The data to send.
+   * @throws IOException if the row of added mutation doesn't match the original row
+   */
+  public RowMutations add(List<? extends Mutation> mutations) throws IOException {
+    for (Mutation mutation : mutations) {
+      if (!Bytes.equals(row, mutation.getRow())) {
+        throw new WrongRowIOException("The row in the recently added Put/Delete <" +
+          Bytes.toStringBinary(mutation.getRow()) + "> doesn't match the original one
<" +
           Bytes.toStringBinary(this.row) + ">");
+      }
     }
-    mutations.add(m);
+    this.mutations.addAll(mutations);
+    return this;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/hbase/blob/adccbb7e/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
index 36b3b90..60c124a 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
@@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 import java.util.Random;
@@ -452,10 +453,9 @@ public class TestFromClientSide3 {
       byte [][] QUALIFIERS = new byte [][] {
         Bytes.toBytes("a"), Bytes.toBytes("b")
       };
-      RowMutations arm = new RowMutations(ROW);
-      Put p = new Put(ROW);
-      p.addColumn(FAMILY, QUALIFIERS[0], VALUE);
-      arm.add(p);
+
+      RowMutations arm = RowMutations.of(Collections.singletonList(
+        new Put(ROW).addColumn(FAMILY, QUALIFIERS[0], VALUE)));
       Object[] batchResult = new Object[1];
       t.batch(Arrays.asList(arm), batchResult);
 
@@ -463,13 +463,9 @@ public class TestFromClientSide3 {
       Result r = t.get(g);
       assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0])));
 
-      arm = new RowMutations(ROW);
-      p = new Put(ROW);
-      p.addColumn(FAMILY, QUALIFIERS[1], VALUE);
-      arm.add(p);
-      Delete d = new Delete(ROW);
-      d.addColumns(FAMILY, QUALIFIERS[0]);
-      arm.add(d);
+      arm = RowMutations.of(Arrays.asList(
+        new Put(ROW).addColumn(FAMILY, QUALIFIERS[1], VALUE),
+        new Delete(ROW).addColumns(FAMILY, QUALIFIERS[0])));
       t.batch(Arrays.asList(arm), batchResult);
       r = t.get(g);
       assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[1])));
@@ -477,10 +473,8 @@ public class TestFromClientSide3 {
 
       // Test that we get the correct remote exception for RowMutations from batch()
       try {
-        arm = new RowMutations(ROW);
-        p = new Put(ROW);
-        p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE);
-        arm.add(p);
+        arm = RowMutations.of(Collections.singletonList(
+          new Put(ROW).addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE)));
         t.batch(Arrays.asList(arm), batchResult);
         fail("Expected RetriesExhaustedWithDetailsException with NoSuchColumnFamilyException");
       } catch(RetriesExhaustedWithDetailsException e) {


Mime
View raw message