Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit eb7901b

Browse files
Fix file format
1 parent e785abf commit eb7901b

3 files changed

Lines changed: 162 additions & 125 deletions

File tree

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

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,10 @@ public enum Op {
9090
/** Deletes rows from a table. Succeeds whether or not the named rows were present. */
9191
DELETE,
9292

93-
/**
94-
* Send a message to a queue, optionally with specified delivery time.
95-
*/
93+
/** Send a message to a queue, optionally with specified delivery time. */
9694
SEND,
9795

98-
/**
99-
* Acknowledge a message in a queue. Ack only succeeds if the message still exists.
100-
*/
96+
/** Acknowledge a message in a queue. Ack only succeeds if the message still exists. */
10197
ACK,
10298
}
10399

@@ -191,16 +187,16 @@ public static Mutation delete(String table, KeySet keySet) {
191187
}
192188

193189
/**
194-
* Returns a builder that can be used to construct an {@link Op#SEND} mutation against {@code queue}; see the
195-
* {@code SEND} documentation for mutation semantics.
190+
* Returns a builder that can be used to construct an {@link Op#SEND} mutation against {@code
191+
* queue}; see the {@code SEND} documentation for mutation semantics.
196192
*/
197193
public static SendBuilder newSendBuilder(String queue) {
198194
return new SendBuilder(queue);
199195
}
200196

201197
/**
202-
* Returns a builder that can be used to construct an {@link Op#ACK} mutation against {@code queue}; see the
203-
* {@code ACK} documentation for mutation semantics.
198+
* Returns a builder that can be used to construct an {@link Op#ACK} mutation against {@code
199+
* queue}; see the {@code ACK} documentation for mutation semantics.
204200
*/
205201
public static AckBuilder newAckBuilder(String queue) {
206202
return new AckBuilder(queue);
@@ -280,9 +276,7 @@ private void checkDuplicateColumns(ImmutableList<String> columnNames) {
280276
}
281277
}
282278

