FSTMutationBatch.mm 6.7 KB

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