Skip to content

Commit 020a5ee

Browse files
authored
Wire DocumentOverlayCache into LocalStore and LocalDocumentsView (#9549)
1 parent 031cd48 commit 020a5ee

File tree

6 files changed

+108
-7
lines changed

6 files changed

+108
-7
lines changed

Firestore/core/src/local/local_documents_view.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <vector>
2121

22+
#include "Firestore/core/src/local/document_overlay_cache.h"
2223
#include "Firestore/core/src/local/index_manager.h"
2324
#include "Firestore/core/src/local/mutation_queue.h"
2425
#include "Firestore/core/src/local/remote_document_cache.h"
@@ -47,9 +48,11 @@ class LocalDocumentsView {
4748
public:
4849
LocalDocumentsView(RemoteDocumentCache* remote_document_cache,
4950
MutationQueue* mutation_queue,
51+
DocumentOverlayCache* document_overlay_cache,
5052
IndexManager* index_manager)
5153
: remote_document_cache_{remote_document_cache},
5254
mutation_queue_{mutation_queue},
55+
document_overlay_cache_{document_overlay_cache},
5356
index_manager_{index_manager} {
5457
}
5558

@@ -136,13 +139,18 @@ class LocalDocumentsView {
136139
return mutation_queue_;
137140
}
138141

142+
DocumentOverlayCache* document_overlay_cache() {
143+
return document_overlay_cache_;
144+
}
145+
139146
IndexManager* index_manager() {
140147
return index_manager_;
141148
}
142149

143150
private:
144151
RemoteDocumentCache* remote_document_cache_;
145152
MutationQueue* mutation_queue_;
153+
DocumentOverlayCache* document_overlay_cache_;
146154
IndexManager* index_manager_;
147155
};
148156

Firestore/core/src/local/local_store.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ LocalStore::LocalStore(Persistence* persistence,
8888
query_engine_(query_engine) {
8989
index_manager_ = persistence->GetIndexManager(initial_user);
9090
mutation_queue_ = persistence->GetMutationQueue(initial_user, index_manager_);
91+
document_overlay_cache_ = persistence->GetDocumentOverlayCache(initial_user);
9192
local_documents_ = absl::make_unique<LocalDocumentsView>(
92-
remote_document_cache_, mutation_queue_, index_manager_);
93+
remote_document_cache_, mutation_queue_, document_overlay_cache_,
94+
index_manager_);
9395
remote_document_cache_->SetIndexManager(index_manager_);
9496

9597
persistence->reference_delegate()->AddInMemoryPins(&local_view_references_);
@@ -130,7 +132,8 @@ DocumentMap LocalStore::HandleUserChange(const User& user) {
130132

131133
// Recreate our LocalDocumentsView using the new MutationQueue.
132134
local_documents_ = absl::make_unique<LocalDocumentsView>(
133-
remote_document_cache_, mutation_queue_, index_manager_);
135+
remote_document_cache_, mutation_queue_, document_overlay_cache_,
136+
index_manager_);
134137
query_engine_->SetLocalDocumentsView(local_documents_.get());
135138

136139
// Union the old/new changed keys.

Firestore/core/src/local/local_store.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "Firestore/core/src/bundle/bundle_metadata.h"
2727
#include "Firestore/core/src/bundle/named_query.h"
2828
#include "Firestore/core/src/core/target_id_generator.h"
29+
#include "Firestore/core/src/local/document_overlay_cache.h"
2930
#include "Firestore/core/src/local/reference_set.h"
3031
#include "Firestore/core/src/local/target_data.h"
3132
#include "Firestore/core/src/model/document.h"
@@ -339,6 +340,12 @@ class LocalStore : public bundle::BundleCallback {
339340
*/
340341
MutationQueue* mutation_queue_ = nullptr;
341342

343+
/**
344+
* The overlays that can be used to short circuit applying all mutations from
345+
* mutation queue.
346+
*/
347+
DocumentOverlayCache* document_overlay_cache_ = nullptr;
348+
342349
/** The set of all cached remote documents. */
343350
RemoteDocumentCache* remote_document_cache_ = nullptr;
344351

Firestore/core/test/unit/local/counting_query_engine.cc

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Firestore/core/src/model/mutable_document.h"
2323
#include "Firestore/core/src/model/mutation_batch.h"
2424
#include "Firestore/core/src/nanopb/byte_string.h"
25+
#include "Firestore/core/src/util/hard_assert.h"
2526

2627
namespace firebase {
2728
namespace firestore {
@@ -43,9 +44,11 @@ void CountingQueryEngine::SetLocalDocumentsView(
4344
local_documents->remote_document_cache(), this);
4445
mutation_queue_ = absl::make_unique<WrappedMutationQueue>(
4546
local_documents->mutation_queue(), this);
47+
document_overlay_cache_ = absl::make_unique<WrappedDocumentOverlayCache>(
48+
local_documents->document_overlay_cache(), this);
4649
local_documents_ = absl::make_unique<LocalDocumentsView>(
4750
remote_documents_.get(), mutation_queue_.get(),
48-
local_documents->index_manager());
51+
document_overlay_cache_.get(), local_documents->index_manager());
4952
QueryEngine::SetLocalDocumentsView(local_documents_.get());
5053
}
5154

@@ -54,6 +57,9 @@ void CountingQueryEngine::ResetCounts() {
5457
mutations_read_by_key_ = 0;
5558
documents_read_by_query_ = 0;
5659
documents_read_by_key_ = 0;
60+
overlays_read_by_key_ = 0;
61+
overlays_read_by_collection_ = 0;
62+
overlays_read_by_collection_group_ = 0;
5763
}
5864

5965
// MARK: - WrappedMutationQueue
@@ -173,6 +179,44 @@ model::MutableDocumentMap WrappedRemoteDocumentCache::GetMatching(
173179
return result;
174180
}
175181

182+
// MARK: - WrappedDocumentOverlayCache
183+
184+
absl::optional<model::Overlay> WrappedDocumentOverlayCache::GetOverlay(
185+
const model::DocumentKey& key) const {
186+
++query_engine_->overlays_read_by_key_;
187+
return subject_->GetOverlay(key);
188+
}
189+
190+
void WrappedDocumentOverlayCache::SaveOverlays(
191+
int largest_batch_id, const MutationByDocumentKeyMap& overlays) {
192+
subject_->SaveOverlays(largest_batch_id, overlays);
193+
}
194+
195+
void WrappedDocumentOverlayCache::RemoveOverlaysForBatchId(int batch_id) {
196+
subject_->RemoveOverlaysForBatchId(batch_id);
197+
}
198+
199+
DocumentOverlayCache::OverlayByDocumentKeyMap
200+
WrappedDocumentOverlayCache::GetOverlays(const model::ResourcePath& collection,
201+
int since_batch_id) const {
202+
auto result = subject_->GetOverlays(collection, since_batch_id);
203+
query_engine_->overlays_read_by_collection_ += result.size();
204+
return result;
205+
}
206+
207+
DocumentOverlayCache::OverlayByDocumentKeyMap
208+
WrappedDocumentOverlayCache::GetOverlays(absl::string_view collection_group,
209+
int since_batch_id,
210+
std::size_t count) const {
211+
auto result = subject_->GetOverlays(collection_group, since_batch_id, count);
212+
query_engine_->overlays_read_by_collection_group_ += result.size();
213+
return result;
214+
}
215+
216+
int WrappedDocumentOverlayCache::GetOverlayCount() const {
217+
HARD_FAIL("WrappedDocumentOverlayCache::GetOverlayCount() not implemented");
218+
}
219+
176220
} // namespace local
177221
} // namespace firestore
178222
} // namespace firebase

