FSTMutationBatch.mm 9.3 KB

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