Search in sources :

Example 1 with VerifyMutation

use of com.google.firebase.firestore.model.mutation.VerifyMutation 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 2 with VerifyMutation

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

the class Transaction method commit.

public Task<Void> commit() {
    ensureCommitNotCalled();
    if (lastWriteError != null) {
        return Tasks.forException(lastWriteError);
    }
    HashSet<DocumentKey> unwritten = new HashSet<>(readVersions.keySet());
    // For each mutation, note that the doc was written.
    for (Mutation mutation : mutations) {
        unwritten.remove(mutation.getKey());
    }
    // For each document that was read but not written to, we want to perform a `verify` operation.
    for (DocumentKey key : unwritten) {
        mutations.add(new VerifyMutation(key, precondition(key)));
    }
    committed = true;
    return datastore.commit(mutations).continueWithTask(Executors.DIRECT_EXECUTOR, task -> {
        if (task.isSuccessful()) {
            return Tasks.forResult(null);
        } else {
            return Tasks.forException(task.getException());
        }
    });
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) VerifyMutation(com.google.firebase.firestore.model.mutation.VerifyMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) VerifyMutation(com.google.firebase.firestore.model.mutation.VerifyMutation) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) HashSet(java.util.HashSet)

Aggregations

DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)2 VerifyMutation (com.google.firebase.firestore.model.mutation.VerifyMutation)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 FieldTransform (com.google.firebase.firestore.model.mutation.FieldTransform)1 Mutation (com.google.firebase.firestore.model.mutation.Mutation)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 DocumentTransform (com.google.firestore.v1.DocumentTransform)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1