Search in sources :

Example 1 with KeySet

use of com.google.spanner.v1.KeySet in project java-spanner by googleapis.

the class AbstractReadContext method readInternalWithOptions.

ResultSet readInternalWithOptions(String table, @Nullable String index, KeySet keys, Iterable<String> columns, final Options readOptions, ByteString partitionToken) {
    beforeReadOrQuery();
    final ReadRequest.Builder builder = ReadRequest.newBuilder().setSession(session.getName()).setTable(checkNotNull(table)).addAllColumns(columns);
    if (readOptions.hasLimit()) {
        builder.setLimit(readOptions.limit());
    }
    keys.appendToProto(builder.getKeySetBuilder());
    if (index != null) {
        builder.setIndex(index);
    }
    if (partitionToken != null) {
        builder.setPartitionToken(partitionToken);
    }
    final int prefetchChunks = readOptions.hasPrefetchChunks() ? readOptions.prefetchChunks() : defaultPrefetchChunks;
    ResumableStreamIterator stream = new ResumableStreamIterator(MAX_BUFFERED_CHUNKS, SpannerImpl.READ, span) {

        @Override
        CloseableIterator<PartialResultSet> startStream(@Nullable ByteString resumeToken) {
            GrpcStreamIterator stream = new GrpcStreamIterator(prefetchChunks);
            TransactionSelector selector = null;
            if (resumeToken != null) {
                builder.setResumeToken(resumeToken);
                selector = getTransactionSelector();
            } else if (!builder.hasTransaction()) {
                selector = getTransactionSelector();
            }
            if (selector != null) {
                builder.setTransaction(selector);
            }
            builder.setRequestOptions(buildRequestOptions(readOptions));
            SpannerRpc.StreamingCall call = rpc.read(builder.build(), stream.consumer(), session.getOptions());
            call.request(prefetchChunks);
            stream.setCall(call, /* withBeginTransaction = */
            builder.getTransaction().hasBegin());
            return stream;
        }
    };
    return new GrpcResultSet(stream, this);
}
Also used : ResumableStreamIterator(com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator) GrpcResultSet(com.google.cloud.spanner.AbstractResultSet.GrpcResultSet) ByteString(com.google.protobuf.ByteString) TransactionSelector(com.google.spanner.v1.TransactionSelector) GrpcStreamIterator(com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator) PartialResultSet(com.google.spanner.v1.PartialResultSet) SpannerRpc(com.google.cloud.spanner.spi.v1.SpannerRpc) Nullable(javax.annotation.Nullable) ReadRequest(com.google.spanner.v1.ReadRequest)

Example 2 with KeySet

use of com.google.spanner.v1.KeySet in project java-spanner by googleapis.

the class AbstractReadContext method readAsync.

@Override
public ListenableAsyncResultSet readAsync(String table, KeySet keys, Iterable<String> columns, ReadOption... options) {
    Options readOptions = Options.fromReadOptions(options);
    final int bufferRows = readOptions.hasBufferRows() ? readOptions.bufferRows() : AsyncResultSetImpl.DEFAULT_BUFFER_SIZE;
    return new AsyncResultSetImpl(executorProvider, readInternal(table, null, keys, columns, options), bufferRows);
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) RequestOptions(com.google.spanner.v1.RequestOptions) QueryOptions(com.google.spanner.v1.ExecuteSqlRequest.QueryOptions)

Example 3 with KeySet

use of com.google.spanner.v1.KeySet in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerProbes method readProber.

/**
 * Probe to test Read and StreamingRead grpc call from Spanner stub.
 */
public static void readProber(SpannerGrpc.SpannerBlockingStub stub) throws ProberException {
    Session session = null;
    try {
        session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
        KeySet keySet = KeySet.newBuilder().setAll(true).build();
        // Probing read call.
        ResultSet response = stub.read(ReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setKeySet(keySet).addColumns("username").addColumns("firstname").addColumns("lastname").build());
        if (response == null) {
            throw new ProberException("Response is null when executing SQL. ");
        } else if (response.getRowsCount() != 1) {
            throw new ProberException(String.format("The number of Responses '%d' is not correct.", response.getRowsCount()));
        } else if (!response.getRows(0).getValuesList().get(0).getStringValue().equals(TEST_USERNAME)) {
            throw new ProberException("Response value is not correct when executing Reader.");
        }
        // Probing streamingRead call.
        Iterator<PartialResultSet> responsePartial = stub.streamingRead(ReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setKeySet(keySet).addColumns("username").addColumns("firstname").addColumns("lastname").build());
        if (responsePartial == null) {
            throw new ProberException("Response is null when executing streaming SQL. ");
        } else if (!responsePartial.next().getValues(0).getStringValue().equals(TEST_USERNAME)) {
            throw new ProberException("Response value is not correct when executing streaming Reader. ");
        }
    } finally {
        deleteSession(stub, session);
    }
}
Also used : KeySet(com.google.spanner.v1.KeySet) ResultSet(com.google.spanner.v1.ResultSet) PartialResultSet(com.google.spanner.v1.PartialResultSet) PartialResultSet(com.google.spanner.v1.PartialResultSet) Session(com.google.spanner.v1.Session)

Example 4 with KeySet

use of com.google.spanner.v1.KeySet in project java-spanner by googleapis.

the class AbstractReadContext method readUsingIndexAsync.

@Override
public ListenableAsyncResultSet readUsingIndexAsync(String table, String index, KeySet keys, Iterable<String> columns, ReadOption... options) {
    Options readOptions = Options.fromReadOptions(options);
    final int bufferRows = readOptions.hasBufferRows() ? readOptions.bufferRows() : AsyncResultSetImpl.DEFAULT_BUFFER_SIZE;
    return new AsyncResultSetImpl(executorProvider, readInternal(table, checkNotNull(index), keys, columns, options), bufferRows);
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) RequestOptions(com.google.spanner.v1.RequestOptions) QueryOptions(com.google.spanner.v1.ExecuteSqlRequest.QueryOptions)

Aggregations

QueryOptions (com.google.spanner.v1.ExecuteSqlRequest.QueryOptions)2 PartialResultSet (com.google.spanner.v1.PartialResultSet)2 RequestOptions (com.google.spanner.v1.RequestOptions)2 TransactionOptions (com.google.spanner.v1.TransactionOptions)2 GrpcResultSet (com.google.cloud.spanner.AbstractResultSet.GrpcResultSet)1 GrpcStreamIterator (com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator)1 ResumableStreamIterator (com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator)1 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)1 ByteString (com.google.protobuf.ByteString)1 KeySet (com.google.spanner.v1.KeySet)1 ReadRequest (com.google.spanner.v1.ReadRequest)1 ResultSet (com.google.spanner.v1.ResultSet)1 Session (com.google.spanner.v1.Session)1 TransactionSelector (com.google.spanner.v1.TransactionSelector)1 Nullable (javax.annotation.Nullable)1