FIRDocumentChange.mm 6.6 KB

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