FSTTransaction.m 9.3 KB

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