Skip to content

Commit dfc2a39

Browse files
committed
chore(spanner): review comments
1 parent 042ef7e commit dfc2a39

File tree

7 files changed

+32
-23
lines changed

7 files changed

+32
-23
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,17 @@ private boolean isFloat32NaN(Value value) {
404404
return value.getType().equals(Type.float32()) && Float.isNaN(value.getFloat32());
405405
}
406406

407-
// Converts Mutation and returns a random Mutation from the available mutation list based on the
408-
// following heuristics:
409-
// 1. Prefer mutations other than INSERT, since INSERT mutations may contain autogenerated columns
410-
// whose information is unavailable on the client.
411-
// 2. If the list only contains INSERT mutations, select the one with the highest number of
412-
// values.
413-
static com.google.spanner.v1.Mutation toProtoGetRandomMutation(
407+
/**
408+
* Converts the list of mutations to the corresponding protobuf mutations and returns a random
409+
* mutation from the available list based on the following heuristics:
410+
*
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.
416+
*/
417+
static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
414418
Iterable<Mutation> mutations, List<com.google.spanner.v1.Mutation> out) {
415419
Mutation last = null;
416420
// The mutation currently being built.
@@ -419,9 +423,9 @@ static com.google.spanner.v1.Mutation toProtoGetRandomMutation(
419423
com.google.spanner.v1.Mutation.Write.Builder write = null;
420424
com.google.spanner.v1.KeySet.Builder keySet = null;
421425

422-
// Store all the mutations excluding INSERT.
426+
// Stores all the mutations excluding INSERT mutations.
423427
List<com.google.spanner.v1.Mutation> allMutationsExcludingInsert = new ArrayList<>();
424-
// Stores INSERT mutation with large number of values.
428+
// Stores the INSERT mutation with largest number of values.
425429
com.google.spanner.v1.Mutation largeInsertMutation =
426430
com.google.spanner.v1.Mutation.getDefaultInstance();
427431

@@ -505,9 +509,9 @@ static com.google.spanner.v1.Mutation toProtoGetRandomMutation(
505509
}
506510

507511
// Select a random mutation based on the heuristic.
508-
if (allMutationsExcludingInsert.size() > 0) {
509-
int randomIndex = ThreadLocalRandom.current().nextInt(allMutationsExcludingInsert.size());
510-
return allMutationsExcludingInsert.get(randomIndex);
512+
if (!allMutationsExcludingInsert.isEmpty()) {
513+
return allMutationsExcludingInsert.get(
514+
ThreadLocalRandom.current().nextInt(allMutationsExcludingInsert.size()));
511515
} else {
512516
return largeInsertMutation;
513517
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ImmutableList<Mutation> getMutations() {
4848

4949
static BatchWriteRequest.MutationGroup toProto(final MutationGroup mutationGroup) {
5050
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
51-
Mutation.toProtoGetRandomMutation(mutationGroup.getMutations(), mutationsProto);
51+
Mutation.toProtoAndReturnRandomMutation(mutationGroup.getMutations(), mutationsProto);
5252
return BatchWriteRequest.MutationGroup.newBuilder().addAllMutations(mutationsProto).build();
5353
}
5454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public CommitResponse writeAtLeastOnceWithOptions(
238238
throws SpannerException {
239239
setActive(null);
240240
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
241-
Mutation.toProtoGetRandomMutation(mutations, mutationsProto);
241+
Mutation.toProtoAndReturnRandomMutation(mutations, mutationsProto);
242242
Options options = Options.fromTransactionOptions(transactionOptions);
243243
final CommitRequest.Builder requestBuilder =
244244
CommitRequest.newBuilder()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ ApiFuture<CommitResponse> commitAsync() {
373373
}
374374
committing = true;
375375
if (!mutations.isEmpty()) {
376-
randomMutation = Mutation.toProtoGetRandomMutation(mutations, mutationsProto);
376+
randomMutation = Mutation.toProtoAndReturnRandomMutation(mutations, mutationsProto);
377377
}
378378
}
379379
final SettableApiFuture<CommitResponse> res = SettableApiFuture.create();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private Mutation getRandomMutation() {
4444

4545
private BatchWriteRequest.MutationGroup getMutationGroupProto(ImmutableList<Mutation> mutations) {
4646
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
47-
Mutation.toProtoGetRandomMutation(mutations, mutationsProto);
47+
Mutation.toProtoAndReturnRandomMutation(mutations, mutationsProto);
4848
return BatchWriteRequest.MutationGroup.newBuilder().addAllMutations(mutationsProto).build();
4949
}
5050

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public void serializationBasic() {
322322
com.google.spanner.v1.Mutation.getDefaultInstance();
323323
proto.add(existingProto);
324324

325-
Mutation.toProtoGetRandomMutation(mutations, proto);
325+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
326326

327327
assertThat(proto.size()).isAtLeast(1);
328328
assertThat(proto.get(0)).isSameInstanceAs(existingProto);
@@ -360,7 +360,8 @@ public void toProtoCoalescingChangeOfTable() {
360360
Mutation.newInsertBuilder("T2").set("C").to("V5").build());
361361

362362
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
363-
com.google.spanner.v1.Mutation mutation = Mutation.toProtoGetRandomMutation(mutations, proto);
363+
com.google.spanner.v1.Mutation mutation =
364+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
364365
// Random mutation returned should be INSERT with large number of values
365366
MatcherAssert.assertThat(
366367
mutation,
@@ -394,7 +395,8 @@ public void toProtoCoalescingChangeOfOperation() {
394395
Mutation.newUpdateBuilder("T").set("C").to("V5").build());
395396

396397
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
397-
com.google.spanner.v1.Mutation mutation = Mutation.toProtoGetRandomMutation(mutations, proto);
398+
com.google.spanner.v1.Mutation mutation =
399+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
398400
// Random mutation returned should be of UPDATE operation
399401
MatcherAssert.assertThat(
400402
mutation,
@@ -427,7 +429,8 @@ public void toProtoCoalescingChangeOfColumn() {
427429
Mutation.newInsertBuilder("T").set("C2").to("V5").build());
428430

429431
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
430-
com.google.spanner.v1.Mutation mutation = Mutation.toProtoGetRandomMutation(mutations, proto);
432+
com.google.spanner.v1.Mutation mutation =
433+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
431434
MatcherAssert.assertThat(
432435
mutation,
433436
matchesProto(
@@ -459,7 +462,8 @@ public void toProtoCoalescingDelete() {
459462
Mutation.delete("T", KeySet.range(KeyRange.closedClosed(Key.of("kc"), Key.of("kd")))));
460463

461464
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
462-
com.google.spanner.v1.Mutation mutation = Mutation.toProtoGetRandomMutation(mutations, proto);
465+
com.google.spanner.v1.Mutation mutation =
466+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
463467
// Random mutation returned should be of DELETE operation
464468
assertTrue(mutation.hasDelete());
465469

@@ -492,7 +496,8 @@ public void toProtoCoalescingDeleteChanges() {
492496
Mutation.newInsertBuilder("T2").set("C").to("V1").build());
493497

494498
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
495-
com.google.spanner.v1.Mutation mutation = Mutation.toProtoGetRandomMutation(mutations, proto);
499+
com.google.spanner.v1.Mutation mutation =
500+
Mutation.toProtoAndReturnRandomMutation(mutations, proto);
496501
assertTrue(mutation.hasDelete());
497502

498503
assertThat(proto.size()).isEqualTo(4);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void testMutation() {
319319
.toStringArray(null)
320320
.build());
321321
final List<com.google.spanner.v1.Mutation> expectedMutations = new ArrayList<>();
322-
Mutation.toProtoGetRandomMutation(mutations, expectedMutations);
322+
Mutation.toProtoAndReturnRandomMutation(mutations, expectedMutations);
323323

324324
databaseClient
325325
.readWriteTransaction()

0 commit comments

Comments
 (0)