FIRDocumentChange.mm 6.4 KB

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