Search in sources :

Example 1 with ObservableReactiveUtil

use of com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManager method doCommit.

@Override
protected Mono<Void> doCommit(TransactionSynchronizationManager transactionSynchronizationManager, GenericReactiveTransaction genericReactiveTransaction) throws TransactionException {
    return Mono.defer(() -> {
        ReactiveFirestoreResourceHolder resourceHolder = extractFirestoreTransaction(genericReactiveTransaction).getResourceHolder();
        CommitRequest.Builder builder = CommitRequest.newBuilder().setDatabase(this.databasePath).setTransaction(resourceHolder.getTransactionId());
        resourceHolder.getWrites().forEach(builder::addWrites);
        return ObservableReactiveUtil.<CommitResponse>unaryCall(obs -> this.firestore.commit(builder.build(), obs)).flatMap(response -> {
            for (Object entity : resourceHolder.getEntities()) {
                this.classMapper.setUpdateTime(entity, Timestamp.fromProto(response.getCommitTime()));
            }
            return Mono.empty();
        });
    });
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(com.google.cloud.spring.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) Timestamp(com.google.cloud.Timestamp) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) Nullable(org.springframework.lang.Nullable) FirestoreClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper) ObservableReactiveUtil(com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil) Assert(org.springframework.util.Assert) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject)

Example 2 with ObservableReactiveUtil

use of com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManager method startTransaction.

private Mono<ReactiveFirestoreResourceHolder> startTransaction(TransactionDefinition definition) {
    TransactionOptions.Builder txOptions = definition.isReadOnly() ? TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.newBuilder().build()) : TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.newBuilder().build());
    BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setOptions(txOptions).setDatabase(this.databasePath).build();
    return ObservableReactiveUtil.<BeginTransactionResponse>unaryCall(obs -> this.firestore.beginTransaction(beginTransactionRequest, obs)).map(beginTransactionResponse -> new ReactiveFirestoreResourceHolder(beginTransactionResponse.getTransaction()));
}
Also used : CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(com.google.cloud.spring.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) Timestamp(com.google.cloud.Timestamp) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) Nullable(org.springframework.lang.Nullable) FirestoreClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper) ObservableReactiveUtil(com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil) Assert(org.springframework.util.Assert) TransactionOptions(com.google.firestore.v1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest)

Aggregations

Timestamp (com.google.cloud.Timestamp)2 FirestoreClassMapper (com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper)2 ObservableReactiveUtil (com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil)2 Util (com.google.cloud.spring.data.firestore.util.Util)2 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)2 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 FirestoreStub (com.google.firestore.v1.FirestoreGrpc.FirestoreStub)2 RollbackRequest (com.google.firestore.v1.RollbackRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 ByteString (com.google.protobuf.ByteString)2 Empty (com.google.protobuf.Empty)2 Nullable (org.springframework.lang.Nullable)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2 TransactionException (org.springframework.transaction.TransactionException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 AbstractReactiveTransactionManager (org.springframework.transaction.reactive.AbstractReactiveTransactionManager)2 GenericReactiveTransaction (org.springframework.transaction.reactive.GenericReactiveTransaction)2