-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for BatchWriteAtLeastOnce (#2520)
* feat: add support for BatchWriteAtleastOnce * test: add batchwrite() support to MockSpannerServiceImpl * test: add commit timestamp to proto * test: add commit timestamp to proto * test: add commit timestamp to proto * consume the stream in tests * refactor tests * refactor tests * test if mutations are correctly applied * null check * skip for emulator * add method documentation * add method documentation * add method documentation * remove autogenerated code * remove autogenerated tests * batchWriteAtleastOnce -> batchWriteAtLeastOnce * batchWriteAtleastOnceWithOptions -> batchWriteAtLeastOnceWithOptions * changes based on updated batch write API * add copyright and doc * address review comments * address review comments * add more documentation --------- Co-authored-by: Arpan Mishra <[email protected]>
- Loading branch information
1 parent
4143bb9
commit 8ea7bd1
Showing
12 changed files
with
586 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
google-cloud-spanner/src/main/java/com/google/cloud/spanner/MutationGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.common.collect.ImmutableList; | ||
import com.google.spanner.v1.BatchWriteRequest; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** Represents a group of Cloud Spanner mutations to be committed together. */ | ||
public class MutationGroup { | ||
private final ImmutableList<Mutation> mutations; | ||
|
||
private MutationGroup(ImmutableList<Mutation> mutations) { | ||
this.mutations = mutations; | ||
} | ||
|
||
/** Creates a {@code MutationGroup} given a vararg of mutations. */ | ||
public static MutationGroup of(Mutation... mutations) { | ||
Preconditions.checkArgument(mutations.length > 0, "Should pass in at least one mutation."); | ||
return new MutationGroup(ImmutableList.copyOf(mutations)); | ||
} | ||
|
||
/** Creates a {@code MutationGroup} given an iterable of mutations. */ | ||
public static MutationGroup of(Iterable<Mutation> mutations) { | ||
return new MutationGroup(ImmutableList.copyOf(mutations)); | ||
} | ||
|
||
/** Returns corresponding mutations for this MutationGroup. */ | ||
public ImmutableList<Mutation> getMutations() { | ||
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(); | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.