FIRDocumentChange.mm 6.5 KB

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