Search in sources :

Example 31 with DocumentKey

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

the class MemoryRemoteDocumentCache method removeAll.

@Override
public void removeAll(Collection<DocumentKey> keys) {
    hardAssert(indexManager != null, "setIndexManager() not called");
    ImmutableSortedMap<DocumentKey, Document> deletedDocs = emptyDocumentMap();
    for (DocumentKey key : keys) {
        docs = docs.remove(key);
        deletedDocs = deletedDocs.insert(key, MutableDocument.newNoDocument(key, SnapshotVersion.NONE));
    }
    indexManager.updateIndexEntries(deletedDocs);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 32 with DocumentKey

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

the class MemoryRemoteDocumentCache method getAll.

@Override
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
    Map<DocumentKey, MutableDocument> result = new HashMap<>();
    // Documents are ordered by key, so we can use a prefix scan to narrow down the documents
    // we need to match the query against.
    DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
    Iterator<Map.Entry<DocumentKey, Document>> iterator = docs.iteratorFrom(prefix);
    while (iterator.hasNext()) {
        Map.Entry<DocumentKey, Document> entry = iterator.next();
        Document doc = entry.getValue();
        DocumentKey key = entry.getKey();
        if (!collection.isPrefixOf(key.getPath())) {
            // We are now scanning the next collection. Abort.
            break;
        }
        if (key.getPath().length() > collection.length() + 1) {
            // Exclude entries from subcollections.
            continue;
        }
        if (IndexOffset.fromDocument(doc).compareTo(offset) <= 0) {
            // The document sorts before the offset.
            continue;
        }
        result.put(doc.getKey(), doc.mutableCopy());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 33 with DocumentKey

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

the class MemoryTargetCache method removeMatchingKeys.

@Override
public void removeMatchingKeys(ImmutableSortedSet<DocumentKey> keys, int targetId) {
    references.removeReferences(keys, targetId);
    ReferenceDelegate referenceDelegate = persistence.getReferenceDelegate();
    for (DocumentKey key : keys) {
        referenceDelegate.removeReference(key);
    }
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 34 with DocumentKey

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

the class QueryEngine method performQueryUsingIndex.

/**
 * Performs an indexed query that evaluates the query based on a collection's persisted index
 * values. Returns {@code null} if an index is not available.
 */
@Nullable
private ImmutableSortedMap<DocumentKey, Document> performQueryUsingIndex(Query query) {
    if (query.matchesAllDocuments()) {
        // Don't use indexes for queries that can be executed by scanning the collection.
        return null;
    }
    Target target = query.toTarget();
    IndexType indexType = indexManager.getIndexType(target);
    if (indexType.equals(IndexType.NONE)) {
        // The target cannot be served from any index.
        return null;
    }
    if (query.hasLimit() && indexType.equals(IndexType.PARTIAL)) {
        // in such cases.
        return performQueryUsingIndex(query.limitToFirst(Target.NO_LIMIT));
    }
    List<DocumentKey> keys = indexManager.getDocumentsMatchingTarget(target);
    hardAssert(keys != null, "index manager must return results for partial and full indexes.");
    ImmutableSortedMap<DocumentKey, Document> indexedDocuments = localDocumentsView.getDocuments(keys);
    IndexOffset offset = indexManager.getMinOffset(target);
    ImmutableSortedSet<Document> previousResults = applyQuery(query, indexedDocuments);
    if (needsRefill(query, keys.size(), previousResults, offset.getReadTime())) {
        // incorporated.
        return performQueryUsingIndex(query.limitToFirst(Target.NO_LIMIT));
    }
    return appendRemainingResults(previousResults, query, offset);
}
Also used : Target(com.google.firebase.firestore.core.Target) DocumentKey(com.google.firebase.firestore.model.DocumentKey) IndexOffset(com.google.firebase.firestore.model.FieldIndex.IndexOffset) IndexType(com.google.firebase.firestore.local.IndexManager.IndexType) Document(com.google.firebase.firestore.model.Document) Nullable(javax.annotation.Nullable)

Example 35 with DocumentKey

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

the class ReferenceSet method containsKey.

public boolean containsKey(DocumentKey key) {
    DocumentReference ref = new DocumentReference(key, 0);
    Iterator<DocumentReference> iterator = referencesByKey.iteratorFrom(ref);
    if (!iterator.hasNext()) {
        return false;
    }
    DocumentKey firstKey = iterator.next().getKey();
    return firstKey.equals(key);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)139 MutableDocument (com.google.firebase.firestore.model.MutableDocument)53 Test (org.junit.Test)36 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)24 Document (com.google.firebase.firestore.model.Document)20 Mutation (com.google.firebase.firestore.model.mutation.Mutation)18 Map (java.util.Map)18 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)10 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 HashSet (java.util.HashSet)10 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)8 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)8 ByteString (com.google.protobuf.ByteString)7 Target (com.google.firebase.firestore.core.Target)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)6