Firestore/core/test/unit/local/counting_query_engine.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <utility>
2222
#include <vector>
2323

24+
#include "Firestore/core/src/local/document_overlay_cache.h"
2425
#include "Firestore/core/src/local/mutation_queue.h"
2526
#include "Firestore/core/src/local/query_engine.h"
2627
#include "Firestore/core/src/local/remote_document_cache.h"
@@ -37,6 +38,7 @@ class Query;
3738
namespace local {
3839

3940
class LocalDocumentsView;
41+
class WrappedDocumentOverlayCache;
4042
class WrappedMutationQueue;
4143
class WrappedRemoteDocumentCache;
4244

@@ -90,17 +92,22 @@ class CountingQueryEngine : public QueryEngine {
9092
}
9193

9294
private:
95+
friend class WrappedDocumentOverlayCache;
9396
friend class WrappedMutationQueue;
9497
friend class WrappedRemoteDocumentCache;
9598

9699
std::unique_ptr<LocalDocumentsView> local_documents_;
97100
std::unique_ptr<WrappedMutationQueue> mutation_queue_;
101+
std::unique_ptr<WrappedDocumentOverlayCache> document_overlay_cache_;
98102
std::unique_ptr<WrappedRemoteDocumentCache> remote_documents_;
99103

100104
size_t mutations_read_by_query_ = 0;
101105
size_t mutations_read_by_key_ = 0;
102106
size_t documents_read_by_query_ = 0;
103107
size_t documents_read_by_key_ = 0;
108+
size_t overlays_read_by_key_ = 0;
109+
size_t overlays_read_by_collection_ = 0;
110+
size_t overlays_read_by_collection_group_ = 0;
104111
};
105112

