FIRDocumentChange.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #import "Firestore/Source/Util/FSTAssert.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. FSTFail(@"Unknown FSTDocumentViewChange: %ld", (long)change.type);
  41. }
  42. }
  43. + (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:(FSTViewSnapshot *)snapshot
  44. firestore:(FIRFirestore *)firestore {
  45. if (snapshot.oldDocuments.isEmpty) {
  46. // Special case the first snapshot because index calculation is easy and fast
  47. FSTDocument *_Nullable lastDocument = nil;
  48. NSUInteger index = 0;
  49. NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
  50. for (FSTDocumentViewChange *change in snapshot.documentChanges) {
  51. FIRDocumentSnapshot *document =
  52. [FIRDocumentSnapshot snapshotWithFirestore:firestore
  53. documentKey:change.document.key
  54. document:change.document
  55. fromCache:snapshot.isFromCache];
  56. FSTAssert(change.type == FSTDocumentViewChangeTypeAdded,
  57. @"Invalid event type for first snapshot");
  58. FSTAssert(!lastDocument ||
  59. snapshot.query.comparator(lastDocument, change.document) == NSOrderedAscending,
  60. @"Got added events in wrong order");
  61. [changes addObject:[[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeAdded
  62. document:document
  63. oldIndex:NSNotFound
  64. newIndex:index++]];
  65. }
  66. return changes;
  67. } else {
  68. // A DocumentSet that is updated incrementally as changes are applied to use to lookup the index
  69. // of a document.
  70. FSTDocumentSet *indexTracker = snapshot.oldDocuments;
  71. NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
  72. for (FSTDocumentViewChange *change in snapshot.documentChanges) {
  73. FIRDocumentSnapshot *document =
  74. [FIRDocumentSnapshot snapshotWithFirestore:firestore
  75. documentKey:change.document.key
  76. document:change.document
  77. fromCache:snapshot.isFromCache];
  78. NSUInteger oldIndex = NSNotFound;
  79. NSUInteger newIndex = NSNotFound;
  80. if (change.type != FSTDocumentViewChangeTypeAdded) {
  81. oldIndex = [indexTracker indexOfKey:change.document.key];
  82. FSTAssert(oldIndex != NSNotFound, @"Index for document not found");
  83. indexTracker = [indexTracker documentSetByRemovingKey:change.document.key];
  84. }
  85. if (change.type != FSTDocumentViewChangeTypeRemoved) {
  86. indexTracker = [indexTracker documentSetByAddingDocument:change.document];
  87. newIndex = [indexTracker indexOfKey:change.document.key];
  88. }
  89. [FIRDocumentChange documentChangeTypeForChange:change];
  90. FIRDocumentChangeType type = [FIRDocumentChange documentChangeTypeForChange:change];
  91. [changes addObject:[[FIRDocumentChange alloc] initWithType:type
  92. document:document
  93. oldIndex:oldIndex
  94. newIndex:newIndex]];
  95. }
  96. return changes;
  97. }
  98. }
  99. @end
  100. @implementation FIRDocumentChange
  101. - (instancetype)initWithType:(FIRDocumentChangeType)type
  102. document:(FIRDocumentSnapshot *)document
  103. oldIndex:(NSUInteger)oldIndex
  104. newIndex:(NSUInteger)newIndex {
  105. if (self = [super init]) {
  106. _type = type;
  107. _document = document;
  108. _oldIndex = oldIndex;
  109. _newIndex = newIndex;
  110. }
  111. return self;
  112. }
  113. @end
  114. NS_ASSUME_NONNULL_END