This is an automated email from the ASF dual-hosted git repository.

granthenke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 17d569b870e8fb3978a8c02bc1170057a42ca7cc
Author: Grant Henke <[email protected]>
AuthorDate: Tue Oct 27 13:52:43 2020 -0500

    [java] KUDU-1563. Add support for UPDATE_IGNORE and DELETE_IGNORE
    
    Implements java support for the `UPDATE_IGNORE' and
    `DELETE_IGNORE` operations.
    
    I manually tested this against an old server version without
    these operations supported and it returns the correct
    error with a message that says: “Unknown operation type”.
    
    Change-Id: I6c3cf71b2a487bb18fe195fe20795216aff8e8f9
    Reviewed-on: http://gerrit.cloudera.org:8080/16665
    Tested-by: Kudu Jenkins
    Reviewed-by: Alexey Serbin <[email protected]>
    Reviewed-by: Andrew Wong <[email protected]>
---
 .../kudu/client/{Insert.java => DeleteIgnore.java} | 10 ++--
 .../main/java/org/apache/kudu/client/Insert.java   |  3 +-
 .../java/org/apache/kudu/client/InsertIgnore.java  |  4 +-
 .../java/org/apache/kudu/client/KuduTable.java     | 20 ++++++++
 .../java/org/apache/kudu/client/Operation.java     |  4 +-
 .../main/java/org/apache/kudu/client/Update.java   |  3 +-
 .../kudu/client/{Insert.java => UpdateIgnore.java} |  9 ++--
 .../org/apache/kudu/client/TestKuduSession.java    | 59 +++++++++++++++++++++-
 8 files changed, 97 insertions(+), 15 deletions(-)

diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/DeleteIgnore.java
similarity index 78%
copy from java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
copy to java/kudu-client/src/main/java/org/apache/kudu/client/DeleteIgnore.java
index 33491d0..10e3cd2 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/DeleteIgnore.java
@@ -21,18 +21,20 @@ import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.yetus.audience.InterfaceStability;
 
 /**
- * Represents a single row insert. Instances of this class should not be 
reused.
+ * Class of Operation for whole row removals ignoring missing rows.
+ * Only columns which are part of the key can be set.
+ * Instances of this class should not be reused.
  */
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
-public class Insert extends Operation {
+public class DeleteIgnore extends Operation {
 
-  Insert(KuduTable table) {
+  DeleteIgnore(KuduTable table) {
     super(table);
   }
 
   @Override
   ChangeType getChangeType() {
-    return ChangeType.INSERT;
+    return ChangeType.DELETE_IGNORE;
   }
 }
diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
index 33491d0..6ac7fc9 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
@@ -21,7 +21,8 @@ import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.yetus.audience.InterfaceStability;
 
 /**
- * Represents a single row insert. Instances of this class should not be 
reused.
+ * Represents a single row insert.
+ * Instances of this class should not be reused.
  */
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
diff --git 
a/java/kudu-client/src/main/java/org/apache/kudu/client/InsertIgnore.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/InsertIgnore.java
index b7be9e5..507d22d 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/InsertIgnore.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/InsertIgnore.java
@@ -21,8 +21,8 @@ import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.yetus.audience.InterfaceStability;
 
 /**
- * Represents a single row insert ignoring duplicate rows. Instances of this 
class should not
- * be reused.
+ * Represents a single row insert ignoring duplicate rows.
+ * Instances of this class should not be reused.
  */
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
diff --git 
a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduTable.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduTable.java
index 2488015..4749fd4 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduTable.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduTable.java
@@ -189,6 +189,26 @@ public class KuduTable {
   }
 
   /**
+   * Get a new update ignore configured with this table's schema. An update 
ignore will
+   * ignore missing row errors. This is useful to update a row only if it 
exists.
+   * The returned object should not be reused.
+   * @return an update ignore with this table's schema
+   */
+  public UpdateIgnore newUpdateIgnore() {
+    return new UpdateIgnore(this);
+  }
+
+  /**
+   * Get a new delete ignore configured with this table's schema. An delete 
ignore will
+   * ignore missing row errors. This is useful to delete a row only if it 
exists.
+   * The returned object should not be reused.
+   * @return a delete ignore with this table's schema
+   */
+  public DeleteIgnore newDeleteIgnore() {
+    return new DeleteIgnore(this);
+  }
+
+  /**
    * Asynchronously get all the tablets for this table.
    * @param deadline max time spent in milliseconds for the deferred result of 
this method to
    *         get called back, if deadline is reached, the deferred result will 
get erred back
diff --git 
a/java/kudu-client/src/main/java/org/apache/kudu/client/Operation.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/Operation.java
index 8cc9edb..7963ccf 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/Operation.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/Operation.java
@@ -73,7 +73,9 @@ public abstract class Operation extends 
KuduRpc<OperationResponse> {
         (byte) RowOperationsPB.Type.EXCLUSIVE_RANGE_LOWER_BOUND.getNumber()),
     INCLUSIVE_RANGE_UPPER_BOUND(
         (byte) RowOperationsPB.Type.INCLUSIVE_RANGE_UPPER_BOUND.getNumber()),
-    INSERT_IGNORE((byte) RowOperationsPB.Type.INSERT_IGNORE.getNumber());
+    INSERT_IGNORE((byte) RowOperationsPB.Type.INSERT_IGNORE.getNumber()),
+    UPDATE_IGNORE((byte) RowOperationsPB.Type.UPDATE_IGNORE.getNumber()),
+    DELETE_IGNORE((byte) RowOperationsPB.Type.DELETE_IGNORE.getNumber());
 
     ChangeType(byte encodedByte) {
       this.encodedByte = encodedByte;
diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/Update.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/Update.java
index 51811b0..267e314 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/Update.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/Update.java
@@ -21,7 +21,8 @@ import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.yetus.audience.InterfaceStability;
 
 /**
- * Operation to update columns on an existing row. Instances of this class 
should not be reused.
+ * Operation to update columns on an existing row.
+ * Instances of this class should not be reused.
  */
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/UpdateIgnore.java
similarity index 82%
copy from java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
copy to java/kudu-client/src/main/java/org/apache/kudu/client/UpdateIgnore.java
index 33491d0..803af32 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/Insert.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/UpdateIgnore.java
@@ -21,18 +21,19 @@ import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.yetus.audience.InterfaceStability;
 
 /**
- * Represents a single row insert. Instances of this class should not be 
reused.
+ * Represents a single row update ignoring missing rows.
+ * Instances of this class should not be reused.
  */
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
-public class Insert extends Operation {
+public class UpdateIgnore extends Operation {
 
-  Insert(KuduTable table) {
+  UpdateIgnore(KuduTable table) {
     super(table);
   }
 
   @Override
   ChangeType getChangeType() {
-    return ChangeType.INSERT;
+    return ChangeType.UPDATE_IGNORE;
   }
 }
diff --git 
a/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduSession.java 
b/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduSession.java
index 5b1864e..f0364ec 100644
--- a/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduSession.java
+++ b/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduSession.java
@@ -423,6 +423,44 @@ public class TestKuduSession {
   }
 
   @Test(timeout = 10000)
+  public void testUpdateIgnore() throws Exception {
+    KuduTable table = client.createTable(tableName, basicSchema, 
getBasicCreateTableOptions());
+    KuduSession session = client.newSession();
+
+    // Test update ignore does not return a row error.
+    assertFalse(session.apply(createUpdateIgnore(table, 1, 1, 
false)).hasRowError());
+    assertEquals(0, scanTableToStrings(table).size());
+
+    assertFalse(session.apply(createInsert(table, 1)).hasRowError());
+    assertEquals(1, scanTableToStrings(table).size());
+
+    // Test update ignore implements normal update.
+    assertFalse(session.apply(createUpdateIgnore(table, 1, 2, 
false)).hasRowError());
+    List<String> rowStrings = scanTableToStrings(table);
+    assertEquals(1, rowStrings.size());
+    assertEquals(
+        "INT32 key=1, INT32 column1_i=2, INT32 column2_i=3, " +
+            "STRING column3_s=a string, BOOL column4_b=true",
+        rowStrings.get(0));
+  }
+
+  @Test(timeout = 10000)
+  public void testDeleteIgnore() throws Exception {
+    KuduTable table = client.createTable(tableName, basicSchema, 
getBasicCreateTableOptions());
+    KuduSession session = client.newSession();
+
+    // Test delete ignore does not return a row error.
+    assertFalse(session.apply(createDeleteIgnore(table, 1)).hasRowError());
+
+    assertFalse(session.apply(createInsert(table, 1)).hasRowError());
+    assertEquals(1, scanTableToStrings(table).size());
+
+    // Test delete ignore implements normal delete.
+    assertFalse(session.apply(createDeleteIgnore(table, 1)).hasRowError());
+    assertEquals(0, scanTableToStrings(table).size());
+  }
+
+  @Test(timeout = 10000)
   public void testInsertManualFlushNonCoveredRange() throws Exception {
     CreateTableOptions createOptions = 
getBasicTableOptionsWithNonCoveredRange();
     createOptions.setNumReplicas(1);
@@ -551,7 +589,18 @@ public class TestKuduSession {
 
   private Upsert createUpsert(KuduTable table, int key, int secondVal, boolean 
hasNull) {
     Upsert upsert = table.newUpsert();
-    PartialRow row = upsert.getRow();
+    populateUpdateRow(upsert.getRow(), key, secondVal, hasNull);
+    return upsert;
+  }
+
+  private UpdateIgnore createUpdateIgnore(KuduTable table, int key, int 
secondVal,
+                                          boolean hasNull) {
+    UpdateIgnore updateIgnore = table.newUpdateIgnore();
+    populateUpdateRow(updateIgnore.getRow(), key, secondVal, hasNull);
+    return updateIgnore;
+  }
+
+  private void populateUpdateRow(PartialRow row, int key, int secondVal, 
boolean hasNull) {
     row.addInt(0, key);
     row.addInt(1, secondVal);
     row.addInt(2, 3);
@@ -561,7 +610,6 @@ public class TestKuduSession {
       row.addString(3, "a string");
     }
     row.addBoolean(4, true);
-    return upsert;
   }
 
   private Delete createDelete(KuduTable table, int key) {
@@ -571,6 +619,13 @@ public class TestKuduSession {
     return delete;
   }
 
+  private DeleteIgnore createDeleteIgnore(KuduTable table, int key) {
+    DeleteIgnore deleteIgnore = table.newDeleteIgnore();
+    PartialRow row = deleteIgnore.getRow();
+    row.addInt(0, key);
+    return deleteIgnore;
+  }
+
   protected InsertIgnore createInsertIgnore(KuduTable table, int key) {
     InsertIgnore insertIgnore = table.newInsertIgnore();
     PartialRow row = insertIgnore.getRow();

Reply via email to