FSTMutationBatch.mm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <algorithm>
  18. #include <utility>
  19. #import "FIRTimestamp.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTMutation.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  23. #include "Firestore/core/src/firebase/firestore/objc/objc_compatibility.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  25. #include "Firestore/core/src/firebase/firestore/util/hashing.h"
  26. namespace objc = firebase::firestore::objc;
  27. using firebase::firestore::model::BatchId;
  28. using firebase::firestore::model::DocumentKey;
  29. using firebase::firestore::model::DocumentKeyHash;
  30. using firebase::firestore::model::DocumentKeySet;
  31. using firebase::firestore::model::DocumentVersionMap;
  32. using firebase::firestore::model::MaybeDocumentMap;
  33. using firebase::firestore::model::SnapshotVersion;
  34. using firebase::firestore::util::Hash;
  35. NS_ASSUME_NONNULL_BEGIN
  36. @implementation FSTMutationBatch {
  37. std::vector<FSTMutation *> _baseMutations;
  38. std::vector<FSTMutation *> _mutations;
  39. }
  40. - (instancetype)initWithBatchID:(BatchId)batchID
  41. localWriteTime:(FIRTimestamp *)localWriteTime
  42. baseMutations:(std::vector<FSTMutation *> &&)baseMutations
  43. mutations:(std::vector<FSTMutation *> &&)mutations {
  44. HARD_ASSERT(!mutations.empty(), "Cannot create an empty mutation batch");
  45. self = [super init];
  46. if (self) {
  47. _batchID = batchID;
  48. _localWriteTime = localWriteTime;
  49. _baseMutations = std::move(baseMutations);
  50. _mutations = std::move(mutations);
  51. }
  52. return self;
  53. }
  54. - (const std::vector<FSTMutation *> &)baseMutations {
  55. return _baseMutations;
  56. }
  57. - (const std::vector<FSTMutation *> &)mutations {
  58. return _mutations;
  59. }
  60. - (BOOL)isEqual:(id)other {
  61. if (self == other) {
  62. return YES;
  63. } else if (![other isKindOfClass:[FSTMutationBatch class]]) {
  64. return NO;
  65. }
  66. FSTMutationBatch *otherBatch = (FSTMutationBatch *)other;
  67. return self.batchID == otherBatch.batchID &&
  68. [self.localWriteTime isEqual:otherBatch.localWriteTime] &&
  69. objc::Equals(_baseMutations, otherBatch.baseMutations) &&
  70. objc::Equals(_mutations, otherBatch.mutations);
  71. }
  72. - (NSUInteger)hash {
  73. NSUInteger result = (NSUInteger)self.batchID;
  74. result = result * 31 + self.localWriteTime.hash;
  75. for (FSTMutation *mutation : _baseMutations) {
  76. result = result * 31 + [mutation hash];
  77. }
  78. for (FSTMutation *mutation : _mutations) {
  79. result = result * 31 + [mutation hash];
  80. }
  81. return result;
  82. }
  83. - (NSString *)description {
  84. return
  85. [NSString stringWithFormat:@"<FSTMutationBatch: id=%d, localWriteTime=%@, mutations=%@>",
  86. self.batchID, self.localWriteTime, objc::Description(_mutations)];
  87. }
  88. - (FSTMaybeDocument *_Nullable)applyToRemoteDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  89. documentKey:(const DocumentKey &)documentKey
  90. mutationBatchResult:
  91. (FSTMutationBatchResult *_Nullable)mutationBatchResult {
  92. HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
  93. "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
  94. maybeDoc.key.ToString());
  95. HARD_ASSERT(mutationBatchResult.mutationResults.size() == _mutations.size(),
  96. "Mismatch between mutations length (%s) and results length (%s)", _mutations.size(),
  97. mutationBatchResult.mutationResults.size());
  98. for (size_t i = 0; i < _mutations.size(); i++) {
  99. FSTMutation *mutation = _mutations[i];
  100. FSTMutationResult *mutationResult = mutationBatchResult.mutationResults[i];
  101. if (mutation.key == documentKey) {
  102. maybeDoc = [mutation applyToRemoteDocument:maybeDoc mutationResult:mutationResult];
  103. }
  104. }
  105. return maybeDoc;
  106. }
  107. - (FSTMaybeDocument *_Nullable)applyToLocalDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  108. documentKey:(const DocumentKey &)documentKey {
  109. HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
  110. "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
  111. maybeDoc.key.ToString());
  112. // First, apply the base state. This allows us to apply non-idempotent transform against a
  113. // consistent set of values.
  114. for (FSTMutation *mutation : _baseMutations) {
  115. if (mutation.key == documentKey) {
  116. maybeDoc = [mutation applyToLocalDocument:maybeDoc
  117. baseDocument:maybeDoc
  118. localWriteTime:self.localWriteTime];
  119. }
  120. }
  121. FSTMaybeDocument *baseDoc = maybeDoc;
  122. // Second, apply all user-provided mutations.
  123. for (FSTMutation *mutation : _mutations) {
  124. if (mutation.key == documentKey) {
  125. maybeDoc = [mutation applyToLocalDocument:maybeDoc
  126. baseDocument:baseDoc
  127. localWriteTime:self.localWriteTime];
  128. }
  129. }
  130. return maybeDoc;
  131. }
  132. - (MaybeDocumentMap)applyToLocalDocumentSet:(const MaybeDocumentMap &)documentSet {
  133. // TODO(mrschmidt): This implementation is O(n^2). If we iterate through the mutations first (as
  134. // done in `applyToLocalDocument:documentKey:`), we can reduce the complexity to O(n).
  135. MaybeDocumentMap mutatedDocuments = documentSet;
  136. for (FSTMutation *mutation : _mutations) {
  137. const DocumentKey &key = mutation.key;
  138. auto maybeDocument = mutatedDocuments.find(key);
  139. FSTMaybeDocument *mutatedDocument = [self
  140. applyToLocalDocument:(maybeDocument != mutatedDocuments.end() ? maybeDocument->second : nil)
  141. documentKey:key];
  142. if (mutatedDocument) {
  143. mutatedDocuments = mutatedDocuments.insert(key, mutatedDocument);
  144. }
  145. }
  146. return mutatedDocuments;
  147. }
  148. - (DocumentKeySet)keys {
  149. DocumentKeySet set;
  150. for (FSTMutation *mutation : _mutations) {
  151. set = set.insert(mutation.key);
  152. }
  153. return set;
  154. }
  155. @end
  156. #pragma mark - FSTMutationBatchResult
  157. @interface FSTMutationBatchResult ()
  158. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  159. commitVersion:(SnapshotVersion)commitVersion
  160. mutationResults:(std::vector<FSTMutationResult *>)mutationResults
  161. streamToken:(nullable NSData *)streamToken
  162. docVersions:(DocumentVersionMap)docVersions NS_DESIGNATED_INITIALIZER;
  163. @end
  164. @implementation FSTMutationBatchResult {
  165. SnapshotVersion _commitVersion;
  166. std::vector<FSTMutationResult *> _mutationResults;
  167. DocumentVersionMap _docVersions;
  168. }
  169. - (instancetype)initWithBatch:(FSTMutationBatch *)batch
  170. commitVersion:(SnapshotVersion)commitVersion
  171. mutationResults:(std::vector<FSTMutationResult *>)mutationResults
  172. streamToken:(nullable NSData *)streamToken
  173. docVersions:(DocumentVersionMap)docVersions {
  174. if (self = [super init]) {
  175. _batch = batch;
  176. _commitVersion = std::move(commitVersion);
  177. _mutationResults = std::move(mutationResults);
  178. _streamToken = streamToken;
  179. _docVersions = std::move(docVersions);
  180. }
  181. return self;
  182. }
  183. - (const SnapshotVersion &)commitVersion {
  184. return _commitVersion;
  185. }
  186. - (const std::vector<FSTMutationResult *> &)mutationResults {
  187. return _mutationResults;
  188. }
  189. - (const DocumentVersionMap &)docVersions {
  190. return _docVersions;
  191. }
  192. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  193. commitVersion:(SnapshotVersion)commitVersion
  194. mutationResults:(std::vector<FSTMutationResult *>)mutationResults
  195. streamToken:(nullable NSData *)streamToken {
  196. HARD_ASSERT(batch.mutations.size() == mutationResults.size(),
  197. "Mutations sent %s must equal results received %s", batch.mutations.size(),
  198. mutationResults.size());
  199. DocumentVersionMap docVersions;
  200. std::vector<FSTMutation *> mutations = batch.mutations;
  201. for (size_t i = 0; i < mutations.size(); i++) {
  202. absl::optional<SnapshotVersion> version = mutationResults[i].version;
  203. if (!version) {
  204. // deletes don't have a version, so we substitute the commitVersion
  205. // of the entire batch.
  206. version = commitVersion;
  207. }
  208. docVersions[mutations[i].key] = version.value();
  209. }
  210. return [[FSTMutationBatchResult alloc] initWithBatch:batch
  211. commitVersion:std::move(commitVersion)
  212. mutationResults:std::move(mutationResults)
  213. streamToken:streamToken
  214. docVersions:std::move(docVersions)];
  215. }
  216. @end
  217. NS_ASSUME_NONNULL_END