FIRDocumentChange.mm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "FIRDocumentChange.h"
  17. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  18. #import "Firestore/Source/Core/FSTQuery.h"
  19. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTDocumentSet.h"
  22. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  23. using firebase::firestore::core::DocumentViewChangeType;
  24. NS_ASSUME_NONNULL_BEGIN
  25. @interface FIRDocumentChange ()
  26. - (instancetype)initWithType:(FIRDocumentChangeType)type
  27. document:(FIRDocumentSnapshot *)document
  28. oldIndex:(NSUInteger)oldIndex
  29. newIndex:(NSUInteger)newIndex NS_DESIGNATED_INITIALIZER;
  30. @end
  31. @implementation FIRDocumentChange (Internal)
  32. + (FIRDocumentChangeType)documentChangeTypeForChange:(FSTDocumentViewChange *)change {
  33. switch (change.type) {
  34. case DocumentViewChangeType::kAdded:
  35. return FIRDocumentChangeTypeAdded;
  36. case DocumentViewChangeType::kModified:
  37. case DocumentViewChangeType::kMetadata:
  38. return FIRDocumentChangeTypeModified;
  39. case DocumentViewChangeType::kRemoved:
  40. return FIRDocumentChangeTypeRemoved;
  41. }
  42. HARD_FAIL("Unknown DocumentViewChangeTyp: %s", change.type);
  43. }
  44. + (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:(FSTViewSnapshot *)snapshot
  45. includeMetadataChanges:(BOOL)includeMetadataChanges
  46. firestore:(FIRFirestore *)firestore {
  47. if (snapshot.oldDocuments.isEmpty) {
  48. // Special case the first snapshot because index calculation is easy and fast. Also all changes
  49. // on the first snapshot are adds so there are also no metadata-only changes to filter out.
  50. FSTDocument *_Nullable lastDocument = nil;
  51. NSUInteger index = 0;
  52. NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
  53. for (FSTDocumentViewChange *change in snapshot.documentChanges) {
  54. FIRQueryDocumentSnapshot *document = [FIRQueryDocumentSnapshot
  55. snapshotWithFirestore:firestore
  56. documentKey:change.document.key
  57. document:change.document
  58. fromCache:snapshot.isFromCache
  59. hasPendingWrites:snapshot.mutatedKeys.contains(change.document.key)];
  60. HARD_ASSERT(change.type == DocumentViewChangeType::kAdded,
  61. "Invalid event type for first snapshot");
  62. HARD_ASSERT(!lastDocument || snapshot.query.comparator(lastDocument, change.document) ==
  63. NSOrderedAscending,
  64. "Got added events in wrong order");
  65. [changes addObject:[[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeAdded
  66. document:document
  67. oldIndex:NSNotFound
  68. newIndex:index++]];
  69. }
  70. return changes;
  71. } else {
  72. // A DocumentSet that is updated incrementally as changes are applied to use to lookup the index
  73. // of a document.
  74. FSTDocumentSet *indexTracker = snapshot.oldDocuments;
  75. NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
  76. for (FSTDocumentViewChange *change in snapshot.documentChanges) {
  77. if (!includeMetadataChanges && change.type == DocumentViewChangeType::kMetadata) {
  78. continue;
  79. }
  80. FIRQueryDocumentSnapshot *document = [FIRQueryDocumentSnapshot
  81. snapshotWithFirestore:firestore
  82. documentKey:change.document.key
  83. document:change.document
  84. fromCache:snapshot.isFromCache
  85. hasPendingWrites:snapshot.mutatedKeys.contains(change.document.key)];
  86. NSUInteger oldIndex = NSNotFound;
  87. NSUInteger newIndex = NSNotFound;
  88. if (change.type != DocumentViewChangeType::kAdded) {
  89. oldIndex = [indexTracker indexOfKey:change.document.key];
  90. HARD_ASSERT(oldIndex != NSNotFound, "Index for document not found");
  91. indexTracker = [indexTracker documentSetByRemovingKey:change.document.key];
  92. }
  93. if (change.type != DocumentViewChangeType::kRemoved) {
  94. indexTracker = [indexTracker documentSetByAddingDocument:change.document];
  95. newIndex = [indexTracker indexOfKey:change.document.key];
  96. }
  97. [FIRDocumentChange documentChangeTypeForChange:change];
  98. FIRDocumentChangeType type = [FIRDocumentChange documentChangeTypeForChange:change];
  99. [changes addObject:[[FIRDocumentChange alloc] initWithType:type
  100. document:document
  101. oldIndex:oldIndex
  102. newIndex:newIndex]];
  103. }
  104. return changes;
  105. }
  106. }
  107. @end
  108. @implementation FIRDocumentChange
  109. - (instancetype)initWithType:(FIRDocumentChangeType)type
  110. document:(FIRQueryDocumentSnapshot *)document
  111. oldIndex:(NSUInteger)oldIndex
  112. newIndex:(NSUInteger)newIndex {
  113. if (self = [super init]) {
  114. _type = type;
  115. _document = document;
  116. _oldIndex = oldIndex;
  117. _newIndex = newIndex;
  118. }
  119. return self;
  120. }
  121. - (BOOL)isEqual:(nullable id)other {
  122. if (other == self) return YES;
  123. if (![other isKindOfClass:[FIRDocumentChange class]]) return NO;
  124. FIRDocumentChange *change = (FIRDocumentChange *)other;
  125. return self.type == change.type && [self.document isEqual:change.document] &&
  126. self.oldIndex == change.oldIndex && self.newIndex == change.newIndex;
  127. }
  128. - (NSUInteger)hash {
  129. NSUInteger result = (NSUInteger)self.type;
  130. result = result * 31u + [self.document hash];
  131. result = result * 31u + (NSUInteger)self.oldIndex;
  132. result = result * 31u + (NSUInteger)self.newIndex;
  133. return result;
  134. }
  135. @end
  136. NS_ASSUME_NONNULL_END