-
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
Merged
schmidt-sebastian
merged 12 commits into
mrschmidt/indexfree-master
from
mrschmidt/indexfree-query-engine
Aug 21, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
72e972c
Add Index-Free query engine
schmidt-sebastian db9a9a7
Formatting firebase-database-collection
schmidt-sebastian d65656b
Use androidX Nullable
schmidt-sebastian 335b3e4
Re-wrap comment
schmidt-sebastian 664f6e3
Addressed feedback
schmidt-sebastian 4b6ac3f
Merge branch 'mrschmidt/indexfree-query-engine' of github.com:firebas…
schmidt-sebastian 62e4f7c
Re-wrap comment
schmidt-sebastian 165b40b
(Mostly) test cleanup
schmidt-sebastian 407b7d8
Merge branch 'mrschmidt/indexfree-query-engine' of github.com:firebas…
schmidt-sebastian 477ba0d
Java Format
schmidt-sebastian 805f41e
Merge branch 'mrschmidt/indexfree-master' into mrschmidt/indexfree-qu…
schmidt-sebastian 10f0c8e
Fix unit tests
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...ase-firestore/src/main/java/com/google/firebase/firestore/local/IndexFreeQueryEngine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
&& 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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably make this less bad if we change this line
firebase-android-sdk/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java
Line 398 in 72e972c
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.
That seems pretty reasonable.