Search in sources :

Example 46 with DocumentKey

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

the class ViewTest method testAddsDocumentsBasedOnQuery.

@Test
public void testAddsDocumentsBasedOnQuery() {
    Query query = messageQuery();
    View view = new View(query, DocumentKey.emptyKeySet());
    MutableDocument doc1 = doc("rooms/eros/messages/1", 0, map("text", "msg1"));
    MutableDocument doc2 = doc("rooms/eros/messages/2", 0, map("text", "msg2"));
    MutableDocument doc3 = doc("rooms/other/messages/1", 0, map("text", "msg3"));
    ImmutableSortedMap<DocumentKey, Document> updates = docUpdates(doc1, doc2, doc3);
    View.DocumentChanges docViewChanges = view.computeDocChanges(updates);
    TargetChange targetChange = ackTarget(doc1, doc2, doc3);
    ViewSnapshot snapshot = view.applyChanges(docViewChanges, targetChange).getSnapshot();
    assertEquals(query, snapshot.getQuery());
    assertEquals(asList(doc1, doc2), snapshot.getDocuments().toList());
    assertEquals(asList(DocumentViewChange.create(Type.ADDED, doc1), DocumentViewChange.create(Type.ADDED, doc2)), snapshot.getChanges());
    assertFalse(snapshot.isFromCache());
    assertTrue(snapshot.didSyncStateChange());
    assertFalse(snapshot.hasPendingWrites());
}
Also used : TargetChange(com.google.firebase.firestore.remote.TargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 47 with DocumentKey

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

the class SQLiteIndexManagerTest method verifyResults.

private void verifyResults(Query query, String... documents) {
    Target target = query.toTarget();
    List<DocumentKey> results = indexManager.getDocumentsMatchingTarget(target);
    assertNotNull("Target cannot be served from index.", results);
    List<DocumentKey> keys = Arrays.stream(documents).map(TestUtil::key).collect(Collectors.toList());
    assertWithMessage("Result for %s", query).that(results).containsExactlyElementsIn(keys).inOrder();
}
Also used : Target(com.google.firebase.firestore.core.Target) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 48 with DocumentKey

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

the class LocalStoreTestCase method testMutationBatchKeys.

@Test
public void testMutationBatchKeys() {
    SetMutation set1 = setMutation("foo/bar", map("foo", "bar"));
    SetMutation set2 = setMutation("foo/baz", map("foo", "baz"));
    MutationBatch batch = new MutationBatch(1, Timestamp.now(), Collections.emptyList(), asList(set1, set2));
    Set<DocumentKey> keys = batch.getKeys();
    assertEquals(2, keys.size());
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) Test(org.junit.Test)

Example 49 with DocumentKey

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

the class LocalStoreTestCase method testRemoteDocumentKeysForTarget.

@Test
public void testRemoteDocumentKeysForTarget() {
    Query query = query("foo");
    allocateQuery(query);
    assertTargetId(2);
    applyRemoteEvent(addedRemoteEvent(doc("foo/baz", 10, map("a", "b")), asList(2), emptyList()));
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 20, map("a", "b")), asList(2), emptyList()));
    writeMutation(setMutation("foo/bonk", map("a", "b")));
    ImmutableSortedSet<DocumentKey> keys = localStore.getRemoteDocumentKeys(2);
    assertSetEquals(asList(key("foo/bar"), key("foo/baz")), keys);
    keys = localStore.getRemoteDocumentKeys(2);
    assertSetEquals(asList(key("foo/bar"), key("foo/baz")), keys);
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Test(org.junit.Test)

Example 50 with DocumentKey

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

the class LruGarbageCollectorTestCase method testRemoveOrphanedDocuments.

@Test
public void testRemoveOrphanedDocuments() {
    // Track documents we expect to be retained so we can verify post-GC.
    // This will contain documents associated with targets that survive GC, as well
    // as any documents with pending mutations.
    Set<DocumentKey> expectedRetained = new HashSet<>();
    // we add two mutations later, for now track them in an array.
    List<Mutation> mutations = new ArrayList<>();
    persistence.runTransaction("add a target and add two documents to it", () -> {
        // Add two documents to first target, queue a mutation on the second document
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc1 = cacheADocumentInTransaction();
        addDocumentToTarget(doc1.getKey(), targetData.getTargetId());
        expectedRetained.add(doc1.getKey());
        MutableDocument doc2 = cacheADocumentInTransaction();
        addDocumentToTarget(doc2.getKey(), targetData.getTargetId());
        expectedRetained.add(doc2.getKey());
        mutations.add(mutation(doc2.getKey()));
    });
    // Add a second query and register a third document on it
    persistence.runTransaction("second query", () -> {
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc3 = cacheADocumentInTransaction();
        addDocumentToTarget(doc3.getKey(), targetData.getTargetId());
        expectedRetained.add(doc3.getKey());
    });
    // cache another document and prepare a mutation on it.
    persistence.runTransaction("queue a mutation", () -> {
        MutableDocument doc4 = cacheADocumentInTransaction();
        mutations.add(mutation(doc4.getKey()));
        expectedRetained.add(doc4.getKey());
    });
    // Insert the mutations. These operations don't have a sequence number, they just
    // serve to keep the mutated documents from being GC'd while the mutations are outstanding.
    persistence.runTransaction("actually register the mutations", () -> {
        Timestamp writeTime = Timestamp.now();
        mutationQueue.addMutationBatch(writeTime, Collections.emptyList(), mutations);
    });
    // Mark 5 documents eligible for GC. This simulates documents that were mutated then ack'd.
    // Since they were ack'd, they are no longer in a mutation queue, and there is nothing keeping
    // them alive.
    Set<DocumentKey> toBeRemoved = new HashSet<>();
    persistence.runTransaction("add orphaned docs (previously mutated, then ack'd)", () -> {
        for (int i = 0; i < 5; i++) {
            MutableDocument doc = cacheADocumentInTransaction();
            toBeRemoved.add(doc.getKey());
            markDocumentEligibleForGcInTransaction(doc.getKey());
        }
    });
    // We expect only the orphaned documents, those not in a mutation or a target, to be removed.
    // use a large sequence number to remove as much as possible
    int removed = garbageCollector.removeOrphanedDocuments(1000);
    assertEquals(toBeRemoved.size(), removed);
    persistence.runTransaction("verify", () -> {
        for (DocumentKey key : toBeRemoved) {
            assertFalse(documentCache.get(key).isValidDocument());
            assertFalse(targetCache.containsKey(key));
        }
        for (DocumentKey key : expectedRetained) {
            assertTrue(documentCache.get(key).isValidDocument());
        }
    });
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) Timestamp(com.google.firebase.Timestamp) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)139 MutableDocument (com.google.firebase.firestore.model.MutableDocument)53 Test (org.junit.Test)36 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)24 Document (com.google.firebase.firestore.model.Document)20 Mutation (com.google.firebase.firestore.model.mutation.Mutation)18 Map (java.util.Map)18 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)10 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 HashSet (java.util.HashSet)10 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)8 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)8 ByteString (com.google.protobuf.ByteString)7 Target (com.google.firebase.firestore.core.Target)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)6