FSTRemoteEvent.h 8.7 KB

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