-
Notifications
You must be signed in to change notification settings - Fork 135
feat: add support for BatchWriteAtLeastOnce #2520
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
Changes from 23 commits
7ff7cb2
43c4f63
abedd15
21d8f67
4858965
81743a7
65abc96
8d76346
a3569ae
4cb0ada
5401b7d
5103b5c
95c3326
5678207
347e9c3
4c7251f
1deaa29
acece36
5f04154
8e15c5c
56f3929
356849e
07f3bfb
1ca0d31
c3e3d7b
a036ced
d8ef791
7c4c73d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,12 @@ | |
|
|
||
| package com.google.cloud.spanner; | ||
|
|
||
| import com.google.api.gax.rpc.ServerStream; | ||
| import com.google.cloud.Timestamp; | ||
| import com.google.cloud.spanner.Options.RpcPriority; | ||
| import com.google.cloud.spanner.Options.TransactionOption; | ||
| import com.google.cloud.spanner.Options.UpdateOption; | ||
| import com.google.spanner.v1.BatchWriteResponse; | ||
|
|
||
| /** | ||
| * Interface for all the APIs that are used to read/write data into a Cloud Spanner database. An | ||
|
|
@@ -191,6 +193,77 @@ CommitResponse writeWithOptions(Iterable<Mutation> mutations, TransactionOption. | |
| CommitResponse writeAtLeastOnceWithOptions( | ||
| Iterable<Mutation> mutations, TransactionOption... options) throws SpannerException; | ||
|
|
||
| /** | ||
| * Applies batch of mutation groups in a collection of efficient transactions. The mutation groups | ||
| * are applied non-atomically in an unspecified order and thus, they must be independent of each | ||
| * other. Partial failure is possible, i.e., some mutation groups may have been applied | ||
| * successfully, while some may have failed. The results of individual batches are streamed into | ||
| * the response as and when the batches are applied. | ||
| * | ||
| * <p>Since this method does not feature replay protection, it may attempt to apply {@code | ||
| * mutation groups} more than once; if the mutation groups are not idempotent, this may lead to a | ||
| * failure being reported when the mutation group was applied once. For example, an insert may | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * fail with {@link ErrorCode#ALREADY_EXISTS} even though the row did not exist before this method | ||
| * was called. For this reason, most users of the library will prefer to use {@link | ||
| * #write(Iterable)} instead. However, {@code batchWriteAtLeastOnce()} method may be appropriate | ||
| * for non-atomically committing multiple mutation groups in a single RPC with low latency. | ||
| * | ||
| * <p>Example of BatchWriteAtLeastOnce | ||
| * | ||
| * <pre>{@code | ||
| * ServerStream<BatchWriteResponse> responses = | ||
| * dbClient.batchWriteAtLeastOnceWithOptions( | ||
| * ImmutableList.of(MUTATION_GROUP1, MUTATION_GROUP2)); | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * for (BatchWriteResponse response : responses) { | ||
| * // Do something when a response is received. | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @return ServerStream\<BatchWriteResponse> | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(Iterable<MutationGroup> mutationGroups) | ||
|
||
| throws SpannerException; | ||
|
|
||
| /** | ||
| * Applies batch of mutation groups in a collection of efficient transactions. The mutation groups | ||
| * are applied non-atomically in an unspecified order and thus, they must be independent of each | ||
| * other. Partial failure is possible, i.e., some mutation groups may have been applied | ||
| * successfully, while some may have failed. The results of individual batches are streamed into | ||
| * the response as and when the batches are applied. | ||
| * | ||
| * <p>Since this method does not feature replay protection, it may attempt to apply {@code | ||
| * mutation groups} more than once; if the mutation groups are not idempotent, this may lead to a | ||
| * failure being reported when the mutation group was applied once. For example, an insert may | ||
| * fail with {@link ErrorCode#ALREADY_EXISTS} even though the row did not exist before this method | ||
| * was called. For this reason, most users of the library will prefer to use {@link | ||
| * #write(Iterable)} instead. However, {@code batchWriteAtLeastOnce()} method may be appropriate | ||
| * for non-atomically committing multiple mutation groups in a single RPC with low latency. | ||
| * | ||
| * <p>Example of BatchWriteAtLeastOnceWithOptions | ||
| * | ||
| * <pre>{@code | ||
| * ServerStream<BatchWriteResponse> responses = | ||
| * dbClient.batchWriteAtLeastOnceWithOptions( | ||
| * ImmutableList.of(MUTATION_GROUP1, MUTATION_GROUP2), | ||
| * Options.tag("batch-write-tag")); | ||
| * for (BatchWriteResponse response : responses) { | ||
| * // Do something when a response is received. | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * Options for a transaction can include: | ||
| * | ||
| * <ul> | ||
| * <li>{@link Options#priority(com.google.cloud.spanner.Options.RpcPriority)}: The {@link | ||
| * RpcPriority} to use for the batch write request. | ||
| * <li>{@link Options#tag(String)}: The transaction tag to use for the batch write request. | ||
| * </ul> | ||
| * | ||
| * @return ServerStream\<BatchWriteResponse> | ||
| */ | ||
| ServerStream<BatchWriteResponse> batchWriteAtLeastOnceWithOptions( | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Iterable<MutationGroup> mutationGroups, TransactionOption... options) throws SpannerException; | ||
|
|
||
| /** | ||
| * Returns a context in which a single read can be performed using {@link TimestampBound#strong()} | ||
| * concurrency. This method will return a {@link ReadContext} that will not return the read | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2023 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 | ||
| * | ||
| * http://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.spanner; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.spanner.v1.BatchWriteRequest; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Represents a group of Cloud Spanner mutations to be committed together. | ||
| */ | ||
| public class MutationGroup { | ||
| private final List<Mutation> mutations; | ||
|
|
||
| private MutationGroup(List<Mutation> mutations) { | ||
| this.mutations = mutations; | ||
| } | ||
|
|
||
| public static MutationGroup of(Mutation... mutations) { | ||
rajatbhatta marked this conversation as resolved.
Show resolved
Hide resolved
rajatbhatta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Preconditions.checkArgument(mutations.length > 0, "Should pass in at least one mutation."); | ||
| return new MutationGroup(Arrays.asList(mutations)); | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public List<Mutation> getMutations() { | ||
rajatbhatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return mutations; | ||
| } | ||
|
|
||
| static BatchWriteRequest.MutationGroup toProto(final MutationGroup mutationGroup) { | ||
| List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>(); | ||
| Mutation.toProto(mutationGroup.getMutations(), mutationsProto); | ||
| return BatchWriteRequest.MutationGroup.newBuilder().addAllMutations(mutationsProto).build(); | ||
olavloite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| static List<BatchWriteRequest.MutationGroup> toListProto( | ||
| final Iterable<MutationGroup> mutationGroups) { | ||
| List<BatchWriteRequest.MutationGroup> mutationGroupsProto = new ArrayList<>(); | ||
| for (MutationGroup group : mutationGroups) { | ||
| mutationGroupsProto.add(toProto(group)); | ||
| } | ||
| return mutationGroupsProto; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.