|
| 1 | +/* |
| 2 | + * Copyright 2018 Google |
| 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_FIREBASE_FIRESTORE_MODEL_MUTATION_H_ |
| 18 | +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MUTATION_H_ |
| 19 | + |
| 20 | +#include <memory> |
| 21 | + |
| 22 | +#include "Firestore/core/src/firebase/firestore/model/document_key.h" |
| 23 | +#include "Firestore/core/src/firebase/firestore/model/field_value.h" |
| 24 | +#include "Firestore/core/src/firebase/firestore/model/maybe_document.h" |
| 25 | +#include "Firestore/core/src/firebase/firestore/model/precondition.h" |
| 26 | +#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" |
| 27 | + |
| 28 | +namespace firebase { |
| 29 | +namespace firestore { |
| 30 | +namespace model { |
| 31 | + |
| 32 | +/** |
| 33 | + * Represents a Mutation of a document. Different subclasses of Mutation will |
| 34 | + * perform different kinds of changes to a base document. For example, a |
| 35 | + * SetMutation replaces the value of a document and a DeleteMutation deletes a |
| 36 | + * document. |
| 37 | + * |
| 38 | + * In addition to the value of the document mutations also operate on the |
| 39 | + * version. For local mutations (mutations that haven't been committed yet), we |
| 40 | + * preserve the existing version for Set, Patch, and Transform mutations. For |
| 41 | + * local deletes, we reset the version to 0. |
| 42 | + * |
| 43 | + * Here's the expected transition table. |
| 44 | + * |
| 45 | + * MUTATION APPLIED TO RESULTS IN |
| 46 | + * SetMutation Document(v3) Document(v3) |
| 47 | + * SetMutation NoDocument(v3) Document(v0) |
| 48 | + * SetMutation null Document(v0) |
| 49 | + * PatchMutation Document(v3) Document(v3) |
| 50 | + * PatchMutation NoDocument(v3) NoDocument(v3) |
| 51 | + * PatchMutation null null |
| 52 | + * TransformMutation Document(v3) Document(v3) |
| 53 | + * TransformMutation NoDocument(v3) NoDocument(v3) |
| 54 | + * TransformMutation null null |
| 55 | + * DeleteMutation Document(v3) NoDocument(v0) |
| 56 | + * DeleteMutation NoDocument(v3) NoDocument(v0) |
| 57 | + * DeleteMutation null NoDocument(v0) |
| 58 | + * |
| 59 | + * For acknowledged mutations, we use the updateTime of the WriteResponse as the |
| 60 | + * resulting version for Set, Patch, and Transform mutations. As deletes have no |
| 61 | + * explicit update time, we use the commitTime of the WriteResponse for |
| 62 | + * acknowledged deletes. |
| 63 | + * |
| 64 | + * If a mutation is acknowledged by the backend but fails the precondition check |
| 65 | + * locally, we return an `UnknownDocument` and rely on Watch to send us the |
| 66 | + * updated version. |
| 67 | + * |
| 68 | + * Note that TransformMutations don't create Documents (in the case of being |
| 69 | + * applied to a NoDocument), even though they would on the backend. This is |
| 70 | + * because the client always combines the TransformMutation with a SetMutation |
| 71 | + * or PatchMutation and we only want to apply the transform if the prior |
| 72 | + * mutation resulted in a Document (always true for a SetMutation, but not |
| 73 | + * necessarily for an PatchMutation). |
| 74 | + */ |
| 75 | +class Mutation { |
| 76 | + public: |
| 77 | + virtual ~Mutation() { |
| 78 | + } |
| 79 | + |
| 80 | + const DocumentKey& key() const { |
| 81 | + return key_; |
| 82 | + } |
| 83 | + const Precondition& precondition() const { |
| 84 | + return precondition_; |
| 85 | + } |
| 86 | + |
| 87 | + // TODO(rsgowman): ApplyToRemoteDocument() |
| 88 | + |
| 89 | + /** |
| 90 | + * Applies this mutation to the given MaybeDocument for the purposes of |
| 91 | + * computing the new local view of a document. Both the input and returned |
| 92 | + * documents can be nullptr. |
| 93 | + * |
| 94 | + * @param maybe_doc The document to mutate. The input document can be nullptr |
| 95 | + * if the client has no knowledge of the pre-mutation state of the |
| 96 | + * document. |
| 97 | + * @param base_doc The state of the document prior to this mutation batch. The |
| 98 | + * input document can be nullptr if the client has no knowledge of the |
| 99 | + * pre-mutation state of the document. |
| 100 | + * @param local_write_time A timestamp indicating the local write time of the |
| 101 | + * batch this mutation is a part of. |
| 102 | + * @return The mutated document. The returned document may be nullptr, but |
| 103 | + * only if maybe_doc was nullptr and the mutation would not create a new |
| 104 | + * document. |
| 105 | + */ |
| 106 | + virtual std::shared_ptr<const MaybeDocument> ApplyToLocalView( |
| 107 | + const std::shared_ptr<const MaybeDocument>& maybe_doc, |
| 108 | + const MaybeDocument* base_doc, |
| 109 | + const Timestamp& local_write_time) const = 0; |
| 110 | + |
| 111 | + protected: |
| 112 | + Mutation(DocumentKey&& key, Precondition&& precondition); |
| 113 | + |
| 114 | + void VerifyKeyMatches(const MaybeDocument* maybe_doc) const; |
| 115 | + |
| 116 | + static SnapshotVersion GetPostMutationVersion(const MaybeDocument* maybe_doc); |
| 117 | + |
| 118 | + private: |
| 119 | + const DocumentKey key_; |
| 120 | + const Precondition precondition_; |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * A mutation that creates or replaces the document at the given key with the |
| 125 | + * object value contents. |
| 126 | + */ |
| 127 | +class SetMutation : public Mutation { |
| 128 | + public: |
| 129 | + SetMutation(DocumentKey&& key, |
| 130 | + FieldValue&& value, |
| 131 | + Precondition&& precondition); |
| 132 | + |
| 133 | + // TODO(rsgowman): ApplyToRemoteDocument() |
| 134 | + |
| 135 | + std::shared_ptr<const MaybeDocument> ApplyToLocalView( |
| 136 | + const std::shared_ptr<const MaybeDocument>& maybe_doc, |
| 137 | + const MaybeDocument* base_doc, |
| 138 | + const Timestamp& local_write_time) const override; |
| 139 | + |
| 140 | + private: |
| 141 | + const FieldValue value_; |
| 142 | +}; |
| 143 | + |
| 144 | +} // namespace model |
| 145 | +} // namespace firestore |
| 146 | +} // namespace firebase |
| 147 | + |
| 148 | +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MUTATION_H_ |
0 commit comments