diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java index 5bd88e55332a..eba7247fc019 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java @@ -37,18 +37,54 @@ public final class RowMutation implements MutationApi, Serializable private final ByteString key; private final Mutation mutation; - private RowMutation(String tableId, ByteString key) { + private RowMutation(String tableId, ByteString key, Mutation mutation) { this.tableId = tableId; this.key = key; - this.mutation = Mutation.create(); + this.mutation = mutation; } + /** Creates a new instance of the mutation builder. */ public static RowMutation create(@Nonnull String tableId, @Nonnull String key) { return create(tableId, ByteString.copyFromUtf8(key)); } + /** Creates a new instance of the mutation builder. */ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key) { - return new RowMutation(tableId, key); + return new RowMutation(tableId, key, Mutation.create()); + } + + /** + * Creates new instance of mutation builder by wrapping existing set of row mutations. + * The builder will be owned by this RowMutation and should not be used by the caller after this call. + * This functionality is intended for advanced usage. + * + *

Sample code: + * + *


+   * Mutation mutation = Mutation.create()
+   *     .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
+   * RowMutation rowMutation = RowMutation.create("[TABLE]", "[ROW_KEY]", mutation);
+   * 
+ */ + public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @Nonnull Mutation mutation) { + return create(tableId, ByteString.copyFromUtf8(key), mutation); + } + + /** + * Creates new instance of mutation builder by wrapping existing set of row mutations. + * The builder will be owned by this RowMutation and should not be used by the caller after this call. + * This functionality is intended for advanced usage. + * + *

Sample code: + * + *


+   * Mutation mutation = Mutation.create()
+   *     .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
+   * RowMutation rowMutation = RowMutation.create("[TABLE]", [BYTE_STRING_ROW_KEY], mutation);
+   * 
+ */ + public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, @Nonnull Mutation mutation) { + return new RowMutation(tableId, key, mutation); } @Override diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java index 080459de83ba..d38ef5bffe8b 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java @@ -90,6 +90,16 @@ public void toBulkProtoTest() { .isIn(timestampRange); } + @Test + public void toProtoTestWithProvidedMutation() { + Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); + + MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); + + assertThat(actualRowMutation.getMutationsList()).isEqualTo(mutation.getMutations()); + } + @Test public void serializationTest() throws IOException, ClassNotFoundException { RowMutation expected =