FSTViewSnapshot.mm 10 KB

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