Skip to content

Commit eb21690

Browse files
Remove SimpleQueryEngine (#7038)
1 parent a9c559b commit eb21690

16 files changed

+127
-348
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 14 additions & 14 deletions
Large diffs are not rendered by default.

Firestore/Example/Tests/Integration/FSTDatastoreTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "Firestore/core/src/local/local_documents_view.h"
3333
#include "Firestore/core/src/local/local_store.h"
3434
#include "Firestore/core/src/local/memory_persistence.h"
35-
#include "Firestore/core/src/local/simple_query_engine.h"
35+
#include "Firestore/core/src/local/query_engine.h"
3636
#include "Firestore/core/src/local/target_data.h"
3737
#include "Firestore/core/src/model/database_id.h"
3838
#include "Firestore/core/src/model/document_key.h"
@@ -64,7 +64,7 @@
6464
using firebase::firestore::local::LocalStore;
6565
using firebase::firestore::local::MemoryPersistence;
6666
using firebase::firestore::local::Persistence;
67-
using firebase::firestore::local::SimpleQueryEngine;
67+
using firebase::firestore::local::QueryEngine;
6868
using firebase::firestore::local::TargetData;
6969
using firebase::firestore::model::BatchId;
7070
using firebase::firestore::model::DatabaseId;
@@ -226,7 +226,7 @@ @implementation FSTDatastoreTests {
226226
std::unique_ptr<Persistence> _persistence;
227227

228228
DatabaseInfo _databaseInfo;
229-
SimpleQueryEngine _queryEngine;
229+
QueryEngine _queryEngine;
230230

231231
std::unique_ptr<ConnectivityMonitor> _connectivityMonitor;
232232
std::unique_ptr<FirebaseMetadataProvider> _firebaseMetadataProvider;

Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
#include "Firestore/core/src/core/listen_options.h"
3737
#include "Firestore/core/src/core/query_listener.h"
3838
#include "Firestore/core/src/core/sync_engine.h"
39-
#include "Firestore/core/src/local/index_free_query_engine.h"
4039
#include "Firestore/core/src/local/local_store.h"
4140
#include "Firestore/core/src/local/persistence.h"
41+
#include "Firestore/core/src/local/query_engine.h"
4242
#include "Firestore/core/src/model/database_id.h"
4343
#include "Firestore/core/src/model/document_key.h"
4444
#include "Firestore/core/src/remote/firebase_metadata_provider.h"
@@ -72,7 +72,7 @@
7272
using firebase::firestore::core::QueryListener;
7373
using firebase::firestore::core::SyncEngine;
7474
using firebase::firestore::core::ViewSnapshot;
75-
using firebase::firestore::local::IndexFreeQueryEngine;
75+
using firebase::firestore::local::QueryEngine;
7676
using firebase::firestore::local::LocalStore;
7777
using firebase::firestore::local::Persistence;
7878
using firebase::firestore::local::TargetData;
@@ -198,7 +198,7 @@ @implementation FSTSyncEngineTestDriver {
198198
std::vector<std::shared_ptr<EventListener<Empty>>> _snapshotsInSyncListeners;
199199
std::shared_ptr<MockDatastore> _datastore;
200200

201-
IndexFreeQueryEngine _queryEngine;
201+
QueryEngine _queryEngine;
202202

203203
int _snapshotsInSyncEvents;
204204
int _waitForPendingWritesEvents;

Firestore/core/src/core/firestore_client.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
#include "Firestore/core/src/core/query_listener.h"
3232
#include "Firestore/core/src/core/sync_engine.h"
3333
#include "Firestore/core/src/core/view.h"
34-
#include "Firestore/core/src/local/index_free_query_engine.h"
3534
#include "Firestore/core/src/local/leveldb_opener.h"
3635
#include "Firestore/core/src/local/leveldb_persistence.h"
3736
#include "Firestore/core/src/local/local_documents_view.h"
3837
#include "Firestore/core/src/local/local_serializer.h"
3938
#include "Firestore/core/src/local/local_store.h"
4039
#include "Firestore/core/src/local/memory_persistence.h"
40+
#include "Firestore/core/src/local/query_engine.h"
4141
#include "Firestore/core/src/local/query_result.h"
4242
#include "Firestore/core/src/model/database_id.h"
4343
#include "Firestore/core/src/model/document_set.h"
@@ -72,12 +72,12 @@ using api::SnapshotMetadata;
7272
using auth::CredentialsProvider;
7373
using auth::User;
7474
using firestore::Error;
75-
using local::IndexFreeQueryEngine;
7675
using local::LevelDbOpener;
7776
using local::LocalSerializer;
7877
using local::LocalStore;
7978
using local::LruParams;
8079
using local::MemoryPersistence;
80+
using local::QueryEngine;
8181
using local::QueryResult;
8282
using model::DatabaseId;
8383
using model::Document;
@@ -197,7 +197,7 @@ void FirestoreClient::Initialize(const User& user, const Settings& settings) {
197197
persistence_ = MemoryPersistence::WithEagerGarbageCollector();
198198
}
199199

200-
query_engine_ = absl::make_unique<IndexFreeQueryEngine>();
200+
query_engine_ = absl::make_unique<QueryEngine>();
201201
local_store_ = absl::make_unique<LocalStore>(persistence_.get(),
202202
query_engine_.get(), user);
203203
connectivity_monitor_ = ConnectivityMonitor::Create(worker_queue_);

Firestore/core/src/local/index_free_query_engine.h

Lines changed: 0 additions & 95 deletions
This file was deleted.

Firestore/core/src/local/index_free_query_engine.cc renamed to Firestore/core/src/local/query_engine.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Firestore/core/src/local/index_free_query_engine.h"
17+
#include "Firestore/core/src/local/query_engine.h"
1818

1919
#include <utility>
2020

@@ -42,15 +42,15 @@ using model::MaybeDocument;
4242
using model::MaybeDocumentMap;
4343
using model::SnapshotVersion;
4444

45-
DocumentMap IndexFreeQueryEngine::GetDocumentsMatchingQuery(
45+
DocumentMap QueryEngine::GetDocumentsMatchingQuery(
4646
const Query& query,
4747
const SnapshotVersion& last_limbo_free_snapshot_version,
4848
const DocumentKeySet& remote_keys) {
4949
HARD_ASSERT(local_documents_view_, "SetLocalDocumentsView() not called");
5050

51-
// Queries that match all documents don't benefit from using IndexFreeQueries.
52-
// It is more efficient to scan all documents in a collection, rather than to
53-
// perform individual lookups.
51+
// Queries that match all documents don't benefit from using key-based
52+
// lookups. It is more efficient to scan all documents in a collection, rather
53+
// than to perform individual lookups.
5454
if (query.MatchesAllDocuments()) {
5555
return ExecuteFullCollectionScan(query);
5656
}
@@ -89,8 +89,8 @@ DocumentMap IndexFreeQueryEngine::GetDocumentsMatchingQuery(
8989
return updated_results;
9090
}
9191

92-
DocumentSet IndexFreeQueryEngine::ApplyQuery(
93-
const Query& query, const MaybeDocumentMap& documents) const {
92+
DocumentSet QueryEngine::ApplyQuery(const Query& query,
93+
const MaybeDocumentMap& documents) const {
9494
// Sort the documents and re-apply the query filter since previously matching
9595
// documents do not necessarily still match the query.
9696
DocumentSet query_results(query.Comparator());
@@ -107,7 +107,7 @@ DocumentSet IndexFreeQueryEngine::ApplyQuery(
107107
return query_results;
108108
}
109109

110-
bool IndexFreeQueryEngine::NeedsRefill(
110+
bool QueryEngine::NeedsRefill(
111111
LimitType limit_type,
112112
const DocumentSet& sorted_previous_results,
113113
const DocumentKeySet& remote_keys,
@@ -138,8 +138,7 @@ bool IndexFreeQueryEngine::NeedsRefill(
138138
document_at_limit_edge->version() > limbo_free_snapshot_version;
139139
}
140140

141-
DocumentMap IndexFreeQueryEngine::ExecuteFullCollectionScan(
142-
const Query& query) {
141+
DocumentMap QueryEngine::ExecuteFullCollectionScan(const Query& query) {
143142
LOG_DEBUG("Using full collection scan to execute query: %s",
144143
query.ToString());
145144
return local_documents_view_->GetDocumentsMatchingQuery(

Firestore/core/src/local/query_engine.h

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,31 @@ namespace firestore {
2424

2525
namespace core {
2626
class Query;
27+
enum class LimitType;
2728
} // namespace core
2829

2930
namespace local {
3031

3132
class LocalDocumentsView;
3233

3334
/**
34-
* Represents a query engine capable of performing queries over the local
35-
* document cache. You must call `SetLocalDocumentsView()` before using.
35+
* A query engine that takes advantage of the target document mapping in the
36+
* TargetCache. Query execution is optimizes by only reading the documents that
37+
* previously matched a query plus any documents that were edited after the
38+
* query was last listened to.
39+
*
40+
* There are some cases where Index-Free queries are not guaranteed to
41+
* produce the same results as full collection scans. In these cases, the
42+
* query processing falls back to full scans. These cases are:
43+
*
44+
* - Limit queries where a document that matched the query previously no
45+
* longer matches the query.
46+
* - Limit queries where a document edit may cause the document to sort below
47+
* another document that is in the local cache.
48+
* - Queries that have never been CURRENT or free of limbo documents.
3649
*/
3750
class QueryEngine {
3851
public:
39-
enum Type { Simple, IndexFree };
40-
4152
virtual ~QueryEngine() = default;
4253

4354
/**
@@ -46,16 +57,42 @@ class QueryEngine {
4657
* The caller owns the LocalDocumentView and must ensure that it outlives the
4758
* QueryEngine.
4859
*/
49-
virtual void SetLocalDocumentsView(LocalDocumentsView* local_documents) = 0;
60+
virtual void SetLocalDocumentsView(LocalDocumentsView* local_documents) {
61+
local_documents_view_ = local_documents;
62+
}
5063

5164
/** Returns all local documents matching the specified query. */
52-
virtual model::DocumentMap GetDocumentsMatchingQuery(
65+
model::DocumentMap GetDocumentsMatchingQuery(
5366
const core::Query& query,
5467
const model::SnapshotVersion& last_limbo_free_snapshot_version,
55-
const model::DocumentKeySet& remote_keys) = 0;
68+
const model::DocumentKeySet& remote_keys);
69+
70+
private:
71+
/** Applies the query filter and sorting to the provided documents. */
72+
model::DocumentSet ApplyQuery(const core::Query& query,
73+
const model::MaybeDocumentMap& documents) const;
74+
75+
/**
76+
* Determines if a limit query needs to be refilled from cache, making it
77+
* ineligible for index-free execution.
78+
*
79+
* @param limit_type The type of limit query for refill calculation.
80+
* @param sorted_previous_results The documents that matched the query when it
81+
* was last synchronized, sorted by the query's comparator.
82+
* @param remote_keys The document keys that matched the query at the last
83+
* snapshot.
84+
* @param limbo_free_snapshot_version The version of the snapshot when the
85+
* query was last synchronized.
86+
*/
87+
bool NeedsRefill(
88+
core::LimitType limit_type,
89+
const model::DocumentSet& sorted_previous_results,
90+
const model::DocumentKeySet& remote_keys,
91+
const model::SnapshotVersion& limbo_free_snapshot_version) const;
92+
93+
model::DocumentMap ExecuteFullCollectionScan(const core::Query& query);
5694

57-
/** Returns the underlying algorithm used by the query engine. */
58-
virtual Type type() const = 0;
95+
LocalDocumentsView* local_documents_view_ = nullptr;
5996
};
6097

6198
} // namespace local

Firestore/core/src/local/simple_query_engine.cc

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)