|
| 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_ |
0 commit comments