Search in sources :

Example 11 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class PatchMutation method applyToRemoteDocument.

@Override
public void applyToRemoteDocument(MutableDocument document, MutationResult mutationResult) {
    verifyKeyMatches(document);
    if (!this.getPrecondition().isValidFor(document)) {
        // Since the mutation was not rejected, we know that the precondition matched on the backend.
        // We therefore must not have the expected version of the document in our cache and return an
        // UnknownDocument with the known updateTime.
        document.convertToUnknownDocument(mutationResult.getVersion());
        return;
    }
    Map<FieldPath, Value> transformResults = serverTransformResults(document, mutationResult.getTransformResults());
    ObjectValue value = document.getData();
    value.setAll(getPatch());
    value.setAll(transformResults);
    document.convertToFoundDocument(mutationResult.getVersion(), document.getData()).setHasCommittedMutations();
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Example 12 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class PatchMutation method applyToLocalView.

@Override
@Nullable
public FieldMask applyToLocalView(MutableDocument document, @Nullable FieldMask previousMask, Timestamp localWriteTime) {
    verifyKeyMatches(document);
    if (!getPrecondition().isValidFor(document)) {
        return previousMask;
    }
    Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
    Map<FieldPath, Value> patches = getPatch();
    ObjectValue value = document.getData();
    value.setAll(patches);
    value.setAll(transformResults);
    document.convertToFoundDocument(document.getVersion(), document.getData()).setHasLocalMutations();
    if (previousMask == null) {
        return null;
    }
    HashSet<FieldPath> mergedMaskSet = new HashSet<>(previousMask.getMask());
    mergedMaskSet.addAll(this.mask.getMask());
    mergedMaskSet.addAll(getFieldTransformPaths());
    return FieldMask.fromSet(mergedMaskSet);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) HashSet(java.util.HashSet) Nullable(androidx.annotation.Nullable)

Example 13 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class Mutation method extractTransformBaseValue.

public ObjectValue extractTransformBaseValue(Document document) {
    ObjectValue baseObject = null;
    for (FieldTransform transform : fieldTransforms) {
        Value existingValue = document.getField(transform.getFieldPath());
        Value coercedValue = transform.getOperation().computeBaseValue(existingValue);
        if (coercedValue != null) {
            if (baseObject == null) {
                baseObject = new ObjectValue();
            }
            baseObject.set(transform.getFieldPath(), coercedValue);
        }
    }
    return baseObject;
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Example 14 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class Mutation method calculateOverlayMutation.

/**
 * A utility method to calculate an {@link Mutation} representing the overlay from the final state
 * of the document, and a {@link FieldMask} representing the fields that are mutated by the local
 * mutations.
 */
public static Mutation calculateOverlayMutation(MutableDocument doc, @Nullable FieldMask mask) {
    if ((!doc.hasLocalMutations()) || (mask != null && mask.getMask().isEmpty())) {
        return null;
    }
    // mask == null when there are Set or Delete being applied to get to the current document.
    if (mask == null) {
        if (doc.isNoDocument()) {
            return new DeleteMutation(doc.getKey(), Precondition.NONE);
        } else {
            return new SetMutation(doc.getKey(), doc.getData(), Precondition.NONE);
        }
    } else {
        ObjectValue docValue = doc.getData();
        ObjectValue patchValue = new ObjectValue();
        HashSet<FieldPath> maskSet = new HashSet<>();
        for (FieldPath path : mask.getMask()) {
            if (!maskSet.contains(path)) {
                Value value = docValue.get(path);
                // result, `foo` is not in `mask`, and the resulting mutation would miss `foo`.
                if (value == null && path.length() > 1) {
                    path = path.popLast();
                }
                patchValue.set(path, docValue.get(path));
                maskSet.add(path);
            }
        }
        return new PatchMutation(doc.getKey(), patchValue, FieldMask.fromSet(maskSet), Precondition.NONE);
    }
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) HashSet(java.util.HashSet)

Example 15 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class SetMutation method applyToRemoteDocument.

@Override
public void applyToRemoteDocument(MutableDocument document, MutationResult mutationResult) {
    verifyKeyMatches(document);
    // Unlike applyToLocalView, if we're applying a mutation to a remote document the server has
    // accepted the mutation so the precondition must have held.
    ObjectValue newData = value.clone();
    Map<FieldPath, Value> transformResults = serverTransformResults(document, mutationResult.getTransformResults());
    newData.setAll(transformResults);
    document.convertToFoundDocument(mutationResult.getVersion(), newData).setHasCommittedMutations();
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Aggregations

ObjectValue (com.google.firebase.firestore.model.ObjectValue)24 Value (com.google.firestore.v1.Value)12 FieldPath (com.google.firebase.firestore.model.FieldPath)8 Test (org.junit.Test)8 MutableDocument (com.google.firebase.firestore.model.MutableDocument)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 ArrayValue (com.google.firestore.v1.ArrayValue)4 MapValue (com.google.firestore.v1.MapValue)4 NullValue (com.google.protobuf.NullValue)4 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)3 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)3 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)3 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3