FSTTransaction.mm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/Core/FSTTransaction.h"
  17. #import <GRPCClient/GRPCCall.h>
  18. #include <map>
  19. #include <vector>
  20. #import "FIRFirestoreErrors.h"
  21. #import "Firestore/Source/API/FSTUserDataConverter.h"
  22. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  23. #import "Firestore/Source/Model/FSTDocument.h"
  24. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  25. #import "Firestore/Source/Model/FSTMutation.h"
  26. #import "Firestore/Source/Remote/FSTDatastore.h"
  27. #import "Firestore/Source/Util/FSTAssert.h"
  28. #import "Firestore/Source/Util/FSTUsageValidation.h"
  29. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  30. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  31. using firebase::firestore::model::DocumentKey;
  32. using firebase::firestore::model::Precondition;
  33. NS_ASSUME_NONNULL_BEGIN
  34. #pragma mark - FSTTransaction
  35. @interface FSTTransaction ()
  36. @property(nonatomic, strong, readonly) FSTDatastore *datastore;
  37. @property(nonatomic, strong, readonly) NSMutableArray *mutations;
  38. @property(nonatomic, assign) BOOL commitCalled;
  39. /**
  40. * An error that may have occurred as a consequence of a write. If set, needs to be raised in the
  41. * completion handler instead of trying to commit.
  42. */
  43. @property(nonatomic, strong, nullable) NSError *lastWriteError;
  44. @end
  45. @implementation FSTTransaction {
  46. std::map<DocumentKey, FSTSnapshotVersion *> _readVersions;
  47. }
  48. + (instancetype)transactionWithDatastore:(FSTDatastore *)datastore {
  49. return [[FSTTransaction alloc] initWithDatastore:datastore];
  50. }
  51. - (instancetype)initWithDatastore:(FSTDatastore *)datastore {
  52. self = [super init];
  53. if (self) {
  54. _datastore = datastore;
  55. _mutations = [NSMutableArray array];
  56. _commitCalled = NO;
  57. }
  58. return self;
  59. }
  60. /**
  61. * Every time a document is read, this should be called to record its version. If we read two
  62. * different versions of the same document, this will return an error through its out parameter.
  63. * When the transaction is committed, the versions recorded will be set as preconditions on the
  64. * writes sent to the backend.
  65. */
  66. - (BOOL)recordVersionForDocument:(FSTMaybeDocument *)doc error:(NSError **)error {
  67. FSTAssert(error != nil, @"nil error parameter");
  68. *error = nil;
  69. FSTSnapshotVersion *docVersion = doc.version;
  70. if ([doc isKindOfClass:[FSTDeletedDocument class]]) {
  71. // For deleted docs, we must record an explicit no version to build the right precondition
  72. // when writing.
  73. docVersion = [FSTSnapshotVersion noVersion];
  74. }
  75. if (_readVersions.find(doc.key) == _readVersions.end()) {
  76. _readVersions[doc.key] = docVersion;
  77. return YES;
  78. } else {
  79. if (error) {
  80. *error =
  81. [NSError errorWithDomain:FIRFirestoreErrorDomain
  82. code:FIRFirestoreErrorCodeFailedPrecondition
  83. userInfo:@{
  84. NSLocalizedDescriptionKey :
  85. @"A document cannot be read twice within a single transaction."
  86. }];
  87. }
  88. return NO;
  89. }
  90. }
  91. - (void)lookupDocumentsForKeys:(const std::vector<DocumentKey> &)keys
  92. completion:(FSTVoidMaybeDocumentArrayErrorBlock)completion {
  93. [self ensureCommitNotCalled];
  94. if (self.mutations.count) {
  95. FSTThrowInvalidUsage(@"FIRIllegalStateException",
  96. @"All reads in a transaction must be done before any writes.");
  97. }
  98. [self.datastore lookupDocuments:keys
  99. completion:^(NSArray<FSTMaybeDocument *> *_Nullable documents,
  100. NSError *_Nullable error) {
  101. if (error) {
  102. completion(nil, error);
  103. return;
  104. }
  105. for (FSTMaybeDocument *doc in documents) {
  106. NSError *recordError = nil;
  107. if (![self recordVersionForDocument:doc error:&recordError]) {
  108. completion(nil, recordError);
  109. return;
  110. }
  111. }
  112. completion(documents, nil);
  113. }];
  114. }
  115. /** Stores mutations to be written when commitWithCompletion is called. */
  116. - (void)writeMutations:(NSArray<FSTMutation *> *)mutations {
  117. [self ensureCommitNotCalled];
  118. [self.mutations addObjectsFromArray:mutations];
  119. }
  120. /**
  121. * Returns version of this doc when it was read in this transaction as a precondition, or no
  122. * precondition if it was not read.
  123. */
  124. - (Precondition)preconditionForDocumentKey:(const DocumentKey &)key {
  125. const auto iter = _readVersions.find(key);
  126. if (iter == _readVersions.end()) {
  127. return Precondition::None();
  128. } else {
  129. return Precondition::UpdateTime(iter->second);
  130. }
  131. }
  132. /**
  133. * Returns the precondition for a document if the operation is an update, based on the provided
  134. * UpdateOptions. Will return none precondition if an error occurred, in which case it sets the
  135. * error parameter.
  136. */
  137. - (Precondition)preconditionForUpdateWithDocumentKey:(const DocumentKey &)key
  138. error:(NSError **)error {
  139. const auto iter = _readVersions.find(key);
  140. if (iter == _readVersions.end()) {
  141. // Document was not read, so we just use the preconditions for an update.
  142. return Precondition::Exists(true);
  143. }
  144. FSTSnapshotVersion *version = iter->second;
  145. if ([version isEqual:[FSTSnapshotVersion noVersion]]) {
  146. // The document was read, but doesn't exist.
  147. // Return an error because the precondition is impossible
  148. if (error) {
  149. *error = [NSError
  150. errorWithDomain:FIRFirestoreErrorDomain
  151. code:FIRFirestoreErrorCodeAborted
  152. userInfo:@{
  153. NSLocalizedDescriptionKey : @"Can't update a document that doesn't exist."
  154. }];
  155. }
  156. return Precondition::None();
  157. } else {
  158. // Document exists, just base precondition on document update time.
  159. return Precondition::UpdateTime(version);
  160. }
  161. }
  162. - (void)setData:(FSTParsedSetData *)data forDocument:(const DocumentKey &)key {
  163. [self writeMutations:[data mutationsWithKey:key
  164. precondition:[self preconditionForDocumentKey:key]]];
  165. }
  166. - (void)updateData:(FSTParsedUpdateData *)data forDocument:(const DocumentKey &)key {
  167. NSError *error = nil;
  168. const Precondition precondition = [self preconditionForUpdateWithDocumentKey:key error:&error];
  169. if (precondition.IsNone()) {
  170. FSTAssert(error, @"Got nil precondition, but error was not set");
  171. self.lastWriteError = error;
  172. } else {
  173. [self writeMutations:[data mutationsWithKey:key precondition:precondition]];
  174. }
  175. }
  176. - (void)deleteDocument:(const DocumentKey &)key {
  177. [self writeMutations:@[ [[FSTDeleteMutation alloc]
  178. initWithKey:key
  179. precondition:[self preconditionForDocumentKey:key]] ]];
  180. // Since the delete will be applied before all following writes, we need to ensure that the
  181. // precondition for the next write will be exists without timestamp.
  182. _readVersions[key] = [FSTSnapshotVersion noVersion];
  183. }
  184. - (void)commitWithCompletion:(FSTVoidErrorBlock)completion {
  185. [self ensureCommitNotCalled];
  186. // Once commitWithCompletion is called once, mark this object so it can't be used again.
  187. self.commitCalled = YES;
  188. // If there was an error writing, raise that error now
  189. if (self.lastWriteError) {
  190. completion(self.lastWriteError);
  191. return;
  192. }
  193. // Make a list of read documents that haven't been written.
  194. FSTDocumentKeySet *unwritten = [FSTDocumentKeySet keySet];
  195. for (const auto &kv : _readVersions) {
  196. unwritten = [unwritten setByAddingObject:kv.first];
  197. };
  198. // For each mutation, note that the doc was written.
  199. for (FSTMutation *mutation in self.mutations) {
  200. unwritten = [unwritten setByRemovingObject:mutation.key];
  201. }
  202. if (unwritten.count) {
  203. // TODO(klimt): This is a temporary restriction, until "verify" is supported on the backend.
  204. completion([NSError
  205. errorWithDomain:FIRFirestoreErrorDomain
  206. code:FIRFirestoreErrorCodeFailedPrecondition
  207. userInfo:@{
  208. NSLocalizedDescriptionKey : @"Every document read in a transaction must also be "
  209. @"written in that transaction."
  210. }]);
  211. } else {
  212. [self.datastore commitMutations:self.mutations
  213. completion:^(NSError *_Nullable error) {
  214. if (error) {
  215. completion(error);
  216. } else {
  217. completion(nil);
  218. }
  219. }];
  220. }
  221. }
  222. - (void)ensureCommitNotCalled {
  223. if (self.commitCalled) {
  224. FSTThrowInvalidUsage(
  225. @"FIRIllegalStateException",
  226. @"A transaction object cannot be used after its update block has completed.");
  227. }
  228. }
  229. @end
  230. NS_ASSUME_NONNULL_END