Search in sources :

Example 1 with IndexType

use of com.google.firebase.firestore.local.IndexManager.IndexType 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)

Aggregations

Target (com.google.firebase.firestore.core.Target)1 IndexType (com.google.firebase.firestore.local.IndexManager.IndexType)1 Document (com.google.firebase.firestore.model.Document)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 IndexOffset (com.google.firebase.firestore.model.FieldIndex.IndexOffset)1 Nullable (javax.annotation.Nullable)1