FSTLocalStore.h 7.6 KB

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