package com.google.firebase.firestore.local;

import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.core.Query;
import com.google.firebase.firestore.core.Target;
import com.google.firebase.firestore.local.IndexManager;
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.FieldIndex;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.Logger;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes3.dex */
public class QueryEngine {
    private static final int DEFAULT_INDEX_AUTO_CREATION_MIN_COLLECTION_SIZE = 100;
    private static final double DEFAULT_RELATIVE_INDEX_READ_COST_PER_DOCUMENT = 2.0d;
    private static final String LOG_TAG = "QueryEngine";
    private IndexManager indexManager;
    private boolean initialized;
    private LocalDocumentsView localDocumentsView;
    private boolean indexAutoCreationEnabled = false;
    private int indexAutoCreationMinCollectionSize = 100;
    private double relativeIndexReadCostPerDocument = DEFAULT_RELATIVE_INDEX_READ_COST_PER_DOCUMENT;

    private ImmutableSortedMap<DocumentKey, Document> appendRemainingResults(Iterable<Document> iterable, Query query, FieldIndex.IndexOffset indexOffset) {
        ImmutableSortedMap<DocumentKey, Document> documentsMatchingQuery = this.localDocumentsView.getDocumentsMatchingQuery(query, indexOffset);
        for (Document document : iterable) {
            documentsMatchingQuery = documentsMatchingQuery.insert(document.getKey(), document);
        }
        return documentsMatchingQuery;
    }

    private ImmutableSortedSet<Document> applyQuery(Query query, ImmutableSortedMap<DocumentKey, Document> immutableSortedMap) {
        ImmutableSortedSet<Document> immutableSortedSet = new ImmutableSortedSet<>(Collections.EMPTY_LIST, query.comparator());
        Iterator<Map.Entry<DocumentKey, Document>> it = immutableSortedMap.iterator();
        while (it.hasNext()) {
            Document value = it.next().getValue();
            if (query.matches(value)) {
                immutableSortedSet = immutableSortedSet.insert(value);
            }
        }
        return immutableSortedSet;
    }

    private void createCacheIndexes(Query query, QueryContext queryContext, int i6) {
        if (queryContext.getDocumentReadCount() < this.indexAutoCreationMinCollectionSize) {
            Logger.debug(LOG_TAG, "SDK will not create cache indexes for query: %s, since it only creates cache indexes for collection contains more than or equal to %s documents.", query.toString(), Integer.valueOf(this.indexAutoCreationMinCollectionSize));
            return;
        }
        Logger.debug(LOG_TAG, "Query: %s, scans %s local documents and returns %s documents as results.", query.toString(), Integer.valueOf(queryContext.getDocumentReadCount()), Integer.valueOf(i6));
        if (queryContext.getDocumentReadCount() > this.relativeIndexReadCostPerDocument * ((double) i6)) {
            this.indexManager.createTargetIndexes(query.toTarget());
            Logger.debug(LOG_TAG, "The SDK decides to create cache indexes for query: %s, as using cache indexes may help improve performance.", query.toString());
        }
    }

    private ImmutableSortedMap<DocumentKey, Document> executeFullCollectionScan(Query query, QueryContext queryContext) {
        if (Logger.isDebugEnabled()) {
            Logger.debug(LOG_TAG, "Using full collection scan to execute query: %s", query.toString());
        }
        return this.localDocumentsView.getDocumentsMatchingQuery(query, FieldIndex.IndexOffset.NONE, queryContext);
    }

    private boolean needsRefill(Query query, int i6, ImmutableSortedSet<Document> immutableSortedSet, SnapshotVersion snapshotVersion) {
        if (!query.hasLimit()) {
            return false;
        }
        if (i6 != immutableSortedSet.size()) {
            return true;
        }
        Document maxEntry = query.getLimitType() == Query.LimitType.LIMIT_TO_FIRST ? immutableSortedSet.getMaxEntry() : immutableSortedSet.getMinEntry();
        if (maxEntry == null) {
            return false;
        }
        return maxEntry.hasPendingWrites() || maxEntry.getVersion().compareTo(snapshotVersion) > 0;
    }

