Skip to content

Commit 01c0314

Browse files
committed
chore(spanner): review comments
1 parent f12729e commit 01c0314

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Mutation.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,12 @@ private boolean isFloat32NaN(Value value) {
408408
* Converts the list of mutations to the corresponding protobuf mutations and returns a random
409409
* mutation from the available list based on the following heuristics:
410410
*
411-
* <p>1. Prefer mutations other than INSERT, as INSERT mutations may contain autogenerated columns
412-
* whose information is unavailable on the client.
413-
*
414-
* <p>2. If the list only contains INSERT mutations, select the one with the highest number of
415-
* values.
411+
* <ol>
412+
* <li>1. Prefer mutations other than INSERT, as INSERT mutations may contain autogenerated
413+
* columns whose information is unavailable on the client.
414+
* <li>If the list only contains INSERT mutations, select the one with the highest number of
415+
* values.
416+
* </ol>
416417
*/
417418
static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
418419
Iterable<Mutation> mutations, List<com.google.spanner.v1.Mutation> out) {
@@ -437,10 +438,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
437438
if (proto != null) {
438439
com.google.spanner.v1.Mutation builtMutation = proto.build();
439440
out.add(builtMutation);
440-
if (checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
441+
// Skip tracking the largest insert mutation if there are mutations other than INSERT.
442+
if (allMutationsExcludingInsert.isEmpty()
443+
&& checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
441444
largestInsertMutation = builtMutation;
442445
}
443-
addMutationsExcludingInsert(builtMutation, allMutationsExcludingInsert);
446+
maybeAddMutationToListExcludingInserts(builtMutation, allMutationsExcludingInsert);
444447
}
445448
proto = com.google.spanner.v1.Mutation.newBuilder();
446449
com.google.spanner.v1.Mutation.Delete.Builder delete =
@@ -464,10 +467,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
464467
if (proto != null) {
465468
com.google.spanner.v1.Mutation builtMutation = proto.build();
466469
out.add(builtMutation);
467-
if (checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
470+
// Skip tracking the largest insert mutation if there are mutations other than INSERT.
471+
if (allMutationsExcludingInsert.isEmpty()
472+
&& checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
468473
largestInsertMutation = builtMutation;
469474
}
470-
addMutationsExcludingInsert(builtMutation, allMutationsExcludingInsert);
475+
maybeAddMutationToListExcludingInserts(builtMutation, allMutationsExcludingInsert);
471476
}
472477
proto = com.google.spanner.v1.Mutation.newBuilder();
473478
switch (mutation.operation) {
@@ -496,10 +501,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
496501
if (proto != null) {
497502
com.google.spanner.v1.Mutation builtMutation = proto.build();
498503
out.add(proto.build());
499-
if (checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
504+
// Skip tracking the largest insert mutation if there are mutations other than INSERT.
505+
if (allMutationsExcludingInsert.isEmpty()
506+
&& checkIfInsertMutationWithLargeValue(builtMutation, largestInsertMutation)) {
500507
largestInsertMutation = builtMutation;
501508
}
502-
addMutationsExcludingInsert(builtMutation, allMutationsExcludingInsert);
509+
maybeAddMutationToListExcludingInserts(builtMutation, allMutationsExcludingInsert);
503510
}
504511

505512
// Select a random mutation based on the heuristic.
@@ -518,7 +525,7 @@ private static boolean checkIfInsertMutationWithLargeValue(
518525
com.google.spanner.v1.Mutation largestInsertMutation) {
519526
// If largestInsertMutation is a default instance of Mutation, replace it with the current
520527
// INSERT mutation, even if it contains zero values.
521-
if (!largestInsertMutation.hasInsert()) {
528+
if (mutation.hasInsert() && !largestInsertMutation.hasInsert()) {
522529
return true;
523530
}
524531
return mutation.hasInsert()
@@ -527,7 +534,7 @@ private static boolean checkIfInsertMutationWithLargeValue(
527534
}
528535

529536
// Stores all mutations that are not of type INSERT.
530-
private static void addMutationsExcludingInsert(
537+
private static void maybeAddMutationToListExcludingInserts(
531538
com.google.spanner.v1.Mutation mutation,
532539
List<com.google.spanner.v1.Mutation> allMutationsExcludingInsert) {
533540
if (!mutation.hasInsert()) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/MutationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ public void toProtoWithEmptyInsertMutations() {
530530
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
531531
com.google.spanner.v1.Mutation mutation =
532532
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
533-
System.out.println(mutation);
534533

535534
// Random mutation returned should be of INSERT operation with empty values
536535
MatcherAssert.assertThat(mutation, matchesProto("insert { table: 'T' values { } }"));

0 commit comments

Comments
 (0)