From 02e68a319986f864e3ad52f58d9a2d5f993dee4c Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 5 Sep 2018 11:35:47 -0400 Subject: [PATCH 1/7] Fix #3637 Bigtable RowMutation should allow passing of a Mutation --- .../bigtable/data/v2/models/RowMutation.java | 14 ++++++++++ .../data/v2/models/RowMutationTest.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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..814e2045e504 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 @@ -43,6 +43,12 @@ private RowMutation(String tableId, ByteString key) { this.mutation = Mutation.create(); } + private RowMutation(String tableId, ByteString key, Mutation mutation) { + this.tableId = tableId; + this.key = key; + this.mutation = mutation; + } + public static RowMutation create(@Nonnull String tableId, @Nonnull String key) { return create(tableId, ByteString.copyFromUtf8(key)); } @@ -51,6 +57,14 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke return new RowMutation(tableId, key); } + public static RowMutation create(@Nonnull String tableId, @Nonnull String key, Mutation mutation) { + return create(tableId, ByteString.copyFromUtf8(key), mutation); + } + + public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, Mutation mutation) { + return new RowMutation(tableId, key, mutation); + } + @Override public RowMutation setCell( @Nonnull String familyName, @Nonnull String qualifier, @Nonnull String value) { 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..b82b35e52124 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,32 @@ public void toBulkProtoTest() { .isIn(timestampRange); } + @Test + public void toProtoTestWithProvidedMutation() { + long timestampMin = System.currentTimeMillis() * 1_000; + + Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); + + MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); + + com.google.common.collect.Range timestampRange = + com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); + + assertThat(actualRowMutation.getTableName()) + .isEqualTo( + TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString()); + assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); + assertThat(actualRowMutation.getEntriesList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) + .isEqualTo(ByteString.copyFromUtf8("fake-value")); + + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) + .isIn(timestampRange); + } + @Test public void serializationTest() throws IOException, ClassNotFoundException { RowMutation expected = From f0bb74aee78f12860cd2149d6b102bc74b55cbff Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 5 Sep 2018 11:39:41 -0400 Subject: [PATCH 2/7] add @Nonnull annotation --- .../com/google/cloud/bigtable/data/v2/models/RowMutation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 814e2045e504..279e37af8f36 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 @@ -57,11 +57,11 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke return new RowMutation(tableId, key); } - public static RowMutation create(@Nonnull String tableId, @Nonnull String key, Mutation mutation) { + public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @Nonnull Mutation mutation) { return create(tableId, ByteString.copyFromUtf8(key), mutation); } - public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, Mutation mutation) { + public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, @Nonnull Mutation mutation) { return new RowMutation(tableId, key, mutation); } From 4a67e232a19bab9e34246ade524baf6da2f90b56 Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 5 Sep 2018 13:15:01 -0400 Subject: [PATCH 3/7] Removed extra construction, updated factory method to pass empty Mutation, added usage in java doc. --- .../bigtable/data/v2/models/RowMutation.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) 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 279e37af8f36..7dfdac48fa58 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,30 +37,48 @@ public final class RowMutation implements MutationApi, Serializable private final ByteString key; private final Mutation mutation; - private RowMutation(String tableId, ByteString key) { - this.tableId = tableId; - this.key = key; - this.mutation = Mutation.create(); - } - private RowMutation(String tableId, ByteString key, Mutation mutation) { this.tableId = tableId; this.key = key; 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 mutation builder. + * + *

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 mutation builder. + * + *

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); } From 8e7d69ac169a067cebec56670d62e8401800a8d6 Mon Sep 17 00:00:00 2001 From: Ajay Date: Thu, 6 Sep 2018 12:24:49 -0400 Subject: [PATCH 4/7] fix with deterministic timestamp --- .../bigtable/data/v2/models/RowMutation.java | 8 +- .../data/v2/models/RowMutationTest.java | 119 +++++++++--------- 2 files changed, 67 insertions(+), 60 deletions(-) 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 7dfdac48fa58..dc7f3c01aaa6 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 @@ -54,7 +54,9 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke } /** - * Creates new instance of mutation builder by wrapping existing mutation builder. + * Creates new instance of mutation builder by wrapping existing 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: * @@ -69,7 +71,9 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @ } /** - * Creates new instance of mutation builder by wrapping existing mutation builder. + * Creates new instance of mutation builder by wrapping existing 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: * 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 b82b35e52124..07aa2fef0fec 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 @@ -38,89 +38,53 @@ public class RowMutationTest { private static final String APP_PROFILE_ID = "fake-profile"; private static final RequestContext REQUEST_CONTEXT = RequestContext.create(INSTANCE_NAME, APP_PROFILE_ID); + private static final String TABLE_ID = "fake-table"; + private static final ByteString ROW_KEY = ByteString.copyFromUtf8("fake-key"); + private static final String FAMILY_NAME = "fake-family"; + private static final ByteString QUALIFIER = ByteString.copyFromUtf8("fake-qualifier"); + private static final ByteString VALUE = ByteString.copyFromUtf8("fake-value"); + private static final String TABLE_NAME = TableName + .of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString(); + private static final long TIMESTAMP = System.currentTimeMillis() * 1_000; @Test public void toProtoTest() { - long timestampMin = System.currentTimeMillis() * 1_000; - RowMutation rowMutation = - RowMutation.create("fake-table", "fake-key") - .setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation.create(TABLE_ID, ROW_KEY) + .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); - com.google.common.collect.Range timestampRange = - com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); - - assertThat(actualRowMutation.getTableName()) - .isEqualTo( - TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") - .toString()); - assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); - assertThat(actualRowMutation.getMutationsList()).hasSize(1); - assertThat(actualRowMutation.getMutations(0).getSetCell().getValue()) - .isEqualTo(ByteString.copyFromUtf8("fake-value")); - assertThat(actualRowMutation.getMutations(0).getSetCell().getTimestampMicros()) - .isIn(timestampRange); + + assertThat(actualRowMutation).isEqualTo(createMutateRowRequest()); } @Test public void toBulkProtoTest() { - long timestampMin = System.currentTimeMillis() * 1_000; - RowMutation rowMutation = - RowMutation.create("fake-table", "fake-key") - .setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation.create(TABLE_ID, ROW_KEY) + .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); - com.google.common.collect.Range timestampRange = - com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); - - assertThat(actualRowMutation.getTableName()) - .isEqualTo( - TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") - .toString()); - assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); - assertThat(actualRowMutation.getEntriesList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) - .isEqualTo(ByteString.copyFromUtf8("fake-value")); - - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) - .isIn(timestampRange); + assertThat(actualRowMutation).isEqualTo(createMutateRowsRequest()); } @Test public void toProtoTestWithProvidedMutation() { - long timestampMin = System.currentTimeMillis() * 1_000; - - Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); - RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); + Mutation mutation = Mutation.create().setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); + RowMutation rowMutation = RowMutation.create(TABLE_ID, ROW_KEY, mutation); - MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); + MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); - com.google.common.collect.Range timestampRange = - com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); - - assertThat(actualRowMutation.getTableName()) - .isEqualTo( - TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") - .toString()); - assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); - assertThat(actualRowMutation.getEntriesList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) - .isEqualTo(ByteString.copyFromUtf8("fake-value")); - - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) - .isIn(timestampRange); + assertThat(actualRowMutation).isEqualTo(createMutateRowRequest()); } @Test public void serializationTest() throws IOException, ClassNotFoundException { RowMutation expected = - RowMutation.create("fake-table", "fake-key") - .setCell("fake-family", "fake-qualifier", 10_000, "fake-value"); + RowMutation.create(TABLE_ID, ROW_KEY) + .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); @@ -132,4 +96,43 @@ public void serializationTest() throws IOException, ClassNotFoundException { RowMutation actual = (RowMutation) ois.readObject(); assertThat(actual.toProto(REQUEST_CONTEXT)).isEqualTo(expected.toProto(REQUEST_CONTEXT)); } + + private static com.google.bigtable.v2.Mutation.SetCell createSetCell() { + return com.google.bigtable.v2.Mutation.SetCell.newBuilder() + .setFamilyName(FAMILY_NAME) + .setColumnQualifier(QUALIFIER) + .setValue(VALUE) + .setTimestampMicros(TIMESTAMP) + .build(); + } + + private static com.google.bigtable.v2.Mutation createSetCellMutation() { + return com.google.bigtable.v2.Mutation.newBuilder() + .setSetCell(createSetCell()) + .build(); + } + + private static MutateRowRequest createMutateRowRequest() { + return MutateRowRequest.newBuilder() + .addMutations(createSetCellMutation()) + .setAppProfileId(APP_PROFILE_ID) + .setTableName(TABLE_NAME) + .setRowKey(ROW_KEY) + .build(); + } + + private static MutateRowsRequest.Entry createEntry() { + return MutateRowsRequest.Entry.newBuilder() + .addMutations(createSetCellMutation()) + .setRowKey(ROW_KEY) + .build(); + } + + private static MutateRowsRequest createMutateRowsRequest() { + return MutateRowsRequest.newBuilder() + .addEntries(createEntry()) + .setAppProfileId(APP_PROFILE_ID) + .setTableName(TABLE_NAME) + .build(); + } } From f5db05c49944809e1cf1de00e89f55076ddad099 Mon Sep 17 00:00:00 2001 From: Ajay Date: Thu, 6 Sep 2018 12:54:18 -0400 Subject: [PATCH 5/7] Revert "fix with deterministic timestamp" This reverts commit 8e7d69ac169a067cebec56670d62e8401800a8d6. --- .../bigtable/data/v2/models/RowMutation.java | 8 +- .../data/v2/models/RowMutationTest.java | 119 +++++++++--------- 2 files changed, 60 insertions(+), 67 deletions(-) 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 dc7f3c01aaa6..7dfdac48fa58 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 @@ -54,9 +54,7 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke } /** - * Creates new instance of mutation builder by wrapping existing 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. + * Creates new instance of mutation builder by wrapping existing mutation builder. * *

Sample code: * @@ -71,9 +69,7 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @ } /** - * Creates new instance of mutation builder by wrapping existing 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. + * Creates new instance of mutation builder by wrapping existing mutation builder. * *

Sample code: * 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 07aa2fef0fec..b82b35e52124 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 @@ -38,53 +38,89 @@ public class RowMutationTest { private static final String APP_PROFILE_ID = "fake-profile"; private static final RequestContext REQUEST_CONTEXT = RequestContext.create(INSTANCE_NAME, APP_PROFILE_ID); - private static final String TABLE_ID = "fake-table"; - private static final ByteString ROW_KEY = ByteString.copyFromUtf8("fake-key"); - private static final String FAMILY_NAME = "fake-family"; - private static final ByteString QUALIFIER = ByteString.copyFromUtf8("fake-qualifier"); - private static final ByteString VALUE = ByteString.copyFromUtf8("fake-value"); - private static final String TABLE_NAME = TableName - .of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") - .toString(); - private static final long TIMESTAMP = System.currentTimeMillis() * 1_000; @Test public void toProtoTest() { + long timestampMin = System.currentTimeMillis() * 1_000; + RowMutation rowMutation = - RowMutation.create(TABLE_ID, ROW_KEY) - .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); + RowMutation.create("fake-table", "fake-key") + .setCell("fake-family", "fake-qualifier", "fake-value"); MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); - - assertThat(actualRowMutation).isEqualTo(createMutateRowRequest()); + com.google.common.collect.Range timestampRange = + com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); + + assertThat(actualRowMutation.getTableName()) + .isEqualTo( + TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString()); + assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); + assertThat(actualRowMutation.getMutationsList()).hasSize(1); + assertThat(actualRowMutation.getMutations(0).getSetCell().getValue()) + .isEqualTo(ByteString.copyFromUtf8("fake-value")); + assertThat(actualRowMutation.getMutations(0).getSetCell().getTimestampMicros()) + .isIn(timestampRange); } @Test public void toBulkProtoTest() { + long timestampMin = System.currentTimeMillis() * 1_000; + RowMutation rowMutation = - RowMutation.create(TABLE_ID, ROW_KEY) - .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); + RowMutation.create("fake-table", "fake-key") + .setCell("fake-family", "fake-qualifier", "fake-value"); MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); - assertThat(actualRowMutation).isEqualTo(createMutateRowsRequest()); + com.google.common.collect.Range timestampRange = + com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); + + assertThat(actualRowMutation.getTableName()) + .isEqualTo( + TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString()); + assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); + assertThat(actualRowMutation.getEntriesList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) + .isEqualTo(ByteString.copyFromUtf8("fake-value")); + + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) + .isIn(timestampRange); } @Test public void toProtoTestWithProvidedMutation() { - Mutation mutation = Mutation.create().setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); - RowMutation rowMutation = RowMutation.create(TABLE_ID, ROW_KEY, mutation); + long timestampMin = System.currentTimeMillis() * 1_000; - MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); + Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); - assertThat(actualRowMutation).isEqualTo(createMutateRowRequest()); + MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); + + com.google.common.collect.Range timestampRange = + com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); + + assertThat(actualRowMutation.getTableName()) + .isEqualTo( + TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString()); + assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); + assertThat(actualRowMutation.getEntriesList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) + .isEqualTo(ByteString.copyFromUtf8("fake-value")); + + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) + .isIn(timestampRange); } @Test public void serializationTest() throws IOException, ClassNotFoundException { RowMutation expected = - RowMutation.create(TABLE_ID, ROW_KEY) - .setCell(FAMILY_NAME, QUALIFIER, TIMESTAMP, VALUE); + RowMutation.create("fake-table", "fake-key") + .setCell("fake-family", "fake-qualifier", 10_000, "fake-value"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); @@ -96,43 +132,4 @@ public void serializationTest() throws IOException, ClassNotFoundException { RowMutation actual = (RowMutation) ois.readObject(); assertThat(actual.toProto(REQUEST_CONTEXT)).isEqualTo(expected.toProto(REQUEST_CONTEXT)); } - - private static com.google.bigtable.v2.Mutation.SetCell createSetCell() { - return com.google.bigtable.v2.Mutation.SetCell.newBuilder() - .setFamilyName(FAMILY_NAME) - .setColumnQualifier(QUALIFIER) - .setValue(VALUE) - .setTimestampMicros(TIMESTAMP) - .build(); - } - - private static com.google.bigtable.v2.Mutation createSetCellMutation() { - return com.google.bigtable.v2.Mutation.newBuilder() - .setSetCell(createSetCell()) - .build(); - } - - private static MutateRowRequest createMutateRowRequest() { - return MutateRowRequest.newBuilder() - .addMutations(createSetCellMutation()) - .setAppProfileId(APP_PROFILE_ID) - .setTableName(TABLE_NAME) - .setRowKey(ROW_KEY) - .build(); - } - - private static MutateRowsRequest.Entry createEntry() { - return MutateRowsRequest.Entry.newBuilder() - .addMutations(createSetCellMutation()) - .setRowKey(ROW_KEY) - .build(); - } - - private static MutateRowsRequest createMutateRowsRequest() { - return MutateRowsRequest.newBuilder() - .addEntries(createEntry()) - .setAppProfileId(APP_PROFILE_ID) - .setTableName(TABLE_NAME) - .build(); - } } From a635303cea7b95e53978e947cf6e72c84eb51b7f Mon Sep 17 00:00:00 2001 From: Ajay Date: Thu, 6 Sep 2018 13:30:28 -0400 Subject: [PATCH 6/7] Fixed as per feedback --- .../bigtable/data/v2/models/RowMutation.java | 8 ++++++-- .../data/v2/models/RowMutationTest.java | 20 ++----------------- 2 files changed, 8 insertions(+), 20 deletions(-) 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 7dfdac48fa58..dc7f3c01aaa6 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 @@ -54,7 +54,9 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke } /** - * Creates new instance of mutation builder by wrapping existing mutation builder. + * Creates new instance of mutation builder by wrapping existing 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: * @@ -69,7 +71,9 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @ } /** - * Creates new instance of mutation builder by wrapping existing mutation builder. + * Creates new instance of mutation builder by wrapping existing 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: * 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 b82b35e52124..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 @@ -92,28 +92,12 @@ public void toBulkProtoTest() { @Test public void toProtoTestWithProvidedMutation() { - long timestampMin = System.currentTimeMillis() * 1_000; - Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); - MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); - - com.google.common.collect.Range timestampRange = - com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); - - assertThat(actualRowMutation.getTableName()) - .isEqualTo( - TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") - .toString()); - assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); - assertThat(actualRowMutation.getEntriesList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) - .isEqualTo(ByteString.copyFromUtf8("fake-value")); + MutateRowRequest actualRowMutation = rowMutation.toProto(REQUEST_CONTEXT); - assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) - .isIn(timestampRange); + assertThat(actualRowMutation.getMutationsList()).isEqualTo(mutation.getMutations()); } @Test From 8b5b037d194d2ae190e8c7b7dc4f4d2197d8af05 Mon Sep 17 00:00:00 2001 From: Ajay Date: Thu, 6 Sep 2018 13:36:44 -0400 Subject: [PATCH 7/7] fix typo --- .../com/google/cloud/bigtable/data/v2/models/RowMutation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 dc7f3c01aaa6..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 @@ -54,7 +54,7 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke } /** - * Creates new instance of mutation builder by wrapping existing existing set of row mutations. + * 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. * @@ -71,7 +71,7 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull String key, @ } /** - * Creates new instance of mutation builder by wrapping existing existing set of row mutations. + * 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. *