FSTMutationBatch.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (mutationBatchResult) {
  64. FSTAssert(mutationBatchResult.mutationResults.count == self.mutations.count,
  65. @"Mismatch between mutations length (%lu) and results length (%lu)",
  66. (unsigned long)self.mutations.count,
  67. (unsigned long)mutationBatchResult.mutationResults.count);
  68. }
  69. for (NSUInteger i = 0; i < self.mutations.count; i++) {
  70. FSTMutation *mutation = self.mutations[i];
  71. FSTMutationResult *_Nullable mutationResult = mutationBatchResult.mutationResults[i];
  72. if ([mutation.key isEqualToKey:documentKey]) {
  73. maybeDoc = [mutation applyTo:maybeDoc
  74. localWriteTime:self.localWriteTime
  75. mutationResult:mutationResult];
  76. }
  77. }
  78. return maybeDoc;
  79. }
  80. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  81. documentKey:(FSTDocumentKey *)documentKey {
  82. return [self applyTo:maybeDoc documentKey:documentKey mutationBatchResult:nil];
  83. }
  84. - (BOOL)isTombstone {
  85. return self.mutations.count == 0;
  86. }
  87. - (FSTMutationBatch *)toTombstone {
  88. return [[FSTMutationBatch alloc] initWithBatchID:self.batchID
  89. localWriteTime:self.localWriteTime
  90. mutations:@[]];
  91. }
  92. // TODO(klimt): This could use NSMutableDictionary instead.
  93. - (FSTDocumentKeySet *)keys {
  94. FSTDocumentKeySet *set = [FSTDocumentKeySet keySet];
  95. for (FSTMutation *mutation in self.mutations) {
  96. set = [set setByAddingObject:mutation.key];
  97. }
  98. return set;
  99. }
  100. @end
  101. #pragma mark - FSTMutationBatchResult
  102. @interface FSTMutationBatchResult ()
  103. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  104. commitVersion:(FSTSnapshotVersion *)commitVersion
  105. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  106. streamToken:(nullable NSData *)streamToken
  107. docVersions:(FSTDocumentVersionDictionary *)docVersions NS_DESIGNATED_INITIALIZER;
  108. @end
  109. @implementation FSTMutationBatchResult
  110. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  111. commitVersion:(FSTSnapshotVersion *)commitVersion
  112. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  113. streamToken:(nullable NSData *)streamToken
  114. docVersions:(FSTDocumentVersionDictionary *)docVersions {
  115. if (self = [super init]) {
  116. _batch = batch;
  117. _commitVersion = commitVersion;
  118. _mutationResults = mutationResults;
  119. _streamToken = streamToken;
  120. _docVersions = docVersions;
  121. }
  122. return self;
  123. }
  124. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  125. commitVersion:(FSTSnapshotVersion *)commitVersion
  126. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  127. streamToken:(nullable NSData *)streamToken {
  128. FSTAssert(batch.mutations.count == mutationResults.count,
  129. @"Mutations sent %lu must equal results received %lu",
  130. (unsigned long)batch.mutations.count, (unsigned long)mutationResults.count);
  131. FSTDocumentVersionDictionary *docVersions =
  132. [FSTDocumentVersionDictionary documentVersionDictionary];
  133. NSArray<FSTMutation *> *mutations = batch.mutations;
  134. for (NSUInteger i = 0; i < mutations.count; i++) {
  135. FSTSnapshotVersion *_Nullable version = mutationResults[i].version;
  136. if (!version) {
  137. // deletes don't have a version, so we substitute the commitVersion
  138. // of the entire batch.
  139. version = commitVersion;
  140. }
  141. docVersions = [docVersions dictionaryBySettingObject:version forKey:mutations[i].key];
  142. }
  143. return [[FSTMutationBatchResult alloc] initWithBatch:batch
  144. commitVersion:commitVersion
  145. mutationResults:mutationResults
  146. streamToken:streamToken
  147. docVersions:docVersions];
  148. }
  149. @end
  150. NS_ASSUME_NONNULL_END