Skip to content

Commit 0e58b7d

Browse files
ajaaympongad
authored andcommitted
bigtable: RowMutation should allow passing of a Mutation (googleapis#3643)
Fixes googleapis#3637
1 parent 37675e9 commit 0e58b7d

File tree

2 files changed

+49
-3
lines changed
  • google-cloud-clients/google-cloud-bigtable/src

2 files changed

+49
-3
lines changed

google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,54 @@ public final class RowMutation implements MutationApi<RowMutation>, Serializable
3737
private final ByteString key;
3838
private final Mutation mutation;
3939

40-
private RowMutation(String tableId, ByteString key) {
40+
private RowMutation(String tableId, ByteString key, Mutation mutation) {
4141
this.tableId = tableId;
4242
this.key = key;
43-
this.mutation = Mutation.create();
43+
this.mutation = mutation;
4444
}
4545

46+
/** Creates a new instance of the mutation builder. */
4647
public static RowMutation create(@Nonnull String tableId, @Nonnull String key) {
4748
return create(tableId, ByteString.copyFromUtf8(key));
4849
}
4950

51+
/** Creates a new instance of the mutation builder. */
5052
public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key) {
51-
return new RowMutation(tableId, key);
53+
return new RowMutation(tableId, key, Mutation.create());
54+
}
55+
56+
/**
57+
* Creates new instance of mutation builder by wrapping existing set of row mutations.
58+
* The builder will be owned by this RowMutation and should not be used by the caller after this call.
59+
* This functionality is intended for advanced usage.
60+
*
61+
* <p>Sample code:
62+
*
63+
* <pre><code>
64+
* Mutation mutation = Mutation.create()
65+
* .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
66+
* RowMutation rowMutation = RowMutation.create("[TABLE]", "[ROW_KEY]", mutation);
67+
* </code></pre>
68+
*/
69+
public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @Nonnull Mutation mutation) {
70+
return create(tableId, ByteString.copyFromUtf8(key), mutation);
71+
}
72+
73+
/**
74+
* Creates new instance of mutation builder by wrapping existing set of row mutations.
75+
* The builder will be owned by this RowMutation and should not be used by the caller after this call.
76+
* This functionality is intended for advanced usage.
77+
*
78+
* <p>Sample code:
79+
*
80+
* <pre><code>
81+
* Mutation mutation = Mutation.create()
82+
* .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
83+
* RowMutation rowMutation = RowMutation.create("[TABLE]", [BYTE_STRING_ROW_KEY], mutation);
84+
* </code></pre>
85+
*/
86+
public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, @Nonnull Mutation mutation) {
87+
return new RowMutation(tableId, key, mutation);
5288
}
5389

5490
@Override

google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public void toBulkProtoTest() {
9090
.isIn(timestampRange);
9191
}
9292

93+
@Test
94+
public void toProtoTestWithProvidedMutation() {
95+
Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value");
96+
RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation);
97+
98+
MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT);
99+
100+
assertThat(actualRowMutation.getMutationsList()).isEqualTo(mutation.getMutations());
101+
}
102+
93103
@Test
94104
public void serializationTest() throws IOException, ClassNotFoundException {
95105
RowMutation expected =

0 commit comments

Comments
 (0)