FSTView.h 5.8 KB

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