106113
/** A MutationQueue that counts document reads. */
@@ -186,6 +193,36 @@ class WrappedRemoteDocumentCache : public RemoteDocumentCache {
186193
CountingQueryEngine* query_engine_ = nullptr;
187194
};
188195

196+
/** A DocumentOverlayCache that counts document reads. */
197+
class WrappedDocumentOverlayCache final : public DocumentOverlayCache {
198+
public:
199+
WrappedDocumentOverlayCache(DocumentOverlayCache* subject,
200+
CountingQueryEngine* query_engine)
201+
: subject_(subject), query_engine_(query_engine) {
202+
}
203+
204+
absl::optional<model::Overlay> GetOverlay(
205+
const model::DocumentKey& key) const override;
206+
207+
void SaveOverlays(int largest_batch_id,
208+
const MutationByDocumentKeyMap& overlays) override;
209+
210+
void RemoveOverlaysForBatchId(int batch_id) override;
211+
212+
OverlayByDocumentKeyMap GetOverlays(const model::ResourcePath& collection,
213+
int since_batch_id) const override;
214+
215+
OverlayByDocumentKeyMap GetOverlays(absl::string_view collection_group,
216+
int since_batch_id,
217+
std::size_t count) const override;
218+
219+
private:
220+
int GetOverlayCount() const override;
221+
222+
DocumentOverlayCache* subject_ = nullptr;
223+
CountingQueryEngine* query_engine_ = nullptr;
224+
};
225+
189226
} // namespace local
190227
} // namespace firestore
191228
} // namespace firebase

Firestore/core/test/unit/local/query_engine_test.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ class QueryEngineTest : public ::testing::Test {
120120
target_cache_(persistence_->target_cache()),
121121
index_manager_(dynamic_cast<MemoryIndexManager*>(
122122
persistence_->GetIndexManager(User::Unauthenticated()))),
123-
local_documents_view_(remote_document_cache_,
124-
persistence_->GetMutationQueue(
125-
User::Unauthenticated(), index_manager_),
126-
index_manager_) {
123+
local_documents_view_(
124+
remote_document_cache_,
125+
persistence_->GetMutationQueue(User::Unauthenticated(),
126+
index_manager_),
127+
persistence_->GetDocumentOverlayCache(User::Unauthenticated()),
128+
index_manager_) {
127129
remote_document_cache_->SetIndexManager(index_manager_);
128130
query_engine_.SetLocalDocumentsView(&local_documents_view_);
129131
}

0 commit comments

Comments
 (0)