FSTMutation.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <vector>
  18. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  19. @class FSTDocument;
  20. @class FSTDocumentKey;
  21. @class FSTFieldValue;
  22. @class FSTMaybeDocument;
  23. @class FSTObjectValue;
  24. @class FSTSnapshotVersion;
  25. @class FIRTimestamp;
  26. NS_ASSUME_NONNULL_BEGIN
  27. #pragma mark - FSTFieldMask
  28. /**
  29. * Provides a set of fields that can be used to partially patch a document. FieldMask is used in
  30. * conjunction with ObjectValue.
  31. *
  32. * Examples:
  33. * foo - Overwrites foo entirely with the provided value. If foo is not present in the companion
  34. * ObjectValue, the field is deleted.
  35. * foo.bar - Overwrites only the field bar of the object foo. If foo is not an object, foo is
  36. * replaced with an object containing bar.
  37. */
  38. @interface FSTFieldMask : NSObject
  39. - (id)init __attribute__((unavailable("Use initWithFields:")));
  40. /**
  41. * Initializes the field mask with the given field paths. Caller is expected to either copy or
  42. * or release the array of fields.
  43. */
  44. - (instancetype)initWithFields:(std::vector<firebase::firestore::model::FieldPath>)fields
  45. NS_DESIGNATED_INITIALIZER;
  46. - (const std::vector<firebase::firestore::model::FieldPath> &)fields;
  47. @end
  48. #pragma mark - FSTFieldTransform
  49. /** Represents a transform within a TransformMutation. */
  50. @protocol FSTTransformOperation <NSObject>
  51. @end
  52. /** Transforms a value into a server-generated timestamp. */
  53. @interface FSTServerTimestampTransform : NSObject <FSTTransformOperation>
  54. + (instancetype)serverTimestampTransform;
  55. @end
  56. /** A field path and the FSTTransformOperation to perform upon it. */
  57. @interface FSTFieldTransform : NSObject
  58. - (instancetype)init NS_UNAVAILABLE;
  59. - (instancetype)initWithPath:(firebase::firestore::model::FieldPath)path
  60. transform:(id<FSTTransformOperation>)transform NS_DESIGNATED_INITIALIZER;
  61. - (const firebase::firestore::model::FieldPath &)path;
  62. @property(nonatomic, strong, readonly) id<FSTTransformOperation> transform;
  63. @end
  64. #pragma mark - FSTPrecondition
  65. typedef NS_ENUM(NSUInteger, FSTPreconditionExists) {
  66. FSTPreconditionExistsNotSet,
  67. FSTPreconditionExistsYes,
  68. FSTPreconditionExistsNo,
  69. };
  70. /**
  71. * Encodes a precondition for a mutation. This follows the model that the backend accepts with the
  72. * special case of an explicit "empty" precondition (meaning no precondition).
  73. */
  74. @interface FSTPrecondition : NSObject
  75. /** Creates a new FSTPrecondition with an exists flag. */
  76. + (FSTPrecondition *)preconditionWithExists:(BOOL)exists;
  77. /** Creates a new FSTPrecondition based on a time the document exists at. */
  78. + (FSTPrecondition *)preconditionWithUpdateTime:(FSTSnapshotVersion *)updateTime;
  79. /** Returns a precondition representing no precondition. */
  80. + (FSTPrecondition *)none;
  81. /**
  82. * Returns true if the preconditions is valid for the given document (or null if no document is
  83. * available).
  84. */
  85. - (BOOL)isValidForDocument:(FSTMaybeDocument *_Nullable)maybeDoc;
  86. /** Returns whether this Precondition represents no precondition. */
  87. - (BOOL)isNone;
  88. /** If set, preconditions a mutation based on the last updateTime. */
  89. @property(nonatomic, strong, readonly, nullable) FSTSnapshotVersion *updateTime;
  90. /**
  91. * If set, preconditions a mutation based on whether the document exists.
  92. * Uses FSTPreconditionExistsNotSet to mark as unset.
  93. */
  94. @property(nonatomic, assign, readonly) FSTPreconditionExists exists;
  95. @end
  96. #pragma mark - FSTMutationResult
  97. @interface FSTMutationResult : NSObject
  98. - (instancetype)init NS_UNAVAILABLE;
  99. - (instancetype)initWithVersion:(FSTSnapshotVersion *_Nullable)version
  100. transformResults:(NSArray<FSTFieldValue *> *_Nullable)transformResults
  101. NS_DESIGNATED_INITIALIZER;
  102. /** The version at which the mutation was committed or null for a delete. */
  103. @property(nonatomic, strong, readonly, nullable) FSTSnapshotVersion *version;
  104. /**
  105. * The resulting fields returned from the backend after a FSTTransformMutation has been committed.
  106. * Contains one FieldValue for each FSTFieldTransform that was in the mutation.
  107. *
  108. * Will be nil if the mutation was not a FSTTransformMutation.
  109. */
  110. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *_Nullable transformResults;
  111. @end
  112. #pragma mark - FSTMutation
  113. /**
  114. * A mutation describes a self-contained change to a document. Mutations can create, replace,
  115. * delete, and update subsets of documents.
  116. *
  117. * ## Subclassing Notes
  118. *
  119. * Subclasses of FSTMutation need to implement -applyTo:hasLocalMutations: to implement the
  120. * actual the behavior of mutation as applied to some source document.
  121. */
  122. @interface FSTMutation : NSObject
  123. - (id)init NS_UNAVAILABLE;
  124. - (instancetype)initWithKey:(FSTDocumentKey *)key
  125. precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER;
  126. /**
  127. * Applies this mutation to the given FSTDocument, FSTDeletedDocument or nil, if we don't have
  128. * information about this document. Both the input and returned documents can be nil.
  129. *
  130. * @param maybeDoc The current state of the document to mutate. The input document should be nil if
  131. * it does not currently exist.
  132. * @param baseDoc The state of the document prior to this mutation batch. The input document should
  133. * be nil if it the document did not exist.
  134. * @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
  135. * a part of.
  136. * @param mutationResult Optional result info from the backend. If omitted, it's assumed that
  137. * this is merely a local (latency-compensated) application, and the resulting document will
  138. * have its hasLocalMutations flag set.
  139. *
  140. * @return The mutated document. The returned document may be nil, but only if maybeDoc was nil
  141. * and the mutation would not create a new document.
  142. *
  143. * NOTE: We preserve the version of the base document only in case of Set or Patch mutation to
  144. * denote what version of original document we've changed. In case of DeleteMutation we always reset
  145. * the version.
  146. *
  147. * Here's the expected transition table.
  148. *
  149. * MUTATION APPLIED TO RESULTS IN
  150. *
  151. * SetMutation Document(v3) Document(v3)
  152. * SetMutation DeletedDocument(v3) Document(v0)
  153. * SetMutation nil Document(v0)
  154. * PatchMutation Document(v3) Document(v3)
  155. * PatchMutation DeletedDocument(v3) DeletedDocument(v3)
  156. * PatchMutation nil nil
  157. * TransformMutation Document(v3) Document(v3)
  158. * TransformMutation DeletedDocument(v3) DeletedDocument(v3)
  159. * TransformMutation nil nil
  160. * DeleteMutation Document(v3) DeletedDocument(v0)
  161. * DeleteMutation DeletedDocument(v3) DeletedDocument(v0)
  162. * DeleteMutation nil DeletedDocument(v0)
  163. *
  164. * Note that FSTTransformMutations don't create FSTDocuments (in the case of being applied to an
  165. * FSTDeletedDocument), even though they would on the backend. This is because the client always
  166. * combines the FSTTransformMutation with a FSTSetMutation or FSTPatchMutation and we only want to
  167. * apply the transform if the prior mutation resulted in an FSTDocument (always true for an
  168. * FSTSetMutation, but not necessarily for an FSTPatchMutation).
  169. */
  170. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  171. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  172. localWriteTime:(FIRTimestamp *)localWriteTime
  173. mutationResult:(nullable FSTMutationResult *)mutationResult;
  174. /**
  175. * A helper version of applyTo for applying mutations locally (without a mutation result from the
  176. * backend).
  177. */
  178. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  179. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  180. localWriteTime:(nullable FIRTimestamp *)localWriteTime;
  181. @property(nonatomic, strong, readonly) FSTDocumentKey *key;
  182. /** The precondition for this mutation. */
  183. @property(nonatomic, strong, readonly) FSTPrecondition *precondition;
  184. @end
  185. #pragma mark - FSTSetMutation
  186. /**
  187. * A mutation that creates or replaces the document at the given key with the object value
  188. * contents.
  189. */
  190. @interface FSTSetMutation : FSTMutation
  191. - (instancetype)initWithKey:(FSTDocumentKey *)key
  192. precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE;
  193. /**
  194. * Initializes the set mutation.
  195. *
  196. * @param key Identifies the location of the document to mutate.
  197. * @param value An object value that describes the contents to store at the location named by the
  198. * key.
  199. * @param precondition The precondition for this mutation.
  200. */
  201. - (instancetype)initWithKey:(FSTDocumentKey *)key
  202. value:(FSTObjectValue *)value
  203. precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER;
  204. /** The object value to use when setting the document. */
  205. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  206. @end
  207. #pragma mark - FSTPatchMutation
  208. /**
  209. * A mutation that modifies fields of the document at the given key with the given values. The
  210. * values are applied through a field mask:
  211. *
  212. * * When a field is in both the mask and the values, the corresponding field is updated.
  213. * * When a field is in neither the mask nor the values, the corresponding field is unmodified.
  214. * * When a field is in the mask but not in the values, the corresponding field is deleted.
  215. * * When a field is not in the mask but is in the values, the values map is ignored.
  216. */
  217. @interface FSTPatchMutation : FSTMutation
  218. /** Returns the precondition for the given FSTPrecondition. */
  219. - (instancetype)initWithKey:(FSTDocumentKey *)key
  220. precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE;
  221. /**
  222. * Initializes a new patch mutation with an explicit FSTFieldMask and FSTObjectValue representing
  223. * the updates to perform
  224. *
  225. * @param key Identifies the location of the document to mutate.
  226. * @param fieldMask The field mask specifying at what locations the data in value should be
  227. * applied.
  228. * @param value An FSTObjectValue containing the data to be written (using the paths in fieldMask
  229. * to determine the locations at which it should be applied).
  230. * @param precondition The precondition for this mutation.
  231. */
  232. - (instancetype)initWithKey:(FSTDocumentKey *)key
  233. fieldMask:(FSTFieldMask *)fieldMask
  234. value:(FSTObjectValue *)value
  235. precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER;
  236. /** The fields and associated values to use when patching the document. */
  237. @property(nonatomic, strong, readonly) FSTObjectValue *value;
  238. /**
  239. * A mask to apply to |value|, where only fields that are in both the fieldMask and the value
  240. * will be updated.
  241. */
  242. @property(nonatomic, strong, readonly) FSTFieldMask *fieldMask;
  243. @end
  244. #pragma mark - FSTTransformMutation
  245. /**
  246. * A mutation that modifies specific fields of the document with transform operations. Currently
  247. * the only supported transform is a server timestamp, but IP Address, increment(n), etc. could
  248. * be supported in the future.
  249. *
  250. * It is somewhat similar to an FSTPatchMutation in that it patches specific fields and has no
  251. * effect when applied to nil or an FSTDeletedDocument (see comment on [FSTMutation applyTo] for
  252. * rationale).
  253. */
  254. @interface FSTTransformMutation : FSTMutation
  255. - (instancetype)initWithKey:(FSTDocumentKey *)key
  256. precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE;
  257. /**
  258. * Initializes a new transform mutation with the specified field transforms.
  259. *
  260. * @param key Identifies the location of the document to mutate.
  261. * @param fieldTransforms A list of FSTFieldTransform objects to perform to the document.
  262. */
  263. - (instancetype)initWithKey:(FSTDocumentKey *)key
  264. fieldTransforms:(NSArray<FSTFieldTransform *> *)fieldTransforms
  265. NS_DESIGNATED_INITIALIZER;
  266. /** The field transforms to use when transforming the document. */
  267. @property(nonatomic, strong, readonly) NSArray<FSTFieldTransform *> *fieldTransforms;
  268. @end
  269. #pragma mark - FSTDeleteMutation
  270. @interface FSTDeleteMutation : FSTMutation
  271. @end
  272. NS_ASSUME_NONNULL_END