FSTRemoteEvent.h 7.2 KB

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