Skip to content

Commit

Permalink
fix with deterministic timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaaym committed Sep 6, 2018
1 parent 4a67e23 commit 8e7d69a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>Sample code:
*
Expand All @@ -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.
*
* <p>Sample code:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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<Long> 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<Long> 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);
Expand All @@ -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();
}
}

0 comments on commit 8e7d69a

Please sign in to comment.