Search in sources :

Example 16 with MutationBatch

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingQuery.

@Test
public void testAllMutationBatchesAffectingQuery() {
    List<Mutation> mutations = asList(setMutation("fob/bar", map("a", 1)), setMutation("foo/bar", map("a", 1)), patchMutation("foo/bar", map("b", 1)), setMutation("foo/bar/suffix/key", map("a", 1)), setMutation("foo/baz", map("a", 1)), setMutation("food/bar", map("a", 1)));
    // Store all the mutations.
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    List<MutationBatch> expected = asList(batches.get(1), batches.get(2), batches.get(4));
    Query query = Query.atPath(path("foo"));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingQuery(query);
    assertEquals(expected, matches);
}
Also used : Query(com.google.firebase.firestore.core.Query) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Example 17 with MutationBatch

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

the class MutationQueueTestCase method testAcknowledgeThenRemove.

@Test
public void testAcknowledgeThenRemove() {
    MutationBatch batch1 = addMutationBatch();
    persistence.runTransaction(name.getMethodName(), () -> {
        mutationQueue.acknowledgeBatch(batch1, WriteStream.EMPTY_STREAM_TOKEN);
        mutationQueue.removeMutationBatch(batch1);
    });
    assertEquals(0, batchCount());
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) Test(org.junit.Test)

Example 18 with MutationBatch

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingQuery_withCompoundBatches.

@Test
public void testAllMutationBatchesAffectingQuery_withCompoundBatches() {
    Map<String, Object> value = map("a", 1);
    // Store all the mutations.
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(setMutation("foo/bar", value), setMutation("foo/bar/baz/quux", value))));
        batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(setMutation("foo/bar", value), setMutation("foo/baz", value))));
    });
    List<MutationBatch> expected = asList(batches.get(0), batches.get(1));
    Query query = Query.atPath(path("foo"));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingQuery(query);
    assertEquals(expected, matches);
}
Also used : Query(com.google.firebase.firestore.core.Query) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 19 with MutationBatch

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

the class MutationQueueTestCase method testStreamToken.

@Test
public void testStreamToken() {
    ByteString streamToken1 = streamToken("token1");
    ByteString streamToken2 = streamToken("token2");
    persistence.runTransaction("initial stream token", () -> mutationQueue.setLastStreamToken(streamToken1));
    MutationBatch batch1 = addMutationBatch();
    addMutationBatch();
    assertEquals(streamToken1, mutationQueue.getLastStreamToken());
    persistence.runTransaction("acknowledgeBatchId", () -> mutationQueue.acknowledgeBatch(batch1, streamToken2));
    assertEquals(streamToken2, mutationQueue.getLastStreamToken());
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 20 with MutationBatch

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

the class RemoteStore method fillWritePipeline.

// Write Stream
/**
 * Attempts to fill our write pipeline with writes from the LocalStore.
 *
 * <p>Called internally to bootstrap or refill the write pipeline and by SyncEngine whenever there
 * are new mutations to process.
 *
 * <p>Starts the write stream if necessary.
 */
public void fillWritePipeline() {
    int lastBatchIdRetrieved = writePipeline.isEmpty() ? MutationBatch.UNKNOWN : writePipeline.getLast().getBatchId();
    while (canAddToWritePipeline()) {
        MutationBatch batch = localStore.getNextMutationBatch(lastBatchIdRetrieved);
        if (batch == null) {
            if (writePipeline.size() == 0) {
                writeStream.markIdle();
            }
            break;
        }
        addToWritePipeline(batch);
        lastBatchIdRetrieved = batch.getBatchId();
    }
    if (shouldStartWriteStream()) {
        startWriteStream();
    }
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch)

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