FSTMutation.h 10 KB

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