Skip to content

Commit 734fb60

Browse files
authored
feat: async work as functional interface (#1068)
Marks the async work interface as a functional interface.
1 parent b036a77 commit 734fb60

File tree

11 files changed

+376
-601
lines changed

11 files changed

+376
-601
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface AsyncRunner {
2727
* Functional interface for executing a read/write transaction asynchronously that returns a
2828
* result of type R.
2929
*/
30+
@FunctionalInterface
3031
interface AsyncWork<R> {
3132
/**
3233
* Performs a single transaction attempt. All reads/writes should be performed using {@code

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -398,21 +398,18 @@ CommitResponse writeAtLeastOnceWithOptions(
398398
* AsyncRunner runner = client.runAsync();
399399
* ApiFuture<Long> rowCount =
400400
* runner.runAsync(
401-
* new AsyncWork<Long>() {
402-
* @Override
403-
* public ApiFuture<Long> doWorkAsync(TransactionContext txn) {
404-
* String column = "FirstName";
405-
* Struct row =
406-
* txn.readRow("Singers", Key.of(singerId), Collections.singleton("Name"));
407-
* String name = row.getString("Name");
408-
* return txn.executeUpdateAsync(
409-
* Statement.newBuilder("UPDATE Singers SET Name=@name WHERE SingerId=@id")
410-
* .bind("id")
411-
* .to(singerId)
412-
* .bind("name")
413-
* .to(name.toUpperCase())
414-
* .build());
415-
* }
401+
* () -> {
402+
* String column = "FirstName";
403+
* Struct row =
404+
* txn.readRow("Singers", Key.of(singerId), Collections.singleton("Name"));
405+
* String name = row.getString("Name");
406+
* return txn.executeUpdateAsync(
407+
* Statement.newBuilder("UPDATE Singers SET Name=@name WHERE SingerId=@id")
408+
* .bind("id")
409+
* .to(singerId)
410+
* .bind("name")
411+
* .to(name.toUpperCase())
412+
* .build());
416413
* },
417414
* executor);
418415
* </code></pre>

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import com.google.api.core.ApiFuture;
2929
import com.google.api.core.ApiFutures;
30-
import com.google.cloud.spanner.AsyncRunner.AsyncWork;
3130
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
3231
import java.util.concurrent.ExecutorService;
3332
import java.util.concurrent.Executors;
@@ -57,14 +56,7 @@ public void testAsyncRunReturnsResultAndCommitResponse() {
5756

5857
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
5958
ApiFuture<Object> result =
60-
runner.runAsync(
61-
new AsyncWork<Object>() {
62-
@Override
63-
public ApiFuture<Object> doWorkAsync(TransactionContext txn) {
64-
return ApiFutures.immediateFuture(expectedResult);
65-
}
66-
},
67-
executor);
59+
runner.runAsync(txn -> ApiFutures.immediateFuture(expectedResult), executor);
6860

6961
assertSame(expectedResult, get(result));
7062
assertSame(expectedCommitResponse, get(runner.getCommitResponse()));
@@ -105,14 +97,7 @@ public void testGetCommitResponseReturnsErrorIfRunFails() {
10597
when(delegate.getCommitResponse()).thenThrow(expectedException);
10698

10799
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
108-
runner.runAsync(
109-
new AsyncWork<Void>() {
110-
@Override
111-
public ApiFuture<Void> doWorkAsync(TransactionContext txn) {
112-
return ApiFutures.immediateFailedFuture(expectedException);
113-
}
114-
},
115-
executor);
100+
runner.runAsync(txn -> ApiFutures.immediateFailedFuture(expectedException), executor);
116101

117102
try {
118103
get(runner.getCommitResponse());
@@ -130,24 +115,10 @@ public void testRunAyncFailsIfCalledMultipleTimes() {
130115
when(delegate.run(any(TransactionCallable.class))).thenReturn(result);
131116

132117
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
133-
runner.runAsync(
134-
new AsyncWork<Object>() {
135-
@Override
136-
public ApiFuture<Object> doWorkAsync(TransactionContext txn) {
137-
return ApiFutures.immediateFuture(result);
138-
}
139-
},
140-
executor);
118+
runner.runAsync(txn -> ApiFutures.immediateFuture(result), executor);
141119

142120
try {
143-
runner.runAsync(
144-
new AsyncWork<Object>() {
145-
@Override
146-
public ApiFuture<Object> doWorkAsync(TransactionContext txn) {
147-
return ApiFutures.immediateFuture(null);
148-
}
149-
},
150-
executor);
121+
runner.runAsync(txn -> ApiFutures.immediateFuture(null), executor);
151122
fail("missing expected exception");
152123
} catch (IllegalStateException e) {
153124
assertTrue(e.getMessage().contains("runAsync() can only be called once"));

0 commit comments

Comments
 (0)