Search in sources :

Example 6 with MutationBatch

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

the class LocalStore method handleUserChange.

// PORTING NOTE: no shutdown for LocalStore or persistence components on Android.
public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {
    // Swap out the mutation queue, grabbing the pending mutation batches before and after.
    List<MutationBatch> oldBatches = mutationQueue.getAllMutationBatches();
    initializeUserComponents(user);
    startIndexManager();
    startMutationQueue();
    List<MutationBatch> newBatches = mutationQueue.getAllMutationBatches();
    // Union the old/new changed keys.
    ImmutableSortedSet<DocumentKey> changedKeys = DocumentKey.emptyKeySet();
    for (List<MutationBatch> batches : asList(oldBatches, newBatches)) {
        for (MutationBatch batch : batches) {
            for (Mutation mutation : batch.getMutations()) {
                changedKeys = changedKeys.insert(mutation.getKey());
            }
        }
    }
    // Return the set of all (potentially) changed documents as the result of the user change.
    return localDocuments.getDocuments(changedKeys);
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation)

Example 7 with MutationBatch

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

the class MemoryMutationQueue method indexOfBatchId.

// Helpers
/**
 * Finds the index of the given batchId in the mutation queue. This operation is O(1).
 *
 * @return The computed index of the batch with the given batchId, based on the state of the
 *     queue. Note this index can be negative if the requested batchId has already been removed
 *     from the queue or past the end of the queue if the batchId is larger than the last added
 *     batch.
 */
private int indexOfBatchId(int batchId) {
    if (queue.isEmpty()) {
        // As an index this is past the end of the queue
        return 0;
    }
    // Examine the front of the queue to figure out the difference between the batchId and indexes
    // in the array. Note that since the queue is ordered by batchId, if the first batch has a
    // larger batchId then the requested batchId doesn't exist in the queue.
    MutationBatch firstBatch = queue.get(0);
    int firstBatchId = firstBatch.getBatchId();
    return batchId - firstBatchId;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch)

Example 8 with MutationBatch

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

the class MemoryMutationQueue method getAllMutationBatchesAffectingDocumentKey.

@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(DocumentKey documentKey) {
    DocumentReference start = new DocumentReference(documentKey, 0);
    List<MutationBatch> result = new ArrayList<>();
    Iterator<DocumentReference> iterator = batchesByDocumentKey.iteratorFrom(start);
    while (iterator.hasNext()) {
        DocumentReference reference = iterator.next();
        if (!documentKey.equals(reference.getKey())) {
            break;
        }
        MutationBatch batch = lookupMutationBatch(reference.getId());
        hardAssert(batch != null, "Batches in the index must exist in the main table");
        result.add(batch);
    }
    return result;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList)

Example 9 with MutationBatch

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

the class MemoryMutationQueue method acknowledgeBatch.

@Override
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
    int batchId = batch.getBatchId();
    int batchIndex = indexOfExistingBatchId(batchId, "acknowledged");
    hardAssert(batchIndex == 0, "Can only acknowledge the first batch in the mutation queue");
    // Verify that the batch in the queue is the one to be acknowledged.
    MutationBatch check = queue.get(batchIndex);
    hardAssert(batchId == check.getBatchId(), "Queue ordering failure: expected batch %d, got batch %d", batchId, check.getBatchId());
    lastStreamToken = checkNotNull(streamToken);
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch)

Example 10 with MutationBatch

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

the class LocalSerializerTest method testMultipleMutationsAreSquashed.

// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testMultipleMutationsAreSquashed() {
    // INPUT:
    // SetMutation -> SetMutation -> TransformMutation -> DeleteMutation -> PatchMutation ->
    // TransformMutation -> PatchMutation
    // OUTPUT (squashed):
    // SetMutation -> SetMutation -> DeleteMutation -> PatchMutation -> PatchMutation
    WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addAllWrites(asList(setProto, setProto, transformProto, deleteProto, patchProto, transformProto, patchProto)).setLocalWriteTime(writeTimeProto).build();
    MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
    assertEquals(5, decoded.getMutations().size());
    List<Write> allExpected = asList(new TestWriteBuilder().addSet().build(), new TestWriteBuilder().addSet().addUpdateTransforms().build(), new TestWriteBuilder().addDelete().build(), new TestWriteBuilder().addPatch().addUpdateTransforms().build(), new TestWriteBuilder().addPatch().build());
    for (int i = 0; i < decoded.getMutations().size(); i++) {
        Mutation mutation = decoded.getMutations().get(i);
        Write encoded = remoteSerializer.encodeMutation(mutation);
        assertEquals(allExpected.get(i), encoded);
    }
}
Also used : Write(com.google.firestore.v1.Write) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) WriteBatch(com.google.firebase.firestore.proto.WriteBatch) Test(org.junit.Test)

Aggregations

MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)40 Test (org.junit.Test)15 Mutation (com.google.firebase.firestore.model.mutation.Mutation)13 ArrayList (java.util.ArrayList)12 DocumentKey (com.google.firebase.firestore.model.DocumentKey)11 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)8 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)6 Write (com.google.firestore.v1.Write)5 ByteString (com.google.protobuf.ByteString)5 HashSet (java.util.HashSet)5 WriteBatch (com.google.firebase.firestore.proto.WriteBatch)4 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)4 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Timestamp (com.google.firebase.Timestamp)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 Query (com.google.firebase.firestore.core.Query)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 MutationBatchResult (com.google.firebase.firestore.model.mutation.MutationBatchResult)2 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)2