Search in sources :

Example 1 with View

use of com.google.firebase.firestore.core.View in project firebase-android-sdk by firebase.

the class DocumentChangeTest method validatePositions.

private static void validatePositions(com.google.firebase.firestore.core.Query query, Collection<MutableDocument> initialDocsList, Collection<MutableDocument> addedList, Collection<MutableDocument> modifiedList, Collection<MutableDocument> removedList) {
    ImmutableSortedMap<DocumentKey, Document> initialDocs = docUpdates(initialDocsList.toArray(new MutableDocument[] {}));
    ImmutableSortedMap<DocumentKey, Document> updates = emptyDocumentMap();
    for (MutableDocument doc : addedList) {
        updates = updates.insert(doc.getKey(), doc);
    }
    for (MutableDocument doc : modifiedList) {
        updates = updates.insert(doc.getKey(), doc);
    }
    for (MutableDocument doc : removedList) {
        updates = updates.insert(doc.getKey(), doc);
    }
    View view = new View(query, DocumentKey.emptyKeySet());
    View.DocumentChanges initialChanges = view.computeDocChanges(initialDocs);
    TargetChange initialTargetChange = ackTarget(initialDocsList.toArray(new MutableDocument[] {}));
    ViewSnapshot initialSnapshot = view.applyChanges(initialChanges, initialTargetChange).getSnapshot();
    View.DocumentChanges updateChanges = view.computeDocChanges(updates);
    TargetChange updateTargetChange = targetChange(ByteString.EMPTY, true, addedList, modifiedList, removedList);
    ViewSnapshot updatedSnapshot = view.applyChanges(updateChanges, updateTargetChange).getSnapshot();
    if (updatedSnapshot == null) {
        // Nothing changed, no positions to verify
        return;
    }
    List<Document> expected = new ArrayList<>(updatedSnapshot.getDocuments().toList());
    List<Document> actual = new ArrayList<>(initialSnapshot.getDocuments().toList());
    FirebaseFirestore firestore = mock(FirebaseFirestore.class);
    List<DocumentChange> changes = DocumentChange.changesFromSnapshot(firestore, MetadataChanges.EXCLUDE, updatedSnapshot);
    for (DocumentChange change : changes) {
        if (change.getType() != Type.ADDED) {
            actual.remove(change.getOldIndex());
        }
        if (change.getType() != Type.REMOVED) {
            actual.add(change.getNewIndex(), change.getDocument().getDocument());
        }
    }
    assertEquals(expected, actual);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) View(com.google.firebase.firestore.core.View) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) TargetChange(com.google.firebase.firestore.remote.TargetChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 2 with View

use of com.google.firebase.firestore.core.View in project firebase-android-sdk by firebase.

the class QueryEngineTestCase method runQuery.

private DocumentSet runQuery(Query query, SnapshotVersion lastLimboFreeSnapshotVersion) {
    Preconditions.checkNotNull(expectFullCollectionScan, "Encountered runQuery() call not wrapped in expectOptimizedCollectionQuery()/expectFullCollectionQuery()");
    ImmutableSortedMap<DocumentKey, Document> docs = queryEngine.getDocumentsMatchingQuery(query, lastLimboFreeSnapshotVersion, targetCache.getMatchingKeysForTargetId(TEST_TARGET_ID));
    View view = new View(query, new ImmutableSortedSet<>(Collections.emptyList(), DocumentKey::compareTo));
    View.DocumentChanges viewDocChanges = view.computeDocChanges(docs);
    return view.applyChanges(viewDocChanges).getSnapshot().getDocuments();
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) View(com.google.firebase.firestore.core.View)

Aggregations

View (com.google.firebase.firestore.core.View)2 Document (com.google.firebase.firestore.model.Document)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)2 MutableDocument (com.google.firebase.firestore.model.MutableDocument)2 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)1 TargetChange (com.google.firebase.firestore.remote.TargetChange)1 ArrayList (java.util.ArrayList)1