Search in sources :

Example 11 with DocumentSet

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

the class QueryEngineTestCase method usesTargetMappingForInitialView.

@Test
public void usesTargetMappingForInitialView() 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);
}
Also used : Query(com.google.firebase.firestore.core.Query) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 12 with DocumentSet

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

the class QueryEngineTestCase method doesNotUseInitialResultsForUnfilteredCollectionQuery.

@Test
public void doesNotUseInitialResultsForUnfilteredCollectionQuery() throws Exception {
    Query query = query("coll");
    DocumentSet docs = expectFullCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
    assertEquals(docSet(query.comparator()), docs);
}
Also used : Query(com.google.firebase.firestore.core.Query) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 13 with DocumentSet

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

the class View method computeDocChanges.

/**
 * Iterates over a set of doc changes, applies the query limit, and computes what the new results
 * should be, what the changes were, and whether we may need to go back to the local cache for
 * more results. Does not make any changes to the view.
 *
 * @param docChanges The doc changes to apply to this view.
 * @param previousChanges If this is being called with a refill, then start with this set of docs
 *     and changes instead of the current view.
 * @return a new set of docs, changes, and refill flag.
 */
public DocumentChanges computeDocChanges(ImmutableSortedMap<DocumentKey, Document> docChanges, @Nullable DocumentChanges previousChanges) {
    DocumentViewChangeSet changeSet = previousChanges != null ? previousChanges.changeSet : new DocumentViewChangeSet();
    DocumentSet oldDocumentSet = previousChanges != null ? previousChanges.documentSet : documentSet;
    ImmutableSortedSet<DocumentKey> newMutatedKeys = previousChanges != null ? previousChanges.mutatedKeys : mutatedKeys;
    DocumentSet newDocumentSet = oldDocumentSet;
    boolean needsRefill = false;
    // Track the last doc in a (full) limit. This is necessary, because some update (a delete, or an
    // update moving a doc past the old limit) might mean there is some other document in the local
    // cache that either should come (1) between the old last limit doc and the new last document,
    // in the case of updates, or (2) after the new last document, in the case of deletes. So we
    // keep this doc at the old limit to compare the updates to.
    // 
    // Note that this should never get used in a refill (when previousChanges is set), because there
    // will only be adds -- no deletes or updates.
    Document lastDocInLimit = (query.hasLimitToFirst() && oldDocumentSet.size() == query.getLimitToFirst()) ? oldDocumentSet.getLastDocument() : null;
    Document firstDocInLimit = (query.hasLimitToLast() && oldDocumentSet.size() == query.getLimitToLast()) ? oldDocumentSet.getFirstDocument() : null;
    for (Map.Entry<DocumentKey, Document> entry : docChanges) {
        DocumentKey key = entry.getKey();
        Document oldDoc = oldDocumentSet.getDocument(key);
        Document newDoc = query.matches(entry.getValue()) ? entry.getValue() : null;
        boolean oldDocHadPendingMutations = oldDoc != null && this.mutatedKeys.contains(oldDoc.getKey());
        // We only consider committed mutations for documents that were mutated during the lifetime of
        // the view.
        boolean newDocHasPendingMutations = newDoc != null && (newDoc.hasLocalMutations() || (this.mutatedKeys.contains(newDoc.getKey()) && newDoc.hasCommittedMutations()));
        boolean changeApplied = false;
        // Calculate change
        if (oldDoc != null && newDoc != null) {
            boolean docsEqual = oldDoc.getData().equals(newDoc.getData());
            if (!docsEqual) {
                if (!shouldWaitForSyncedDocument(oldDoc, newDoc)) {
                    changeSet.addChange(DocumentViewChange.create(Type.MODIFIED, newDoc));
                    changeApplied = true;
                    if ((lastDocInLimit != null && query.comparator().compare(newDoc, lastDocInLimit) > 0) || (firstDocInLimit != null && query.comparator().compare(newDoc, firstDocInLimit) < 0)) {
                        // This doc moved from inside the limit to outside the limit. That means there may be
                        // some doc in the local cache that should be included instead.
                        needsRefill = true;
                    }
                }
            } else if (oldDocHadPendingMutations != newDocHasPendingMutations) {
                changeSet.addChange(DocumentViewChange.create(Type.METADATA, newDoc));
                changeApplied = true;
            }
        } else if (oldDoc == null && newDoc != null) {
            changeSet.addChange(DocumentViewChange.create(Type.ADDED, newDoc));
            changeApplied = true;
        } else if (oldDoc != null && newDoc == null) {
            changeSet.addChange(DocumentViewChange.create(Type.REMOVED, oldDoc));
            changeApplied = true;
            if (lastDocInLimit != null || firstDocInLimit != null) {
                // A doc was removed from a full limit query. We'll need to requery from the local cache
                // to see if we know about some other doc that should be in the results.
                needsRefill = true;
            }
        }
        if (changeApplied) {
            if (newDoc != null) {
                newDocumentSet = newDocumentSet.add(newDoc);
                if (newDoc.hasLocalMutations()) {
                    newMutatedKeys = newMutatedKeys.insert(newDoc.getKey());
                } else {
                    newMutatedKeys = newMutatedKeys.remove(newDoc.getKey());
                }
            } else {
                newDocumentSet = newDocumentSet.remove(key);
                newMutatedKeys = newMutatedKeys.remove(key);
            }
        }
    }
    // Drop documents out to meet limitToFirst/limitToLast requirement.
    if (query.hasLimitToFirst() || query.hasLimitToLast()) {
        long limit = query.hasLimitToFirst() ? query.getLimitToFirst() : query.getLimitToLast();
        for (long i = newDocumentSet.size() - limit; i > 0; --i) {
            Document oldDoc = query.hasLimitToFirst() ? newDocumentSet.getLastDocument() : newDocumentSet.getFirstDocument();
            newDocumentSet = newDocumentSet.remove(oldDoc.getKey());
            newMutatedKeys = newMutatedKeys.remove(oldDoc.getKey());
            changeSet.addChange(DocumentViewChange.create(Type.REMOVED, oldDoc));
        }
    }
    hardAssert(!needsRefill || previousChanges == null, "View was refilled using docs that themselves needed refilling.");
    return new DocumentChanges(newDocumentSet, changeSet, newMutatedKeys, needsRefill);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Document(com.google.firebase.firestore.model.Document) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap)

