FSTLocalStore.h 7.3 KB

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