Search in sources :

Example 26 with Document

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

the class SQLiteRemoteDocumentCache method removeAll.

@Override
public void removeAll(Collection<DocumentKey> keys) {
    if (keys.isEmpty())
        return;
    List<Object> encodedPaths = new ArrayList<>();
    ImmutableSortedMap<DocumentKey, Document> deletedDocs = emptyDocumentMap();
    for (DocumentKey key : keys) {
        encodedPaths.add(EncodedPath.encode(key.getPath()));
        deletedDocs = deletedDocs.insert(key, MutableDocument.newNoDocument(key, SnapshotVersion.NONE));
    }
    SQLitePersistence.LongQuery longQuery = new SQLitePersistence.LongQuery(db, "DELETE FROM remote_documents WHERE path IN (", encodedPaths, ")");
    while (longQuery.hasMoreSubqueries()) {
        longQuery.executeNextSubquery();
    }
    indexManager.updateIndexEntries(deletedDocs);
}
Also used : ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 27 with Document

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

the class LocalStore method executeQuery.

/**
 * Runs the specified query against the local store and returns the results, potentially taking
 * advantage of query data from previous executions (such as the set of remote keys).
 *
 * @param usePreviousResults Whether results from previous executions can be used to optimize this
 *     query execution.
 */
public QueryResult executeQuery(Query query, boolean usePreviousResults) {
    TargetData targetData = getTargetData(query.toTarget());
    SnapshotVersion lastLimboFreeSnapshotVersion = SnapshotVersion.NONE;
    ImmutableSortedSet<DocumentKey> remoteKeys = DocumentKey.emptyKeySet();
    if (targetData != null) {
        lastLimboFreeSnapshotVersion = targetData.getLastLimboFreeSnapshotVersion();
        remoteKeys = this.targetCache.getMatchingKeysForTargetId(targetData.getTargetId());
    }
    ImmutableSortedMap<DocumentKey, Document> documents = queryEngine.getDocumentsMatchingQuery(query, usePreviousResults ? lastLimboFreeSnapshotVersion : SnapshotVersion.NONE, remoteKeys);
    return new QueryResult(documents, remoteKeys);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 28 with Document

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

the class LocalDocumentsView method computeViews.

/*Computes the local view for doc */
private ImmutableSortedMap<DocumentKey, Document> computeViews(Map<DocumentKey, MutableDocument> docs, Map<DocumentKey, Overlay> overlays, Set<DocumentKey> existenceStateChanged) {
    ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
    Map<DocumentKey, MutableDocument> recalculateDocuments = 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) {
            overlay.getMutation().applyToLocalView(doc, null, Timestamp.now());
        }
    }
    recalculateAndSaveOverlays(recalculateDocuments);
    for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
        results = results.insert(entry.getKey(), entry.getValue());
    }
    return results;
}
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) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) 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)

Example 29 with Document

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

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