FSTMutation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /** The version at which the mutation was committed. */
  40. - (const firebase::firestore::model::SnapshotVersion &)version;
  41. /**
  42. * The resulting fields returned from the backend after a FSTTransformMutation has been committed.
  43. * Contains one FieldValue for each FieldTransform that was in the mutation.
  44. *
  45. * Will be nil if the mutation was not a FSTTransformMutation.
  46. */
  47. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *_Nullable transformResults;
  48. @end
  49. #pragma mark - FSTMutation
  50. /**
  51. * A mutation describes a self-contained change to a document. Mutations can create, replace,
  52. * delete, and update subsets of documents.
  53. *
  54. * ## Subclassing Notes
  55. *
  56. * Subclasses of FSTMutation need to implement -applyTo:hasLocalMutations: to implement the
  57. * actual the behavior of mutation as applied to some source document.
  58. */
  59. @interface FSTMutation : NSObject
  60. - (id)init NS_UNAVAILABLE;
  61. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  62. precondition:(firebase::firestore::model::Precondition)precondition
  63. NS_DESIGNATED_INITIALIZER;
  64. /**
  65. * Applies this mutation to the given FSTDocument, FSTDeletedDocument or nil for the purposes of
  66. * computing a new remote document. Both the input and returned documents can be nil.
  67. *
  68. * @param maybeDoc The current state of the document to mutate. The input document should be nil if
  69. * it does not currently exist.
  70. * @param mutationResult Optional result info from the backend. If omitted, it's assumed that
  71. * this is merely a local (latency-compensated) application, and the resulting document will
  72. * have its hasLocalMutations flag set.
  73. *
  74. * @return The mutated document. The returned document may be nil, but only if maybeDoc was nil
  75. * and the mutation would not create a new document.
  76. *
  77. * NOTE: We preserve the version of the base document only in case of Set or Patch mutation to
  78. * denote what version of original document we've changed. In case of DeleteMutation we always reset
  79. * the version.
  80. *
  81. * Here's the expected transition table.
  82. *
  83. * MUTATION APPLIED TO RESULTS IN
  84. *
  85. * SetMutation Document(v3) Document(v3)
  86. * SetMutation DeletedDocument(v3) Document(v0)
  87. * SetMutation nil Document(v0)
  88. * PatchMutation Document(v3) Document(v3)
  89. * PatchMutation DeletedDocument(v3) DeletedDocument(v3)
  90. * PatchMutation nil nil
  91. * TransformMutation Document(v3) Document(v3)
  92. * TransformMutation DeletedDocument(v3) DeletedDocument(v3)
  93. * TransformMutation nil nil
  94. * DeleteMutation Document(v3) DeletedDocument(v0)
  95. * DeleteMutation DeletedDocument(v3) DeletedDocument(v0)
  96. * DeleteMutation nil DeletedDocument(v0)
  97. *
  98. * Note that FSTTransformMutations don't create FSTDocuments (in the case of being applied to an
  99. * FSTDeletedDocument), even though they would on the backend. This is because the client always
  100. * combines the FSTTransformMutation with a FSTSetMutation or FSTPatchMutation and we only want to
  101. * apply the transform if the prior mutation resulted in an FSTDocument (always true for an
  102. * FSTSetMutation, but not necessarily for an FSTPatchMutation).
  103. */
  104. - (nullable FSTMaybeDocument *)applyToRemoteDocument:(nullable FSTMaybeDocument *)maybeDoc
  105. mutationResult:(FSTMutationResult *)mutationResult;
  106. /**
  107. * Applies this mutation to the given MaybeDocument for the purposes of computing the new local view
  108. * of a document. Both the input and returned documents can be null.
  109. *
  110. * @param maybeDoc The current state of the document to mutate. The input document should be nil if
  111. * it does not currently exist.
  112. * @param baseDoc The state of the document prior to this mutation batch. The input document should
  113. * be nil if it the document did not exist.
  114. * @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
  115. * a part of.
  116. */
  117. - (nullable FSTMaybeDocument *)applyToLocalDocument:(nullable FSTMaybeDocument *)maybeDoc
  118. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  119. localWriteTime:(FIRTimestamp *)localWriteTime;
  120. - (const firebase::firestore::model::DocumentKey &)key;
  121. - (const firebase::firestore::model::Precondition &)precondition;
  122. @end
  123. #pragma mark - FSTSetMutation
  124. /**
  125. * A mutation that creates or replaces the document at the given key with the object value
  126. * contents.
  127. */
  128. @interface FSTSetMutation : FSTMutation
  129. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  130. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  131. /**
  132. * Initializes the set mutation.
  133. *
  134. * @param key Identifies the location of the document to mutate.
  135. * @param value An object value that describes the contents to store at the location named by the
  136. * key.
  137. * @param precondition The precondition for this mutation.
  138. */
  139. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  140. value:(FSTObjectValue *)value
  141. precondition:(firebase::firestore::model::Precondition)precondition
  142. NS_DESIGNATED_INITIALIZER;
  143. /** The object value to use when setting the document. */
  144. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  145. @end
  146. #pragma mark - FSTPatchMutation
  147. /**
  148. * A mutation that modifies fields of the document at the given key with the given values. The
  149. * values are applied through a field mask:
  150. *
  151. * * When a field is in both the mask and the values, the corresponding field is updated.
  152. * * When a field is in neither the mask nor the values, the corresponding field is unmodified.
  153. * * When a field is in the mask but not in the values, the corresponding field is deleted.
  154. * * When a field is not in the mask but is in the values, the values map is ignored.
  155. */
  156. @interface FSTPatchMutation : FSTMutation
  157. /** Returns the precondition for the given Precondition. */
  158. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  159. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  160. /**
  161. * Initializes a new patch mutation with an explicit FieldMask and FSTObjectValue representing
  162. * the updates to perform
  163. *
  164. * @param key Identifies the location of the document to mutate.
  165. * @param fieldMask The field mask specifying at what locations the data in value should be
  166. * applied.
  167. * @param value An FSTObjectValue containing the data to be written (using the paths in fieldMask
  168. * to determine the locations at which it should be applied).
  169. * @param precondition The precondition for this mutation.
  170. */
  171. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  172. fieldMask:(firebase::firestore::model::FieldMask)fieldMask
  173. value:(FSTObjectValue *)value
  174. precondition:(firebase::firestore::model::Precondition)precondition
  175. NS_DESIGNATED_INITIALIZER;
  176. /**
  177. * A mask to apply to |value|, where only fields that are in both the fieldMask and the value
  178. * will be updated.
  179. */
  180. - (const firebase::firestore::model::FieldMask &)fieldMask;
  181. /** The fields and associated values to use when patching the document. */
  182. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  183. @end
  184. #pragma mark - FSTTransformMutation
  185. /**
  186. * A mutation that modifies specific fields of the document with transform operations. Currently
  187. * the only supported transform is a server timestamp, but IP Address, increment(n), etc. could
  188. * be supported in the future.
  189. *
  190. * It is somewhat similar to an FSTPatchMutation in that it patches specific fields and has no
  191. * effect when applied to nil or an FSTDeletedDocument (see comment on [FSTMutation applyTo] for
  192. * rationale).
  193. */
  194. @interface FSTTransformMutation : FSTMutation
  195. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  196. precondition:(firebase::firestore::model::Precondition)precondition NS_UNAVAILABLE;
  197. /**
  198. * Initializes a new transform mutation with the specified field transforms.
  199. *
  200. * @param key Identifies the location of the document to mutate.
  201. * @param fieldTransforms A list of FieldTransform objects to perform to the document.
  202. */
  203. - (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
  204. fieldTransforms:(std::vector<firebase::firestore::model::FieldTransform>)fieldTransforms
  205. NS_DESIGNATED_INITIALIZER;
  206. /** The field transforms to use when transforming the document. */
  207. - (const std::vector<firebase::firestore::model::FieldTransform> &)fieldTransforms;
  208. @end
  209. #pragma mark - FSTDeleteMutation
  210. @interface FSTDeleteMutation : FSTMutation
  211. @end
  212. NS_ASSUME_NONNULL_END