FSTView.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  18. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  21. #include "Firestore/core/src/firebase/firestore/model/types.h"
  22. #include "absl/types/optional.h"
  23. @class FSTQuery;
  24. namespace firebase {
  25. namespace firestore {
  26. namespace remote {
  27. class TargetChange;
  28. } // namespace remote
  29. } // namespace firestore
  30. } // namespace firebase
  31. namespace core = firebase::firestore::core;
  32. namespace model = firebase::firestore::model;
  33. namespace remote = firebase::firestore::remote;
  34. NS_ASSUME_NONNULL_BEGIN
  35. #pragma mark - FSTViewDocumentChanges
  36. /** The result of applying a set of doc changes to a view. */
  37. @interface FSTViewDocumentChanges : NSObject
  38. - (instancetype)init NS_UNAVAILABLE;
  39. - (const model::DocumentKeySet &)mutatedKeys;
  40. /** The new set of docs that should be in the view. */
  41. - (const model::DocumentSet &)documentSet;
  42. /** The diff of this these docs with the previous set of docs. */
  43. - (const core::DocumentViewChangeSet &)changeSet;
  44. /**
  45. * Whether the set of documents passed in was not sufficient to calculate the new state of the view
  46. * and there needs to be another pass based on the local cache.
  47. */
  48. @property(nonatomic, assign, readonly) BOOL needsRefill;
  49. @end
  50. #pragma mark - FSTLimboDocumentChange
  51. typedef NS_ENUM(NSInteger, FSTLimboDocumentChangeType) {
  52. FSTLimboDocumentChangeTypeAdded = 0,
  53. FSTLimboDocumentChangeTypeRemoved,
  54. };
  55. // A change to a particular document wrt to whether it is in "limbo".
  56. @interface FSTLimboDocumentChange : NSObject
  57. + (instancetype)changeWithType:(FSTLimboDocumentChangeType)type key:(model::DocumentKey)key;
  58. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  59. - (const model::DocumentKey &)key;
  60. @property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
  61. @end
  62. #pragma mark - FSTViewChange
  63. // A set of changes to a view.
  64. @interface FSTViewChange : NSObject
  65. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  66. - (absl::optional<core::ViewSnapshot> &)snapshot;
  67. @property(nonatomic, strong, readonly) NSArray<FSTLimboDocumentChange *> *limboChanges;
  68. @end
  69. #pragma mark - FSTView
  70. /**
  71. * View is responsible for computing the final merged truth of what docs are in a query. It gets
  72. * notified of local and remote changes to docs, and applies the query filters and limits to
  73. * determine the most correct possible results.
  74. */
  75. @interface FSTView : NSObject
  76. - (instancetype)init NS_UNAVAILABLE;
  77. - (instancetype)initWithQuery:(FSTQuery *)query
  78. remoteDocuments:(model::DocumentKeySet)remoteDocuments NS_DESIGNATED_INITIALIZER;
  79. /**
  80. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  81. * should be, what the changes were, and whether we may need to go back to the local cache for
  82. * more results. Does not make any changes to the view.
  83. *
  84. * @param docChanges The doc changes to apply to this view.
  85. * @return a new set of docs, changes, and refill flag.
  86. */
  87. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(const model::MaybeDocumentMap &)docChanges;
  88. /**
  89. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  90. * should be, what the changes were, and whether we may need to go back to the local cache for
  91. * more results. Does not make any changes to the view.
  92. *
  93. * @param docChanges The doc changes to apply to this view.
  94. * @param previousChanges If this is being called with a refill, then start with this set of docs
  95. * and changes instead of the current view.
  96. * @return a new set of docs, changes, and refill flag.
  97. */
  98. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(const model::MaybeDocumentMap &)docChanges
  99. previousChanges:
  100. (nullable FSTViewDocumentChanges *)previousChanges;
  101. /**
  102. * Updates the view with the given ViewDocumentChanges.
  103. *
  104. * @param docChanges The set of changes to make to the view's docs.
  105. * @return A new FSTViewChange with the given docs, changes, and sync state.
  106. */
  107. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;
  108. /**
  109. * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
  110. * the given (optional) target change.
  111. *
  112. * @param docChanges The set of changes to make to the view's docs.
  113. * @param targetChange A target change to apply for computing limbo docs and sync state.
  114. * @return A new FSTViewChange with the given docs, changes, and sync state.
  115. */
  116. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
  117. targetChange:
  118. (const absl::optional<remote::TargetChange> &)targetChange;
  119. /**
  120. * Applies an OnlineState change to the view, potentially generating an FSTViewChange if the
  121. * view's syncState changes as a result.
  122. */
  123. - (FSTViewChange *)applyChangedOnlineState:(model::OnlineState)onlineState;
  124. /**
  125. * The set of remote documents that the server has told us belongs to the target associated with
  126. * this view.
  127. */
  128. - (const model::DocumentKeySet &)syncedDocuments;
  129. @end
  130. NS_ASSUME_NONNULL_END