FSTViewSnapshot.mm 10 KB

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