18
18
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MUTATION_H_
19
19
20
20
#include < memory>
21
+ #include < utility>
22
+ #include < vector>
21
23
22
24
#include " Firestore/core/src/firebase/firestore/model/document_key.h"
23
25
#include " Firestore/core/src/firebase/firestore/model/field_mask.h"
@@ -30,6 +32,56 @@ namespace firebase {
30
32
namespace firestore {
31
33
namespace model {
32
34
35
+ using MaybeDocumentPtr = std::shared_ptr<const MaybeDocument>;
36
+
37
+ /* *
38
+ * The result of applying a mutation to the server. This is a model of the
39
+ * WriteResult proto message.
40
+ *
41
+ * Note that MutationResult does not name which document was mutated. The
42
+ * association is implied positionally: for each entry in the array of
43
+ * Mutations, there's a corresponding entry in the array of MutationResults.
44
+ */
45
+ class MutationResult {
46
+ public:
47
+ MutationResult (
48
+ SnapshotVersion&& version,
49
+ const std::shared_ptr<const std::vector<FieldValue>>& transform_results)
50
+ : version_(std::move(version)),
51
+ transform_results_ (std::move(transform_results)) {
52
+ }
53
+
54
+ /* *
55
+ * The version at which the mutation was committed.
56
+ *
57
+ * - For most operations, this is the update_time in the WriteResult.
58
+ * - For deletes, it is the commit_time of the WriteResponse (because
59
+ * deletes are not stored and have no update_time).
60
+ *
61
+ * Note that these versions can be different: No-op writes will not change
62
+ * the update_time even though the commit_time advances.
63
+ */
64
+ const SnapshotVersion& version () const {
65
+ return version_;
66
+ }
67
+
68
+ /* *
69
+ * The resulting fields returned from the backend after a TransformMutation
70
+ * has been committed. Contains one FieldValue for each FieldTransform
71
+ * that was in the mutation.
72
+ *
73
+ * Will be null if the mutation was not a TransformMutation.
74
+ */
75
+ const std::shared_ptr<const std::vector<FieldValue>>& transform_results ()
76
+ const {
77
+ return transform_results_;
78
+ }
79
+
80
+ private:
81
+ const SnapshotVersion version_;
82
+ const std::shared_ptr<const std::vector<FieldValue>> transform_results_;
83
+ };
84
+
33
85
/* *
34
86
* Represents a Mutation of a document. Different subclasses of Mutation will
35
87
* perform different kinds of changes to a base document. For example, a
@@ -85,7 +137,24 @@ class Mutation {
85
137
return precondition_;
86
138
}
87
139
88
- // TODO(rsgowman): ApplyToRemoteDocument()
140
+ /* *
141
+ * Applies this mutation to the given MaybeDocument for the purposes of
142
+ * computing a new remote document. If the input document doesn't match the
143
+ * expected state (e.g. it is null or outdated), an `UnknownDocument` can be
144
+ * returned.
145
+ *
146
+ * @param maybe_doc The document to mutate. The input document can be nullptr
147
+ * if the client has no knowledge of the pre-mutation state of the
148
+ * document.
149
+ * @param mutation_result The result of applying the mutation from the
150
+ * backend.
151
+ * @return The mutated document. The returned document may be an
152
+ * UnknownDocument if the mutation could not be applied to the locally
153
+ * cached base document.
154
+ */
155
+ virtual MaybeDocumentPtr ApplyToRemoteDocument (
156
+ const MaybeDocumentPtr& maybe_doc,
157
+ const MutationResult& mutation_result) const = 0;
89
158
90
159
/* *
91
160
* Applies this mutation to the given MaybeDocument for the purposes of
@@ -104,8 +173,8 @@ class Mutation {
104
173
* only if maybe_doc was nullptr and the mutation would not create a new
105
174
* document.
106
175
*/
107
- virtual std::shared_ptr< const MaybeDocument> ApplyToLocalView (
108
- const std::shared_ptr< const MaybeDocument> & maybe_doc,
176
+ virtual MaybeDocumentPtr ApplyToLocalView (
177
+ const MaybeDocumentPtr & maybe_doc,
109
178
const MaybeDocument* base_doc,
110
179
const Timestamp& local_write_time) const = 0;
111
180
@@ -131,10 +200,12 @@ class SetMutation : public Mutation {
131
200
FieldValue&& value,
132
201
Precondition&& precondition);
133
202
134
- // TODO(rsgowman): ApplyToRemoteDocument()
203
+ MaybeDocumentPtr ApplyToRemoteDocument (
204
+ const MaybeDocumentPtr& maybe_doc,
205
+ const MutationResult& mutation_result) const override ;
135
206
136
- std::shared_ptr< const MaybeDocument> ApplyToLocalView (
137
- const std::shared_ptr< const MaybeDocument> & maybe_doc,
207
+ MaybeDocumentPtr ApplyToLocalView (
208
+ const MaybeDocumentPtr & maybe_doc,
138
209
const MaybeDocument* base_doc,
139
210
const Timestamp& local_write_time) const override ;
140
211
@@ -162,10 +233,12 @@ class PatchMutation : public Mutation {
162
233
FieldMask&& mask,
163
234
Precondition&& precondition);
164
235
165
- // TODO(rsgowman): ApplyToRemoteDocument()
236
+ MaybeDocumentPtr ApplyToRemoteDocument (
237
+ const MaybeDocumentPtr& maybe_doc,
238
+ const MutationResult& mutation_result) const override ;
166
239
167
- std::shared_ptr< const MaybeDocument> ApplyToLocalView (
168
- const std::shared_ptr< const MaybeDocument> & maybe_doc,
240
+ MaybeDocumentPtr ApplyToLocalView (
241
+ const MaybeDocumentPtr & maybe_doc,
169
242
const MaybeDocument* base_doc,
170
243
const Timestamp& local_write_time) const override ;
171
244
@@ -177,6 +250,21 @@ class PatchMutation : public Mutation {
177
250
const FieldMask mask_;
178
251
};
179
252
253
+ /* * Represents a Delete operation. */
254
+ class DeleteMutation : public Mutation {
255
+ public:
256
+ DeleteMutation (DocumentKey&& key, Precondition&& precondition);
257
+
258
+ MaybeDocumentPtr ApplyToRemoteDocument (
259
+ const MaybeDocumentPtr& maybe_doc,
260
+ const MutationResult& mutation_result) const override ;
261
+
262
+ MaybeDocumentPtr ApplyToLocalView (
263
+ const MaybeDocumentPtr& maybe_doc,
264
+ const MaybeDocument* base_doc,
265
+ const Timestamp& local_write_time) const override ;
266
+ };
267
+
180
268
} // namespace model
181
269
} // namespace firestore
182
270
} // namespace firebase
0 commit comments