-
Notifications
You must be signed in to change notification settings - Fork 617
Add Index-Free query engine #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
72e972c
db9a9a7
d65656b
335b3e4
664f6e3
4b6ac3f
62e4f7c
165b40b
407b7d8
477ba0d
805f41e
10f0c8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,150 @@ | ||||
// Copyright 2019 Google LLC | ||||
// | ||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||
// you may not use this file except in compliance with the License. | ||||
// You may obtain a copy of the License at | ||||
// | ||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||
// | ||||
// Unless required by applicable law or agreed to in writing, software | ||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
// See the License for the specific language governing permissions and | ||||
// limitations under the License. | ||||
|
||||
package com.google.firebase.firestore.local; | ||||
|
||||
import static com.google.firebase.firestore.util.Assert.hardAssert; | ||||
|
||||
import androidx.annotation.Nullable; | ||||
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.model.Document; | ||||
import com.google.firebase.firestore.model.DocumentCollections; | ||||
import com.google.firebase.firestore.model.DocumentKey; | ||||
import com.google.firebase.firestore.model.MaybeDocument; | ||||
import com.google.firebase.firestore.model.SnapshotVersion; | ||||
import java.util.Map; | ||||
|
||||
/** | ||||
* A query engine that takes advantage of the target document mapping in the QueryCache. The | ||||
* IndexFreeQueryEngine optimizes query execution by only reading the documents previously matched a | ||||
* query plus any documents that were edited after the query was last listened to. | ||||
* | ||||
* <p>There are some cases where Index-Free queries are not guaranteed to produce to the same | ||||
* results as full collection scans. In these case, the IndexFreeQueryEngine falls back to a full | ||||
* query processing. These cases are: | ||||
* | ||||
* <ol> | ||||
* <li>Limit queries where a document that matched the query previously no longer matches the | ||||
* query. In this case, we have to scan all local documents since a document that was sent to | ||||
* us as part of a different query result may now fall into the limit. | ||||
* <li>Limit queries that include edits that occurred after the last remote snapshot (both | ||||
* latency-compensated or committed). Even if an edited document continues to match the query, | ||||
* an edit may cause a document to sort below another document that is in the local cache. | ||||
* <li>Queries where the last snapshot contained Limbo documents. Even though a Limbo document is | ||||
* not part of the backend result set, we need to include Limbo documents in local views to | ||||
* ensure consistency between different Query views. If there exists a previous query snapshot | ||||
* that contained no limbo documents, we can instead use the older snapshot version for | ||||
* Index-Free processing. | ||||
* </ol> | ||||
*/ | ||||
public class IndexFreeQueryEngine implements QueryEngine { | ||||
private LocalDocumentsView localDocumentsView; | ||||
|
||||
@Override | ||||
public void setLocalDocumentsView(LocalDocumentsView localDocuments) { | ||||
this.localDocumentsView = localDocuments; | ||||
} | ||||
|
||||
@Override | ||||
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery( | ||||
Query query, @Nullable QueryData queryData, ImmutableSortedSet<DocumentKey> remoteKeys) { | ||||
hardAssert(localDocumentsView != null, "setLocalDocumentsView() not called"); | ||||
|
||||
// Queries that match all document don't benefit from using IndexFreeQueries. It is more | ||||
// efficient to scan all documents in a collection, rather than to perform individual lookups. | ||||
if (query.matchesAllDocuments()) { | ||||
return executeFullCollectionScan(query); | ||||
} | ||||
|
||||
// Queries that have never seen a snapshot without limbo free documents should also be run as a | ||||
// full collection scan. | ||||
if (queryData == null | ||||
|| queryData.getLastLimboFreeSnapshotVersion().equals(SnapshotVersion.NONE)) { | ||||
return executeFullCollectionScan(query); | ||||
} | ||||
|
||||
ImmutableSortedMap<DocumentKey, Document> result = | ||||
executeIndexFreeQuery(query, queryData, remoteKeys); | ||||
|
||||
return result != null ? result : executeFullCollectionScan(query); | ||||
} | ||||
|
||||
/** | ||||
* Attempts index-free query execution. Returns the set of query results on success, otherwise | ||||
* returns null. | ||||
*/ | ||||
private @Nullable ImmutableSortedMap<DocumentKey, Document> executeIndexFreeQuery( | ||||
Query query, QueryData queryData, ImmutableSortedSet<DocumentKey> remoteKeys) { | ||||
// Fetch the documents that matched the query at the last snapshot. | ||||
ImmutableSortedMap<DocumentKey, MaybeDocument> previousResults = | ||||
localDocumentsView.getDocuments(remoteKeys); | ||||
|
||||
// Limit queries are not eligible for index-free query execution if any part of the result was | ||||
// modified after we received the last query snapshot. This makes sure that we re-populate the | ||||
// view with older documents that may sort before the modified document. | ||||
if (query.hasLimit() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is tragic if there's nothing we can do about it. Users use limits all the time and refine views by applying additional filters. If two different queries run against the same data each will invalidate the other's ability to run index free. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably make this less bad if we change this line Line 398 in 72e972c
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems pretty reasonable. |
||||
&& containsUpdatesSinceSnapshotVersion(previousResults, queryData.getSnapshotVersion())) { | ||||
return null; | ||||
} | ||||
|
||||
ImmutableSortedMap<DocumentKey, Document> results = DocumentCollections.emptyDocumentMap(); | ||||
|
||||
// Re-apply the query filter since previously matching documents do not necessarily still | ||||
// match the query. | ||||
for (Map.Entry<DocumentKey, MaybeDocument> entry : previousResults) { | ||||
MaybeDocument maybeDoc = entry.getValue(); | ||||
if (maybeDoc instanceof Document && query.matches((Document) maybeDoc)) { | ||||
Document doc = (Document) maybeDoc; | ||||
results = results.insert(entry.getKey(), doc); | ||||
} else if (query.hasLimit()) { | ||||
// Limit queries with documents that no longer match need to be re-filled from cache. | ||||
return null; | ||||
} | ||||
} | ||||
|
||||
// Retrieve all results for documents that were updated since the last limbo-document free | ||||
// remote snapshot. | ||||
ImmutableSortedMap<DocumentKey, Document> updatedResults = | ||||
localDocumentsView.getDocumentsMatchingQuery( | ||||
query, queryData.getLastLimboFreeSnapshotVersion()); | ||||
|
||||
results = results.insertAll(updatedResults); | ||||
|
||||
return results; | ||||
} | ||||
|
||||
@Override | ||||
public void handleDocumentChange(MaybeDocument oldDocument, MaybeDocument newDocument) { | ||||
// No indexes to update. | ||||
} | ||||
|
||||
private boolean containsUpdatesSinceSnapshotVersion( | ||||
ImmutableSortedMap<DocumentKey, MaybeDocument> previousResults, | ||||
SnapshotVersion sinceSnapshotVersion) { | ||||
for (Map.Entry<DocumentKey, MaybeDocument> doc : previousResults) { | ||||
if (doc.getValue().hasPendingWrites() | ||||
|| doc.getValue().getVersion().compareTo(sinceSnapshotVersion) > 0) { | ||||
return true; | ||||
} | ||||
} | ||||
|
||||
return false; | ||||
} | ||||
|
||||
private ImmutableSortedMap<DocumentKey, Document> executeFullCollectionScan(Query query) { | ||||
return localDocumentsView.getDocumentsMatchingQuery(query, SnapshotVersion.NONE); | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Order bys also remove documents because they implicitly restrict the result set to those documents containing the field on which to order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my often suggested workaround for "not null" queries. Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix isn't exactly right because users can explicitly
orderBy("__name__")
which is the one case where we don't lose any entries. I think it's:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't feel like there was much to gain from special-casing this, but since this is now a generalized API in Query itself, it probably makes sense to address this case.