Search in sources :

Example 1 with FieldTransform

use of com.google.firestore.v1.DocumentTransform.FieldTransform in project firebase-android-sdk by firebase.

the class LocalSerializer method decodeMutationBatch.

/**
 * Decodes a WriteBatch proto into a MutationBatch model.
 */
MutationBatch decodeMutationBatch(com.google.firebase.firestore.proto.WriteBatch batch) {
    int batchId = batch.getBatchId();
    Timestamp localWriteTime = rpcSerializer.decodeTimestamp(batch.getLocalWriteTime());
    int baseMutationsCount = batch.getBaseWritesCount();
    List<Mutation> baseMutations = new ArrayList<>(baseMutationsCount);
    for (int i = 0; i < baseMutationsCount; i++) {
        baseMutations.add(rpcSerializer.decodeMutation(batch.getBaseWrites(i)));
    }
    List<Mutation> mutations = new ArrayList<>(batch.getWritesCount());
    // TODO(b/174608374): Remove this code once we perform a schema migration.
    for (int i = 0; i < batch.getWritesCount(); ++i) {
        Write currentMutation = batch.getWrites(i);
        boolean hasTransform = i + 1 < batch.getWritesCount() && batch.getWrites(i + 1).hasTransform();
        if (hasTransform) {
            hardAssert(batch.getWrites(i).hasUpdate(), "TransformMutation should be preceded by a patch or set mutation");
            Builder newMutationBuilder = Write.newBuilder(currentMutation);
            Write transformMutation = batch.getWrites(i + 1);
            for (FieldTransform fieldTransform : transformMutation.getTransform().getFieldTransformsList()) {
                newMutationBuilder.addUpdateTransforms(fieldTransform);
            }
            mutations.add(rpcSerializer.decodeMutation(newMutationBuilder.build()));
            ++i;
        } else {
            mutations.add(rpcSerializer.decodeMutation(currentMutation));
        }
    }
    return new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
}
Also used : Write(com.google.firestore.v1.Write) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) Builder(com.google.firestore.v1.Write.Builder) FieldTransform(com.google.firestore.v1.DocumentTransform.FieldTransform) ArrayList(java.util.ArrayList) Mutation(com.google.firebase.firestore.model.mutation.Mutation) Timestamp(com.google.firebase.Timestamp)

Example 2 with FieldTransform

use of com.google.firestore.v1.DocumentTransform.FieldTransform in project firebase-android-sdk by firebase.

the class RemoteSerializer method decodeMutation.

public Mutation decodeMutation(com.google.firestore.v1.Write mutation) {
    Precondition precondition = mutation.hasCurrentDocument() ? decodePrecondition(mutation.getCurrentDocument()) : Precondition.NONE;
    List<FieldTransform> fieldTransforms = new ArrayList<>();
    for (DocumentTransform.FieldTransform fieldTransform : mutation.getUpdateTransformsList()) {
        fieldTransforms.add(decodeFieldTransform(fieldTransform));
    }
    switch(mutation.getOperationCase()) {
        case UPDATE:
            if (mutation.hasUpdateMask()) {
                return new PatchMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), decodeDocumentMask(mutation.getUpdateMask()), precondition, fieldTransforms);
            } else {
                return new SetMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), precondition, fieldTransforms);
            }
        case DELETE:
            return new DeleteMutation(decodeKey(mutation.getDelete()), precondition);
        case VERIFY:
            return new VerifyMutation(decodeKey(mutation.getVerify()), precondition);
        default:
            throw fail("Unknown mutation operation: %d", mutation.getOperationCase());
    }
}
Also used : DocumentTransform(com.google.firestore.v1.DocumentTransform) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) Precondition(com.google.firebase.firestore.model.mutation.Precondition) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) VerifyMutation(com.google.firebase.firestore.model.mutation.VerifyMutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation)

Example 3 with FieldTransform

use of com.google.firestore.v1.DocumentTransform.FieldTransform 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 4 with FieldTransform

use of com.google.firestore.v1.DocumentTransform.FieldTransform 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 5 with FieldTransform

use of com.google.firestore.v1.DocumentTransform.FieldTransform in project java-firestore by googleapis.

the class LocalFirestoreHelper method transform.

public static List<FieldTransform> transform(String fieldPath, FieldTransform fieldTransform, Object... fieldPathOrTransform) {
    List<FieldTransform> transforms = new ArrayList<>();
    FieldTransform.Builder transformBuilder = FieldTransform.newBuilder();
    transformBuilder.setFieldPath(fieldPath).mergeFrom(fieldTransform);
    transforms.add(transformBuilder.build());
    for (int i = 0; i < fieldPathOrTransform.length; i += 2) {
        String path = (String) fieldPathOrTransform[i];
        FieldTransform transform = (FieldTransform) fieldPathOrTransform[i + 1];
        transforms.add(FieldTransform.newBuilder().setFieldPath(path).mergeFrom(transform).build());
    }
    return transforms;
}
Also used : FieldTransform(com.google.firestore.v1.DocumentTransform.FieldTransform) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString)

Aggregations

ObjectValue (com.google.firebase.firestore.model.ObjectValue)3 Value (com.google.firestore.v1.Value)3 ArrayList (java.util.ArrayList)3 FieldPath (com.google.firebase.firestore.model.FieldPath)2 FieldTransform (com.google.firestore.v1.DocumentTransform.FieldTransform)2 HashMap (java.util.HashMap)2 Timestamp (com.google.firebase.Timestamp)1 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)1 FieldTransform (com.google.firebase.firestore.model.mutation.FieldTransform)1 Mutation (com.google.firebase.firestore.model.mutation.Mutation)1 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)1 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)1 Precondition (com.google.firebase.firestore.model.mutation.Precondition)1 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)1 VerifyMutation (com.google.firebase.firestore.model.mutation.VerifyMutation)1 DocumentTransform (com.google.firestore.v1.DocumentTransform)1 Write (com.google.firestore.v1.Write)1 Builder (com.google.firestore.v1.Write.Builder)1 ByteString (com.google.protobuf.ByteString)1