Search in sources :

Example 1 with EncodingOptions

use of com.google.cloud.firestore.UserDataConverter.EncodingOptions in project java-firestore by googleapis.

the class UpdateBuilder method performUpdate.

private T performUpdate(@Nonnull DocumentReference documentReference, @Nonnull final Map<FieldPath, Object> fields, @Nonnull Precondition precondition) {
    verifyNotCommitted();
    Preconditions.checkArgument(!fields.isEmpty(), "Data for update() cannot be empty.");
    Tracing.getTracer().getCurrentSpan().addAnnotation(TraceUtil.SPAN_NAME_UPDATEDOCUMENT);
    Map<String, Object> deconstructedMap = expandObject(fields);
    DocumentSnapshot documentSnapshot = DocumentSnapshot.fromObject(firestore, documentReference, deconstructedMap, new EncodingOptions() {

        @Override
        public boolean allowDelete(FieldPath fieldPath) {
            return fields.containsKey(fieldPath);
        }

        @Override
        public boolean allowTransform() {
            return true;
        }
    });
    List<FieldPath> fieldPaths = new ArrayList<>(fields.keySet());
    DocumentTransform documentTransform = DocumentTransform.fromFieldPathMap(documentReference, fields);
    fieldPaths.removeAll(documentTransform.getFields());
    FieldMask fieldMask = new FieldMask(fieldPaths);
    Write.Builder write = documentSnapshot.toPb();
    write.setCurrentDocument(precondition.toPb());
    write.setUpdateMask(fieldMask.toPb());
    if (!documentTransform.isEmpty()) {
        write.addAllUpdateTransforms(documentTransform.toPb());
    }
    writes.add(new WriteOperation(documentReference, write));
    return wrapResult(writes.size() - 1);
}
Also used : Write(com.google.firestore.v1.Write) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) EncodingOptions(com.google.cloud.firestore.UserDataConverter.EncodingOptions)

Aggregations

EncodingOptions (com.google.cloud.firestore.UserDataConverter.EncodingOptions)1 Write (com.google.firestore.v1.Write)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1