Skip to content

Commit 7c8e0ac

Browse files
committed
add logic in queryEngine
1 parent 1fb30cf commit 7c8e0ac

17 files changed

+229
-27
lines changed

Firestore/core/src/local/index_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class IndexManager {
113113
virtual std::vector<model::FieldIndex> GetFieldIndexes() const = 0;
114114

115115
/** Creates a full matched field index which serves the given target. */
116-
virtual void CreateTargetIndexes(const core::Target& target) const = 0;
116+
virtual void CreateTargetIndexes(const core::Target& target) = 0;
117117

118118
/**
119119
* Iterates over all field indexes that are used to serve the given target,

Firestore/core/src/local/leveldb_index_manager.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,17 @@ absl::optional<model::FieldIndex> LevelDbIndexManager::GetFieldIndex(
464464
return result;
465465
}
466466

467-
void LevelDbIndexManager::CreateTargetIndexes(const core::Target&) const {
467+
void LevelDbIndexManager::CreateTargetIndexes(const core::Target& target) {
468+
HARD_ASSERT(started_, "IndexManager not started");
469+
470+
for (const auto& subTarget : GetSubTargets(target)) {
471+
IndexManager::IndexType type = GetIndexType(subTarget);
472+
if (type == IndexManager::IndexType::NONE ||
473+
type == IndexManager::IndexType::PARTIAL) {
474+
TargetIndexMatcher targetIndexMatcher(subTarget);
475+
AddFieldIndex(targetIndexMatcher.BuildTargetIndex());
476+
}
477+
}
468478
}
469479

470480
model::IndexOffset LevelDbIndexManager::GetMinOffset(

Firestore/core/src/local/leveldb_index_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class LevelDbIndexManager : public IndexManager {
6969

7070
std::vector<model::FieldIndex> GetFieldIndexes() const override;
7171

72-
void CreateTargetIndexes(const core::Target&) const override;
72+
void CreateTargetIndexes(const core::Target& target) override;
7373

7474
model::IndexOffset GetMinOffset(const core::Target& target) override;
7575

Firestore/core/src/local/leveldb_remote_document_cache.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Firestore/core/src/local/leveldb_key.h"
2626
#include "Firestore/core/src/local/leveldb_persistence.h"
2727
#include "Firestore/core/src/local/local_serializer.h"
28+
#include "Firestore/core/src/local/query_context.h"
2829
#include "Firestore/core/src/model/document_key_set.h"
2930
#include "Firestore/core/src/model/model_fwd.h"
3031
#include "Firestore/core/src/model/mutable_document.h"
@@ -227,6 +228,16 @@ MutableDocumentMap LevelDbRemoteDocumentCache::GetDocumentsMatchingQuery(
227228
const model::IndexOffset& offset,
228229
absl::optional<size_t> limit,
229230
const model::OverlayByDocumentKeyMap& mutated_docs) const {
231+
absl::optional<QueryContext> context;
232+
return GetDocumentsMatchingQuery(query, offset, context, limit, mutated_docs);
233+
}
234+
235+
MutableDocumentMap LevelDbRemoteDocumentCache::GetDocumentsMatchingQuery(
236+
const core::Query& query,
237+
const model::IndexOffset& offset,
238+
absl::optional<QueryContext>& context,
239+
absl::optional<size_t> limit,
240+
const model::OverlayByDocumentKeyMap& mutated_docs) const {
230241
// Use the query path as a prefix for testing if a document matches the query.
231242

232243
// Execute an index-free query and filter by read time. This is safe since
@@ -262,6 +273,10 @@ MutableDocumentMap LevelDbRemoteDocumentCache::GetDocumentsMatchingQuery(
262273
}
263274
}
264275

276+
// The next step is going to check every document in remote_map, so it will go
277+
// through total of remote_map.size() documents.
278+
context->incrementDocumentReadCount(remote_map.size());
279+
265280
return LevelDbRemoteDocumentCache::GetAllExisting(std::move(remote_map),
266281
query, mutated_docs);
267282
}

Firestore/core/src/local/leveldb_remote_document_cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ class LevelDbRemoteDocumentCache : public RemoteDocumentCache {
7070
const model::IndexOffset& offset,
7171
absl::optional<size_t> limit = absl::nullopt,
7272
const model::OverlayByDocumentKeyMap& mutated_docs = {}) const override;
73+
model::MutableDocumentMap GetDocumentsMatchingQuery(
74+
const core::Query& query,
75+
const model::IndexOffset& offset,
76+
absl::optional<QueryContext>& context,
77+
absl::optional<size_t> limit = absl::nullopt,
78+
const model::OverlayByDocumentKeyMap& mutated_docs = {}) const override;
7379

7480
void SetIndexManager(IndexManager* manager) override;
7581

Firestore/core/src/local/local_documents_view.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,20 @@ Document LocalDocumentsView::GetDocument(
7575

7676
DocumentMap LocalDocumentsView::GetDocumentsMatchingQuery(
7777
const Query& query, const model::IndexOffset& offset) {
78+
absl::optional<QueryContext> null_context;
79+
return GetDocumentsMatchingQuery(query, offset, null_context);
80+
}
81+
82+
DocumentMap LocalDocumentsView::GetDocumentsMatchingQuery(
83+
const Query& query,
84+
const model::IndexOffset& offset,
85+
absl::optional<QueryContext>& context) {
7886
if (query.IsDocumentQuery()) {
7987
return GetDocumentsMatchingDocumentQuery(query.path());
8088
} else if (query.IsCollectionGroupQuery()) {
81-
return GetDocumentsMatchingCollectionGroupQuery(query, offset);
89+
return GetDocumentsMatchingCollectionGroupQuery(query, offset, context);
8290
} else {
83-
return GetDocumentsMatchingCollectionQuery(query, offset);
91+
return GetDocumentsMatchingCollectionQuery(query, offset, context);
8492
}
8593
}
8694

@@ -96,7 +104,9 @@ DocumentMap LocalDocumentsView::GetDocumentsMatchingDocumentQuery(
96104
}
97105

98106
model::DocumentMap LocalDocumentsView::GetDocumentsMatchingCollectionGroupQuery(
99-
const Query& query, const IndexOffset& offset) {
107+
const Query& query,
108+
const IndexOffset& offset,
109+
absl::optional<QueryContext>& context) {
100110
HARD_ASSERT(
101111
query.path().empty(),
102112
"Currently we only support collection group queries at the root.");
@@ -112,7 +122,7 @@ model::DocumentMap LocalDocumentsView::GetDocumentsMatchingCollectionGroupQuery(
112122
Query collection_query =
113123
query.AsCollectionQueryAtPath(parent.Append(collection_id));
114124
DocumentMap collection_results =
115-
GetDocumentsMatchingCollectionQuery(collection_query, offset);
125+
GetDocumentsMatchingCollectionQuery(collection_query, offset, context);
116126
for (const auto& kv : collection_results) {
117127
const DocumentKey& key = kv.first;
118128
results = results.insert(key, Document(kv.second));
@@ -153,13 +163,15 @@ LocalWriteResult LocalDocumentsView::GetNextDocuments(
153163
}
154164

155165
DocumentMap LocalDocumentsView::GetDocumentsMatchingCollectionQuery(
156-
const Query& query, const IndexOffset& offset) {
166+
const Query& query,
167+
const IndexOffset& offset,
168+
absl::optional<QueryContext>& context) {
157169
// Get locally mutated documents
158170
OverlayByDocumentKeyMap overlays = document_overlay_cache_->GetOverlays(
159171
query.path(), offset.largest_batch_id());
160172
MutableDocumentMap remote_documents =
161173
remote_document_cache_->GetDocumentsMatchingQuery(
162-
query, offset, absl::nullopt, overlays);
174+
query, offset, context, absl::nullopt, overlays);
163175

164176
// As documents might match the query because of their overlay we need to
165177
// include documents for all overlays in the initial document set.

Firestore/core/src/local/local_documents_view.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Firestore/core/src/local/document_overlay_cache.h"
2626
#include "Firestore/core/src/local/index_manager.h"
2727
#include "Firestore/core/src/local/mutation_queue.h"
28+
#include "Firestore/core/src/local/query_context.h"
2829
#include "Firestore/core/src/local/remote_document_cache.h"
2930
#include "Firestore/core/src/model/document.h"
3031
#include "Firestore/core/src/model/model_fwd.h"
@@ -39,10 +40,9 @@ class Query;
3940
} // namespace core
4041

4142
namespace local {
42-
class LocalWriteResult;
43-
} // namespace local
4443

45-
namespace local {
44+
class LocalWriteResult;
45+
class QueryContext;
4646

4747
/**
4848
* A readonly view of the local state of all documents we're tracking (i.e. we
@@ -141,6 +141,20 @@ class LocalDocumentsView {
141141
virtual model::DocumentMap GetDocumentsMatchingQuery(
142142
const core::Query& query, const model::IndexOffset& offset);
143143

144+
/**
145+
* Performs a query against the local view of all documents.
146+
*
147+
* @param query The query to match documents against.
148+
* @param offset Read time and document key to start scanning by (exclusive).
149+
* @param context A optional tracker to keep a record of important details
150+
* during database local query execution.
151+
*/
152+
// Virtual for testing.
153+
virtual model::DocumentMap GetDocumentsMatchingQuery(
154+
const core::Query& query,
155+
const model::IndexOffset& offset,
156+
absl::optional<QueryContext>& context);
157+
144158
private:
145159
friend class QueryEngine;
146160

@@ -155,11 +169,15 @@ class LocalDocumentsView {
155169
const model::ResourcePath& doc_path);
156170

157171
model::DocumentMap GetDocumentsMatchingCollectionGroupQuery(
158-
const core::Query& query, const model::IndexOffset& offset);
172+
const core::Query& query,
173+
const model::IndexOffset& offset,
174+
absl::optional<QueryContext>& context);
159175

