FSTRemoteEvent.h 7.5 KB

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