Skip to content

Commit

Permalink
bigtable: RowMutation should allow passing of a Mutation (googleapis#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaaym authored and pongad committed Sep 8, 2018
1 parent 37675e9 commit 0e58b7d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,54 @@ public final class RowMutation implements MutationApi<RowMutation>, 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.
*
* <p>Sample code:
*
* <pre><code>
* Mutation mutation = Mutation.create()
* .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
* RowMutation rowMutation = RowMutation.create("[TABLE]", "[ROW_KEY]", mutation);
* </code></pre>
*/
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.
*
* <p>Sample code:
*
* <pre><code>
* Mutation mutation = Mutation.create()
* .setCell("[FAMILY_NAME]", "[QUALIFIER]", [TIMESTAMP], "[VALUE]");
* RowMutation rowMutation = RowMutation.create("[TABLE]", [BYTE_STRING_ROW_KEY], mutation);
* </code></pre>
*/
public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, @Nonnull Mutation mutation) {
return new RowMutation(tableId, key, mutation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 0e58b7d

Please sign in to comment.