Example 14 with DocumentSet

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

the class QueryEngineTestCase method limitQueriesUseInitialResultsIfLastDocumentInLimitIsUnchanged.

@Test
public void limitQueriesUseInitialResultsIfLastDocumentInLimitIsUnchanged() throws Exception {
    Query query = query("coll").orderBy(orderBy("order")).limitToFirst(2);
    addDocument(doc("coll/a", 1, map("order", 1)));
    addDocument(doc("coll/b", 1, map("order", 3)));
    persistQueryMapping(key("coll/a"), key("coll/b"));
    // Update "coll/a" but make sure it still sorts before "coll/b"
    addDocumentWithEventVersion(version(1), doc("coll/a", 1, map("order", 2)));
    addMutation(DOC_A_EMPTY_PATCH);
    // Since the last document in the limit didn't change (and hence we know that all documents
    // written prior to query execution still sort after "coll/b"), we should use an Index-Free
    // query.
    DocumentSet docs = expectOptimizedCollectionScan(() -> runQuery(query, LAST_LIMBO_FREE_SNAPSHOT));
    assertEquals(docSet(query.comparator(), doc("coll/a", 1, map("order", 2)).setHasLocalMutations(), doc("coll/b", 1, map("order", 3))), docs);
}
Also used : Query(com.google.firebase.firestore.core.Query) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 15 with DocumentSet

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

the class QueryEngineTestCase method doesNotUseInitialResultsForLimitToLastQueryWhenFirstDocumentHasBeenUpdatedOutOfBand.

@Test
public void doesNotUseInitialResultsForLimitToLastQueryWhenFirstDocumentHasBeenUpdatedOutOfBand() throws Exception {
    Query query = query("coll").filter(filter("matches", "==", true)).orderBy(orderBy("order", "asc")).limitToLast(1);
    // Add a query mapping for a document that matches, but that sorts below another document based
    // due to an update that the SDK received after the query's snapshot was persisted.
    addDocument(UPDATED_DOC_A);
    persistQueryMapping(UPDATED_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)

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