FSTMutation.h 12 KB

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