use of com.google.cloud.spanner.AsyncResultSet.CallbackResponse in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClient method singleQueryAsync.
public ApiFuture<Void> singleQueryAsync() {
final AsyncResultSet asyncResultSet = dbClient.singleUse().executeQueryAsync(Statement.of("SELECT Key,StringValue FROM " + TABLE_NAME + " WHERE -123 != " + uniqueString()));
final ApiFuture<Void> queryFuture = asyncResultSet.setCallback(executor, new ReadyCallback() {
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
while (true) {
switch(resultSet.tryNext()) {
case OK:
// System.out.println(resultSet.getString("StringValue"));
break;
case NOT_READY:
return CallbackResponse.CONTINUE;
case DONE:
return CallbackResponse.DONE;
default:
throw new IllegalStateException();
}
}
}
});
return queryFuture;
}
use of com.google.cloud.spanner.AsyncResultSet.CallbackResponse in project cloud-spanner-r2dbc by GoogleCloudPlatform.
the class ReactiveResultSetCallbackTest method callbackEmitsOnOk.
@Test
void callbackEmitsOnOk() {
when(this.mockResultSet.tryNext()).thenReturn(CursorState.OK);
Struct struct = Struct.newBuilder().add(Value.string("some result")).build();
when(this.mockResultSet.getCurrentRowAsStruct()).thenReturn(struct);
StepVerifier.create(Flux.<SpannerClientLibraryRow>create(sink -> {
ReactiveResultSetCallback cb = new ReactiveResultSetCallback(sink, this.mockResultSet);
CallbackResponse response = cb.cursorReady(this.mockResultSet);
assertThat(response).isSameAs(CallbackResponse.CONTINUE);
})).assertNext(r -> assertThat(r.get(1)).isEqualTo("some result")).thenCancel().verify();
}
use of com.google.cloud.spanner.AsyncResultSet.CallbackResponse in project java-spanner by googleapis.
the class ITAsyncExamplesTest method readUsingIndexAsync.
@Test
public void readUsingIndexAsync() throws Exception {
final SettableApiFuture<List<String>> future = SettableApiFuture.create();
try (AsyncResultSet rs = client.singleUse().readUsingIndexAsync(TABLE_NAME, INDEX_NAME, KeySet.all(), ALL_COLUMNS)) {
rs.setCallback(executor, new ReadyCallback() {
final List<String> values = new LinkedList<>();
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
try {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
future.set(values);
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
values.add(resultSet.getString("StringValue"));
break;
}
}
} catch (Throwable t) {
future.setException(t);
return CallbackResponse.DONE;
}
}
});
}
assertThat(future.get()).containsExactlyElementsIn(ALL_VALUES_IN_PK_ORDER);
}
use of com.google.cloud.spanner.AsyncResultSet.CallbackResponse in project java-spanner by googleapis.
the class ITAsyncExamplesTest method executeQueryAsync.
@Test
public void executeQueryAsync() throws Exception {
final ImmutableList<String> keys = ImmutableList.of("k3", "k4");
final SettableApiFuture<List<String>> future = SettableApiFuture.create();
try (AsyncResultSet rs = client.singleUse().executeQueryAsync(Statement.newBuilder("SELECT StringValue FROM TestTable WHERE Key IN UNNEST(@keys)").bind("keys").toStringArray(keys).build())) {
rs.setCallback(executor, new ReadyCallback() {
final List<String> values = new LinkedList<>();
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
try {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
future.set(values);
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
values.add(resultSet.getString("StringValue"));
break;
}
}
} catch (Throwable t) {
future.setException(t);
return CallbackResponse.DONE;
}
}
});
}
assertThat(future.get()).containsExactly("v3", "v4");
}
use of com.google.cloud.spanner.AsyncResultSet.CallbackResponse in project cloud-spanner-r2dbc by GoogleCloudPlatform.
the class ReactiveResultSetCallbackTest method callbackStopsSinkOnCompletion.
@Test
void callbackStopsSinkOnCompletion() {
when(this.mockResultSet.tryNext()).thenReturn(CursorState.DONE);
StepVerifier.create(Flux.<SpannerClientLibraryRow>create(sink -> {
CallbackResponse response = new ReactiveResultSetCallback(sink, this.mockResultSet).cursorReady(this.mockResultSet);
assertThat(response).isSameAs(CallbackResponse.DONE);
})).verifyComplete();
}
Aggregations