FSTMutation.h 12 KB

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