    private ImmutableSortedMap<DocumentKey, Document> performQueryUsingIndex(Query query) {
        if (query.matchesAllDocuments()) {
            return null;
        }
        Target target = query.toTarget();
        IndexManager.IndexType indexType = this.indexManager.getIndexType(target);
        if (indexType.equals(IndexManager.IndexType.NONE)) {
            return null;
        }
        if (query.hasLimit() && indexType.equals(IndexManager.IndexType.PARTIAL)) {
            return performQueryUsingIndex(query.limitToFirst(-1L));
        }
        List<DocumentKey> documentsMatchingTarget = this.indexManager.getDocumentsMatchingTarget(target);
        Assert.hardAssert(documentsMatchingTarget != null, "index manager must return results for partial and full indexes.", new Object[0]);
        ImmutableSortedMap<DocumentKey, Document> documents = this.localDocumentsView.getDocuments(documentsMatchingTarget);
        FieldIndex.IndexOffset minOffset = this.indexManager.getMinOffset(target);
        ImmutableSortedSet<Document> immutableSortedSetApplyQuery = applyQuery(query, documents);
        return needsRefill(query, documentsMatchingTarget.size(), immutableSortedSetApplyQuery, minOffset.getReadTime()) ? performQueryUsingIndex(query.limitToFirst(-1L)) : appendRemainingResults(immutableSortedSetApplyQuery, query, minOffset);
    }

    private ImmutableSortedMap<DocumentKey, Document> performQueryUsingRemoteKeys(Query query, ImmutableSortedSet<DocumentKey> immutableSortedSet, SnapshotVersion snapshotVersion) {
        if (query.matchesAllDocuments() || snapshotVersion.equals(SnapshotVersion.NONE)) {
            return null;
        }
        ImmutableSortedSet<Document> immutableSortedSetApplyQuery = applyQuery(query, this.localDocumentsView.getDocuments(immutableSortedSet));
        if (needsRefill(query, immutableSortedSet.size(), immutableSortedSetApplyQuery, snapshotVersion)) {
            return null;
        }
        if (Logger.isDebugEnabled()) {
            Logger.debug(LOG_TAG, "Re-using previous result from %s to execute query: %s", snapshotVersion.toString(), query.toString());
        }
        return appendRemainingResults(immutableSortedSetApplyQuery, query, FieldIndex.IndexOffset.createSuccessor(snapshotVersion, -1));
    }

    public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(Query query, SnapshotVersion snapshotVersion, ImmutableSortedSet<DocumentKey> immutableSortedSet) {
        Assert.hardAssert(this.initialized, "initialize() not called", new Object[0]);
        ImmutableSortedMap<DocumentKey, Document> immutableSortedMapPerformQueryUsingIndex = performQueryUsingIndex(query);
        if (immutableSortedMapPerformQueryUsingIndex != null) {
            return immutableSortedMapPerformQueryUsingIndex;
        }
        ImmutableSortedMap<DocumentKey, Document> immutableSortedMapPerformQueryUsingRemoteKeys = performQueryUsingRemoteKeys(query, immutableSortedSet, snapshotVersion);
        if (immutableSortedMapPerformQueryUsingRemoteKeys != null) {
            return immutableSortedMapPerformQueryUsingRemoteKeys;
        }
        QueryContext queryContext = new QueryContext();
        ImmutableSortedMap<DocumentKey, Document> immutableSortedMapExecuteFullCollectionScan = executeFullCollectionScan(query, queryContext);
        if (immutableSortedMapExecuteFullCollectionScan != null && this.indexAutoCreationEnabled) {
            createCacheIndexes(query, queryContext, immutableSortedMapExecuteFullCollectionScan.size());
        }
        return immutableSortedMapExecuteFullCollectionScan;
    }

    public void initialize(LocalDocumentsView localDocumentsView, IndexManager indexManager) {
        this.localDocumentsView = localDocumentsView;
        this.indexManager = indexManager;
        this.initialized = true;
    }

    public void setIndexAutoCreationEnabled(boolean z6) {
        this.indexAutoCreationEnabled = z6;
    }

    public void setIndexAutoCreationMinCollectionSize(int i6) {
        this.indexAutoCreationMinCollectionSize = i6;
    }

    public void setRelativeIndexReadCostPerDocument(double d6) {
        this.relativeIndexReadCostPerDocument = d6;
    }
}
