FSTViewSnapshot.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/Core/FSTViewSnapshot.h"
  17. #include <string>
  18. #include <utility>
  19. #import "Firestore/Source/Core/FSTQuery.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTDocumentSet.h"
  22. #include "Firestore/core/src/firebase/firestore/immutable/sorted_map.h"
  23. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  25. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  26. #include "Firestore/core/src/firebase/firestore/util/string_format.h"
  27. #include "absl/strings/str_join.h"
  28. using firebase::firestore::core::DocumentViewChangeType;
  29. using firebase::firestore::immutable::SortedMap;
  30. using firebase::firestore::model::DocumentKey;
  31. using firebase::firestore::util::WrapNSString;
  32. using firebase::firestore::util::StringFormat;
  33. NS_ASSUME_NONNULL_BEGIN
  34. #pragma mark - FSTDocumentViewChange
  35. @interface FSTDocumentViewChange ()
  36. + (instancetype)changeWithDocument:(FSTDocument *)document type:(DocumentViewChangeType)type;
  37. - (instancetype)initWithDocument:(FSTDocument *)document
  38. type:(DocumentViewChangeType)type NS_DESIGNATED_INITIALIZER;
  39. @end
  40. @implementation FSTDocumentViewChange
  41. + (instancetype)changeWithDocument:(FSTDocument *)document type:(DocumentViewChangeType)type {
  42. return [[FSTDocumentViewChange alloc] initWithDocument:document type:type];
  43. }
  44. - (instancetype)initWithDocument:(FSTDocument *)document type:(DocumentViewChangeType)type {
  45. self = [super init];
  46. if (self) {
  47. _document = document;
  48. _type = type;
  49. }
  50. return self;
  51. }
  52. - (BOOL)isEqual:(id)other {
  53. if (self == other) {
  54. return YES;
  55. }
  56. if (![other isKindOfClass:[FSTDocumentViewChange class]]) {
  57. return NO;
  58. }
  59. FSTDocumentViewChange *otherChange = (FSTDocumentViewChange *)other;
  60. return [self.document isEqual:otherChange.document] && self.type == otherChange.type;
  61. }
  62. - (NSString *)description {
  63. return [NSString
  64. stringWithFormat:@"<FSTDocumentViewChange type:%ld doc:%@>", (long)self.type, self.document];
  65. }
  66. @end
  67. #pragma mark - FSTDocumentViewChangeSet
  68. @implementation FSTDocumentViewChangeSet {
  69. /** The set of all changes tracked so far, with redundant changes merged. */
  70. SortedMap<DocumentKey, FSTDocumentViewChange *> _changeMap;
  71. }
  72. + (instancetype)changeSet {
  73. return [[FSTDocumentViewChangeSet alloc] init];
  74. }
  75. - (NSString *)description {
  76. std::string result = absl::StrJoin(
  77. _changeMap, ",",
  78. [](std::string *out, const std::pair<DocumentKey, FSTDocumentViewChange *> &kv) {
  79. out->append(StringFormat("%s: %s", kv.first, kv.second));
  80. });
  81. return WrapNSString(std::string{"{"} + result + "}");
  82. }
  83. - (void)addChange:(FSTDocumentViewChange *)change {
  84. const DocumentKey &key = change.document.key;
  85. auto oldChangeIter = _changeMap.find(key);
  86. if (oldChangeIter == _changeMap.end()) {
  87. _changeMap = _changeMap.insert(key, change);
  88. return;
  89. }
  90. FSTDocumentViewChange *oldChange = oldChangeIter->second;
  91. // Merge the new change with the existing change.
  92. if (change.type != DocumentViewChangeType::kAdded &&
  93. oldChange.type == DocumentViewChangeType::kMetadata) {
  94. _changeMap = _changeMap.insert(key, change);
  95. } else if (change.type == DocumentViewChangeType::kMetadata &&
  96. oldChange.type != DocumentViewChangeType::kRemoved) {
  97. FSTDocumentViewChange *newChange = [FSTDocumentViewChange changeWithDocument:change.document
  98. type:oldChange.type];
  99. _changeMap = _changeMap.insert(key, newChange);
  100. } else if (change.type == DocumentViewChangeType::kModified &&
  101. oldChange.type == DocumentViewChangeType::kModified) {
  102. FSTDocumentViewChange *newChange =
  103. [FSTDocumentViewChange changeWithDocument:change.document
  104. type:DocumentViewChangeType::kModified];
  105. _changeMap = _changeMap.insert(key, newChange);
  106. } else if (change.type == DocumentViewChangeType::kModified &&
  107. oldChange.type == DocumentViewChangeType::kAdded) {
  108. FSTDocumentViewChange *newChange =
  109. [FSTDocumentViewChange changeWithDocument:change.document
  110. type:DocumentViewChangeType::kAdded];
  111. _changeMap = _changeMap.insert(key, newChange);
  112. } else if (change.type == DocumentViewChangeType::kRemoved &&
  113. oldChange.type == DocumentViewChangeType::kAdded) {
  114. _changeMap = _changeMap.erase(key);
  115. } else if (change.type == DocumentViewChangeType::kRemoved &&
  116. oldChange.type == DocumentViewChangeType::kModified) {
  117. FSTDocumentViewChange *newChange =
  118. [FSTDocumentViewChange changeWithDocument:oldChange.document
  119. type:DocumentViewChangeType::kRemoved];
  120. _changeMap = _changeMap.insert(key, newChange);
  121. } else if (change.type == DocumentViewChangeType::kAdded &&
  122. oldChange.type == DocumentViewChangeType::kRemoved) {
  123. FSTDocumentViewChange *newChange =
  124. [FSTDocumentViewChange changeWithDocument:change.document
  125. type:DocumentViewChangeType::kModified];
  126. _changeMap = _changeMap.insert(key, newChange);
  127. } else {
  128. // This includes these cases, which don't make sense:
  129. // Added -> Added
  130. // Removed -> Removed
  131. // Modified -> Added
  132. // Removed -> Modified
  133. // Metadata -> Added
  134. // Removed -> Metadata
  135. HARD_FAIL("Unsupported combination of changes: %s after %s", change.type, oldChange.type);
  136. }
  137. }
  138. - (NSArray<FSTDocumentViewChange *> *)changes {
  139. NSMutableArray<FSTDocumentViewChange *> *changes = [NSMutableArray array];
  140. for (const auto &kv : _changeMap) {
  141. FSTDocumentViewChange *change = kv.second;
  142. [changes addObject:change];
  143. }
  144. return changes;
  145. }
  146. @end
  147. #pragma mark - FSTViewSnapshot
  148. @implementation FSTViewSnapshot
  149. - (instancetype)initWithQuery:(FSTQuery *)query
  150. documents:(FSTDocumentSet *)documents
  151. oldDocuments:(FSTDocumentSet *)oldDocuments
  152. documentChanges:(NSArray<FSTDocumentViewChange *> *)documentChanges
  153. fromCache:(BOOL)fromCache
  154. mutatedKeys:(DocumentKeySet)mutatedKeys
  155. syncStateChanged:(BOOL)syncStateChanged
  156. excludesMetadataChanges:(BOOL)excludesMetadataChanges {
  157. self = [super init];
  158. if (self) {
  159. _query = query;
  160. _documents = documents;
  161. _oldDocuments = oldDocuments;
  162. _documentChanges = documentChanges;
  163. _fromCache = fromCache;
  164. _mutatedKeys = mutatedKeys;
  165. _syncStateChanged = syncStateChanged;
  166. _excludesMetadataChanges = excludesMetadataChanges;
  167. }
  168. return self;
  169. }
  170. + (instancetype)snapshotForInitialDocuments:(FSTDocumentSet *)documents
  171. query:(FSTQuery *)query
  172. mutatedKeys:(DocumentKeySet)mutatedKeys
  173. fromCache:(BOOL)fromCache
  174. excludesMetadataChanges:(BOOL)excludesMetadataChanges {
  175. NSMutableArray<FSTDocumentViewChange *> *viewChanges = [NSMutableArray array];
  176. for (FSTDocument *doc in documents.documentEnumerator) {
  177. [viewChanges
  178. addObject:[FSTDocumentViewChange changeWithDocument:doc
  179. type:DocumentViewChangeType::kAdded]];
  180. }
  181. return [[FSTViewSnapshot alloc]
  182. initWithQuery:query
  183. documents:documents
  184. oldDocuments:[FSTDocumentSet documentSetWithComparator:query.comparator]
  185. documentChanges:viewChanges
  186. fromCache:fromCache
  187. mutatedKeys:mutatedKeys
  188. syncStateChanged:YES
  189. excludesMetadataChanges:excludesMetadataChanges];
  190. }
  191. - (BOOL)hasPendingWrites {
  192. return _mutatedKeys.size() != 0;
  193. }
  194. - (NSString *)description {
  195. return [NSString
  196. stringWithFormat:@"<FSTViewSnapshot query:%@ documents:%@ oldDocument:%@ changes:%@ "
  197. "fromCache:%@ mutatedKeys:%zu syncStateChanged:%@ "
  198. "excludesMetadataChanges%@>",
  199. self.query, self.documents, self.oldDocuments, self.documentChanges,
  200. (self.fromCache ? @"YES" : @"NO"),
  201. static_cast<size_t>(self.mutatedKeys.size()),
  202. (self.syncStateChanged ? @"YES" : @"NO"),
  203. (self.excludesMetadataChanges ? @"YES" : @"NO")];
  204. }
  205. - (BOOL)isEqual:(id)object {
  206. if (self == object) {
  207. return YES;
  208. } else if (![object isKindOfClass:[FSTViewSnapshot class]]) {
  209. return NO;
  210. }
  211. FSTViewSnapshot *other = object;
  212. return [self.query isEqual:other.query] && [self.documents isEqual:other.documents] &&
  213. [self.oldDocuments isEqual:other.oldDocuments] &&
  214. [self.documentChanges isEqualToArray:other.documentChanges] &&
  215. self.fromCache == other.fromCache && self.mutatedKeys == other.mutatedKeys &&
  216. self.syncStateChanged == other.syncStateChanged &&
  217. self.excludesMetadataChanges == other.excludesMetadataChanges;
  218. }
  219. - (NSUInteger)hash {
  220. // Note: We are omitting `mutatedKeys` from the hash, since we don't have a straightforward
  221. // way to compute its hash value. Since `FSTViewSnapshot` is currently not stored in an
  222. // NSDictionary, this has no side effects.
  223. NSUInteger result = [self.query hash];
  224. result = 31 * result + [self.documents hash];
  225. result = 31 * result + [self.oldDocuments hash];
  226. result = 31 * result + [self.documentChanges hash];
  227. result = 31 * result + (self.fromCache ? 1231 : 1237);
  228. result = 31 * result + (self.syncStateChanged ? 1231 : 1237);
  229. result = 31 * result + (self.excludesMetadataChanges ? 1231 : 1237);
  230. return result;
  231. }
  232. @end
  233. NS_ASSUME_NONNULL_END