FSTLocalStore.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <vector>
  18. #import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
  19. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  23. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  24. #include "Firestore/core/src/firebase/firestore/model/types.h"
  25. namespace firebase {
  26. namespace firestore {
  27. namespace remote {
  28. class RemoteEvent;
  29. } // namespace remote
  30. } // namespace firestore
  31. } // namespace firebase
  32. @class FSTLocalViewChanges;
  33. @class FSTLocalWriteResult;
  34. @class FSTMutation;
  35. @class FSTMutationBatch;
  36. @class FSTMutationBatchResult;
  37. @class FSTQuery;
  38. @class FSTQueryData;
  39. @protocol FSTPersistence;
  40. namespace auth = firebase::firestore::auth;
  41. namespace local = firebase::firestore::local;
  42. namespace model = firebase::firestore::model;
  43. namespace remote = firebase::firestore::remote;
  44. NS_ASSUME_NONNULL_BEGIN
  45. /**
  46. * Local storage in the Firestore client. Coordinates persistence components like the mutation
  47. * queue and remote document cache to present a latency compensated view of stored data.
  48. *
  49. * The LocalStore is responsible for accepting mutations from the Sync Engine. Writes from the
  50. * client are put into a queue as provisional Mutations until they are processed by the RemoteStore
  51. * and confirmed as having been written to the server.
  52. *
  53. * The local store provides the local version of documents that have been modified locally. It
  54. * maintains the constraint:
  55. *
  56. * LocalDocument = RemoteDocument + Active(LocalMutations)
  57. *
  58. * (Active mutations are those that are enqueued and have not been previously acknowledged or
  59. * rejected).
  60. *
  61. * The RemoteDocument ("ground truth") state is provided via the applyChangeBatch method. It will
  62. * be some version of a server-provided document OR will be a server-provided document PLUS
  63. * acknowledged mutations:
  64. *
  65. * RemoteDocument' = RemoteDocument + Acknowledged(LocalMutations)
  66. *
  67. * Note that this "dirty" version of a RemoteDocument will not be identical to a server base
  68. * version, since it has LocalMutations added to it pending getting an authoritative copy from the
  69. * server.
  70. *
  71. * Since LocalMutations can be rejected by the server, we have to be able to revert a LocalMutation
  72. * that has already been applied to the LocalDocument (typically done by replaying all remaining
  73. * LocalMutations to the RemoteDocument to re-apply).
  74. *
  75. * It also maintains the persistence of mapping queries to resume tokens and target ids.
  76. *
  77. * The LocalStore must be able to efficiently execute queries against its local cache of the
  78. * documents, to provide the initial set of results before any remote changes have been received.
  79. */
  80. @interface FSTLocalStore : NSObject
  81. /** Creates a new instance of the FSTLocalStore with its required dependencies as parameters. */
  82. - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
  83. initialUser:(const auth::User &)initialUser NS_DESIGNATED_INITIALIZER;
  84. - (instancetype)init NS_UNAVAILABLE;
  85. /** Performs any initial startup actions required by the local store. */
  86. - (void)start;
  87. /**
  88. * Tells the FSTLocalStore that the currently authenticated user has changed.
  89. *
  90. * In response the local store switches the mutation queue to the new user and returns any
  91. * resulting document changes.
  92. */
  93. - (model::MaybeDocumentMap)userDidChange:(const auth::User &)user;
  94. /** Accepts locally generated Mutations and commits them to storage. */
  95. - (FSTLocalWriteResult *)locallyWriteMutations:(std::vector<FSTMutation *> &&)mutations;
  96. /** Returns the current value of a document with a given key, or nil if not found. */
  97. - (nullable FSTMaybeDocument *)readDocument:(const model::DocumentKey &)key;
  98. /**
  99. * Acknowledges the given batch.
  100. *
  101. * On the happy path when a batch is acknowledged, the local store will
  102. *
  103. * + remove the batch from the mutation queue;
  104. * + apply the changes to the remote document cache;
  105. * + recalculate the latency compensated view implied by those changes (there may be mutations in
  106. * the queue that affect the documents but haven't been acknowledged yet); and
  107. * + give the changed documents back the sync engine
  108. *
  109. * @return The resulting (modified) documents.
  110. */
  111. - (model::MaybeDocumentMap)acknowledgeBatchWithResult:(FSTMutationBatchResult *)batchResult;
  112. /**
  113. * Removes mutations from the MutationQueue for the specified batch. LocalDocuments will be
  114. * recalculated.
  115. *
  116. * @return The resulting (modified) documents.
  117. */
  118. - (model::MaybeDocumentMap)rejectBatchID:(model::BatchId)batchID;
  119. /** Returns the last recorded stream token for the current user. */
  120. - (nullable NSData *)lastStreamToken;
  121. /**
  122. * Sets the stream token for the current user without acknowledging any mutation batch. This is
  123. * usually only useful after a stream handshake or in response to an error that requires clearing
  124. * the stream token.
  125. */
  126. - (void)setLastStreamToken:(nullable NSData *)streamToken;
  127. /**
  128. * Returns the last consistent snapshot processed (used by the RemoteStore to determine whether to
  129. * buffer incoming snapshots from the backend).
  130. */
  131. - (const model::SnapshotVersion &)lastRemoteSnapshotVersion;
  132. /**
  133. * Updates the "ground-state" (remote) documents. We assume that the remote event reflects any
  134. * write batches that have been acknowledged or rejected (i.e. we do not re-apply local mutations
  135. * to updates from this event).
  136. *
  137. * LocalDocuments are re-calculated if there are remaining mutations in the queue.
  138. */
  139. - (model::MaybeDocumentMap)applyRemoteEvent:(const remote::RemoteEvent &)remoteEvent;
  140. /**
  141. * Returns the keys of the documents that are associated with the given targetID in the remote
  142. * table.
  143. */
  144. - (model::DocumentKeySet)remoteDocumentKeysForTarget:(model::TargetId)targetID;
  145. /**
  146. * Assigns @a query an internal ID so that its results can be pinned so they don't get GC'd.
  147. * A query must be allocated in the local store before the store can be used to manage its view.
  148. */
  149. - (FSTQueryData *)allocateQuery:(FSTQuery *)query;
  150. /** Unpin all the documents associated with @a query. */
  151. - (void)releaseQuery:(FSTQuery *)query;
  152. /** Runs @a query against all the documents in the local store and returns the results. */
  153. - (model::DocumentMap)executeQuery:(FSTQuery *)query;
  154. /** Notify the local store of the changed views to locally pin / unpin documents. */
  155. - (void)notifyLocalViewChanges:(NSArray<FSTLocalViewChanges *> *)viewChanges;
  156. /**
  157. * Gets the mutation batch after the passed in batchId in the mutation queue or nil if empty.
  158. *
  159. * @param batchID The batch to search after, or -1 for the first mutation in the queue.
  160. * @return the next mutation or nil if there wasn't one.
  161. */
  162. - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(model::BatchId)batchID;
  163. - (local::LruResults)collectGarbage:(FSTLRUGarbageCollector *)garbageCollector;
  164. @end
  165. NS_ASSUME_NONNULL_END