FSTRemoteEvent.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <set>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  23. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  24. #include "Firestore/core/src/firebase/firestore/model/types.h"
  25. @class FSTDocument;
  26. @class FSTExistenceFilter;
  27. @class FSTMaybeDocument;
  28. @class FSTWatchChange;
  29. @class FSTQueryData;
  30. @class FSTDocumentWatchChange;
  31. @class FSTWatchTargetChange;
  32. @class FSTExistenceFilterWatchChange;
  33. NS_ASSUME_NONNULL_BEGIN
  34. /**
  35. * Interface implemented by RemoteStore to expose target metadata to the FSTWatchChangeAggregator.
  36. */
  37. @protocol FSTTargetMetadataProvider
  38. /**
  39. * Returns the set of remote document keys for the given target ID as of the last raised snapshot.
  40. */
  41. - (firebase::firestore::model::DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetID;
  42. /**
  43. * Returns the FSTQueryData for an active target ID or 'null' if this query has become inactive
  44. */
  45. - (nullable FSTQueryData *)queryDataForTarget:(FSTBoxedTargetID *)targetID;
  46. @end
  47. #pragma mark - FSTTargetChange
  48. /**
  49. * An FSTTargetChange specifies the set of changes for a specific target as part of an
  50. * FSTRemoteEvent. These changes track which documents are added, modified or emoved, as well as the
  51. * target's resume token and whether the target is marked CURRENT.
  52. *
  53. * The actual changes *to* documents are not part of the FSTTargetChange since documents may be part
  54. * of multiple targets.
  55. */
  56. @interface FSTTargetChange : NSObject
  57. /**
  58. * Creates a new target change with the given SnapshotVersion.
  59. */
  60. - (instancetype)initWithResumeToken:(NSData *)resumeToken
  61. current:(BOOL)current
  62. addedDocuments:(firebase::firestore::model::DocumentKeySet)addedDocuments
  63. modifiedDocuments:(firebase::firestore::model::DocumentKeySet)modifiedDocuments
  64. removedDocuments:(firebase::firestore::model::DocumentKeySet)removedDocuments
  65. NS_DESIGNATED_INITIALIZER;
  66. - (instancetype)init NS_UNAVAILABLE;
  67. /**
  68. * An opaque, server-assigned token that allows watching a query to be resumed after
  69. * disconnecting without retransmitting all the data that matches the query. The resume token
  70. * essentially identifies a point in time from which the server should resume sending results.
  71. */
  72. @property(nonatomic, strong, readonly) NSData *resumeToken;
  73. /**
  74. * The "current" (synced) status of this target. Note that "current" has special meaning in the RPC
  75. * protocol that implies that a target is both up-to-date and consistent with the rest of the watch
  76. * stream.
  77. */
  78. @property(nonatomic, assign, readonly) BOOL current;
  79. /**
  80. * The set of documents that were newly assigned to this target as part of this remote event.
  81. */
  82. - (const firebase::firestore::model::DocumentKeySet &)addedDocuments;
  83. /**
  84. * The set of documents that were already assigned to this target but received an update during this
  85. * remote event.
  86. */
  87. - (const firebase::firestore::model::DocumentKeySet &)modifiedDocuments;
  88. /**
  89. * The set of documents that were removed from this target as part of this remote event.
  90. */
  91. - (const firebase::firestore::model::DocumentKeySet &)removedDocuments;
  92. @end
  93. #pragma mark - FSTRemoteEvent
  94. /**
  95. * An event from the RemoteStore. It is split into targetChanges (changes to the state or the set
  96. * of documents in our watched targets) and documentUpdates (changes to the actual documents).
  97. */
  98. @interface FSTRemoteEvent : NSObject
  99. - (instancetype)
  100. initWithSnapshotVersion:(firebase::firestore::model::SnapshotVersion)snapshotVersion
  101. targetChanges:
  102. (std::unordered_map<firebase::firestore::model::TargetId, FSTTargetChange *>)
  103. targetChanges
  104. targetMismatches:
  105. (std::unordered_set<firebase::firestore::model::TargetId>)targetMismatches
  106. documentUpdates:
  107. (std::unordered_map<firebase::firestore::model::DocumentKey,
  108. FSTMaybeDocument *,
  109. firebase::firestore::model::DocumentKeyHash>)documentUpdates
  110. limboDocuments:(firebase::firestore::model::DocumentKeySet)limboDocuments;
  111. /** The snapshot version this event brings us up to. */
  112. - (const firebase::firestore::model::SnapshotVersion &)snapshotVersion;
  113. /**
  114. * A set of which document updates are due only to limbo resolution targets.
  115. */
  116. - (const firebase::firestore::model::DocumentKeySet &)limboDocumentChanges;
  117. /** A map from target to changes to the target. See TargetChange. */
  118. - (const std::unordered_map<firebase::firestore::model::TargetId, FSTTargetChange *> &)
  119. targetChanges;
  120. /**
  121. * A set of targets that is known to be inconsistent. Listens for these targets should be
  122. * re-established without resume tokens.
  123. */
  124. - (const std::unordered_set<firebase::firestore::model::TargetId> &)targetMismatches;
  125. /**
  126. * A set of which documents have changed or been deleted, along with the doc's new values (if not
  127. * deleted).
  128. */
  129. - (const std::unordered_map<firebase::firestore::model::DocumentKey,
  130. FSTMaybeDocument *,
  131. firebase::firestore::model::DocumentKeyHash> &)documentUpdates;
  132. @end
  133. #pragma mark - FSTWatchChangeAggregator
  134. /**
  135. * A helper class to accumulate watch changes into a FSTRemoteEvent and other target
  136. * information.
  137. */
  138. @interface FSTWatchChangeAggregator : NSObject
  139. - (instancetype)initWithTargetMetadataProvider:(id<FSTTargetMetadataProvider>)targetMetadataProvider
  140. NS_DESIGNATED_INITIALIZER;
  141. - (instancetype)init NS_UNAVAILABLE;
  142. /** Processes and adds the FSTDocumentWatchChange to the current set of changes. */
  143. - (void)handleDocumentChange:(FSTDocumentWatchChange *)documentChange;
  144. /** Processes and adds the WatchTargetChange to the current set of changes. */
  145. - (void)handleTargetChange:(FSTWatchTargetChange *)targetChange;
  146. /** Removes the in-memory state for the provided target. */
  147. - (void)removeTarget:(firebase::firestore::model::TargetId)targetID;
  148. /**
  149. * Handles existence filters and synthesizes deletes for filter mismatches. Targets that are
  150. * invalidated by filter mismatches are added to `targetMismatches`.
  151. */
  152. - (void)handleExistenceFilter:(FSTExistenceFilterWatchChange *)existenceFilter;
  153. /**
  154. * Increment the number of acks needed from watch before we can consider the server to be 'in-sync'
  155. * with the client's active targets.
  156. */
  157. - (void)recordTargetRequest:(FSTBoxedTargetID *)targetID;
  158. /**
  159. * Converts the current state into a remote event with the snapshot version taken from the
  160. * initializer.
  161. */
  162. - (FSTRemoteEvent *)remoteEventAtSnapshotVersion:
  163. (const firebase::firestore::model::SnapshotVersion &)snapshotVersion;
  164. @end
  165. NS_ASSUME_NONNULL_END