Search in sources :

Example 21 with Document

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

the class LocalStoreTestCase method assertNotContains.

/**
 * Asserts that the given local store does not contain the given document.
 */
private void assertNotContains(String keyPathString) {
    DocumentKey key = DocumentKey.fromPathString(keyPathString);
    Document actual = localStore.readDocument(key);
    assertFalse(actual.isValidDocument());
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 22 with Document

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

the class SQLiteQueryEngineTest method combinesIndexedWithNonIndexedResults.

@Test
public void combinesIndexedWithNonIndexedResults() throws Exception {
    MutableDocument doc1 = doc("coll/a", 1, map("foo", true));
    MutableDocument doc2 = doc("coll/b", 2, map("foo", true));
    MutableDocument doc3 = doc("coll/c", 3, map("foo", true));
    MutableDocument doc4 = doc("coll/d", 3, map("foo", true));
    indexManager.addFieldIndex(fieldIndex("coll", "foo", Kind.ASCENDING));
    addDocument(doc1);
    addDocument(doc2);
    indexManager.updateIndexEntries(docMap(doc1, doc2));
    indexManager.updateCollectionGroup("coll", IndexOffset.fromDocument(doc2));
    addDocument(doc3);
    addMutation(setMutation("coll/d", map("foo", true)));
    Query queryWithFilter = query("coll").filter(filter("foo", "==", true));
    ImmutableSortedMap<DocumentKey, Document> results = expectOptimizedCollectionScan(() -> queryEngine.getDocumentsMatchingQuery(queryWithFilter, SnapshotVersion.NONE, DocumentKey.emptyKeySet()));
    assertTrue(results.containsKey(doc1.getKey()));
    assertTrue(results.containsKey(doc2.getKey()));
    assertTrue(results.containsKey(doc3.getKey()));
    assertTrue(results.containsKey(doc4.getKey()));
}
Also used : Query(com.google.firebase.firestore.core.Query) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 23 with Document

use of com.google.firebase.firestore.model.Document 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)

Example 24 with Document

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

the class SQLiteIndexManager method updateIndexEntries.

@Override
public void updateIndexEntries(ImmutableSortedMap<DocumentKey, Document> documents) {
    hardAssert(started, "IndexManager not started");
    for (Map.Entry<DocumentKey, Document> entry : documents) {
        Collection<FieldIndex> fieldIndexes = getFieldIndexes(entry.getKey().getCollectionGroup());
        for (FieldIndex fieldIndex : fieldIndexes) {
            SortedSet<IndexEntry> existingEntries = getExistingIndexEntries(entry.getKey(), fieldIndex);
            SortedSet<IndexEntry> newEntries = computeIndexEntries(entry.getValue(), fieldIndex);
            if (!existingEntries.equals(newEntries)) {
                updateEntries(entry.getValue(), existingEntries, newEntries);
            }
        }
    }
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) DocumentKey(com.google.firebase.firestore.model.DocumentKey) IndexEntry(com.google.firebase.firestore.index.IndexEntry) Document(com.google.firebase.firestore.model.Document) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap)

Example 25 with Document

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

the class MemoryLruReferenceDelegate method removeOrphanedDocuments.

@Override
public int removeOrphanedDocuments(long upperBound) {
    MemoryRemoteDocumentCache cache = persistence.getRemoteDocumentCache();
    List<DocumentKey> docsToRemove = new ArrayList<>();
    for (Document doc : cache.getDocuments()) {
        DocumentKey key = doc.getKey();
        if (!isPinned(key, upperBound)) {
            docsToRemove.add(key);
            orphanedSequenceNumbers.remove(key);
        }
    }
    cache.removeAll(docsToRemove);
    return docsToRemove.size();
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) Document(com.google.firebase.firestore.model.Document)

Aggregations

Document (com.google.firebase.firestore.model.Document)29 DocumentKey (com.google.firebase.firestore.model.DocumentKey)23 MutableDocument (com.google.firebase.firestore.model.MutableDocument)21 Map (java.util.Map)8 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)4 Query (com.google.firebase.firestore.core.Query)3 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 TargetChange (com.google.firebase.firestore.remote.TargetChange)3 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 Task (com.google.android.gms.tasks.Task)2 Timestamp (com.google.firebase.Timestamp)2 View (com.google.firebase.firestore.core.View)2 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)2 DocumentSet (com.google.firebase.firestore.model.DocumentSet)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)2