Search in sources :

Example 96 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument 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(), FieldMask.EMPTY, 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 97 with MutableDocument

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

the class LocalDocumentsView method getDocument.

/**
 * Returns the the local view of the document identified by {@code key}.
 *
 * @return Local view of the document or a an invalid document if we don't have any cached state
 *     for it.
 */
Document getDocument(DocumentKey key) {
    Overlay overlay = documentOverlayCache.getOverlay(key);
    MutableDocument document = getBaseDocument(key, overlay);
    if (overlay != null) {
        overlay.getMutation().applyToLocalView(document, FieldMask.EMPTY, Timestamp.now());
    }
    return document;
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 98 with MutableDocument

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

the class LocalDocumentsView method computeViews.

/**
 * Computes the local view for the given documents.
 *
 * @param docs The documents to compute views for. It also has the base version of the documents.
 * @param overlays The overlays that need to be applied to the given base version of the
 *     documents.
 * @param existenceStateChanged A set of documents whose existence states might have changed. This
 *     is used to determine if we need to re-calculate overlays from mutation queues.
 * @return A map represents the local documents view.
 */
private Map<DocumentKey, OverlayedDocument> computeViews(Map<DocumentKey, MutableDocument> docs, Map<DocumentKey, Overlay> overlays, Set<DocumentKey> existenceStateChanged) {
    Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
    Map<DocumentKey, FieldMask> mutatedFields = 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) {
            mutatedFields.put(doc.getKey(), overlay.getMutation().getFieldMask());
            overlay.getMutation().applyToLocalView(doc, overlay.getMutation().getFieldMask(), Timestamp.now());
        }
    }
    Map<DocumentKey, FieldMask> recalculatedFields = recalculateAndSaveOverlays(recalculateDocuments);
    mutatedFields.putAll(recalculatedFields);
    Map<DocumentKey, OverlayedDocument> result = new HashMap<>();
    for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
        result.put(entry.getKey(), new OverlayedDocument(entry.getValue(), mutatedFields.get(entry.getKey())));
    }
    return result;
}
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) 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) FieldMask(com.google.firebase.firestore.model.mutation.FieldMask)

Example 99 with MutableDocument

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

the class LocalSerializer method decodeDocument.

/**
 * Decodes a Document proto to the equivalent model.
 */
private MutableDocument decodeDocument(com.google.firestore.v1.Document document, boolean hasCommittedMutations) {
    DocumentKey key = rpcSerializer.decodeKey(document.getName());
    SnapshotVersion version = rpcSerializer.decodeVersion(document.getUpdateTime());
    MutableDocument result = MutableDocument.newFoundDocument(key, version, ObjectValue.fromMap(document.getFieldsMap()));
    return hasCommittedMutations ? result.setHasCommittedMutations() : result;
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 100 with MutableDocument

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

the class QueryListenerTest method testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges.

@Test
public void testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges() {
    List<ViewSnapshot> fullAccum = new ArrayList<>();
    Query query = Query.atPath(path("rooms"));
    MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros")).setHasLocalMutations();
    MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades")).setHasLocalMutations();
    MutableDocument doc1Prime = doc("rooms/eros", 1, map("name", "eros"));
    MutableDocument doc2Prime = doc("rooms/hades", 2, map("name", "hades"));
    MutableDocument doc3 = doc("rooms/other", 3, map("name", "other"));
    ListenOptions options = new ListenOptions();
    options.includeQueryMetadataChanges = true;
    QueryListener fullListener = queryListener(query, options, fullAccum);
    View view = new View(query, DocumentKey.emptyKeySet());
    ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
    ViewSnapshot snap2 = applyChanges(view, doc1Prime);
    ViewSnapshot snap3 = applyChanges(view, doc3);
    ViewSnapshot snap4 = applyChanges(view, doc2Prime);
    fullListener.onViewSnapshot(snap1);
    // Emits no events
    fullListener.onViewSnapshot(snap2);
    fullListener.onViewSnapshot(snap3);
    // Metadata change event
    fullListener.onViewSnapshot(snap4);
    ViewSnapshot expectedSnapshot4 = new ViewSnapshot(snap4.getQuery(), snap4.getDocuments(), snap3.getDocuments(), asList(), snap4.isFromCache(), snap4.getMutatedKeys(), snap4.didSyncStateChange(), /* excludeMetadataChanges= */
    true);
    // This test excludes document metadata changes
    assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE), expectedSnapshot4), fullAccum);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6