Skip to content

Commit

Permalink
Bigtable: Add createUnsafe factory method in Mutation model (#3800)
Browse files Browse the repository at this point in the history
* creates unsafe mutation which allows server side timestamp

* update doc and use constant for server side timestamp

* update doc
  • Loading branch information
ajaaym authored and chingor13 committed Oct 10, 2018
1 parent 636588c commit 8548be5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.Mutation.DeleteFromColumn;
import com.google.bigtable.v2.Mutation.DeleteFromFamily;
Expand Down Expand Up @@ -43,18 +44,38 @@ public final class Mutation implements MutationApi<Mutation>, Serializable {
static final int MAX_MUTATIONS = 100_000;
@InternalApi("Visible for testing")
static final int MAX_BYTE_SIZE = 200 * 1024 * 1024;
@InternalApi("Visible for testing")
static final long SERVER_SIDE_TIMESTAMP = -1;

private final boolean allowServersideTimestamp;

private transient ImmutableList.Builder<com.google.bigtable.v2.Mutation> mutations =
ImmutableList.builder();

private int numMutations;
private long byteSize;

/**
* Creates new instance of Mutation object.
*/
public static Mutation create() {
return new Mutation();
return new Mutation(false);
}

/**
* Creates new instance of Mutation object which allows setCell operation to use server
* side timestamp. This is dangerous because mutations will no longer be idempotent, which
* might cause multiple duplicate values to be stored in Bigtable. This option should only
* be used for advanced usecases with extreme care.
*/
@BetaApi
public static Mutation createUnsafe() {
return new Mutation(true);
}

private Mutation() {}
private Mutation(boolean allowServersideTimestamp) {
this.allowServersideTimestamp = allowServersideTimestamp;
}

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
Expand Down Expand Up @@ -102,7 +123,9 @@ public Mutation setCell(
Validations.validateFamily(familyName);
Preconditions.checkNotNull(qualifier, "qualifier can't be null.");
Preconditions.checkNotNull(value, "value can't be null.");
Preconditions.checkArgument(timestamp != -1, "Serverside timestamps are not supported");
if (!allowServersideTimestamp) {
Preconditions.checkArgument(timestamp != SERVER_SIDE_TIMESTAMP, "Serverside timestamps are not supported");
}

addMutation(
com.google.bigtable.v2.Mutation.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public void setCellTest() {
assertThat(actual.get(3).getSetCell().getTimestampMicros()).isIn(expectedTimestampRange);
}

@Test
public void setCellWithServerSideTimestamp() {
Mutation mutation = Mutation.createUnsafe();
mutation
.setCell(
"fake-family",
ByteString.copyFromUtf8("fake-qualifier"),
Mutation.SERVER_SIDE_TIMESTAMP,
ByteString.copyFromUtf8("fake-value"));
List<com.google.bigtable.v2.Mutation> actual = mutation.getMutations();
assertThat(actual.get(0).getSetCell().getTimestampMicros()).isEqualTo(Mutation.SERVER_SIDE_TIMESTAMP);
}

@Test
public void deleteColumnTest() {
mutation
Expand Down

0 comments on commit 8548be5

Please sign in to comment.