Search in sources :

Example 26 with Mutation

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

the class SQLiteMutationQueue method addMutationBatch.

@Override
public MutationBatch addMutationBatch(Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
    int batchId = nextBatchId;
    nextBatchId += 1;
    MutationBatch batch = new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
    MessageLite proto = serializer.encodeMutationBatch(batch);
    db.execute("INSERT INTO mutations (uid, batch_id, mutations) VALUES (?, ?, ?)", uid, batchId, proto.toByteArray());
    // PORTING NOTE: Unlike LevelDB, these entries must be unique.
    // Since user and batchId are fixed within this function body, it's enough to track unique keys
    // added in this batch.
    Set<DocumentKey> inserted = new HashSet<>();
    SQLiteStatement indexInserter = db.prepare("INSERT INTO document_mutations (uid, path, batch_id) VALUES (?, ?, ?)");
    for (Mutation mutation : mutations) {
        DocumentKey key = mutation.getKey();
        if (!inserted.add(key)) {
            continue;
        }
        String path = EncodedPath.encode(key.getPath());
        db.execute(indexInserter, uid, path, batchId);
        indexManager.addToCollectionParentIndex(key.getCollectionPath());
    }
    return batch;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) SQLiteStatement(android.database.sqlite.SQLiteStatement) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) ByteString(com.google.protobuf.ByteString) MessageLite(com.google.protobuf.MessageLite) HashSet(java.util.HashSet)

Example 27 with Mutation

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

Example 28 with Mutation

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

the class MemoryMutationQueue method removeMutationBatch.

@Override
public void removeMutationBatch(MutationBatch batch) {
    // Find the position of the first batch for removal. This need not be the first entry in the
    // queue.
    int batchIndex = indexOfExistingBatchId(batch.getBatchId(), "removed");
    hardAssert(batchIndex == 0, "Can only remove the first entry of the mutation queue");
    queue.remove(0);
    // Remove entries from the index too.
    ImmutableSortedSet<DocumentReference> references = batchesByDocumentKey;
    for (Mutation mutation : batch.getMutations()) {
        DocumentKey key = mutation.getKey();
        persistence.getReferenceDelegate().removeMutationReference(key);
        DocumentReference reference = new DocumentReference(key, batch.getBatchId());
        references = references.remove(reference);
    }
    batchesByDocumentKey = references;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation)

Example 29 with Mutation

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

the class MemoryMutationQueue method addMutationBatch.

@Override
public MutationBatch addMutationBatch(Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
    hardAssert(!mutations.isEmpty(), "Mutation batches should not be empty");
    int batchId = nextBatchId;
    nextBatchId += 1;
    int size = queue.size();
    if (size > 0) {
        MutationBatch prior = queue.get(size - 1);
        hardAssert(prior.getBatchId() < batchId, "Mutation batchIds must be monotonically increasing order");
    }
    MutationBatch batch = new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
    queue.add(batch);
    // Track references by document key and index collection parents.
    for (Mutation mutation : mutations) {
        batchesByDocumentKey = batchesByDocumentKey.insert(new DocumentReference(mutation.getKey(), batchId));
        indexManager.addToCollectionParentIndex(mutation.getKey().getCollectionPath());
    }
    return batch;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) Mutation(com.google.firebase.firestore.model.mutation.Mutation)

Example 30 with Mutation

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

the class WriteStream method writeMutations.

/**
 * Sends a list of mutations to the Firestore backend to apply
 *
 * @param mutations The mutations
 */
void writeMutations(List<Mutation> mutations) {
    hardAssert(isOpen(), "Writing mutations requires an opened stream");
    hardAssert(handshakeComplete, "Handshake must be complete before writing mutations");
    WriteRequest.Builder request = WriteRequest.newBuilder();
    for (Mutation mutation : mutations) {
        request.addWrites(serializer.encodeMutation(mutation));
    }
    request.setStreamToken(lastStreamToken);
    writeRequest(request.build());
}
Also used : WriteRequest(com.google.firestore.v1.WriteRequest) Mutation(com.google.firebase.firestore.model.mutation.Mutation)

Aggregations

Mutation (com.google.firebase.firestore.model.mutation.Mutation)46 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)29 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)27 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)25 Test (org.junit.Test)25 DocumentKey (com.google.firebase.firestore.model.DocumentKey)18 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)13 Write (com.google.firestore.v1.Write)12 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)8 TestUtil.verifyMutation (com.google.firebase.firestore.testutil.TestUtil.verifyMutation)8 ArrayList (java.util.ArrayList)8 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)7 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 Map (java.util.Map)4 Timestamp (com.google.firebase.Timestamp)3 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 SQLiteStatement (android.database.sqlite.SQLiteStatement)2