Search in sources :

Example 6 with Write

use of com.google.firestore.v1.Write in project spring-cloud-gcp by spring-cloud.

the class FirestoreTemplate method deleteDocumentsByName.

private Flux<String> deleteDocumentsByName(Flux<String> documentNames) {
    return Mono.subscriberContext().flatMapMany(ctx -> {
        Optional<TransactionContext> transactionContext = ctx.getOrEmpty(TransactionContext.class);
        if (transactionContext.isPresent()) {
            ReactiveFirestoreResourceHolder holder = (ReactiveFirestoreResourceHolder) transactionContext.get().getResources().get(this.firestore);
            List<Write> writes = holder.getWrites();
            // In a transaction, all write operations should be sent in the commit request, so we just collect them
            return Flux.from(documentNames).doOnNext(t -> writes.add(createDeleteWrite(t)));
        }
        return commitWrites(documentNames, this::createDeleteWrite);
    });
}
Also used : Write(com.google.firestore.v1.Write) ReactiveFirestoreResourceHolder(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreResourceHolder) TransactionContext(org.springframework.transaction.reactive.TransactionContext)

Example 7 with Write

use of com.google.firestore.v1.Write in project spring-cloud-gcp by spring-cloud.

the class FirestoreTemplate method saveAll.

/**
 * {@inheritDoc}
 *
 * <p>The buffer size and buffer timeout settings for {@link #saveAll} can be modified by calling
 * {@link #setWriteBufferSize} and {@link #setWriteBufferTimeout}.
 */
@Override
public <T> Flux<T> saveAll(Publisher<T> instances) {
    return Mono.subscriberContext().flatMapMany(ctx -> {
        Optional<TransactionContext> transactionContext = ctx.getOrEmpty(TransactionContext.class);
        if (transactionContext.isPresent()) {
            ReactiveFirestoreResourceHolder holder = (ReactiveFirestoreResourceHolder) transactionContext.get().getResources().get(this.firestore);
            List<Write> writes = holder.getWrites();
            // In a transaction, all write operations should be sent in the commit request, so we just collect them
            return Flux.from(instances).doOnNext(t -> writes.add(createUpdateWrite(t)));
        }
        return commitWrites(instances, this::createUpdateWrite);
    });
}
Also used : Write(com.google.firestore.v1.Write) ReactiveFirestoreResourceHolder(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreResourceHolder) TransactionContext(org.springframework.transaction.reactive.TransactionContext)

Example 8 with Write

use of com.google.firestore.v1.Write in project spring-cloud-gcp by spring-cloud.

the class FirestoreTemplate method createUpdateWrite.

private <T> Write createUpdateWrite(T entity) {
    Builder builder = Write.newBuilder();
    if (getIdValue(entity) == null) {
        builder.setCurrentDocument(Precondition.newBuilder().setExists(false).build());
    }
    String resourceName = buildResourceName(entity);
    Document document = getClassMapper().entityToDocument(entity, resourceName);
    return builder.setUpdate(document).build();
}
Also used : Builder(com.google.firestore.v1.Write.Builder) Document(com.google.firestore.v1.Document)

Example 9 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class DatastoreV1Test method testWriteValidationFailsWithNoProject.

@Test
public void testWriteValidationFailsWithNoProject() throws Exception {
    Write write = DatastoreIO.v1().write();
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("projectId ValueProvider");
    write.validate(null);
}
Also used : Write(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write) Test(org.junit.Test)

Example 10 with Write

use of com.google.firestore.v1.Write in project beam by apache.

the class DatastoreV1Test method testWriteValidationFailsWithNoProjectInStaticValueProvider.

@Test
public void testWriteValidationFailsWithNoProjectInStaticValueProvider() throws Exception {
    Write write = DatastoreIO.v1().write().withProjectId(StaticValueProvider.<String>of(null));
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("projectId");
    write.validate(null);
}
Also used : Write(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 Write (com.google.firestore.v1.Write)16 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)11 Element (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element)11 FirestoreProtoHelpers.newWrite (org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite)9 Instant (org.joda.time.Instant)9 BatchWriteRequest (com.google.firestore.v1.BatchWriteRequest)8 BatchWriteResponse (com.google.firestore.v1.BatchWriteResponse)8 ArrayList (java.util.ArrayList)5 Write (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Write)5 WriteFailure (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure)5 ApiException (com.google.api.gax.rpc.ApiException)3 Value (com.google.firestore.v1.Value)3 List (java.util.List)3 Lists.newArrayList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList)3 ArrayValue (com.google.firestore.v1.ArrayValue)2 Document (com.google.firestore.v1.Document)2 ByteString (com.google.protobuf.ByteString)2 Code (com.google.rpc.Code)2 IOException (java.io.IOException)2