FSTMutation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright 2017 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. #import <Foundation/Foundation.h>
  17. #include <memory>
  18. #include <vector>
  19. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  21. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  22. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  23. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  24. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  25. #include "Firestore/core/src/firebase/firestore/model/transform_operations.h"
  26. #include "absl/types/optional.h"
  27. @class FSTDocument;
  28. @class FSTFieldValue;
  29. @class FSTMaybeDocument;
  30. @class FSTObjectValue;
  31. @class FIRTimestamp;
  32. NS_ASSUME_NONNULL_BEGIN
  33. #pragma mark - FSTMutationResult
  34. @interface FSTMutationResult : NSObject
  35. - (instancetype)init NS_UNAVAILABLE;
  36. - (instancetype)initWithVersion:(firebase::firestore::model::SnapshotVersion)version
  37. transformResults:(NSArray<FSTFieldValue *> *_Nullable)transformResults
  38. NS_DESIGNATED_INITIALIZER;
  39. /**
  40. * The version at which the mutation was committed.
  41. *
  42. * - For most operations, this is the updateTime in the WriteResult.
  43. * - For deletes, it is the commitTime of the WriteResponse (because deletes are not stored
  44. * and have no updateTime).
  45. *
  46. * Note that these versions can be different: No-op writes will not change the updateTime even
  47. * though the commitTime advances.
  48. */
  49. - (const firebase::firestore::model::SnapshotVersion &)version;
  50. /**
  51. * The resulting fields returned from the backend after a FSTTransformMutation has been committed.
  52. * Contains one FieldValue for each FieldTransform that was in the mutation.
  53. *
  54. * Will be nil if the mutation was not a FSTTransformMutation.
  55. */
  56. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *_Nullable transformResults;
  57. @end
  58. #pragma mark - FSTMutation
  59. /**
  60. * Represents a Mutation of a document. Different subclasses of Mutation will perform different
  61. * kinds of changes to a base document. For example, an FSTSetMutation replaces the value of a
  62. * document and an FSTDeleteMutation deletes a document.
  63. *
  64. * Subclasses of FSTMutation need to implement `applyToRemoteDocument:mutationResult:` and
  65. * `applyToLocalDocument:baseDocument:localWriteTime:` to implement the actual the behavior of
  66. * mutations as applied to some source document.
  67. *
  68. * In addition to the value of the document mutations also operate on the version. For local
  69. * mutations (mutations that haven't been committed yet), we preserve the existing version for Set,
  70. * Patch, and Transform mutations. For local deletes, we reset the version to 0.
  71. *
  72. * Here's the expected transition table.
  73. *
  74. * MUTATION APPLIED TO RESULTS IN
  75. *
  76. * SetMutation Document(v3) Document(v3)
  77. * SetMutation DeletedDocument(v3) Document(v0)
  78. * SetMutation nil Document(v0)
  79. * PatchMutation Document(v3) Document(v3)
  80. * PatchMutation DeletedDocument(v3) DeletedDocument(v3)
  81. * PatchMutation nil nil
  82. * TransformMutation Document(v3) Document(v3)
  83. * TransformMutation DeletedDocument(v3) DeletedDocument(v3)
  84. * TransformMutation nil nil
  85. * DeleteMutation Document(v3) DeletedDocument(v0)
  86. * DeleteMutation DeletedDocument(v3) DeletedDocument(v0)
  87. * DeleteMutation nil DeletedDocument(v0)
  88. *
  89. * For acknowledged mutations, we use the updateTime of the WriteResponse as the resulting version
  90. * for Set, Patch, and Transform mutations. As deletes have no explicit update time, we use the
  91. * commitTime of the WriteResponse for acknowledged deletes.
  92. *
  93. * If a mutation is acknowledged by the backend but fails the precondition check locally, we
  94. * return an `FSTUnknownDocument` and rely on Watch to send us the updated version.
  95. *
  96. * Note that FSTTransformMutations don't create Documents (in the case of being applied to an
  97. * FSTDeletedDocument), even though they would on the backend. This is because the client always
  98. * combines the FSTTransformMutations with a FSTSetMutation or FSTPatchMutation and we only want to
  99. * apply the transform if the prior mutation resulted in an FSTDocument (always true for an
  100. * FSTSetMutation, but not necessarily for an FSTPatchMutation).
  101. */
  102. @interface FSTMutation : NSObject
  103. - (id)init NS_UNAVAILABLE;
  104. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  105. precondition:(firebase::firestore::model::Precondition)precondition
  106. NS_DESIGNATED_INITIALIZER;
  107. /**
  108. * Applies this mutation to the given FSTMaybeDocument for the purposes of computing a new remote
  109. * document. If the input document doesn't match the expected state (e.g. it is nil or outdated),
  110. * an `FSTUnknownDocument` can be returned.
  111. *
  112. * @param maybeDoc The document to mutate. The input document can be nil if the client has no
  113. * knowledge of the pre-mutation state of the document.
  114. * @param mutationResult The result of applying the mutation from the backend.
  115. * @return The mutated document. The returned document may be an FSTUnknownDocument if the mutation
  116. * could not be applied to the locally cached base document.
  117. */
  118. - (FSTMaybeDocument *)applyToRemoteDocument:(nullable FSTMaybeDocument *)maybeDoc
  119. mutationResult:(FSTMutationResult *)mutationResult;
  120. /**
  121. * Applies this mutation to the given FSTMaybeDocument for the purposes of computing the new local
  122. * view of a document. Both the input and returned documents can be nil.
  123. *
  124. * @param maybeDoc The document to mutate. The input document can be nil if the client has no
  125. * knowledge of the pre-mutation state of the document.
  126. * @param baseDoc The state of the document prior to this mutation batch. The input document can
  127. * be nil if the client has no knowledge of the pre-mutation state of the document.
  128. * @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
  129. * a part of.
  130. * @return The mutated document. The returned document may be nil, but only if maybeDoc was nil
  131. * and the mutation would not create a new document.
  132. */
  133. - (nullable FSTMaybeDocument *)applyToLocalDocument:(nullable FSTMaybeDocument *)maybeDoc
  134. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  135. localWriteTime:(FIRTimestamp *)localWriteTime;
  136. - (const firebase::firestore::model::DocumentKey &)key;
  137. - (const firebase::firestore::model::Precondition &)precondition;
  138. @end
  139. #pragma mark - FSTSetMutation
  140. /**
  141. * A mutation that creates or replaces the document at the given key with the object value
  142. * contents.
  143. */
  144. @interface FSTSetMutation : FSTMutation
  145. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  146. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  147. /**
  148. * Initializes the set mutation.
  149. *
  150. * @param key Identifies the location of the document to mutate.
  151. * @param value An object value that describes the contents to store at the location named by the
  152. * key.
  153. * @param precondition The precondition for this mutation.
  154. */
  155. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  156. value:(FSTObjectValue *)value
  157. precondition:(firebase::firestore::model::Precondition)precondition
  158. NS_DESIGNATED_INITIALIZER;
  159. /** The object value to use when setting the document. */
  160. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  161. @end
  162. #pragma mark - FSTPatchMutation
  163. /**
  164. * A mutation that modifies fields of the document at the given key with the given values. The
  165. * values are applied through a field mask:
  166. *
  167. * * When a field is in both the mask and the values, the corresponding field is updated.
  168. * * When a field is in neither the mask nor the values, the corresponding field is unmodified.
  169. * * When a field is in the mask but not in the values, the corresponding field is deleted.
  170. * * When a field is not in the mask but is in the values, the values map is ignored.
  171. */
  172. @interface FSTPatchMutation : FSTMutation
  173. /** Returns the precondition for the given Precondition. */
  174. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  175. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  176. /**
  177. * Initializes a new patch mutation with an explicit FieldMask and FSTObjectValue representing
  178. * the updates to perform
  179. *
  180. * @param key Identifies the location of the document to mutate.
  181. * @param fieldMask The field mask specifying at what locations the data in value should be
  182. * applied.
  183. * @param value An FSTObjectValue containing the data to be written (using the paths in fieldMask
  184. * to determine the locations at which it should be applied).
  185. * @param precondition The precondition for this mutation.
  186. */
  187. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  188. fieldMask:(firebase::firestore::model::FieldMask)fieldMask
  189. value:(FSTObjectValue *)value
  190. precondition:(firebase::firestore::model::Precondition)precondition
  191. NS_DESIGNATED_INITIALIZER;
  192. /**
  193. * A mask to apply to |value|, where only fields that are in both the fieldMask and the value
  194. * will be updated.
  195. */
  196. - (const firebase::firestore::model::FieldMask &)fieldMask;
  197. /** The fields and associated values to use when patching the document. */
  198. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  199. @end
  200. #pragma mark - FSTTransformMutation
  201. /**
  202. * A mutation that modifies specific fields of the document with transform operations. Currently
  203. * the only supported transform is a server timestamp, but IP Address, increment(n), etc. could
  204. * be supported in the future.
  205. *
  206. * It is somewhat similar to an FSTPatchMutation in that it patches specific fields and has no
  207. * effect when applied to nil or an FSTDeletedDocument (see comment on [FSTMutation applyTo] for
  208. * rationale).
  209. */
  210. @interface FSTTransformMutation : FSTMutation
  211. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  212. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  213. /**
  214. * Initializes a new transform mutation with the specified field transforms.
  215. *
  216. * @param key Identifies the location of the document to mutate.
  217. * @param fieldTransforms A list of FieldTransform objects to perform to the document.
  218. */
  219. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  220. fieldTransforms:(std::vector<firebase::firestore::model::FieldTransform>)fieldTransforms
  221. NS_DESIGNATED_INITIALIZER;
  222. /** The field transforms to use when transforming the document. */
  223. - (const std::vector<firebase::firestore::model::FieldTransform> &)fieldTransforms;
  224. @end
  225. #pragma mark - FSTDeleteMutation
  226. @interface FSTDeleteMutation : FSTMutation
  227. @end
  228. NS_ASSUME_NONNULL_END