Search in sources :

Example 11 with Overlay

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

the class MemoryDocumentOverlayCache method getOverlays.

@Override
public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceBatchId) {
    Map<DocumentKey, Overlay> result = new HashMap<>();
    int immediateChildrenPathLength = collection.length() + 1;
    DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
    Map<DocumentKey, Overlay> view = overlays.tailMap(prefix);
    for (Overlay overlay : view.values()) {
        DocumentKey key = overlay.getKey();
        if (!collection.isPrefixOf(key.getPath())) {
            break;
        }
        // Documents from sub-collections
        if (key.getPath().length() != immediateChildrenPathLength) {
            continue;
        }
        if (overlay.getLargestBatchId() > sinceBatchId) {
            result.put(overlay.getKey(), overlay);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 12 with Overlay

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

the class LocalDocumentsView method computeViews.

/*Computes the local view for doc */
private ImmutableSortedMap<DocumentKey, Document> computeViews(Map<DocumentKey, MutableDocument> docs, Map<DocumentKey, Overlay> overlays, Set<DocumentKey> existenceStateChanged) {
    ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
    Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
    for (MutableDocument doc : docs.values()) {
        Overlay overlay = overlays.get(doc.getKey());
        // but would now match.
        if (existenceStateChanged.contains(doc.getKey()) && (overlay == null || overlay.getMutation() instanceof PatchMutation)) {
            recalculateDocuments.put(doc.getKey(), doc);
        } else if (overlay != null) {
            overlay.getMutation().applyToLocalView(doc, null, Timestamp.now());
        }
    }
    recalculateAndSaveOverlays(recalculateDocuments);
    for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
        results = results.insert(entry.getKey(), entry.getValue());
    }
    return results;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Overlay(com.google.firebase.firestore.model.mutation.Overlay) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 13 with Overlay

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

the class LocalDocumentsView method getDocumentsMatchingCollectionQuery.

private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionQuery(Query query, IndexOffset offset) {
    Map<DocumentKey, MutableDocument> remoteDocuments = remoteDocumentCache.getAll(query.getPath(), offset);
    Map<DocumentKey, Overlay> overlays = documentOverlayCache.getOverlays(query.getPath(), offset.getLargestBatchId());
    // for all overlays in the initial document set.
    for (Map.Entry<DocumentKey, Overlay> entry : overlays.entrySet()) {
        if (!remoteDocuments.containsKey(entry.getKey())) {
            remoteDocuments.put(entry.getKey(), MutableDocument.newInvalidDocument(entry.getKey()));
        }
    }
    // Apply the overlays and match against the query.
    ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
    for (Map.Entry<DocumentKey, MutableDocument> docEntry : remoteDocuments.entrySet()) {
        Overlay overlay = overlays.get(docEntry.getKey());
        if (overlay != null) {
            overlay.getMutation().applyToLocalView(docEntry.getValue(), null, Timestamp.now());
        }
        // Finally, insert the documents that still match the query
        if (query.matches(docEntry.getValue())) {
            results = results.insert(docEntry.getKey(), docEntry.getValue());
        }
    }
    return results;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Overlay(com.google.firebase.firestore.model.mutation.Overlay) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 14 with Overlay

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

the class DocumentOverlayCacheTestCase method testGetAllOverlaysForCollection.

@Test
public void testGetAllOverlaysForCollection() {
    Mutation m1 = patchMutation("coll/doc1", map("foo", "bar"));
    Mutation m2 = setMutation("coll/doc2", map("foo", "bar"));
    Mutation m3 = deleteMutation("coll/doc3");
    // m4 and m5 are not under "coll"
    Mutation m4 = setMutation("coll/doc1/sub/sub_doc", map("foo", "bar"));
    Mutation m5 = setMutation("other/doc1", map("foo", "bar"));
    saveOverlays(3, m1, m2, m3, m4, m5);
    Map<DocumentKey, Overlay> overlays = cache.getOverlays(path("coll"), -1);
    verifyOverlayContains(overlays, "coll/doc1", "coll/doc2", "coll/doc3");
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Overlay(com.google.firebase.firestore.model.mutation.Overlay) Test(org.junit.Test)

Aggregations

Overlay (com.google.firebase.firestore.model.mutation.Overlay)14 DocumentKey (com.google.firebase.firestore.model.DocumentKey)10 HashMap (java.util.HashMap)7 MutableDocument (com.google.firebase.firestore.model.MutableDocument)4 Document (com.google.firebase.firestore.model.Document)3 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Test (org.junit.Test)3 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 Executor (java.util.concurrent.Executor)1