FSTMutation.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "Firestore/Source/Model/FSTMutation.h"
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #import "FIRTimestamp.h"
  22. #import "Firestore/Source/Model/FSTDocument.h"
  23. #import "Firestore/Source/Model/FSTFieldValue.h"
  24. #import "Firestore/Source/Util/FSTClasses.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  26. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  27. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  28. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  29. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  30. #include "Firestore/core/src/firebase/firestore/model/transform_operations.h"
  31. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  32. using firebase::firestore::model::ArrayTransform;
  33. using firebase::firestore::model::DocumentKey;
  34. using firebase::firestore::model::FieldMask;
  35. using firebase::firestore::model::FieldPath;
  36. using firebase::firestore::model::FieldTransform;
  37. using firebase::firestore::model::Precondition;
  38. using firebase::firestore::model::ServerTimestampTransform;
  39. using firebase::firestore::model::SnapshotVersion;
  40. using firebase::firestore::model::TransformOperation;
  41. using firebase::firestore::util::Hash;
  42. NS_ASSUME_NONNULL_BEGIN
  43. #pragma mark - FSTMutationResult
  44. @implementation FSTMutationResult {
  45. SnapshotVersion _version;
  46. }
  47. - (instancetype)initWithVersion:(SnapshotVersion)version
  48. transformResults:(nullable NSArray<FSTFieldValue *> *)transformResults {
  49. if (self = [super init]) {
  50. _version = std::move(version);
  51. _transformResults = transformResults;
  52. }
  53. return self;
  54. }
  55. - (const SnapshotVersion &)version {
  56. return _version;
  57. }
  58. @end
  59. #pragma mark - FSTMutation
  60. @implementation FSTMutation {
  61. DocumentKey _key;
  62. Precondition _precondition;
  63. }
  64. - (instancetype)initWithKey:(DocumentKey)key precondition:(Precondition)precondition {
  65. if (self = [super init]) {
  66. _key = std::move(key);
  67. _precondition = std::move(precondition);
  68. }
  69. return self;
  70. }
  71. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  72. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  73. localWriteTime:(FIRTimestamp *)localWriteTime
  74. mutationResult:(nullable FSTMutationResult *)mutationResult {
  75. @throw FSTAbstractMethodException(); // NOLINT
  76. }
  77. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  78. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  79. localWriteTime:(nullable FIRTimestamp *)localWriteTime {
  80. return
  81. [self applyTo:maybeDoc baseDocument:baseDoc localWriteTime:localWriteTime mutationResult:nil];
  82. }
  83. - (const DocumentKey &)key {
  84. return _key;
  85. }
  86. - (const firebase::firestore::model::Precondition &)precondition {
  87. return _precondition;
  88. }
  89. @end
  90. #pragma mark - FSTSetMutation
  91. @implementation FSTSetMutation
  92. - (instancetype)initWithKey:(DocumentKey)key
  93. value:(FSTObjectValue *)value
  94. precondition:(Precondition)precondition {
  95. if (self = [super initWithKey:std::move(key) precondition:std::move(precondition)]) {
  96. _value = value;
  97. }
  98. return self;
  99. }
  100. - (NSString *)description {
  101. return [NSString stringWithFormat:@"<FSTSetMutation key=%s value=%@ precondition=%@>",
  102. self.key.ToString().c_str(), self.value,
  103. self.precondition.description()];
  104. }
  105. - (BOOL)isEqual:(id)other {
  106. if (other == self) {
  107. return YES;
  108. }
  109. if (![other isKindOfClass:[FSTSetMutation class]]) {
  110. return NO;
  111. }
  112. FSTSetMutation *otherMutation = (FSTSetMutation *)other;
  113. return self.key == otherMutation.key && [self.value isEqual:otherMutation.value] &&
  114. self.precondition == otherMutation.precondition;
  115. }
  116. - (NSUInteger)hash {
  117. return Hash(self.key, self.precondition, [self.value hash]);
  118. }
  119. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  120. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  121. localWriteTime:(FIRTimestamp *)localWriteTime
  122. mutationResult:(nullable FSTMutationResult *)mutationResult {
  123. if (mutationResult) {
  124. HARD_ASSERT(!mutationResult.transformResults, "Transform results received by FSTSetMutation.");
  125. }
  126. if (!self.precondition.IsValidFor(maybeDoc)) {
  127. return maybeDoc;
  128. }
  129. BOOL hasLocalMutations = (mutationResult == nil);
  130. if (!maybeDoc || [maybeDoc isMemberOfClass:[FSTDeletedDocument class]]) {
  131. // If the document didn't exist before, create it.
  132. return [FSTDocument documentWithData:self.value
  133. key:self.key
  134. version:SnapshotVersion::None()
  135. hasLocalMutations:hasLocalMutations];
  136. }
  137. HARD_ASSERT([maybeDoc isMemberOfClass:[FSTDocument class]], "Unknown MaybeDocument type %s",
  138. [maybeDoc class]);
  139. FSTDocument *doc = (FSTDocument *)maybeDoc;
  140. HARD_ASSERT(doc.key == self.key, "Can only set a document with the same key");
  141. return [FSTDocument documentWithData:self.value
  142. key:doc.key
  143. version:doc.version
  144. hasLocalMutations:hasLocalMutations];
  145. }
  146. @end
  147. #pragma mark - FSTPatchMutation
  148. @implementation FSTPatchMutation {
  149. FieldMask _fieldMask;
  150. }
  151. - (instancetype)initWithKey:(DocumentKey)key
  152. fieldMask:(FieldMask)fieldMask
  153. value:(FSTObjectValue *)value
  154. precondition:(Precondition)precondition {
  155. self = [super initWithKey:std::move(key) precondition:std::move(precondition)];
  156. if (self) {
  157. _fieldMask = std::move(fieldMask);
  158. _value = value;
  159. }
  160. return self;
  161. }
  162. - (const firebase::firestore::model::FieldMask &)fieldMask {
  163. return _fieldMask;
  164. }
  165. - (BOOL)isEqual:(id)other {
  166. if (other == self) {
  167. return YES;
  168. }
  169. if (![other isKindOfClass:[FSTPatchMutation class]]) {
  170. return NO;
  171. }
  172. FSTPatchMutation *otherMutation = (FSTPatchMutation *)other;
  173. return self.key == otherMutation.key && self.fieldMask == otherMutation.fieldMask &&
  174. [self.value isEqual:otherMutation.value] &&
  175. self.precondition == otherMutation.precondition;
  176. }
  177. - (NSUInteger)hash {
  178. return Hash(self.key, self.precondition, self.fieldMask, [self.value hash]);
  179. }
  180. - (NSString *)description {
  181. return [NSString stringWithFormat:@"<FSTPatchMutation key=%s mask=%s value=%@ precondition=%@>",
  182. self.key.ToString().c_str(), self.fieldMask.ToString().c_str(),
  183. self.value, self.precondition.description()];
  184. }
  185. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  186. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  187. localWriteTime:(FIRTimestamp *)localWriteTime
  188. mutationResult:(nullable FSTMutationResult *)mutationResult {
  189. if (mutationResult) {
  190. HARD_ASSERT(!mutationResult.transformResults,
  191. "Transform results received by FSTPatchMutation.");
  192. }
  193. if (!self.precondition.IsValidFor(maybeDoc)) {
  194. return maybeDoc;
  195. }
  196. BOOL hasLocalMutations = (mutationResult == nil);
  197. if (!maybeDoc || [maybeDoc isMemberOfClass:[FSTDeletedDocument class]]) {
  198. // Precondition applied, so create the document if necessary
  199. const DocumentKey &key = maybeDoc ? maybeDoc.key : self.key;
  200. SnapshotVersion version = maybeDoc ? maybeDoc.version : SnapshotVersion::None();
  201. maybeDoc = [FSTDocument documentWithData:[FSTObjectValue objectValue]
  202. key:key
  203. version:std::move(version)
  204. hasLocalMutations:hasLocalMutations];
  205. }
  206. HARD_ASSERT([maybeDoc isMemberOfClass:[FSTDocument class]], "Unknown MaybeDocument type %s",
  207. [maybeDoc class]);
  208. FSTDocument *doc = (FSTDocument *)maybeDoc;
  209. HARD_ASSERT(doc.key == self.key, "Can only patch a document with the same key");
  210. FSTObjectValue *newData = [self patchObjectValue:doc.data];
  211. return [FSTDocument documentWithData:newData
  212. key:doc.key
  213. version:doc.version
  214. hasLocalMutations:hasLocalMutations];
  215. }
  216. - (FSTObjectValue *)patchObjectValue:(FSTObjectValue *)objectValue {
  217. FSTObjectValue *result = objectValue;
  218. for (const FieldPath &fieldPath : self.fieldMask) {
  219. if (!fieldPath.empty()) {
  220. FSTFieldValue *newValue = [self.value valueForPath:fieldPath];
  221. if (newValue) {
  222. result = [result objectBySettingValue:newValue forPath:fieldPath];
  223. } else {
  224. result = [result objectByDeletingPath:fieldPath];
  225. }
  226. }
  227. }
  228. return result;
  229. }
  230. @end
  231. @implementation FSTTransformMutation {
  232. /** The field transforms to use when transforming the document. */
  233. std::vector<FieldTransform> _fieldTransforms;
  234. }
  235. - (instancetype)initWithKey:(DocumentKey)key
  236. fieldTransforms:(std::vector<FieldTransform>)fieldTransforms {
  237. // NOTE: We set a precondition of exists: true as a safety-check, since we always combine
  238. // FSTTransformMutations with a FSTSetMutation or FSTPatchMutation which (if successful) should
  239. // end up with an existing document.
  240. if (self = [super initWithKey:std::move(key) precondition:Precondition::Exists(true)]) {
  241. _fieldTransforms = std::move(fieldTransforms);
  242. }
  243. return self;
  244. }
  245. - (const std::vector<FieldTransform> &)fieldTransforms {
  246. return _fieldTransforms;
  247. }
  248. - (BOOL)isEqual:(id)other {
  249. if (other == self) {
  250. return YES;
  251. }
  252. if (![other isKindOfClass:[FSTTransformMutation class]]) {
  253. return NO;
  254. }
  255. FSTTransformMutation *otherMutation = (FSTTransformMutation *)other;
  256. return self.key == otherMutation.key && self.fieldTransforms == otherMutation.fieldTransforms &&
  257. self.precondition == otherMutation.precondition;
  258. }
  259. - (NSUInteger)hash {
  260. NSUInteger result = self.key.Hash();
  261. result = 31 * result + self.precondition.Hash();
  262. for (const auto &transform : self.fieldTransforms) {
  263. result = 31 * result + transform.Hash();
  264. }
  265. return result;
  266. }
  267. - (NSString *)description {
  268. std::string fieldTransforms;
  269. for (const auto &transform : self.fieldTransforms) {
  270. fieldTransforms += " " + transform.path().CanonicalString();
  271. }
  272. return [NSString stringWithFormat:@"<FSTTransformMutation key=%s transforms=%s precondition=%@>",
  273. self.key.ToString().c_str(), fieldTransforms.c_str(),
  274. self.precondition.description()];
  275. }
  276. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  277. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  278. localWriteTime:(FIRTimestamp *)localWriteTime
  279. mutationResult:(nullable FSTMutationResult *)mutationResult {
  280. if (mutationResult) {
  281. HARD_ASSERT(mutationResult.transformResults,
  282. "Transform results missing for FSTTransformMutation.");
  283. }
  284. if (!self.precondition.IsValidFor(maybeDoc)) {
  285. return maybeDoc;
  286. }
  287. // We only support transforms with precondition exists, so we can only apply it to an existing
  288. // document
  289. HARD_ASSERT([maybeDoc isMemberOfClass:[FSTDocument class]], "Unknown MaybeDocument type %s",
  290. [maybeDoc class]);
  291. FSTDocument *doc = (FSTDocument *)maybeDoc;
  292. HARD_ASSERT(doc.key == self.key, "Can only transform a document with the same key");
  293. BOOL hasLocalMutations = (mutationResult == nil);
  294. NSArray<FSTFieldValue *> *transformResults;
  295. if (mutationResult) {
  296. transformResults =
  297. [self serverTransformResultsWithBaseDocument:baseDoc
  298. serverTransformResults:mutationResult.transformResults];
  299. } else {
  300. transformResults =
  301. [self localTransformResultsWithBaseDocument:baseDoc writeTime:localWriteTime];
  302. }
  303. FSTObjectValue *newData = [self transformObject:doc.data transformResults:transformResults];
  304. return [FSTDocument documentWithData:newData
  305. key:doc.key
  306. version:doc.version
  307. hasLocalMutations:hasLocalMutations];
  308. }
  309. /**
  310. * Creates an array of "transform results" (a transform result is a field value representing the
  311. * result of applying a transform) for use after a FSTTransformMutation has been acknowledged by
  312. * the server.
  313. *
  314. * @param baseDocument The document prior to applying this mutation batch.
  315. * @param serverTransformResults The transform results received by the server.
  316. * @return The transform results array.
  317. */
  318. - (NSArray<FSTFieldValue *> *)
  319. serverTransformResultsWithBaseDocument:(nullable FSTMaybeDocument *)baseDocument
  320. serverTransformResults:(NSArray<FSTFieldValue *> *)serverTransformResults {
  321. NSMutableArray<FSTFieldValue *> *transformResults = [NSMutableArray array];
  322. HARD_ASSERT(self.fieldTransforms.size() == serverTransformResults.count,
  323. "server transform result count (%s) should match field transforms count (%s)",
  324. (unsigned long)serverTransformResults.count, self.fieldTransforms.size());
  325. for (NSUInteger i = 0; i < serverTransformResults.count; i++) {
  326. const FieldTransform &fieldTransform = self.fieldTransforms[i];
  327. const TransformOperation &transform = fieldTransform.transformation();
  328. FSTFieldValue *previousValue = nil;
  329. if ([baseDocument isMemberOfClass:[FSTDocument class]]) {
  330. previousValue = [((FSTDocument *)baseDocument) fieldForPath:fieldTransform.path()];
  331. }
  332. [transformResults
  333. addObject:transform.ApplyToRemoteDocument(previousValue, serverTransformResults[i])];
  334. }
  335. return transformResults;
  336. }
  337. /**
  338. * Creates an array of "transform results" (a transform result is a field value representing the
  339. * result of applying a transform) for use when applying an FSTTransformMutation locally.
  340. *
  341. * @param baseDocument The document prior to applying this mutation batch.
  342. * @param localWriteTime The local time of the transform mutation (used to generate
  343. * FSTServerTimestampValues).
  344. * @return The transform results array.
  345. */
  346. - (NSArray<FSTFieldValue *> *)localTransformResultsWithBaseDocument:
  347. (nullable FSTMaybeDocument *)baseDocument
  348. writeTime:(FIRTimestamp *)localWriteTime {
  349. NSMutableArray<FSTFieldValue *> *transformResults = [NSMutableArray array];
  350. for (const FieldTransform &fieldTransform : self.fieldTransforms) {
  351. const TransformOperation &transform = fieldTransform.transformation();
  352. FSTFieldValue *previousValue = nil;
  353. if ([baseDocument isMemberOfClass:[FSTDocument class]]) {
  354. previousValue = [((FSTDocument *)baseDocument) fieldForPath:fieldTransform.path()];
  355. }
  356. [transformResults addObject:transform.ApplyToLocalView(previousValue, localWriteTime)];
  357. }
  358. return transformResults;
  359. }
  360. - (FSTObjectValue *)transformObject:(FSTObjectValue *)objectValue
  361. transformResults:(NSArray<FSTFieldValue *> *)transformResults {
  362. HARD_ASSERT(transformResults.count == self.fieldTransforms.size(),
  363. "Transform results length mismatch.");
  364. for (size_t i = 0; i < self.fieldTransforms.size(); i++) {
  365. const FieldTransform &fieldTransform = self.fieldTransforms[i];
  366. const FieldPath &fieldPath = fieldTransform.path();
  367. objectValue = [objectValue objectBySettingValue:transformResults[i] forPath:fieldPath];
  368. }
  369. return objectValue;
  370. }
  371. @end
  372. #pragma mark - FSTDeleteMutation
  373. @implementation FSTDeleteMutation
  374. - (BOOL)isEqual:(id)other {
  375. if (other == self) {
  376. return YES;
  377. }
  378. if (![other isKindOfClass:[FSTDeleteMutation class]]) {
  379. return NO;
  380. }
  381. FSTDeleteMutation *otherMutation = (FSTDeleteMutation *)other;
  382. return self.key == otherMutation.key && self.precondition == otherMutation.precondition;
  383. }
  384. - (NSUInteger)hash {
  385. return Hash(self.key, self.precondition);
  386. }
  387. - (NSString *)description {
  388. return [NSString stringWithFormat:@"<FSTDeleteMutation key=%s precondition=%@>",
  389. self.key.ToString().c_str(), self.precondition.description()];
  390. }
  391. - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
  392. baseDocument:(nullable FSTMaybeDocument *)baseDoc
  393. localWriteTime:(FIRTimestamp *)localWriteTime
  394. mutationResult:(nullable FSTMutationResult *)mutationResult {
  395. if (mutationResult) {
  396. HARD_ASSERT(!mutationResult.transformResults,
  397. "Transform results received by FSTDeleteMutation.");
  398. }
  399. if (!self.precondition.IsValidFor(maybeDoc)) {
  400. return maybeDoc;
  401. }
  402. if (maybeDoc) {
  403. HARD_ASSERT(maybeDoc.key == self.key, "Can only delete a document with the same key");
  404. }
  405. return [FSTDeletedDocument documentWithKey:self.key version:SnapshotVersion::None()];
  406. }
  407. @end
  408. NS_ASSUME_NONNULL_END