FIRDocumentChange.mm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 =
  54. [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  55. documentKey:change.document.key
  56. document:change.document
  57. fromCache:snapshot.isFromCache];
  58. HARD_ASSERT(change.type == FSTDocumentViewChangeTypeAdded,
  59. "Invalid event type for first snapshot");
  60. HARD_ASSERT(!lastDocument || snapshot.query.comparator(lastDocument, change.document) ==
  61. NSOrderedAscending,
  62. "Got added events in wrong order");
  63. [changes addObject:[[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeAdded
  64. document:document
  65. oldIndex:NSNotFound
  66. newIndex:index++]];
  67. }
  68. return changes;
  69. } else {
  70. // A DocumentSet that is updated incrementally as changes are applied to use to lookup the index
  71. // of a document.
  72. FSTDocumentSet *indexTracker = snapshot.oldDocuments;
  73. NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
  74. for (FSTDocumentViewChange *change in snapshot.documentChanges) {
  75. if (!includeMetadataChanges && change.type == FSTDocumentViewChangeTypeMetadata) {
  76. continue;
  77. }
  78. FIRQueryDocumentSnapshot *document =
  79. [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  80. documentKey:change.document.key
  81. document:change.document
  82. fromCache:snapshot.isFromCache];
  83. NSUInteger oldIndex = NSNotFound;
  84. NSUInteger newIndex = NSNotFound;
  85. if (change.type != FSTDocumentViewChangeTypeAdded) {
  86. oldIndex = [indexTracker indexOfKey:change.document.key];
  87. HARD_ASSERT(oldIndex != NSNotFound, "Index for document not found");
  88. indexTracker = [indexTracker documentSetByRemovingKey:change.document.key];
  89. }
  90. if (change.type != FSTDocumentViewChangeTypeRemoved) {
  91. indexTracker = [indexTracker documentSetByAddingDocument:change.document];
  92. newIndex = [indexTracker indexOfKey:change.document.key];
  93. }
  94. [FIRDocumentChange documentChangeTypeForChange:change];
  95. FIRDocumentChangeType type = [FIRDocumentChange documentChangeTypeForChange:change];
  96. [changes addObject:[[FIRDocumentChange alloc] initWithType:type
  97. document:document
  98. oldIndex:oldIndex
  99. newIndex:newIndex]];
  100. }
  101. return changes;
  102. }
  103. }
  104. @end
  105. @implementation FIRDocumentChange
  106. - (instancetype)initWithType:(FIRDocumentChangeType)type
  107. document:(FIRQueryDocumentSnapshot *)document
  108. oldIndex:(NSUInteger)oldIndex
  109. newIndex:(NSUInteger)newIndex {
  110. if (self = [super init]) {
  111. _type = type;
  112. _document = document;
  113. _oldIndex = oldIndex;
  114. _newIndex = newIndex;
  115. }
  116. return self;
  117. }
  118. - (BOOL)isEqual:(nullable id)other {
  119. if (other == self) return YES;
  120. if (![other isKindOfClass:[FIRDocumentChange class]]) return NO;
  121. FIRDocumentChange *change = (FIRDocumentChange *)other;
  122. return self.type == change.type && [self.document isEqual:change.document] &&
  123. self.oldIndex == change.oldIndex && self.newIndex == change.newIndex;
  124. }
  125. - (NSUInteger)hash {
  126. NSUInteger result = (NSUInteger)self.type;
  127. result = result * 31u + [self.document hash];
  128. result = result * 31u + (NSUInteger)self.oldIndex;
  129. result = result * 31u + (NSUInteger)self.newIndex;
  130. return result;
  131. }
  132. @end
  133. NS_ASSUME_NONNULL_END