Search in sources :

Example 11 with FieldPath

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

the class Mutation method localTransformResults.

/**
 * Creates a list of "transform results" (a transform result is a field value representing the
 * result of applying a transform) for use when applying a transform locally.
 *
 * @param localWriteTime The local time of the mutation (used to generate ServerTimestampValues).
 * @param mutableDocument The document to apply transforms on.
 * @return A map of fields to transform results.
 */
protected Map<FieldPath, Value> localTransformResults(Timestamp localWriteTime, MutableDocument mutableDocument) {
    Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
    for (FieldTransform fieldTransform : fieldTransforms) {
        TransformOperation transform = fieldTransform.getOperation();
        Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
        transformResults.put(fieldTransform.getFieldPath(), transform.applyToLocalView(previousValue, localWriteTime));
    }
    return transformResults;
}
Also used : HashMap(java.util.HashMap) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Example 12 with FieldPath

use of com.google.firebase.firestore.model.FieldPath 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.
 */
@Nullable
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) Nullable(androidx.annotation.Nullable)

Example 13 with FieldPath

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

the class Mutation method serverTransformResults.

/**
 * Creates a list of "transform results" (a transform result is a field value representing the
 * result of applying a transform) for use after a mutation containing transforms has been
 * acknowledged by the server.
 *
 * @param mutableDocument The current state of the document after applying all previous mutations.
 * @param serverTransformResults The transform results received by the server.
 * @return A map of fields to transform results.
 */
protected Map<FieldPath, Value> serverTransformResults(MutableDocument mutableDocument, List<Value> serverTransformResults) {
    Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
    hardAssert(fieldTransforms.size() == serverTransformResults.size(), "server transform count (%d) should match field transform count (%d)", serverTransformResults.size(), fieldTransforms.size());
    for (int i = 0; i < serverTransformResults.size(); i++) {
        FieldTransform fieldTransform = fieldTransforms.get(i);
        TransformOperation transform = fieldTransform.getOperation();
        Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
        transformResults.put(fieldTransform.getFieldPath(), transform.applyToRemoteDocument(previousValue, serverTransformResults.get(i)));
    }
    return transformResults;
}
Also used : HashMap(java.util.HashMap) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Example 14 with FieldPath

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

the class BundleSerializer method decodeUnaryFilter.

private void decodeUnaryFilter(List<Filter> result, JSONObject unaryFilter) throws JSONException {
    FieldPath fieldPath = decodeFieldReference(unaryFilter.getJSONObject("field"));
    String operator = unaryFilter.getString("op");
    switch(operator) {
        case "IS_NAN":
            result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.EQUAL, Values.NAN_VALUE));
            break;
        case "IS_NULL":
            result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.EQUAL, Values.NULL_VALUE));
            break;
        case "IS_NOT_NAN":
            result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.NOT_EQUAL, Values.NAN_VALUE));
            break;
        case "IS_NOT_NULL":
            result.add(FieldFilter.create(fieldPath, FieldFilter.Operator.NOT_EQUAL, Values.NULL_VALUE));
            break;
        default:
            throw new IllegalArgumentException("Unexpected unary filter: " + operator);
    }
}
Also used : FieldPath(com.google.firebase.firestore.model.FieldPath) ByteString(com.google.protobuf.ByteString)

Example 15 with FieldPath

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

the class BundleSerializer method decodeOrderBy.

private List<OrderBy> decodeOrderBy(@Nullable JSONArray orderBys) throws JSONException {
    List<OrderBy> result = new ArrayList<>();
    if (orderBys != null) {
        for (int i = 0; i < orderBys.length(); ++i) {
            JSONObject orderBy = orderBys.getJSONObject(i);
            FieldPath fieldPath = decodeFieldReference(orderBy.getJSONObject("field"));
            String directionString = orderBy.optString("direction", "ASCENDING");
            OrderBy.Direction direction = directionString.equals("ASCENDING") ? OrderBy.Direction.ASCENDING : OrderBy.Direction.DESCENDING;
            result.add(OrderBy.getInstance(direction, fieldPath));
        }
    }
    return result;
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) JSONObject(org.json.JSONObject) FieldPath(com.google.firebase.firestore.model.FieldPath) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString)

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