Search in sources :

Example 1 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project spring-cloud-gcp by spring-cloud.

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(), new FirestoreMappingContext());
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 2 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project java-spanner by googleapis.

the class SpannerClient method rollback.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any
 * transaction that includes one or more [Read][google.spanner.v1.Spanner.Read] or
 * [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately decides not to
 * commit.
 *
 * <p>`Rollback` returns `OK` if it successfully aborts the transaction, the transaction was
 * already aborted, or the transaction is not found. `Rollback` never returns `ABORTED`.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (SpannerClient spannerClient = SpannerClient.create()) {
 *   String session =
 *       SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString();
 *   ByteString transactionId = ByteString.EMPTY;
 *   spannerClient.rollback(session, transactionId);
 * }
 * }</pre>
 *
 * @param session Required. The session in which the transaction to roll back is running.
 * @param transactionId Required. The transaction to roll back.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void rollback(String session, ByteString transactionId) {
    RollbackRequest request = RollbackRequest.newBuilder().setSession(session).setTransactionId(transactionId).build();
    rollback(request);
}
Also used : RollbackRequest(com.google.spanner.v1.RollbackRequest)

Example 3 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project java-spanner by googleapis.

the class SpannerClient method rollback.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any
 * transaction that includes one or more [Read][google.spanner.v1.Spanner.Read] or
 * [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately decides not to
 * commit.
 *
 * <p>`Rollback` returns `OK` if it successfully aborts the transaction, the transaction was
 * already aborted, or the transaction is not found. `Rollback` never returns `ABORTED`.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (SpannerClient spannerClient = SpannerClient.create()) {
 *   SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
 *   ByteString transactionId = ByteString.EMPTY;
 *   spannerClient.rollback(session, transactionId);
 * }
 * }</pre>
 *
 * @param session Required. The session in which the transaction to roll back is running.
 * @param transactionId Required. The transaction to roll back.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void rollback(SessionName session, ByteString transactionId) {
    RollbackRequest request = RollbackRequest.newBuilder().setSession(session == null ? null : session.toString()).setTransactionId(transactionId).build();
    rollback(request);
}
Also used : RollbackRequest(com.google.spanner.v1.RollbackRequest)

Example 4 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project java-spanner by googleapis.

the class SpannerClientTest method rollbackTest.

@Test
public void rollbackTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockSpanner.addResponse(expectedResponse);
    SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
    ByteString transactionId = ByteString.EMPTY;
    client.rollback(session, transactionId);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    RollbackRequest actualRequest = ((RollbackRequest) actualRequests.get(0));
    Assert.assertEquals(session.toString(), actualRequest.getSession());
    Assert.assertEquals(transactionId, actualRequest.getTransactionId());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.spanner.v1.RollbackRequest) SessionName(com.google.spanner.v1.SessionName) Test(org.junit.Test)

Example 5 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project java-spanner by googleapis.

the class SpannerClientTest method rollbackTest2.

@Test
public void rollbackTest2() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockSpanner.addResponse(expectedResponse);
    String session = "session1984987798";
    ByteString transactionId = ByteString.EMPTY;
    client.rollback(session, transactionId);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    RollbackRequest actualRequest = ((RollbackRequest) actualRequests.get(0));
    Assert.assertEquals(session, actualRequest.getSession());
    Assert.assertEquals(transactionId, actualRequest.getTransactionId());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.spanner.v1.RollbackRequest) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)5 Empty (com.google.protobuf.Empty)5 RollbackRequest (com.google.spanner.v1.RollbackRequest)5 RollbackRequest (com.google.firestore.v1.RollbackRequest)4 Test (org.junit.Test)4 AbstractMessage (com.google.protobuf.AbstractMessage)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 Document (com.google.firestore.v1.Document)2 GetDocumentRequest (com.google.firestore.v1.GetDocumentRequest)2 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)1 TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)1 FirestoreTemplate (com.google.cloud.spring.data.firestore.FirestoreTemplate)1 FirestoreDefaultClassMapper (com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper)1 FirestoreMappingContext (com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext)1 FirestoreBlockingStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub)1 RollbackRequest (com.google.firestore.v1beta1.RollbackRequest)1 SessionName (com.google.spanner.v1.SessionName)1 TransactionSelector (com.google.spanner.v1.TransactionSelector)1