FSTRemoteEvent.h 7.4 KB

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