Skip to content

Commit 37c273f

Browse files
authored
firestore::local::MemoryDocumentOverlayCache added. (#9330)
1 parent 4a7a99e commit 37c273f

15 files changed

+1016
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 44 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_LOCAL_DOCUMENT_OVERLAY_CACHE_H_
18+
#define FIRESTORE_CORE_SRC_LOCAL_DOCUMENT_OVERLAY_CACHE_H_
19+
20+
#include <cstdlib>
21+
#include <string>
22+
#include <unordered_map>
23+
24+
#include "Firestore/core/src/model/document_key.h"
25+
#include "Firestore/core/src/model/mutation.h"
26+
#include "Firestore/core/src/model/mutation/overlay.h"
27+
#include "Firestore/core/src/model/resource_path.h"
28+
#include "absl/types/optional.h"
29+
30+
namespace firebase {
31+
namespace firestore {
32+
namespace local {
33+
34+
/**
35+
* Provides methods to read and write document overlays.
36+
*
37+
* An overlay is a saved `Mutation`, that gives a local view of a document when
38+
* applied to the remote version of the document.
39+
*
40+
* Each overlay stores the largest batch ID that is included in the overlay,
41+
* which allows us to remove the overlay once all batches leading up to it have
42+
* been acknowledged.
43+
*/
44+
class DocumentOverlayCache {
45+
public:
46+
using OverlayByDocumentKeyMap = std::unordered_map<model::DocumentKey,
47+
model::mutation::Overlay,
48+
model::DocumentKeyHash>;
49+
using MutationByDocumentKeyMap = std::unordered_map<model::DocumentKey,
50+
model::Mutation,
51+
model::DocumentKeyHash>;
52+
53+
virtual ~DocumentOverlayCache() = default;
54+
55+
/**
56+
* Gets the saved overlay mutation for the given document key.
57+
*
58+
* Returns an empty optional if there is no overlay for that key.
59+
*/
60+
virtual absl::optional<model::mutation::Overlay> GetOverlay(
61+
const model::DocumentKey& key) const = 0;
62+
63+
/**
64+
* Saves the given document key to mutation map to persistence as overlays.
65+
*
66+
* All overlays will have their largest batch id set to `largestBatchId`.
67+
*/
68+
virtual void SaveOverlays(int largest_batch_id,
69+
const MutationByDocumentKeyMap& overlays) = 0;
70+
71+
/** Removes the overlay whose largest-batch-id equals to the given ID. */
72+
virtual void RemoveOverlaysForBatchId(int batch_id) = 0;
73+
74+
/**
75+
* Returns all saved overlays for the given collection.
76+
*
77+
* @param collection The collection path to get the overlays for.
78+
* @param since_batch_id The minimum batch ID to filter by (exclusive).
79+
* Only overlays that contain a change past `sinceBatchId` are returned.
80+
* @return Mapping of each document key in the collection to its overlay.
81+
*/
82+
virtual OverlayByDocumentKeyMap GetOverlays(
83+
const model::ResourcePath& collection, int since_batch_id) const = 0;
84+
85+
/**
86+
* Returns `count` overlays with a batch ID higher than `sinceBatchId` for the
87+
* provided collection group, processed by ascending batch ID.
88+
*
89+
* This method always returns all overlays for a batch even if the last batch
90+
* contains more documents than the remaining limit.
91+
*
92+
* @param collection_group The collection group to get the overlays for.
93+
* @param since_batch_id The minimum batch ID to filter by (exclusive).
94+
* Only overlays that contain a change past `sinceBatchId` are returned.
95+
* @param count The number of overlays to return. Can be exceeded if the last
96+
* batch contains more entries.
97+
* @return Mapping of each document key in the collection group to its
98+
* overlay.
99+
*/
100+
virtual OverlayByDocumentKeyMap GetOverlays(
101+
const std::string& collection_group,
102+
int since_batch_id,
103+
std::size_t count) const = 0;
104+
};
105+
106+
} // namespace local
107+
} // namespace firestore
108+
} // namespace firebase
109+
110+
#endif // FIRESTORE_CORE_SRC_LOCAL_DOCUMENT_OVERLAY_CACHE_H_
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/local/leveldb_document_overlay_cache.h"
18+
19+
namespace firebase {
20+
namespace firestore {
21+
namespace local {
22+
23+
using model::DocumentKey;
24+
using model::Mutation;
25+
using model::ResourcePath;
26+
using model::mutation::Overlay;
27+
28+
// TODO(dconeybe) Implement these methods.
29+
30+
LevelDbDocumentOverlayCache::LevelDbDocumentOverlayCache() {
31+
}
32+
33+
absl::optional<Overlay> LevelDbDocumentOverlayCache::GetOverlay(
34+
const DocumentKey& key) const {
35+
(void)key;
36+
return absl::nullopt;
37+
}
38+
39+
void LevelDbDocumentOverlayCache::SaveOverlays(
40+
int largest_batch_id, const MutationByDocumentKeyMap& overlays) {
41+
(void)largest_batch_id;
42+
(void)overlays;
43+
}
44+
45+
void LevelDbDocumentOverlayCache::RemoveOverlaysForBatchId(int batch_id) {
46+
(void)batch_id;
47+
}
48+
49+
DocumentOverlayCache::OverlayByDocumentKeyMap
50+
LevelDbDocumentOverlayCache::GetOverlays(const ResourcePath& collection,
51+
int since_batch_id) const {
52+
(void)collection;
53+
(void)since_batch_id;
54+
return {};
55+
}
56+
57+
DocumentOverlayCache::OverlayByDocumentKeyMap
58+
LevelDbDocumentOverlayCache::GetOverlays(const std::string& collection_group,
59+
int since_batch_id,
60+
std::size_t count) const {
61+
(void)collection_group;
62+
(void)since_batch_id;
63+
(void)count;
64+
return {};
65+
}
66+
67+
} // namespace local
68+
} // namespace firestore
69+
} // namespace firebase
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_
18+
#define FIRESTORE_CORE_SRC_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_
19+
20+
#include <cstdlib>
21+
#include <string>
22+
23+
#include "Firestore/core/src/local/document_overlay_cache.h"
24+
25+
namespace firebase {
26+
namespace firestore {
27+
namespace local {
28+
29+
class LevelDbDocumentOverlayCache final : public DocumentOverlayCache {
30+
public:
31+
LevelDbDocumentOverlayCache();
32+
33+
LevelDbDocumentOverlayCache(const LevelDbDocumentOverlayCache&) = delete;
34+
LevelDbDocumentOverlayCache& operator=(const LevelDbDocumentOverlayCache&) =
35+
delete;
36+
37+
LevelDbDocumentOverlayCache(LevelDbDocumentOverlayCache&&) = delete;
38+
LevelDbDocumentOverlayCache& operator=(LevelDbDocumentOverlayCache&&) =
39+
delete;
40+
41+
absl::optional<model::mutation::Overlay> GetOverlay(
42+
const model::DocumentKey& key) const override;
43+
44+
void SaveOverlays(int largest_batch_id,
45+
const MutationByDocumentKeyMap& overlays) override;
46+
47+
void RemoveOverlaysForBatchId(int batch_id) override;
48+
49+
OverlayByDocumentKeyMap GetOverlays(const model::ResourcePath& collection,
50+
int since_batch_id) const override;
51+
52+
OverlayByDocumentKeyMap GetOverlays(const std::string& collection_group,
53+
int since_batch_id,
54+
std::size_t count) const override;
55+
};
56+
57+
} // namespace local
58+
} // namespace firestore
59+
} // namespace firebase
60+
61+
#endif // FIRESTORE_CORE_SRC_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_

Firestore/core/src/local/leveldb_persistence.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ LevelDbBundleCache* LevelDbPersistence::bundle_cache() {
253253
return bundle_cache_.get();
254254
}
255255

256+
LevelDbDocumentOverlayCache* LevelDbPersistence::document_overlay_cache(
257+
const User& user) {
258+
// TODO(dconeybe) Implement this once LevelDbDocumentOverlayCache is done.
259+
(void)user;
260+
return nullptr;
261+
}
262+
256263
void LevelDbPersistence::RunInternal(absl::string_view label,
257264
std::function<void()> block) {
258265
HARD_ASSERT(transaction_ == nullptr,

Firestore/core/src/local/leveldb_persistence.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "Firestore/core/src/credentials/user.h"
2525
#include "Firestore/core/src/local/leveldb_bundle_cache.h"
26+
#include "Firestore/core/src/local/leveldb_document_overlay_cache.h"
2627
#include "Firestore/core/src/local/leveldb_index_manager.h"
2728
#include "Firestore/core/src/local/leveldb_lru_reference_delegate.h"
2829
#include "Firestore/core/src/local/leveldb_mutation_queue.h"
@@ -80,6 +81,9 @@ class LevelDbPersistence : public Persistence {
8081

8182
LevelDbBundleCache* bundle_cache() override;
8283

84+
LevelDbDocumentOverlayCache* document_overlay_cache(
85+
const credentials::User& user) override;
86+
8387
LevelDbMutationQueue* GetMutationQueueForUser(
8488
const credentials::User& user) override;
8589

0 commit comments

Comments
 (0)