160176
/** Queries the remote documents and overlays mutations. */
161177
model::DocumentMap GetDocumentsMatchingCollectionQuery(
162-
const core::Query& query, const model::IndexOffset& offset);
178+
const core::Query& query,
179+
const model::IndexOffset& offset,
180+
absl::optional<QueryContext>& context);
163181

164182
RemoteDocumentCache* remote_document_cache() {
165183
return remote_document_cache_;

Firestore/core/src/local/memory_index_manager.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ std::vector<model::FieldIndex> MemoryIndexManager::GetFieldIndexes() const {
8989
return {};
9090
}
9191

92-
void MemoryIndexManager::CreateTargetIndexes(const core::Target&) const {
92+
void MemoryIndexManager::CreateTargetIndexes(const core::Target&) {
9393
}
9494

9595
model::IndexOffset MemoryIndexManager::GetMinOffset(const core::Target&) {

Firestore/core/src/local/memory_index_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class MemoryIndexManager : public IndexManager {
6767

6868
std::vector<model::FieldIndex> GetFieldIndexes() const override;
6969

70-
void CreateTargetIndexes(const core::Target&) const override;
70+
void CreateTargetIndexes(const core::Target&) override;
7171

7272
model::IndexOffset GetMinOffset(const core::Target&) override;
7373

Firestore/core/src/local/memory_remote_document_cache.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "Firestore/core/src/core/query.h"
2020
#include "Firestore/core/src/local/memory_lru_reference_delegate.h"
2121
#include "Firestore/core/src/local/memory_persistence.h"
22+
#include "Firestore/core/src/local/query_context.h"
2223
#include "Firestore/core/src/local/sizer.h"
2324
#include "Firestore/core/src/model/document.h"
2425
#include "Firestore/core/src/model/overlay.h"
@@ -87,6 +88,16 @@ MutableDocumentMap MemoryRemoteDocumentCache::GetAll(const std::string&,
8788
MutableDocumentMap MemoryRemoteDocumentCache::GetDocumentsMatchingQuery(
8889
const core::Query& query,
8990
const model::IndexOffset& offset,
91+
absl::optional<size_t> limit,
92+
const model::OverlayByDocumentKeyMap& mutated_docs) const {
93+
absl::optional<QueryContext> context;
94+
return GetDocumentsMatchingQuery(query, offset, context, limit, mutated_docs);
95+
}
96+
97+
MutableDocumentMap MemoryRemoteDocumentCache::GetDocumentsMatchingQuery(
98+
const core::Query& query,
99+
const model::IndexOffset& offset,
100+
absl::optional<QueryContext>&,
90101
absl::optional<size_t>,
91102
const model::OverlayByDocumentKeyMap& mutated_docs) const {
92103
MutableDocumentMap results;

0 commit comments

Comments
 (0)