FSTMutation.h 10 KB

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