Search in sources :

Example 6 with FieldPath

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

the class TestUtil method patchMutationHelper.

private static PatchMutation patchMutationHelper(String path, Map<String, Object> values, Precondition precondition, @Nullable List<FieldPath> updateMask) {
    // Replace '<DELETE>' from JSON
    for (Entry<String, Object> entry : values.entrySet()) {
        if (entry.getValue().equals(DELETE_SENTINEL)) {
            values.put(entry.getKey(), FieldValue.delete());
        }
    }
    UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
    ParsedUpdateData parsed = dataReader.parseUpdateData(values);
    // `mergeMutation()` provides an update mask for the merged fields, whereas `patchMutation()`
    // requires the update mask to be parsed from the values.
    Collection<FieldPath> mask = updateMask != null ? updateMask : parsed.getFieldMask().getMask();
    // We sort the fieldMaskPaths to make the order deterministic in tests. (Otherwise, when we
    // flatten a Set to a proto repeated field, we'll end up comparing in iterator order and
    // possibly consider {foo,bar} != {bar,foo}.)
    SortedSet<FieldPath> fieldMaskPaths = new TreeSet<>(mask);
    // The order of the transforms doesn't matter, but we sort them so tests can assume a particular
    // order.
    ArrayList<FieldTransform> fieldTransforms = new ArrayList<>(parsed.getFieldTransforms());
    Collections.sort(fieldTransforms, (ft1, ft2) -> ft1.getFieldPath().compareTo(ft2.getFieldPath()));
    return new PatchMutation(key(path), parsed.getData(), FieldMask.fromSet(fieldMaskPaths), precondition, fieldTransforms);
}
Also used : FieldPath(com.google.firebase.firestore.model.FieldPath) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ParsedUpdateData(com.google.firebase.firestore.core.UserData.ParsedUpdateData) UserDataReader(com.google.firebase.firestore.UserDataReader) TreeSet(java.util.TreeSet) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation)

Example 7 with FieldPath

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

the class RemoteSerializer method decodeOrderBy.

private OrderBy decodeOrderBy(Order proto) {
    FieldPath fieldPath = FieldPath.fromServerFormat(proto.getField().getFieldPath());
    OrderBy.Direction direction;
    switch(proto.getDirection()) {
        case ASCENDING:
            direction = Direction.ASCENDING;
            break;
        case DESCENDING:
            direction = Direction.DESCENDING;
            break;
        default:
            throw fail("Unrecognized direction %d", proto.getDirection());
    }
    return OrderBy.getInstance(direction, fieldPath);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) FieldPath(com.google.firebase.firestore.model.FieldPath) Direction(com.google.firebase.firestore.core.OrderBy.Direction)

Example 8 with FieldPath

use of com.google.firebase.firestore.model.FieldPath 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 9 with FieldPath

use of com.google.firebase.firestore.model.FieldPath 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 10 with FieldPath

use of com.google.firebase.firestore.model.FieldPath 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

FieldPath (com.google.firebase.firestore.model.FieldPath)22 ObjectValue (com.google.firebase.firestore.model.ObjectValue)10 Value (com.google.firestore.v1.Value)9 ArrayList (java.util.ArrayList)7 Nullable (androidx.annotation.Nullable)3 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)3 ByteString (com.google.protobuf.ByteString)3 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)2 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)2 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)2 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 OrderBy (com.google.firebase.firestore.core.OrderBy)2 Direction (com.google.firebase.firestore.core.OrderBy.Direction)2 ParseContext (com.google.firebase.firestore.core.UserData.ParseContext)2 FieldIndex (com.google.firebase.firestore.model.FieldIndex)2 ArrayValue (com.google.firestore.v1.ArrayValue)2 MapValue (com.google.firestore.v1.MapValue)2 NullValue (com.google.protobuf.NullValue)2 HashMap (java.util.HashMap)2