FSTViewSnapshot.mm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. hasPendingWrites:(BOOL)hasPendingWrites
  149. syncStateChanged:(BOOL)syncStateChanged {
  150. self = [super init];
  151. if (self) {
  152. _query = query;
  153. _documents = documents;
  154. _oldDocuments = oldDocuments;
  155. _documentChanges = documentChanges;
  156. _fromCache = fromCache;
  157. _hasPendingWrites = hasPendingWrites;
  158. _syncStateChanged = syncStateChanged;
  159. }
  160. return self;
  161. }
  162. - (NSString *)description {
  163. return [NSString stringWithFormat:
  164. @"<FSTViewSnapshot query:%@ documents:%@ oldDocument:%@ changes:%@ "
  165. "fromCache:%@ hasPendingWrites:%@ syncStateChanged:%@>",
  166. self.query, self.documents, self.oldDocuments, self.documentChanges,
  167. (self.fromCache ? @"YES" : @"NO"), (self.hasPendingWrites ? @"YES" : @"NO"),
  168. (self.syncStateChanged ? @"YES" : @"NO")];
  169. }
  170. - (BOOL)isEqual:(id)object {
  171. if (self == object) {
  172. return YES;
  173. } else if (![object isKindOfClass:[FSTViewSnapshot class]]) {
  174. return NO;
  175. }
  176. FSTViewSnapshot *other = object;
  177. return [self.query isEqual:other.query] && [self.documents isEqual:other.documents] &&
  178. [self.oldDocuments isEqual:other.oldDocuments] &&
  179. [self.documentChanges isEqualToArray:other.documentChanges] &&
  180. self.fromCache == other.fromCache && self.hasPendingWrites == other.hasPendingWrites &&
  181. self.syncStateChanged == other.syncStateChanged;
  182. }
  183. - (NSUInteger)hash {
  184. NSUInteger result = [self.query hash];
  185. result = 31 * result + [self.documents hash];
  186. result = 31 * result + [self.oldDocuments hash];
  187. result = 31 * result + [self.documentChanges hash];
  188. result = 31 * result + (self.fromCache ? 1231 : 1237);
  189. result = 31 * result + (self.hasPendingWrites ? 1231 : 1237);
  190. result = 31 * result + (self.syncStateChanged ? 1231 : 1237);
  191. return result;
  192. }
  193. @end
  194. NS_ASSUME_NONNULL_END