FSTMutationBatch.mm 6.7 KB

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