FSTMutationBatch.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/FSTMutationBatch.h"
  17. #import "FIRTimestamp.h"
  18. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. #import "Firestore/Source/Model/FSTMutation.h"
  21. #import "Firestore/Source/Util/FSTAssert.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  23. using firebase::firestore::model::DocumentKey;
  24. NS_ASSUME_NONNULL_BEGIN
  25. const FSTBatchID kFSTBatchIDUnknown = -1;
  26. @implementation FSTMutationBatch
  27. - (instancetype)initWithBatchID:(FSTBatchID)batchID
  28. localWriteTime:(FIRTimestamp *)localWriteTime
  29. mutations:(NSArray<FSTMutation *> *)mutations {
  30. self = [super init];
  31. if (self) {
  32. _batchID = batchID;
  33. _localWriteTime = localWriteTime;
  34. _mutations = mutations;
  35. }
  36. return self;
  37. }
  38. - (BOOL)isEqual:(id)other {
  39. if (self == other) {
  40. return YES;
  41. } else if (![other isKindOfClass:[FSTMutationBatch class]]) {
  42. return NO;
  43. }
  44. FSTMutationBatch *otherBatch = (FSTMutationBatch *)other;
  45. return self.batchID == otherBatch.batchID &&
  46. [self.localWriteTime isEqual:otherBatch.localWriteTime] &&
  47. [self.mutations isEqual:otherBatch.mutations];
  48. }
  49. - (NSUInteger)hash {
  50. NSUInteger result = (NSUInteger)self.batchID;
  51. result = result * 31 + self.localWriteTime.hash;
  52. result = result * 31 + self.mutations.hash;
  53. return result;
  54. }
  55. - (NSString *)description {
  56. return [NSString stringWithFormat:@"<FSTMutationBatch: id=%d, localWriteTime=%@, mutations=%@>",
  57. self.batchID, self.localWriteTime, self.mutations];
  58. }
  59. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  60. documentKey:(const DocumentKey &)documentKey
  61. mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult {
  62. FSTAssert(!maybeDoc || [maybeDoc.key isEqualToKey:documentKey],
  63. @"applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString().c_str(),
  64. maybeDoc.key.ToString().c_str());
  65. FSTMaybeDocument *baseDoc = maybeDoc;
  66. if (mutationBatchResult) {
  67. FSTAssert(mutationBatchResult.mutationResults.count == self.mutations.count,
  68. @"Mismatch between mutations length (%lu) and results length (%lu)",
  69. (unsigned long)self.mutations.count,
  70. (unsigned long)mutationBatchResult.mutationResults.count);
  71. }
  72. for (NSUInteger i = 0; i < self.mutations.count; i++) {
  73. FSTMutation *mutation = self.mutations[i];
  74. FSTMutationResult *_Nullable mutationResult = mutationBatchResult.mutationResults[i];
  75. if ([mutation.key isEqualToKey:documentKey]) {
  76. maybeDoc = [mutation applyTo:maybeDoc
  77. baseDocument:baseDoc
  78. localWriteTime:self.localWriteTime
  79. mutationResult:mutationResult];
  80. }
  81. }
  82. return maybeDoc;
  83. }
  84. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  85. documentKey:(const DocumentKey &)documentKey {
  86. return [self applyTo:maybeDoc documentKey:documentKey mutationBatchResult:nil];
  87. }
  88. - (BOOL)isTombstone {
  89. return self.mutations.count == 0;
  90. }
  91. - (FSTMutationBatch *)toTombstone {
  92. return [[FSTMutationBatch alloc] initWithBatchID:self.batchID
  93. localWriteTime:self.localWriteTime
  94. mutations:@[]];
  95. }
  96. // TODO(klimt): This could use NSMutableDictionary instead.
  97. - (FSTDocumentKeySet *)keys {
  98. FSTDocumentKeySet *set = [FSTDocumentKeySet keySet];
  99. for (FSTMutation *mutation in self.mutations) {
  100. set = [set setByAddingObject:mutation.key];
  101. }
  102. return set;
  103. }
  104. @end
  105. #pragma mark - FSTMutationBatchResult
  106. @interface FSTMutationBatchResult ()
  107. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  108. commitVersion:(FSTSnapshotVersion *)commitVersion
  109. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  110. streamToken:(nullable NSData *)streamToken
  111. docVersions:(FSTDocumentVersionDictionary *)docVersions NS_DESIGNATED_INITIALIZER;
  112. @end
  113. @implementation FSTMutationBatchResult
  114. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  115. commitVersion:(FSTSnapshotVersion *)commitVersion
  116. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  117. streamToken:(nullable NSData *)streamToken
  118. docVersions:(FSTDocumentVersionDictionary *)docVersions {
  119. if (self = [super init]) {
  120. _batch = batch;
  121. _commitVersion = commitVersion;
  122. _mutationResults = mutationResults;
  123. _streamToken = streamToken;
  124. _docVersions = docVersions;
  125. }
  126. return self;
  127. }
  128. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  129. commitVersion:(FSTSnapshotVersion *)commitVersion
  130. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  131. streamToken:(nullable NSData *)streamToken {
  132. FSTAssert(batch.mutations.count == mutationResults.count,
  133. @"Mutations sent %lu must equal results received %lu",
  134. (unsigned long)batch.mutations.count, (unsigned long)mutationResults.count);
  135. FSTDocumentVersionDictionary *docVersions =
  136. [FSTDocumentVersionDictionary documentVersionDictionary];
  137. NSArray<FSTMutation *> *mutations = batch.mutations;
  138. for (NSUInteger i = 0; i < mutations.count; i++) {
  139. FSTSnapshotVersion *_Nullable version = mutationResults[i].version;
  140. if (!version) {
  141. // deletes don't have a version, so we substitute the commitVersion
  142. // of the entire batch.
  143. version = commitVersion;
  144. }
  145. docVersions = [docVersions dictionaryBySettingObject:version forKey:mutations[i].key];
  146. }
  147. return [[FSTMutationBatchResult alloc] initWithBatch:batch
  148. commitVersion:commitVersion
  149. mutationResults:mutationResults
  150. streamToken:streamToken
  151. docVersions:docVersions];
  152. }
  153. @end
  154. NS_ASSUME_NONNULL_END