FSTMutationBatch.mm 6.5 KB

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