Search in sources :

Example 6 with CallbackResponse

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;
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback)

Example 7 with CallbackResponse

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();
}
Also used : CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) Struct(com.google.cloud.spanner.Struct) Test(org.junit.jupiter.api.Test)

Example 8 with CallbackResponse

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);
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback) LinkedList(java.util.LinkedList) Test(org.junit.Test) SerialIntegrationTest(com.google.cloud.spanner.SerialIntegrationTest)

Example 9 with CallbackResponse

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");
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback) LinkedList(java.util.LinkedList) Test(org.junit.Test) SerialIntegrationTest(com.google.cloud.spanner.SerialIntegrationTest)

Example 10 with CallbackResponse

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();
}
Also used : CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) Test(org.junit.jupiter.api.Test)

Aggregations

CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)10 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)8 ReadyCallback (com.google.cloud.spanner.AsyncResultSet.ReadyCallback)8 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 ApiFuture (com.google.api.core.ApiFuture)3 SettableApiFuture (com.google.api.core.SettableApiFuture)3 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)3 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)3 Statement (com.google.cloud.spanner.Statement)3 Collections2 (com.google.common.collect.Collections2)3 Lists (com.google.common.collect.Lists)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 TimeUnit (java.util.concurrent.TimeUnit)3