FSTViewSnapshot.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/FSTDocumentKey.h"
  20. #import "Firestore/Source/Model/FSTDocumentSet.h"
  21. #import "Firestore/Source/Util/FSTAssert.h"
  22. #import "Firestore/third_party/Immutable/FSTImmutableSortedDictionary.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. #pragma mark - FSTDocumentViewChange
  25. @interface FSTDocumentViewChange ()
  26. + (instancetype)changeWithDocument:(FSTDocument *)document type:(FSTDocumentViewChangeType)type;
  27. - (instancetype)initWithDocument:(FSTDocument *)document
  28. type:(FSTDocumentViewChangeType)type NS_DESIGNATED_INITIALIZER;
  29. @end
  30. @implementation FSTDocumentViewChange
  31. + (instancetype)changeWithDocument:(FSTDocument *)document type:(FSTDocumentViewChangeType)type {
  32. return [[FSTDocumentViewChange alloc] initWithDocument:document type:type];
  33. }
  34. - (instancetype)initWithDocument:(FSTDocument *)document type:(FSTDocumentViewChangeType)type {
  35. self = [super init];
  36. if (self) {
  37. _document = document;
  38. _type = type;
  39. }
  40. return self;
  41. }
  42. - (BOOL)isEqual:(id)other {
  43. if (self == other) {
  44. return YES;
  45. }
  46. if (![other isKindOfClass:[FSTDocumentViewChange class]]) {
  47. return NO;
  48. }
  49. FSTDocumentViewChange *otherChange = (FSTDocumentViewChange *)other;
  50. return [self.document isEqual:otherChange.document] && self.type == otherChange.type;
  51. }
  52. - (NSString *)description {
  53. return [NSString
  54. stringWithFormat:@"<FSTDocumentViewChange type:%ld doc:%@>", (long)self.type, self.document];
  55. }
  56. @end
  57. #pragma mark - FSTDocumentViewChangeSet
  58. @interface FSTDocumentViewChangeSet ()
  59. /** The set of all changes tracked so far, with redundant changes merged. */
  60. @property(nonatomic, strong)
  61. FSTImmutableSortedDictionary<FSTDocumentKey *, FSTDocumentViewChange *> *changeMap;
  62. @end
  63. @implementation FSTDocumentViewChangeSet
  64. + (instancetype)changeSet {
  65. return [[FSTDocumentViewChangeSet alloc] init];
  66. }
  67. - (instancetype)init {
  68. self = [super init];
  69. if (self) {
  70. _changeMap = [FSTImmutableSortedDictionary dictionaryWithComparator:FSTDocumentKeyComparator];
  71. }
  72. return self;
  73. }
  74. - (NSString *)description {
  75. return [self.changeMap description];
  76. }
  77. - (void)addChange:(FSTDocumentViewChange *)change {
  78. FSTDocumentKey *key = change.document.key;
  79. FSTDocumentViewChange *oldChange = [self.changeMap objectForKey:key];
  80. if (!oldChange) {
  81. self.changeMap = [self.changeMap dictionaryBySettingObject:change forKey:key];
  82. return;
  83. }
  84. // Merge the new change with the existing change.
  85. if (change.type != FSTDocumentViewChangeTypeAdded &&
  86. oldChange.type == FSTDocumentViewChangeTypeMetadata) {
  87. self.changeMap = [self.changeMap dictionaryBySettingObject:change forKey:key];
  88. } else if (change.type == FSTDocumentViewChangeTypeMetadata &&
  89. oldChange.type != FSTDocumentViewChangeTypeRemoved) {
  90. FSTDocumentViewChange *newChange =
  91. [FSTDocumentViewChange changeWithDocument:change.document type:oldChange.type];
  92. self.changeMap = [self.changeMap dictionaryBySettingObject:newChange forKey:key];
  93. } else if (change.type == FSTDocumentViewChangeTypeModified &&
  94. oldChange.type == FSTDocumentViewChangeTypeModified) {
  95. FSTDocumentViewChange *newChange =
  96. [FSTDocumentViewChange changeWithDocument:change.document
  97. type:FSTDocumentViewChangeTypeModified];
  98. self.changeMap = [self.changeMap dictionaryBySettingObject:newChange forKey:key];
  99. } else if (change.type == FSTDocumentViewChangeTypeModified &&
  100. oldChange.type == FSTDocumentViewChangeTypeAdded) {
  101. FSTDocumentViewChange *newChange =
  102. [FSTDocumentViewChange changeWithDocument:change.document
  103. type:FSTDocumentViewChangeTypeAdded];
  104. self.changeMap = [self.changeMap dictionaryBySettingObject:newChange forKey:key];
  105. } else if (change.type == FSTDocumentViewChangeTypeRemoved &&
  106. oldChange.type == FSTDocumentViewChangeTypeAdded) {
  107. self.changeMap = [self.changeMap dictionaryByRemovingObjectForKey:key];
  108. } else if (change.type == FSTDocumentViewChangeTypeRemoved &&
  109. oldChange.type == FSTDocumentViewChangeTypeModified) {
  110. FSTDocumentViewChange *newChange =
  111. [FSTDocumentViewChange changeWithDocument:oldChange.document
  112. type:FSTDocumentViewChangeTypeRemoved];
  113. self.changeMap = [self.changeMap dictionaryBySettingObject:newChange forKey:key];
  114. } else if (change.type == FSTDocumentViewChangeTypeAdded &&
  115. oldChange.type == FSTDocumentViewChangeTypeRemoved) {
  116. FSTDocumentViewChange *newChange =
  117. [FSTDocumentViewChange changeWithDocument:change.document
  118. type:FSTDocumentViewChangeTypeModified];
  119. self.changeMap = [self.changeMap dictionaryBySettingObject:newChange forKey:key];
  120. } else {
  121. // This includes these cases, which don't make sense:
  122. // Added -> Added
  123. // Removed -> Removed
  124. // Modified -> Added
  125. // Removed -> Modified
  126. // Metadata -> Added
  127. // Removed -> Metadata
  128. FSTFail(@"Unsupported combination of changes: %ld after %ld", (long)change.type,
  129. (long)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