FSTRemoteEvent.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <Foundation/Foundation.h>
  17. #include <map>
  18. #import "Firestore/Source/Core/FSTTypes.h"
  19. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  20. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. @class FSTDocument;
  23. @class FSTExistenceFilter;
  24. @class FSTMaybeDocument;
  25. @class FSTSnapshotVersion;
  26. @class FSTWatchChange;
  27. @class FSTQueryData;
  28. NS_ASSUME_NONNULL_BEGIN
  29. #pragma mark - FSTTargetMapping
  30. /**
  31. * TargetMapping represents a change to the documents in a query from the server. This can either
  32. * be an incremental Update or a full Reset.
  33. *
  34. * <p>This is an empty abstract class so that all the different kinds of changes can have a common
  35. * base class.
  36. */
  37. @interface FSTTargetMapping : NSObject
  38. @end
  39. #pragma mark - FSTResetMapping
  40. /** The new set of documents to replace the current documents for a target. */
  41. @interface FSTResetMapping : FSTTargetMapping
  42. /**
  43. * Creates a new mapping with the keys for the given documents added. This is intended primarily
  44. * for testing.
  45. */
  46. + (FSTResetMapping *)mappingWithDocuments:(NSArray<FSTDocument *> *)documents;
  47. /** The new set of documents for the target. */
  48. @property(nonatomic, strong, readonly) FSTDocumentKeySet *documents;
  49. @end
  50. #pragma mark - FSTUpdateMapping
  51. /**
  52. * A target should update its set of documents with the given added/removed set of documents.
  53. */
  54. @interface FSTUpdateMapping : FSTTargetMapping
  55. /**
  56. * Creates a new mapping with the keys for the given documents added. This is intended primarily
  57. * for testing.
  58. */
  59. + (FSTUpdateMapping *)mappingWithAddedDocuments:(NSArray<FSTDocument *> *)added
  60. removedDocuments:(NSArray<FSTDocument *> *)removed;
  61. - (FSTDocumentKeySet *)applyTo:(FSTDocumentKeySet *)keys;
  62. /** The documents added to the target. */
  63. @property(nonatomic, strong, readonly) FSTDocumentKeySet *addedDocuments;
  64. /** The documents removed from the target. */
  65. @property(nonatomic, strong, readonly) FSTDocumentKeySet *removedDocuments;
  66. @end
  67. #pragma mark - FSTTargetChange
  68. /**
  69. * Represents an update to the current status of a target, either explicitly having no new state, or
  70. * the new value to set. Note "current" has special meaning in the RPC protocol that implies that a
  71. * target is both up-to-date and consistent with the rest of the watch stream.
  72. */
  73. typedef NS_ENUM(NSUInteger, FSTCurrentStatusUpdate) {
  74. /** The current status is not affected and should not be modified */
  75. FSTCurrentStatusUpdateNone,
  76. /** The target must be marked as no longer "current" */
  77. FSTCurrentStatusUpdateMarkNotCurrent,
  78. /** The target must be marked as "current" */
  79. FSTCurrentStatusUpdateMarkCurrent,
  80. };
  81. /**
  82. * A part of an FSTRemoteEvent specifying set of changes to a specific target. These changes track
  83. * what documents are currently included in the target as well as the current snapshot version and
  84. * resume token but the actual changes *to* documents are not part of the FSTTargetChange since
  85. * documents may be part of multiple targets.
  86. */
  87. @interface FSTTargetChange : NSObject
  88. /**
  89. * Creates a new target change with the given documents. Instances of FSTDocument are considered
  90. * added. Instance of FSTDeletedDocument are considered removed. This is intended primarily for
  91. * testing.
  92. */
  93. + (instancetype)changeWithDocuments:(NSArray<FSTMaybeDocument *> *)docs
  94. currentStatusUpdate:(FSTCurrentStatusUpdate)currentStatusUpdate;
  95. /**
  96. * The new "current" (synced) status of this target. Set to CurrentStatusUpdateNone if the status
  97. * should not be updated. Note "current" has special meaning for in the RPC protocol that implies
  98. * that a target is both up-to-date and consistent with the rest of the watch stream.
  99. */
  100. @property(nonatomic, assign, readonly) FSTCurrentStatusUpdate currentStatusUpdate;
  101. /** A set of changes to documents in this target. */
  102. @property(nonatomic, strong, readonly) FSTTargetMapping *mapping;
  103. /**
  104. * The snapshot version representing the last state at which this target received a consistent
  105. * snapshot from the backend.
  106. */
  107. @property(nonatomic, strong, readonly) FSTSnapshotVersion *snapshotVersion;
  108. /**
  109. * An opaque, server-assigned token that allows watching a query to be resumed after disconnecting
  110. * without retransmitting all the data that matches the query. The resume token essentially
  111. * identifies a point in time from which the server should resume sending results.
  112. */
  113. @property(nonatomic, strong, readonly) NSData *resumeToken;
  114. @end
  115. #pragma mark - FSTRemoteEvent
  116. /**
  117. * An event from the RemoteStore. It is split into targetChanges (changes to the state or the set
  118. * of documents in our watched targets) and documentUpdates (changes to the actual documents).
  119. */
  120. @interface FSTRemoteEvent : NSObject
  121. + (instancetype)
  122. eventWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  123. targetChanges:(NSMutableDictionary<NSNumber *, FSTTargetChange *> *)targetChanges
  124. documentUpdates:
  125. (std::map<firebase::firestore::model::DocumentKey, FSTMaybeDocument *>)documentUpdates;
  126. /** The snapshot version this event brings us up to. */
  127. @property(nonatomic, strong, readonly) FSTSnapshotVersion *snapshotVersion;
  128. /** A map from target to changes to the target. See TargetChange. */
  129. @property(nonatomic, strong, readonly)
  130. NSDictionary<FSTBoxedTargetID *, FSTTargetChange *> *targetChanges;
  131. /**
  132. * A set of which documents have changed or been deleted, along with the doc's new values
  133. * (if not deleted).
  134. */
  135. - (const std::map<firebase::firestore::model::DocumentKey, FSTMaybeDocument *> &)documentUpdates;
  136. /** Adds a document update to this remote event */
  137. - (void)addDocumentUpdate:(FSTMaybeDocument *)document;
  138. /** Handles an existence filter mismatch */
  139. - (void)handleExistenceFilterMismatchForTargetID:(FSTBoxedTargetID *)targetID;
  140. - (void)synthesizeDeleteForLimboTargetChange:(FSTTargetChange *)targetChange
  141. key:(const firebase::firestore::model::DocumentKey &)key;
  142. /**
  143. * Strips out mapping changes that aren't actually changes. That is, if the document already
  144. * existed in the target, and is being added in the target, and this is not a reset, we can
  145. * skip doing the work to associate the document with the target because it has already been done.
  146. */
  147. - (void)filterUpdatesFromTargetChange:(FSTTargetChange *)targetChange
  148. existingDocuments:(FSTDocumentKeySet *)existingDocuments;
  149. @end
  150. #pragma mark - FSTWatchChangeAggregator
  151. /**
  152. * A helper class to accumulate watch changes into a FSTRemoteEvent and other target
  153. * information.
  154. */
  155. @interface FSTWatchChangeAggregator : NSObject
  156. - (instancetype)
  157. initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  158. listenTargets:(NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *)listenTargets
  159. pendingTargetResponses:(NSDictionary<FSTBoxedTargetID *, NSNumber *> *)pendingTargetResponses
  160. NS_DESIGNATED_INITIALIZER;
  161. - (instancetype)init NS_UNAVAILABLE;
  162. /** The number of pending responses that are being waited on from watch */
  163. @property(nonatomic, strong, readonly)
  164. NSMutableDictionary<FSTBoxedTargetID *, NSNumber *> *pendingTargetResponses;
  165. /** Aggregates a watch change into the current state */
  166. - (void)addWatchChange:(FSTWatchChange *)watchChange;
  167. /** Aggregates all provided watch changes to the current state in order */
  168. - (void)addWatchChanges:(NSArray<FSTWatchChange *> *)watchChanges;
  169. /**
  170. * Converts the current state into a remote event with the snapshot version taken from the
  171. * initializer.
  172. */
  173. - (FSTRemoteEvent *)remoteEvent;
  174. /** The existence filters - if any - for the given target IDs. */
  175. @property(nonatomic, strong, readonly)
  176. NSDictionary<FSTBoxedTargetID *, FSTExistenceFilter *> *existenceFilters;
  177. @end
  178. NS_ASSUME_NONNULL_END