Search in sources :

Example 6 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.

the class AsyncReadExample method asyncRead.

// Execute a query asynchronously and process the results in a callback.
static void asyncRead(DatabaseClient client) throws InterruptedException, ExecutionException, TimeoutException {
    ApiFuture<Void> finished;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try (AsyncResultSet resultSet = client.singleUse().readAsync("Albums", // Read all rows in a table.
    KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle"))) {
        // Setting a callback will automatically start the iteration over the results of the query
        // using the specified executor. The callback will be called at least once. The returned
        // ApiFuture is done when the callback has returned DONE and all resources used by the
        // AsyncResultSet have been released.
        finished = resultSet.setCallback(executor, new ReadyCallback() {

            @Override
            public CallbackResponse cursorReady(AsyncResultSet resultSet) {
                try {
                    while (true) {
                        switch(resultSet.tryNext()) {
                            // OK: There is a row ready.
                            case OK:
                                System.out.printf("%d %d %s%n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2));
                                break;
                            // DONE: There are no more rows in the result set.
                            case DONE:
                                return CallbackResponse.DONE;
                            // NOT_READY: There are currently no more rows in the buffer.
                            case NOT_READY:
                                return CallbackResponse.CONTINUE;
                            default:
                                throw new IllegalStateException();
                        }
                    }
                } catch (SpannerException e) {
                    System.out.printf("Error in callback: %s%n", e.getMessage());
                    return CallbackResponse.DONE;
                }
            }
        });
    }
    // This ApiFuture is done when the callback has returned DONE and all resources of the
    // asynchronous result set have been released.
    finished.get(30L, TimeUnit.SECONDS);
    executor.shutdown();
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ExecutorService(java.util.concurrent.ExecutorService) SpannerException(com.google.cloud.spanner.SpannerException) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback)

Example 7 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClient method singleReadAsync.

public ApiFuture<Void> singleReadAsync() {
    final AsyncResultSet asyncResultSet = dbClient.singleUse().readAsync(TABLE_NAME, READ_KEY_SET, ALL_COLUMNS);
    final ApiFuture<Void> readFuture = 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 readFuture;
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback)

Example 8 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback 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 9 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback 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 10 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback 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)

Aggregations

AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)12 ReadyCallback (com.google.cloud.spanner.AsyncResultSet.ReadyCallback)12 CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)8 ExecutorService (java.util.concurrent.ExecutorService)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Test (org.junit.Test)6 SpannerException (com.google.cloud.spanner.SpannerException)5 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 Executors (java.util.concurrent.Executors)3