FSTRemoteEvent.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/Core/FSTTypes.h"
  22. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  23. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  24. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  25. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.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:(std::unordered_map<FSTTargetID, FSTTargetChange *>)targetChanges
  103. targetMismatches:(std::unordered_set<FSTTargetID>)targetMismatches
  104. documentUpdates:
  105. (std::unordered_map<firebase::firestore::model::DocumentKey,
  106. FSTMaybeDocument *,
  107. firebase::firestore::model::DocumentKeyHash>)documentUpdates
  108. limboDocuments:(firebase::firestore::model::DocumentKeySet)limboDocuments;
  109. /** The snapshot version this event brings us up to. */
  110. - (const firebase::firestore::model::SnapshotVersion &)snapshotVersion;
  111. /**
  112. * A set of which document updates are due only to limbo resolution targets.
  113. */
  114. - (const firebase::firestore::model::DocumentKeySet &)limboDocumentChanges;
  115. /** A map from target to changes to the target. See TargetChange. */
  116. - (const std::unordered_map<FSTTargetID, FSTTargetChange *> &)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<FSTTargetID> &)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. /**
  144. * Handles existence filters and synthesizes deletes for filter mismatches. Targets that are
  145. * invalidated by filter mismatches are added to `targetMismatches`.
  146. */
  147. - (void)handleExistenceFilter:(FSTExistenceFilterWatchChange *)existenceFilter;
  148. /**
  149. * Increment the number of acks needed from watch before we can consider the server to be 'in-sync'
  150. * with the client's active targets.
  151. */
  152. - (void)recordTargetRequest:(FSTBoxedTargetID *)targetID;
  153. /**
  154. * Converts the current state into a remote event with the snapshot version taken from the
  155. * initializer.
  156. */
  157. - (FSTRemoteEvent *)remoteEventAtSnapshotVersion:
  158. (const firebase::firestore::model::SnapshotVersion &)snapshotVersion;
  159. @end
  160. NS_ASSUME_NONNULL_END