283-
/**
284-
* Builder for {@link Op#SEND} mutation.
285-
*/
279+
/** Builder for {@link Op#SEND} mutation. */
286280
public static class SendBuilder {
287281
private final String queue;
288282
private Key key;
@@ -311,13 +305,12 @@ public SendBuilder setDeliveryTime(Instant deliveryTime) {
311305
public Mutation build() {
312306
checkState(key != null, "Key must be set for Send mutation");
313307
checkState(payload != null, "Payload must be set for Send mutation");
314-
return new Mutation(null, Op.SEND, null, null, null, queue, key, payload, deliveryTime, false);
308+
return new Mutation(
309+
null, Op.SEND, null, null, null, queue, key, payload, deliveryTime, false);
315310
}
316311
}
317312

318-
/**
319-
* Builder for {@link Op#ACK} mutation.
320-
*/
313+
/** Builder for {@link Op#ACK} mutation. */
321314
public static class AckBuilder {
322315
private final String queue;
323316
private Key key;
@@ -364,30 +357,34 @@ public Iterable<String> getColumns() {
364357
}
365358

366359
/**
367-
* For all types except {@link Op#DELETE}, {@link Op#SEND}, and {@link Op#ACK}, returns the values that this mutation
368-
* will write. The number of elements returned is always the same as the number returned by {@link #getColumns()},
369-
* and the {@code i}th value corresponds to the {@code i}th column.
360+
* For all types except {@link Op#DELETE}, {@link Op#SEND}, and {@link Op#ACK}, returns the values
361+
* that this mutation will write. The number of elements returned is always the same as the number
362+
* returned by {@link #getColumns()}, and the {@code i}th value corresponds to the {@code i}th
363+
* column.
370364
*
371-
* @throws IllegalStateException
372-
* if {@code operation() == Op.DELETE or operation() == Op.SEND or operation() == Op.ACK}
365+
* @throws IllegalStateException if {@code operation() == Op.DELETE or operation() == Op.SEND or
366+
* operation() == Op.ACK}
373367
*/
374368
public Iterable<Value> getValues() {
375-
checkState(operation != Op.DELETE && operation != Op.SEND && operation != Op.ACK,
369+
checkState(
370+
operation != Op.DELETE && operation != Op.SEND && operation != Op.ACK,
376371
"values() cannot be called for a DELETE/SEND/ACK mutation");
377372
return values;
378373
}
379374

380375
/** Returns the name of the queue that this mutation will affect. */
381376
public String getQueue() {
382-
checkState(operation == Op.SEND || operation == Op.ACK, "getQueue() can only be called " +
383-
"for SEND or ACK mutations");
377+
checkState(
378+
operation == Op.SEND || operation == Op.ACK,
379+
"getQueue() can only be called " + "for SEND or ACK mutations");
384380
return queue;
385381
}
386382

387383
/** Returns the key of the message to the queue that this mutation will affect. */
388384
public Key getKey() {
389-
checkState(operation == Op.SEND || operation == Op.ACK, "getKey() can only be called for " +
390-
"SEND or ACK mutations");
385+
checkState(
386+
operation == Op.SEND || operation == Op.ACK,
387+
"getKey() can only be called for " + "SEND or ACK mutations");
391388
return key;
392389
}
393390

@@ -404,22 +401,27 @@ public Instant getDeliveryTime() {
404401
return deliveryTime;
405402
}
406403

407-
/** Returns whether an error will be ignored for an ACK mutation that affects a message that does not exist */
404+
/**
405+
* Returns whether an error will be ignored for an ACK mutation that affects a message that does
406+
* not exist
407+
*/
408408
public boolean getIgnoreNotFound() {
409409
checkState(operation == Op.ACK, "getIgnoreNotFound() can only be called for an ACK mutation");
410410
return ignoreNotFound;
411411
}
412412

413413
/**
414-
* For all types except {@link Op#DELETE}, {@link Op#SEND}, and {@link Op#ACK}, constructs a map from column name to
415-
* value. This is mainly intended as a convenience for testing; direct access via {@link #getColumns()} and
416-
* {@link #getValues()} is more efficient.
414+
* For all types except {@link Op#DELETE}, {@link Op#SEND}, and {@link Op#ACK}, constructs a map
415+
* from column name to value. This is mainly intended as a convenience for testing; direct access
416+
* via {@link #getColumns()} and {@link #getValues()} is more efficient.
417417
*
418-
* @throws IllegalStateException if {@code operation() == Op.DELETE or operation() == Op.SEND or operation() ==
419-
* Op.ACK}, or if any duplicate columns are present. Detection of duplicates does not consider case.
418+
* @throws IllegalStateException if {@code operation() == Op.DELETE or operation() == Op.SEND or
419+
* operation() == Op.ACK}, or if any duplicate columns are present. Detection of duplicates
420+
* does not consider case.
420421
*/
421422
public Map<String, Value> asMap() {
422-
checkState(operation != Op.DELETE && operation != Op.SEND && operation != Op.ACK,
423+
checkState(
424+
operation != Op.DELETE && operation != Op.SEND && operation != Op.ACK,
423425
"asMap() cannot be called for a DELETE/SEND/ACK mutation");
424426
LinkedHashMap<String, Value> map = new LinkedHashMap<>();
425427
for (int i = 0; i < columns.size(); ++i) {
@@ -544,7 +546,8 @@ && areValuesEqual(values, that.values)
544546

545547
@Override
546548
public int hashCode() {
547-
return Objects.hash(operation, table, columns, values, keySet, key, payload, deliveryTime, ignoreNotFound);
549+
return Objects.hash(
550+
operation, table, columns, values, keySet, key, payload, deliveryTime, ignoreNotFound);
548551
}
549552

550553
/**
@@ -622,7 +625,8 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
622625
if (last != null && last.operation == Op.DELETE && mutation.table.equals(last.table)) {
623626
mutation.keySet.appendToProto(keySet);
624627
} else {
625-
largestInsertMutation = flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
628+
largestInsertMutation =
629+
flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
626630
proto = com.google.spanner.v1.Mutation.newBuilder();
627631
com.google.spanner.v1.Mutation.Delete.Builder delete =
628632
proto.getDeleteBuilder().setTable(mutation.table);
@@ -631,23 +635,29 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
631635
}
632636
write = null;
633637
} else if (mutation.operation == Op.SEND) {
634-
largestInsertMutation = flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
638+
largestInsertMutation =
639+
flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
635640
proto = com.google.spanner.v1.Mutation.newBuilder();
636-
com.google.spanner.v1.Mutation.Send.Builder send = proto.getSendBuilder()
637-
.setQueue(mutation.queue)
638-
.setKey(mutation.key.toProto())
639-
.setPayload(mutation.payload.toProto());
641+
com.google.spanner.v1.Mutation.Send.Builder send =
642+
proto
643+
.getSendBuilder()
644+
.setQueue(mutation.queue)
645+
.setKey(mutation.key.toProto())
646+
.setPayload(mutation.payload.toProto());
640647
if (mutation.getDeliveryTime() != null) {
641648
Instant deliveryTime = mutation.getDeliveryTime();
642-
Timestamp.Builder timeBuilder = send.getDeliverTimeBuilder()
643-
.setSeconds(deliveryTime.getEpochSecond())
644-
.setNanos(deliveryTime.getNano());
649+
Timestamp.Builder timeBuilder =
650+
send.getDeliverTimeBuilder()
651+
.setSeconds(deliveryTime.getEpochSecond())
652+
.setNanos(deliveryTime.getNano());
645653
send.setDeliverTime(timeBuilder);
646654
}
647655
} else if (mutation.operation == Op.ACK) {
648-
largestInsertMutation = flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
656+
largestInsertMutation =
657+
flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
649658
proto = com.google.spanner.v1.Mutation.newBuilder();
650-
proto.getAckBuilder()
659+
proto
660+
.getAckBuilder()
651661
.setQueue(mutation.queue)
652662
.setKey(mutation.getKey().toProto())
653663
.setIgnoreNotFound(mutation.ignoreNotFound);
@@ -663,7 +673,8 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
663673
// Same as previous mutation: coalesce values to reduce request size.
664674
write.addValues(values);
665675
} else {
666-
largestInsertMutation = flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
676+
largestInsertMutation =
677+
flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
667678
proto = com.google.spanner.v1.Mutation.newBuilder();
668679
switch (mutation.operation) {
669680
case INSERT:
@@ -688,7 +699,8 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
688699
last = mutation;
689700
}
690701
// Flush last item.
691-
largestInsertMutation = flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
702+
largestInsertMutation =
703+
flushMutation(out, proto, allMutationsExcludingInsert, largestInsertMutation);
692704

693705
// Select a random mutation based on the heuristic.
694706
if (!allMutationsExcludingInsert.isEmpty()) {
@@ -699,10 +711,11 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
699711
}
700712
}
701713

702-
private static com.google.spanner.v1.Mutation flushMutation(List<com.google.spanner.v1.Mutation> out,
703-
com.google.spanner.v1.Mutation.Builder proto,
704-
List<com.google.spanner.v1.Mutation> allMutationsExcludingInsert,
705-
com.google.spanner.v1.Mutation largestInsertMutation) {
714+
private static com.google.spanner.v1.Mutation flushMutation(
715+
List<com.google.spanner.v1.Mutation> out,
716+
com.google.spanner.v1.Mutation.Builder proto,
717+
List<com.google.spanner.v1.Mutation> allMutationsExcludingInsert,
718+
com.google.spanner.v1.Mutation largestInsertMutation) {
706719
if (proto != null) {
707720
com.google.spanner.v1.Mutation builtMutation = proto.build();
708721
out.add(builtMutation);

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public void send() {
191191
Key key = Key.of(123);
192192
Value payload = Value.bytes(ByteArray.copyFrom("payload"));
193193
Instant deliverAt = Instant.now().plusSeconds(3600);
194-
Mutation m = Mutation.newSendBuilder("TestQueue")
194+
Mutation m =
195+
Mutation.newSendBuilder("TestQueue")
195196
.setKey(key)
196197
.setPayload(payload)
197198
.setDeliveryTime(deliverAt)
@@ -202,21 +203,24 @@ public void send() {
202203
assertThat(m.getPayload()).isEqualTo(payload);
203204
assertThat(m.getDeliveryTime()).isEqualTo(deliverAt);
204205
assertThat(m.toString())
205-
.isEqualTo("send(TestQueue{key=[123], payload=" + payload + ", deliveryTime=" + deliverAt + "})");
206+
.isEqualTo(
207+
"send(TestQueue{key=[123], payload=" + payload + ", deliveryTime=" + deliverAt + "})");
206208
}
207209

208210
@Test
209211
public void sendMissingKey() {
210212
IllegalStateException e =
211-
assertThrows(IllegalStateException.class,
213+
assertThrows(
214+
IllegalStateException.class,
212215
() -> Mutation.newSendBuilder("TestQueue").setPayload(Value.string("payload")).build());
213216
assertThat(e.getMessage()).contains("Key must be set");
214217
}
215218

216219
@Test
217220
public void sendMissingPayload() {
218221
IllegalStateException e =
219-
assertThrows(IllegalStateException.class,
222+
assertThrows(
223+
IllegalStateException.class,
220224
() -> Mutation.newSendBuilder("TestQueue").setKey(Key.of("k1")).build());
221225
assertThat(e.getMessage()).contains("Payload must be set");
222226
}
@@ -235,7 +239,8 @@ public void ackIgnoreNotFound() {
235239
@Test
236240
public void ackMissingKey() {
237241
IllegalStateException e =
238-
assertThrows(IllegalStateException.class, () -> Mutation.newAckBuilder("TestQueue").build());
242+
assertThrows(
243+
IllegalStateException.class, () -> Mutation.newAckBuilder("TestQueue").build());
239244
assertThat(e.getMessage()).contains("Key must be set");
240245
}
241246

@@ -375,17 +380,32 @@ public void equalsAndHashCode_sendAndAck() {
375380
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload1).build(),
376381
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload1).build());
377382
// Different key
378-
tester.addEqualityGroup(Mutation.newSendBuilder("TestQueue").setKey(key2).setPayload(payload1).build());
383+
tester.addEqualityGroup(
384+
Mutation.newSendBuilder("TestQueue").setKey(key2).setPayload(payload1).build());
379385
// Different payload
380-
tester.addEqualityGroup(Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload2).build());
386+
tester.addEqualityGroup(
387+
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload2).build());
381388
// Different queue
382-
tester.addEqualityGroup(Mutation.newSendBuilder("TestQueue2").setKey(key1).setPayload(payload1).build());
389+
tester.addEqualityGroup(
390+
Mutation.newSendBuilder("TestQueue2").setKey(key1).setPayload(payload1).build());
383391
// Different time
384392
tester.addEqualityGroup(
385-
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload1).setDeliveryTime(time1).build(),
386-
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload1).setDeliveryTime(time1).build());
393+
Mutation.newSendBuilder("TestQueue")
394+
.setKey(key1)
395+
.setPayload(payload1)
396+
.setDeliveryTime(time1)
397+
.build(),
398+
Mutation.newSendBuilder("TestQueue")
399+
.setKey(key1)
400+
.setPayload(payload1)
401+
.setDeliveryTime(time1)
402+
.build());
387403
tester.addEqualityGroup(
388-
Mutation.newSendBuilder("TestQueue").setKey(key1).setPayload(payload1).setDeliveryTime(time2).build());
404+
Mutation.newSendBuilder("TestQueue")
405+
.setKey(key1)
406+
.setPayload(payload1)
407+
.setDeliveryTime(time2)
408+
.build());
389409

390410
// ACK
391411
tester.addEqualityGroup(
@@ -408,16 +428,19 @@ public void equalsAndHashCode_sendAndAck() {
408428

409429
@Test
410430
public void serializationBasic() {
411-
Instant time = Instant.now();
431+
Instant time = Instant.now();
412432
List<Mutation> mutations =
413433
Arrays.asList(
414434
Mutation.newInsertBuilder("T").set("C").to("V").build(),
415435
Mutation.newUpdateBuilder("T").set("C").to("V").build(),
416436
Mutation.newInsertOrUpdateBuilder("T").set("C").to("V").build(),
417437
Mutation.newReplaceBuilder("T").set("C").to("V").build(),
418438
Mutation.delete("T", KeySet.singleKey(Key.of("k"))),
419-
Mutation.newSendBuilder("Q").setKey(Key.of("k")).setPayload(Value.string("p"))
420-
.setDeliveryTime(time).build(),
439+
Mutation.newSendBuilder("Q")
440+
.setKey(Key.of("k"))
441+
.setPayload(Value.string("p"))
442+
.setDeliveryTime(time)
443+
.build(),
421444
Mutation.newAckBuilder("Q").setKey(Key.of("k")).setIgnoreNotFound(true).build());
422445

423446
List<com.google.spanner.v1.Mutation> proto = new ArrayList<>();
@@ -454,11 +477,16 @@ public void serializationBasic() {
454477
matchesProto("delete { table: 'T' key_set { keys { values { string_value: 'k' } } } }"));
455478
MatcherAssert.assertThat(
456479
proto.get(5),
457-
matchesProto("send { queue: 'Q' key { values { string_value: 'k' } } deliver_time { seconds: " +
458-
time.getEpochSecond() + " nanos: " + time.getNano() + " } payload { string_value: 'p' } }"));
480+
matchesProto(
481+
"send { queue: 'Q' key { values { string_value: 'k' } } deliver_time { seconds: "
482+
+ time.getEpochSecond()
483+
+ " nanos: "
484+
+ time.getNano()
485+
+ " } payload { string_value: 'p' } }"));
459486
MatcherAssert.assertThat(
460487
proto.get(6),
461-
matchesProto("ack { queue: 'Q' key { values { string_value: 'k' } } ignore_not_found: true }"));
488+
matchesProto(
489+
"ack { queue: 'Q' key { values { string_value: 'k' } } ignore_not_found: true }"));
462490
}
463491

464492
@Test

0 commit comments

Comments
 (0)