Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 @@ -18,6 +18,7 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.gax.batching.Batcher;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.ServerStream;
Expand All @@ -33,10 +34,12 @@
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowAdapter;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -865,29 +868,8 @@ public UnaryCallable<RowMutation, Void> mutateRowCallable() {
return stub.mutateRowCallable();
}

/**
* Mutates multiple rows in a batch. Each individual row is mutated atomically as in MutateRow,
* but the entire batch is not executed atomically.
*
* <p>Sample code:
*
* <pre>{@code
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
* try (BulkMutationBatcher batcher = bigtableDataClient.newBulkMutationBatcher()) {
* for (String someValue : someCollection) {
* RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
* .setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
*
* ApiFuture<Void> entryFuture = batcher.add(mutation);
* }
* } catch (BulkMutationFailure failure) {
* // Handle error
* }
* // After `batcher` is closed, all mutations have been applied
* }
* }</pre>
*/
@BetaApi("This surface is likely to change as the batching surface evolves.")
/** @deprecated Please use {@link #newBulkMutationBatcher(String)} API. */
@Deprecated
public BulkMutationBatcher newBulkMutationBatcher() {
return new BulkMutationBatcher(stub.bulkMutateRowsBatchingCallable());
}
Expand Down Expand Up @@ -921,6 +903,34 @@ public void bulkMutateRows(BulkMutation mutation) {
ApiExceptions.callAndTranslateApiException(bulkMutateRowsAsync(mutation));
}

/**
* Mutates multiple rows in a batch. Each individual row is mutated atomically as in MutateRow,
* but the entire batch is not executed atomically.
*
* <p>Sample Code:
*
* <pre>{@code
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
* try (Batcher<RowMutationEntry, Void> batcher = bigtableDataClient.newBulkMutationBatcher("[TABLE]")) {
* for (String someValue : someCollection) {
* ApiFuture<Void> entryFuture =
* batcher.add(
* RowMutationEntry.create("[ROW KEY]")
* .setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
* }
*
* // Blocks until mutations are applied on all submitted row entries.
* batcher.flush();
* }
* // Before `batcher` is closed, all remaining(If any) mutations are applied.
* }
* }</pre>
*/
@BetaApi("This surface is likely to change as the batching surface evolves.")
public Batcher<RowMutationEntry, Void> newBulkMutationBatcher(@Nonnull String tableId) {
return stub.newMutateRowsBatcher(tableId);
}

/**
* Convenience method to mutate multiple rows in a batch. Each individual row is mutated
* atomically as in MutateRow, but the entire batch is not executed atomically. Unlike {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* <p>This class is meant for manual batching, while {@link BulkMutationBatcher} is meant for
* automatic batching with flow control.
*/
public final class BulkMutation implements Serializable {
public final class BulkMutation implements Serializable, Cloneable {
private static final long serialVersionUID = 3522061250439399088L;

private final String tableId;
Expand Down Expand Up @@ -89,6 +89,16 @@ public BulkMutation add(@Nonnull ByteString rowKey, @Nonnull Mutation mutation)
return this;
}

/**
* Add mutation for a particular row. The changes in the mutation will be applied atomic. However
* there is no guarantees about the relative ordering between mutations affecting different rows.
*/
public BulkMutation add(@Nonnull RowMutationEntry entry) {
Preconditions.checkNotNull(entry, "Row mutation entry can't be null");
builder.addEntries(entry.toProto());
return this;
}

@InternalApi
public MutateRowsRequest toProto(RequestContext requestContext) {
String tableName =
Expand All @@ -100,4 +110,12 @@ public MutateRowsRequest toProto(RequestContext requestContext) {
.setAppProfileId(requestContext.getAppProfileId())
.build();
}

/** Creates a copy of {@link BulkMutation}. */
@Override
public BulkMutation clone() {
BulkMutation bulkMutation = BulkMutation.create(tableId);
bulkMutation.builder = this.builder.clone();
return bulkMutation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
import org.threeten.bp.Duration;

/**
* Tracker for outstanding bulk mutations. Allows for the caller to wait for all of the outstanding
* mutations to complete.
*
* @see com.google.cloud.bigtable.data.v2.BigtableDataClient#newBulkMutationBatcher() for example
* usage.
* @deprecated Please use {@link
* com.google.cloud.bigtable.data.v2.BigtableDataClient#newBulkMutationBatcher(String)} API.
*/
@Deprecated
@BetaApi("This surface is likely to change as the batching surface evolves.")
public final class BulkMutationBatcher implements AutoCloseable {
private final UnaryCallable<RowMutation, Void> callable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.InternalApi;
import com.google.bigtable.v2.MutateRowsRequest;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;
import java.io.Serializable;
import javax.annotation.Nonnull;

/**
* Represents a list of mutations targeted at a single row. It is meant to be used as an parameter
* for {@link com.google.cloud.bigtable.data.v2.BigtableDataClient#newBulkMutationBatcher(String)}.
*
* <p>Note: The changes in the mutation will be applied atomically but the ordering between
* different RowMutationEntry instances is not guaranteed.
*/
public class RowMutationEntry implements MutationApi<RowMutationEntry>, Serializable {
private static final long serialVersionUID = 1974738836742298192L;

private final ByteString key;
private final Mutation mutation;

private RowMutationEntry(@Nonnull ByteString key, @Nonnull Mutation mutation) {
Preconditions.checkNotNull(key, "Row key can't be null");
Preconditions.checkNotNull(mutation, "Row mutation can't be null");

this.key = key;
this.mutation = mutation;
}

/** Creates a new instance of the mutation builder. */
public static RowMutationEntry create(@Nonnull String key) {
Preconditions.checkNotNull(key, "Row key can't be null");
return create(ByteString.copyFromUtf8(key));
}

/** Creates a new instance of the mutation builder. */
public static RowMutationEntry create(@Nonnull ByteString key) {
return new RowMutationEntry(key, Mutation.create());
}

/** {@inheritDoc} */
@Override
public RowMutationEntry setCell(
@Nonnull String familyName, @Nonnull String qualifier, @Nonnull String value) {
mutation.setCell(familyName, qualifier, value);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry setCell(
@Nonnull String familyName,
@Nonnull String qualifier,
long timestamp,
@Nonnull String value) {
mutation.setCell(familyName, qualifier, timestamp, value);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry setCell(
@Nonnull String familyName, @Nonnull ByteString qualifier, @Nonnull ByteString value) {
mutation.setCell(familyName, qualifier, value);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry setCell(
@Nonnull String familyName,
@Nonnull ByteString qualifier,
long timestamp,
@Nonnull ByteString value) {
mutation.setCell(familyName, qualifier, timestamp, value);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry deleteCells(@Nonnull String familyName, @Nonnull String qualifier) {
mutation.deleteCells(familyName, qualifier);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry deleteCells(@Nonnull String familyName, @Nonnull ByteString qualifier) {
mutation.deleteCells(familyName, qualifier);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry deleteCells(
@Nonnull String familyName,
@Nonnull ByteString qualifier,
@Nonnull Range.TimestampRange timestampRange) {
mutation.deleteCells(familyName, qualifier, timestampRange);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry deleteFamily(@Nonnull String familyName) {
mutation.deleteFamily(familyName);
return this;
}

/** {@inheritDoc} */
@Override
public RowMutationEntry deleteRow() {
mutation.deleteRow();
return this;
}

@InternalApi
public MutateRowsRequest.Entry toProto() {
return MutateRowsRequest.Entry.newBuilder()
.setRowKey(key)
.addAllMutations(mutation.getMutations())
.build();
}
}
Loading