FSTMutationBatch.mm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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)applyToRemoteDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  64. documentKey:(const DocumentKey &)documentKey
  65. mutationBatchResult:
  66. (FSTMutationBatchResult *_Nullable)mutationBatchResult {
  67. HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
  68. "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
  69. maybeDoc.key.ToString());
  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. for (NSUInteger i = 0; i < self.mutations.count; i++) {
  74. FSTMutation *mutation = self.mutations[i];
  75. FSTMutationResult *mutationResult = mutationBatchResult.mutationResults[i];
  76. if (mutation.key == documentKey) {
  77. maybeDoc = [mutation applyToRemoteDocument:maybeDoc mutationResult:mutationResult];
  78. }
  79. }
  80. return maybeDoc;
  81. }
  82. - (FSTMaybeDocument *_Nullable)applyToLocalDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  83. documentKey:(const DocumentKey &)documentKey {
  84. HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
  85. "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
  86. maybeDoc.key.ToString());
  87. FSTMaybeDocument *baseDoc = maybeDoc;
  88. for (NSUInteger i = 0; i < self.mutations.count; i++) {
  89. FSTMutation *mutation = self.mutations[i];
  90. if (mutation.key == documentKey) {
  91. maybeDoc = [mutation applyToLocalDocument:maybeDoc
  92. baseDocument:baseDoc
  93. localWriteTime:self.localWriteTime];
  94. }
  95. }
  96. return maybeDoc;
  97. }
  98. - (BOOL)isTombstone {
  99. return self.mutations.count == 0;
  100. }
  101. - (FSTMutationBatch *)toTombstone {
  102. return [[FSTMutationBatch alloc] initWithBatchID:self.batchID
  103. localWriteTime:self.localWriteTime
  104. mutations:@[]];
  105. }
  106. // TODO(klimt): This could use NSMutableDictionary instead.
  107. - (DocumentKeySet)keys {
  108. DocumentKeySet set;
  109. for (FSTMutation *mutation in self.mutations) {
  110. set = set.insert(mutation.key);
  111. }
  112. return set;
  113. }
  114. @end
  115. #pragma mark - FSTMutationBatchResult
  116. @interface FSTMutationBatchResult ()
  117. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  118. commitVersion:(SnapshotVersion)commitVersion
  119. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  120. streamToken:(nullable NSData *)streamToken
  121. docVersions:(DocumentVersionMap)docVersions NS_DESIGNATED_INITIALIZER;
  122. @end
  123. @implementation FSTMutationBatchResult {
  124. SnapshotVersion _commitVersion;
  125. DocumentVersionMap _docVersions;
  126. }
  127. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  128. commitVersion:(SnapshotVersion)commitVersion
  129. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  130. streamToken:(nullable NSData *)streamToken
  131. docVersions:(DocumentVersionMap)docVersions {
  132. if (self = [super init]) {
  133. _batch = batch;
  134. _commitVersion = std::move(commitVersion);
  135. _mutationResults = mutationResults;
  136. _streamToken = streamToken;
  137. _docVersions = std::move(docVersions);
  138. }
  139. return self;
  140. }
  141. - (const SnapshotVersion &)commitVersion {
  142. return _commitVersion;
  143. }
  144. - (const DocumentVersionMap &)docVersions {
  145. return _docVersions;
  146. }
  147. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  148. commitVersion:(SnapshotVersion)commitVersion
  149. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  150. streamToken:(nullable NSData *)streamToken {
  151. HARD_ASSERT(batch.mutations.count == mutationResults.count,
  152. "Mutations sent %s must equal results received %s", batch.mutations.count,
  153. mutationResults.count);
  154. DocumentVersionMap docVersions;
  155. NSArray<FSTMutation *> *mutations = batch.mutations;
  156. for (NSUInteger i = 0; i < mutations.count; i++) {
  157. absl::optional<SnapshotVersion> version = mutationResults[i].version;
  158. if (!version) {
  159. // deletes don't have a version, so we substitute the commitVersion
  160. // of the entire batch.
  161. version = commitVersion;
  162. }
  163. docVersions[mutations[i].key] = version.value();
  164. }
  165. return [[FSTMutationBatchResult alloc] initWithBatch:batch
  166. commitVersion:std::move(commitVersion)
  167. mutationResults:mutationResults
  168. streamToken:streamToken
  169. docVersions:std::move(docVersions)];
  170. }
  171. @end
  172. NS_ASSUME_NONNULL_END