Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigtable: Add createUnsafe factory method in Mutation model #3800

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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