|
16 | 16 |
|
17 | 17 | #include "Firestore/core/src/local/leveldb_document_overlay_cache.h"
|
18 | 18 |
|
| 19 | +#include <map> |
| 20 | +#include <string> |
| 21 | +#include <unordered_set> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include "Firestore/core/src/credentials/user.h" |
| 25 | +#include "Firestore/core/src/local/leveldb_key.h" |
| 26 | +#include "Firestore/core/src/local/leveldb_persistence.h" |
| 27 | +#include "Firestore/core/src/local/local_serializer.h" |
| 28 | +#include "Firestore/core/src/nanopb/message.h" |
| 29 | +#include "Firestore/core/src/nanopb/reader.h" |
| 30 | +#include "Firestore/core/src/util/hard_assert.h" |
| 31 | +#include "absl/strings/match.h" |
| 32 | +#include "absl/strings/string_view.h" |
| 33 | +#include "absl/types/optional.h" |
| 34 | + |
19 | 35 | namespace firebase {
|
20 | 36 | namespace firestore {
|
21 | 37 | namespace local {
|
22 | 38 |
|
| 39 | +using credentials::User; |
23 | 40 | using model::DocumentKey;
|
24 | 41 | using model::Mutation;
|
25 | 42 | using model::ResourcePath;
|
26 | 43 | using model::mutation::Overlay;
|
| 44 | +using model::mutation::OverlayHash; |
| 45 | +using nanopb::Message; |
| 46 | +using nanopb::StringReader; |
27 | 47 |
|
28 |
| -// TODO(dconeybe) Implement these methods. |
29 |
| - |
30 |
| -LevelDbDocumentOverlayCache::LevelDbDocumentOverlayCache() { |
| 48 | +LevelDbDocumentOverlayCache::LevelDbDocumentOverlayCache( |
| 49 | + const User& user, LevelDbPersistence* db, LocalSerializer* serializer) |
| 50 | + : db_(NOT_NULL(db)), |
| 51 | + serializer_(NOT_NULL(serializer)), |
| 52 | + user_id_(user.is_authenticated() ? user.uid() : "") { |
31 | 53 | }
|
32 | 54 |
|
33 | 55 | absl::optional<Overlay> LevelDbDocumentOverlayCache::GetOverlay(
|
34 | 56 | const DocumentKey& key) const {
|
35 |
| - (void)key; |
36 |
| - return absl::nullopt; |
| 57 | + const std::string leveldb_key_prefix = |
| 58 | + LevelDbDocumentOverlayKey::KeyPrefix(user_id_, key); |
| 59 | + |
| 60 | + auto it = db_->current_transaction()->NewIterator(); |
| 61 | + it->Seek(leveldb_key_prefix); |
| 62 | + |
| 63 | + if (!(it->Valid() && absl::StartsWith(it->key(), leveldb_key_prefix))) { |
| 64 | + return absl::nullopt; |
| 65 | + } |
| 66 | + |
| 67 | + LevelDbDocumentOverlayKey decoded_key; |
| 68 | + HARD_ASSERT(decoded_key.Decode(it->key())); |
| 69 | + return ParseOverlay(decoded_key, it->value()); |
37 | 70 | }
|
38 | 71 |
|
39 | 72 | void LevelDbDocumentOverlayCache::SaveOverlays(
|
40 | 73 | int largest_batch_id, const MutationByDocumentKeyMap& overlays) {
|
41 |
| - (void)largest_batch_id; |
42 |
| - (void)overlays; |
| 74 | + for (const auto& overlays_entry : overlays) { |
| 75 | + SaveOverlay(largest_batch_id, overlays_entry.first, overlays_entry.second); |
| 76 | + } |
43 | 77 | }
|
44 | 78 |
|
45 | 79 | void LevelDbDocumentOverlayCache::RemoveOverlaysForBatchId(int batch_id) {
|
46 |
| - (void)batch_id; |
| 80 | + // TODO(dconeybe) Implement an index so that this query can be performed |
| 81 | + // without requiring a full table scan. |
| 82 | + |
| 83 | + ForEachOverlay([&](absl::string_view encoded_key, |
| 84 | + const LevelDbDocumentOverlayKey& decoded_key, |
| 85 | + absl::string_view) { |
| 86 | + if (decoded_key.largest_batch_id() == batch_id) { |
| 87 | + db_->current_transaction()->Delete(encoded_key); |
| 88 | + } |
| 89 | + }); |
47 | 90 | }
|
48 | 91 |
|
49 | 92 | DocumentOverlayCache::OverlayByDocumentKeyMap
|
50 | 93 | LevelDbDocumentOverlayCache::GetOverlays(const ResourcePath& collection,
|
51 | 94 | int since_batch_id) const {
|
52 |
| - (void)collection; |
53 |
| - (void)since_batch_id; |
54 |
| - return {}; |
| 95 | + // TODO(dconeybe) Implement an index so that this query can be performed |
| 96 | + // without requiring a full table scan. |
| 97 | + |
| 98 | + OverlayByDocumentKeyMap result; |
| 99 | + |
| 100 | + const size_t immediate_children_path_length{collection.size() + 1}; |
| 101 | + |
| 102 | + ForEachOverlay([&](absl::string_view, |
| 103 | + const LevelDbDocumentOverlayKey& decoded_key, |
| 104 | + absl::string_view encoded_mutation) { |
| 105 | + const DocumentKey key = decoded_key.document_key(); |
| 106 | + if (!collection.IsPrefixOf(key.path())) { |
| 107 | + return; |
| 108 | + } |
| 109 | + // Documents from sub-collections |
| 110 | + if (key.path().size() != immediate_children_path_length) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (decoded_key.largest_batch_id() > since_batch_id) { |
| 115 | + result[key] = ParseOverlay(decoded_key, encoded_mutation); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + return result; |
55 | 120 | }
|
56 | 121 |
|
57 | 122 | DocumentOverlayCache::OverlayByDocumentKeyMap
|
58 | 123 | LevelDbDocumentOverlayCache::GetOverlays(const std::string& collection_group,
|
59 | 124 | int since_batch_id,
|
60 | 125 | std::size_t count) const {
|
61 |
| - (void)collection_group; |
62 |
| - (void)since_batch_id; |
63 |
| - (void)count; |
64 |
| - return {}; |
| 126 | + // TODO(dconeybe) Implement an index so that this query can be performed |
| 127 | + // without requiring a full table scan. |
| 128 | + |
| 129 | + // Load ALL overlays for the given `collection_group` whose largest_batch_id |
| 130 | + // are greater than the given `since_batch_id`. By using a `std::map` keyed |
| 131 | + // by largest_batch_id, the loop below can iterate over it ordered by |
| 132 | + // largest_batch_id. |
| 133 | + std::map<int, std::unordered_set<Overlay, OverlayHash>> overlays_by_batch_id; |
| 134 | + ForEachOverlay([&](absl::string_view, |
| 135 | + const LevelDbDocumentOverlayKey& decoded_key, |
| 136 | + absl::string_view encoded_mutation) { |
| 137 | + if (decoded_key.largest_batch_id() <= since_batch_id) { |
| 138 | + return; |
| 139 | + } |
| 140 | + if (decoded_key.document_key().HasCollectionId(collection_group)) { |
| 141 | + overlays_by_batch_id[decoded_key.largest_batch_id()].emplace( |
| 142 | + ParseOverlay(decoded_key, encoded_mutation)); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + // Trim down the overlays loaded above to respect the given `count`, and |
| 147 | + // return them. |
| 148 | + // |
| 149 | + // Note that, as documented, all overlays for the largest_batch_id that pushes |
| 150 | + // the size of the result set above the given `count` will be returned, even |
| 151 | + // though this likely means that the size of the result set will be strictly |
| 152 | + // greater than the given `count`. |
| 153 | + OverlayByDocumentKeyMap result; |
| 154 | + for (auto& overlays_by_batch_id_entry : overlays_by_batch_id) { |
| 155 | + for (auto& overlay : overlays_by_batch_id_entry.second) { |
| 156 | + DocumentKey key = overlay.key(); |
| 157 | + result[key] = std::move(overlay); |
| 158 | + } |
| 159 | + if (result.size() >= count) { |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return result; |
| 165 | +} |
| 166 | + |
| 167 | +Overlay LevelDbDocumentOverlayCache::ParseOverlay( |
| 168 | + const LevelDbDocumentOverlayKey& key, |
| 169 | + absl::string_view encoded_mutation) const { |
| 170 | + StringReader reader{encoded_mutation}; |
| 171 | + auto maybe_message = Message<google_firestore_v1_Write>::TryParse(&reader); |
| 172 | + Mutation mutation = serializer_->DecodeMutation(&reader, *maybe_message); |
| 173 | + if (!reader.ok()) { |
| 174 | + HARD_FAIL("Mutation proto failed to parse: %s", reader.status().ToString()); |
| 175 | + } |
| 176 | + return Overlay(key.largest_batch_id(), std::move(mutation)); |
| 177 | +} |
| 178 | + |
| 179 | +void LevelDbDocumentOverlayCache::SaveOverlay(int largest_batch_id, |
| 180 | + const DocumentKey& key, |
| 181 | + const Mutation& mutation) { |
| 182 | + DeleteOverlay(key); |
| 183 | + const std::string leveldb_key = |
| 184 | + LevelDbDocumentOverlayKey::Key(user_id_, key, largest_batch_id); |
| 185 | + auto serialized_mutation = serializer_->EncodeMutation(mutation); |
| 186 | + db_->current_transaction()->Put(leveldb_key, serialized_mutation); |
| 187 | +} |
| 188 | + |
| 189 | +void LevelDbDocumentOverlayCache::DeleteOverlay(const model::DocumentKey& key) { |
| 190 | + const std::string leveldb_key_prefix = |
| 191 | + LevelDbDocumentOverlayKey::KeyPrefix(user_id_, key); |
| 192 | + auto it = db_->current_transaction()->NewIterator(); |
| 193 | + for (it->Seek(leveldb_key_prefix); |
| 194 | + it->Valid() && absl::StartsWith(it->key(), leveldb_key_prefix); |
| 195 | + it->Next()) { |
| 196 | + db_->current_transaction()->Delete(it->key()); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +void LevelDbDocumentOverlayCache::ForEachOverlay( |
| 201 | + std::function<void(absl::string_view encoded_key, |
| 202 | + const LevelDbDocumentOverlayKey& decoded_key, |
| 203 | + absl::string_view encoded_mutation)> callback) const { |
| 204 | + auto it = db_->current_transaction()->NewIterator(); |
| 205 | + const std::string user_key = LevelDbDocumentOverlayKey::KeyPrefix(user_id_); |
| 206 | + |
| 207 | + for (it->Seek(user_key); it->Valid() && absl::StartsWith(it->key(), user_key); |
| 208 | + it->Next()) { |
| 209 | + LevelDbDocumentOverlayKey decoded_key; |
| 210 | + HARD_ASSERT(decoded_key.Decode(it->key())); |
| 211 | + callback(it->key(), decoded_key, it->value()); |
| 212 | + } |
65 | 213 | }
|
66 | 214 |
|
67 | 215 | } // namespace local
|
|
0 commit comments