Search in sources :

Example 16 with DocumentSet

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

the class QueryEngineTestCase method doesNotUseInitialResultsForLimitQueryWhenLastDocumentHasPendingWrite.

@Test
public void doesNotUseInitialResultsForLimitQueryWhenLastDocumentHasPendingWrite() throws Exception {
    Query query = query("coll").filter(filter("matches", "==", true)).orderBy(orderBy("order", "desc")).limitToFirst(1);
    // Add a query mapping for a document that matches, but that sorts below another document due to
    // a pending write.
    addDocumentWithEventVersion(version(1), MATCHING_DOC_A);
    addMutation(DOC_A_EMPTY_PATCH);
    persistQueryMapping(MATCHING_DOC_A.getKey());
    addDocument(MATCHING_DOC_B);
    DocumentSet docs = expectFullCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
    assertEquals(docSet(query.comparator(), MATCHING_DOC_B), docs);
}
Also used : Query(com.google.firebase.firestore.core.Query) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 17 with DocumentSet

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

the class QueryEngineTestCase method includesChangesSinceInitialResults.

@Test
public void includesChangesSinceInitialResults() throws Exception {
    Query query = query("coll").filter(filter("matches", "==", true));
    addDocument(MATCHING_DOC_A, MATCHING_DOC_B);
    persistQueryMapping(MATCHING_DOC_A.getKey(), MATCHING_DOC_B.getKey());
    DocumentSet docs = expectOptimizedCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
    assertEquals(docSet(query.comparator(), MATCHING_DOC_A, MATCHING_DOC_B), docs);
    addDocument(UPDATED_MATCHING_DOC_B);
    docs = expectOptimizedCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
    assertEquals(docSet(query.comparator(), MATCHING_DOC_A, UPDATED_MATCHING_DOC_B), docs);
}
Also used : Query(com.google.firebase.firestore.core.Query) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 18 with DocumentSet

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

the class DocumentChange method changesFromSnapshot.

/**
 * Creates the list of document changes from a {@code ViewSnapshot}.
 */
static List<DocumentChange> changesFromSnapshot(FirebaseFirestore firestore, MetadataChanges metadataChanges, ViewSnapshot snapshot) {
    List<DocumentChange> documentChanges = new ArrayList<>();
    if (snapshot.getOldDocuments().isEmpty()) {
        // Special case the first snapshot because index calculation is easy and fast. Also all
        // changes on the first snapshot are adds so there are also no metadata-only changes to filter
        // out.
        int index = 0;
        Document lastDoc = null;
        for (DocumentViewChange change : snapshot.getChanges()) {
            Document document = change.getDocument();
            QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
            hardAssert(change.getType() == DocumentViewChange.Type.ADDED, "Invalid added event for first snapshot");
            hardAssert(lastDoc == null || snapshot.getQuery().comparator().compare(lastDoc, document) < 0, "Got added events in wrong order");
            documentChanges.add(new DocumentChange(documentSnapshot, Type.ADDED, -1, index++));
            lastDoc = document;
        }
    } else {
        // A DocumentSet that is updated incrementally as changes are applied to use to lookup the
        // index of a document.
        DocumentSet indexTracker = snapshot.getOldDocuments();
        for (DocumentViewChange change : snapshot.getChanges()) {
            if (metadataChanges == MetadataChanges.EXCLUDE && change.getType() == DocumentViewChange.Type.METADATA) {
                continue;
            }
            Document document = change.getDocument();
            QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
            int oldIndex, newIndex;
            Type type = getType(change);
            if (type != Type.ADDED) {
                oldIndex = indexTracker.indexOf(document.getKey());
                hardAssert(oldIndex >= 0, "Index for document not found");
                indexTracker = indexTracker.remove(document.getKey());
            } else {
                oldIndex = -1;
            }
            if (type != Type.REMOVED) {
                indexTracker = indexTracker.add(document);
                newIndex = indexTracker.indexOf(document.getKey());
                hardAssert(newIndex >= 0, "Index for document not found");
            } else {
                newIndex = -1;
            }
            documentChanges.add(new DocumentChange(documentSnapshot, type, oldIndex, newIndex));
        }
    }
    return documentChanges;
}
Also used : DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) ArrayList(java.util.ArrayList) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Document(com.google.firebase.firestore.model.Document)

Aggregations

DocumentSet (com.google.firebase.firestore.model.DocumentSet)18 Test (org.junit.Test)14 Query (com.google.firebase.firestore.core.Query)12 DocumentViewChange (com.google.firebase.firestore.core.DocumentViewChange)3 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)2 Document (com.google.firebase.firestore.model.Document)2 MutableDocument (com.google.firebase.firestore.model.MutableDocument)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)1 SyncState (com.google.firebase.firestore.core.ViewSnapshot.SyncState)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1