Search in sources :

Example 11 with TransactionSelector

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

the class MockSpannerServiceImpl method getTransactionId.

private ByteString getTransactionId(Session session, TransactionSelector tx) {
    ByteString transactionId = null;
    switch(tx.getSelectorCase()) {
        case SELECTOR_NOT_SET:
        case SINGLE_USE:
            transactionId = null;
            break;
        case BEGIN:
            transactionId = beginTransaction(session, tx.getBegin()).getId();
            break;
        case ID:
            Transaction transaction = transactions.get(tx.getId());
            if (transaction == null) {
                Optional<Boolean> aborted = Optional.fromNullable(abortedTransactions.get(tx.getId()));
                if (aborted.or(Boolean.FALSE)) {
                    throwTransactionAborted(tx.getId());
                } else {
                    throwTransactionNotFound(tx.getId());
                }
            } else {
                transactionId = transaction.getId();
                transactionLastUsed.put(transactionId, Instant.now());
            }
            break;
        default:
            throw Status.UNIMPLEMENTED.asRuntimeException();
    }
    return transactionId;
}
Also used : Transaction(com.google.spanner.v1.Transaction) ByteString(com.google.protobuf.ByteString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 12 with TransactionSelector

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

the class MockSpannerServiceImpl method returnPartialResultSet.

private void returnPartialResultSet(ResultSet resultSet, ByteString transactionId, TransactionSelector transactionSelector, StreamObserver<PartialResultSet> responseObserver, SimulatedExecutionTime executionTime) {
    ResultSetMetadata metadata = resultSet.getMetadata();
    if (transactionId == null) {
        Transaction transaction = getTemporaryTransactionOrNull(transactionSelector);
        metadata = metadata.toBuilder().setTransaction(transaction).build();
    } else {
        metadata = metadata.toBuilder().setTransaction(ignoreNextInlineBeginRequest.getAndSet(false) ? Transaction.getDefaultInstance() : Transaction.newBuilder().setId(transactionId).build()).build();
    }
    resultSet = resultSet.toBuilder().setMetadata(metadata).build();
    PartialResultSetsIterator iterator = new PartialResultSetsIterator(resultSet);
    long index = 0L;
    while (iterator.hasNext()) {
        SimulatedExecutionTime.checkStreamException(index, executionTime.exceptions, executionTime.streamIndices);
        responseObserver.onNext(iterator.next());
        index++;
    }
    responseObserver.onCompleted();
}
Also used : Transaction(com.google.spanner.v1.Transaction) ResultSetMetadata(com.google.spanner.v1.ResultSetMetadata)

Example 13 with TransactionSelector

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

the class SpannerProbes method partitionProber.

/**
 * Probe to test PartitionQuery and PartitionRead grpc call from Spanner stub.
 */
public static void partitionProber(SpannerGrpc.SpannerBlockingStub stub) {
    Session session = null;
    try {
        session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
        // Probing partition query call.
        TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
        TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
        stub.partitionQuery(PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + TABLE).setTransaction(selector).build());
        // Probing partition read call.
        stub.partitionRead(PartitionReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setTransaction(selector).setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("username").addColumns("firstname").addColumns("lastname").build());
    } finally {
        deleteSession(stub, session);
    }
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) Session(com.google.spanner.v1.Session)

Example 14 with TransactionSelector

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

the class GcpManagedChannelTest method testGetKeysFromRequest.

@Test
public void testGetKeysFromRequest() {
    String expected = "thisisaname";
    TransactionSelector selector = TransactionSelector.getDefaultInstance();
    PartitionReadRequest req = PartitionReadRequest.newBuilder().setSession(expected).setTable("jenny").setTransaction(selector).addColumns("users").build();
    List<String> result = getKeysFromMessage(req, "session");
    assertEquals(expected, result.get(0));
    result = getKeysFromMessage(req, "fakesession");
    assertEquals(0, result.size());
}
Also used : TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionReadRequest(com.google.spanner.v1.PartitionReadRequest) Test(org.junit.Test)

Example 15 with TransactionSelector

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

the class SpannerTestCases method testPartitionQuery.

void testPartitionQuery() throws InterruptedException {
    System.out.println("\nTestPartitionQuery");
    ManagedChannel channel = getChannel();
    SpannerBlockingStub stub = getBlockingStub(channel);
    Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
    TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
    PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
    BlockingCall<PartitionQueryRequest, PartitionResponse> blockingCall = (PartitionQueryRequest req) -> stub.partitionQuery(req);
    doTestBlocking(channel, request, blockingCall);
    stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) SpannerBlockingStub(com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionQueryRequest(com.google.spanner.v1.PartitionQueryRequest) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session)

Aggregations

TransactionSelector (com.google.spanner.v1.TransactionSelector)13 ByteString (com.google.protobuf.ByteString)7 TransactionOptions (com.google.spanner.v1.TransactionOptions)6 Session (com.google.spanner.v1.Session)5 PartitionResponse (com.google.spanner.v1.PartitionResponse)4 Test (org.junit.Test)4 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)3 Transaction (com.google.spanner.v1.Transaction)3 GrpcResultSet (com.google.cloud.spanner.AbstractResultSet.GrpcResultSet)2 GrpcStreamIterator (com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator)2 ResumableStreamIterator (com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator)2 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2 PartialResultSet (com.google.spanner.v1.PartialResultSet)2 ResultSetMetadata (com.google.spanner.v1.ResultSetMetadata)2 SpannerStub (com.google.spanner.v1.SpannerGrpc.SpannerStub)2 ManagedChannel (io.grpc.ManagedChannel)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2