Search in sources :

Example 36 with MutationBatch

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

the class MutationQueueTestCase method testCountBatches.

@Test
public void testCountBatches() {
    assertEquals(0, batchCount());
    assertTrue(mutationQueue.isEmpty());
    MutationBatch batch1 = addMutationBatch();
    assertEquals(1, batchCount());
    assertFalse(mutationQueue.isEmpty());
    MutationBatch batch2 = addMutationBatch();
    assertEquals(2, batchCount());
    assertFalse(mutationQueue.isEmpty());
    removeMutationBatches(batch1);
    assertEquals(1, batchCount());
    removeMutationBatches(batch2);
    assertEquals(0, batchCount());
    assertTrue(mutationQueue.isEmpty());
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) Test(org.junit.Test)

Example 37 with MutationBatch

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentKey.

@Test
public void testAllMutationBatchesAffectingDocumentKey() {
    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));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKey(key("foo/bar"));
    assertEquals(expected, matches);
}
Also used : 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 38 with MutationBatch

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

the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys.

// PORTING NOTE: this test only applies to Android, because it's the only platform where the
// implementation of getAllMutationBatchesAffectingDocumentKeys might split the input into several
// queries.
@Test
public void testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys() {
    List<Mutation> mutations = new ArrayList<>();
    // Make sure to force SQLite implementation to split the large query into several smaller ones.
    int lotsOfMutations = 2000;
    for (int i = 0; i < lotsOfMutations; i++) {
        mutations.add(setMutation("foo/" + i, map("a", 1)));
    }
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    // To make it easier validating the large resulting set, use a simple criteria to evaluate --
    // query all keys with an even number in them and make sure the corresponding batches make it
    // into the results.
    ImmutableSortedSet<DocumentKey> evenKeys = DocumentKey.emptyKeySet();
    List<MutationBatch> expected = new ArrayList<>();
    for (int i = 2; i < lotsOfMutations; i += 2) {
        evenKeys = evenKeys.insert(key("foo/" + i));
        expected.add(batches.get(i));
    }
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKeys(evenKeys);
    assertThat(matches).containsExactlyElementsIn(expected).inOrder();
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) 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 39 with MutationBatch

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

the class MutationQueueTestCase method testLookupMutationBatch.

@Test
public void testLookupMutationBatch() {
    // Searching on an empty queue should not find a non-existent batch
    MutationBatch notFound = mutationQueue.lookupMutationBatch(42);
    assertNull(notFound);
    List<MutationBatch> batches = createBatches(10);
    List<MutationBatch> removed = removeFirstBatches(3, batches);
    // After removing, a batch should not be found
    for (MutationBatch batch : removed) {
        notFound = mutationQueue.lookupMutationBatch(batch.getBatchId());
        assertNull(notFound);
    }
    // Remaining entries should still be found
    for (MutationBatch batch : batches) {
        MutationBatch found = mutationQueue.lookupMutationBatch(batch.getBatchId());
        assertNotNull(found);
        assertEquals(batch.getBatchId(), found.getBatchId());
    }
    // Even on a nonempty queue searching should not find a non-existent batch
    notFound = mutationQueue.lookupMutationBatch(42);
    assertNull(notFound);
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) Test(org.junit.Test)

Example 40 with MutationBatch

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

the class QueryEngineTestCase method addMutation.

/**
 * Adds a mutation to the mutation queue.
 */
protected void addMutation(Mutation mutation) {
    persistence.runTransaction("addMutation", () -> {
        MutationBatch batch = mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), Collections.singletonList(mutation));
        Map<DocumentKey, Mutation> overlayMap = Collections.singletonMap(mutation.getKey(), mutation);
        documentOverlayCache.saveOverlays(batch.getBatchId(), overlayMap);
    });
}
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) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation)

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