FSTSyncEngine.mm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "Firestore/Source/Core/FSTSyncEngine.h"
  17. #include <map>
  18. #include <memory>
  19. #include <set>
  20. #include <unordered_map>
  21. #include <utility>
  22. #include <vector>
  23. #import "FIRFirestoreErrors.h"
  24. #import "Firestore/Source/Core/FSTQuery.h"
  25. #import "Firestore/Source/Core/FSTView.h"
  26. #import "Firestore/Source/Local/FSTLocalStore.h"
  27. #import "Firestore/Source/Local/FSTLocalViewChanges.h"
  28. #import "Firestore/Source/Local/FSTLocalWriteResult.h"
  29. #import "Firestore/Source/Local/FSTQueryData.h"
  30. #import "Firestore/Source/Model/FSTDocument.h"
  31. #import "Firestore/Source/Model/FSTMutationBatch.h"
  32. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  33. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  34. #include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
  35. #include "Firestore/core/src/firebase/firestore/core/transaction.h"
  36. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  37. #include "Firestore/core/src/firebase/firestore/local/reference_set.h"
  38. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  39. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  40. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  41. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  42. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  43. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  44. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  45. #include "Firestore/core/src/firebase/firestore/util/log.h"
  46. #include "Firestore/core/src/firebase/firestore/util/status.h"
  47. #include "absl/types/optional.h"
  48. using firebase::firestore::FirestoreErrorCode;
  49. using firebase::firestore::auth::HashUser;
  50. using firebase::firestore::auth::User;
  51. using firebase::firestore::core::TargetIdGenerator;
  52. using firebase::firestore::core::Transaction;
  53. using firebase::firestore::core::ViewSnapshot;
  54. using firebase::firestore::local::ReferenceSet;
  55. using firebase::firestore::model::BatchId;
  56. using firebase::firestore::model::DocumentKey;
  57. using firebase::firestore::model::DocumentKeySet;
  58. using firebase::firestore::model::DocumentMap;
  59. using firebase::firestore::model::MaybeDocumentMap;
  60. using firebase::firestore::model::ListenSequenceNumber;
  61. using firebase::firestore::model::OnlineState;
  62. using firebase::firestore::model::SnapshotVersion;
  63. using firebase::firestore::model::TargetId;
  64. using firebase::firestore::remote::RemoteEvent;
  65. using firebase::firestore::remote::RemoteStore;
  66. using firebase::firestore::remote::TargetChange;
  67. using firebase::firestore::util::AsyncQueue;
  68. using firebase::firestore::util::MakeNSError;
  69. using firebase::firestore::util::Status;
  70. NS_ASSUME_NONNULL_BEGIN
  71. // Limbo documents don't use persistence, and are eagerly GC'd. So, listens for them don't need
  72. // real sequence numbers.
  73. static const ListenSequenceNumber kIrrelevantSequenceNumber = -1;
  74. #pragma mark - FSTQueryView
  75. /**
  76. * FSTQueryView contains all of the info that FSTSyncEngine needs to track for a particular
  77. * query and view.
  78. */
  79. @interface FSTQueryView : NSObject
  80. - (instancetype)initWithQuery:(FSTQuery *)query
  81. targetID:(TargetId)targetID
  82. resumeToken:(NSData *)resumeToken
  83. view:(FSTView *)view NS_DESIGNATED_INITIALIZER;
  84. - (instancetype)init NS_UNAVAILABLE;
  85. /** The query itself. */
  86. @property(nonatomic, strong, readonly) FSTQuery *query;
  87. /** The targetID created by the client that is used in the watch stream to identify this query. */
  88. @property(nonatomic, assign, readonly) TargetId targetID;
  89. /**
  90. * An identifier from the datastore backend that indicates the last state of the results that
  91. * was received. This can be used to indicate where to continue receiving new doc changes for the
  92. * query.
  93. */
  94. @property(nonatomic, copy, readonly) NSData *resumeToken;
  95. /**
  96. * The view is responsible for computing the final merged truth of what docs are in the query.
  97. * It gets notified of local and remote changes, and applies the query filters and limits to
  98. * determine the most correct possible results.
  99. */
  100. @property(nonatomic, strong, readonly) FSTView *view;
  101. @end
  102. @implementation FSTQueryView
  103. - (instancetype)initWithQuery:(FSTQuery *)query
  104. targetID:(TargetId)targetID
  105. resumeToken:(NSData *)resumeToken
  106. view:(FSTView *)view {
  107. if (self = [super init]) {
  108. _query = query;
  109. _targetID = targetID;
  110. _resumeToken = resumeToken;
  111. _view = view;
  112. }
  113. return self;
  114. }
  115. @end
  116. #pragma mark - LimboResolution
  117. /** Tracks a limbo resolution. */
  118. class LimboResolution {
  119. public:
  120. LimboResolution() {
  121. }
  122. explicit LimboResolution(const DocumentKey &key) : key{key} {
  123. }
  124. DocumentKey key;
  125. /**
  126. * Set to true once we've received a document. This is used in remoteKeysForTarget and
  127. * ultimately used by `WatchChangeAggregator` to decide whether it needs to manufacture a delete
  128. * event for the target once the target is CURRENT.
  129. */
  130. bool document_received = false;
  131. };
  132. #pragma mark - FSTSyncEngine
  133. @interface FSTSyncEngine ()
  134. /** The local store, used to persist mutations and cached documents. */
  135. @property(nonatomic, strong, readonly) FSTLocalStore *localStore;
  136. /** FSTQueryViews for all active queries, indexed by query. */
  137. @property(nonatomic, strong, readonly)
  138. NSMutableDictionary<FSTQuery *, FSTQueryView *> *queryViewsByQuery;
  139. @end
  140. @implementation FSTSyncEngine {
  141. /** The remote store for sending writes, watches, etc. to the backend. */
  142. RemoteStore *_remoteStore;
  143. /** Used for creating the TargetId for the listens used to resolve limbo documents. */
  144. TargetIdGenerator _targetIdGenerator;
  145. /** Stores user completion blocks, indexed by user and BatchId. */
  146. std::unordered_map<User, NSMutableDictionary<NSNumber *, FSTVoidErrorBlock> *, HashUser>
  147. _mutationCompletionBlocks;
  148. /** FSTQueryViews for all active queries, indexed by target ID. */
  149. std::unordered_map<TargetId, FSTQueryView *> _queryViewsByTarget;
  150. /**
  151. * When a document is in limbo, we create a special listen to resolve it. This maps the
  152. * DocumentKey of each limbo document to the TargetId of the listen resolving it.
  153. */
  154. std::map<DocumentKey, TargetId> _limboTargetsByKey;
  155. /**
  156. * Basically the inverse of limboTargetsByKey, a map of target ID to a LimboResolution (which
  157. * includes the DocumentKey as well as whether we've received a document for the target).
  158. */
  159. std::map<TargetId, LimboResolution> _limboResolutionsByTarget;
  160. User _currentUser;
  161. /** Used to track any documents that are currently in limbo. */
  162. ReferenceSet _limboDocumentRefs;
  163. }
  164. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  165. remoteStore:(RemoteStore *)remoteStore
  166. initialUser:(const User &)initialUser {
  167. if (self = [super init]) {
  168. _localStore = localStore;
  169. _remoteStore = remoteStore;
  170. _queryViewsByQuery = [NSMutableDictionary dictionary];
  171. _targetIdGenerator = TargetIdGenerator::SyncEngineTargetIdGenerator();
  172. _currentUser = initialUser;
  173. }
  174. return self;
  175. }
  176. - (TargetId)listenToQuery:(FSTQuery *)query {
  177. [self assertDelegateExistsForSelector:_cmd];
  178. HARD_ASSERT(self.queryViewsByQuery[query] == nil, "We already listen to query: %s", query);
  179. FSTQueryData *queryData = [self.localStore allocateQuery:query];
  180. ViewSnapshot viewSnapshot = [self initializeViewAndComputeSnapshotForQueryData:queryData];
  181. [self.syncEngineDelegate handleViewSnapshots:{viewSnapshot}];
  182. _remoteStore->Listen(queryData);
  183. return queryData.targetID;
  184. }
  185. - (ViewSnapshot)initializeViewAndComputeSnapshotForQueryData:(FSTQueryData *)queryData {
  186. DocumentMap docs = [self.localStore executeQuery:queryData.query];
  187. DocumentKeySet remoteKeys = [self.localStore remoteDocumentKeysForTarget:queryData.targetID];
  188. FSTView *view = [[FSTView alloc] initWithQuery:queryData.query
  189. remoteDocuments:std::move(remoteKeys)];
  190. FSTViewDocumentChanges *viewDocChanges = [view computeChangesWithDocuments:docs.underlying_map()];
  191. FSTViewChange *viewChange = [view applyChangesToDocuments:viewDocChanges];
  192. HARD_ASSERT(viewChange.limboChanges.count == 0,
  193. "View returned limbo docs before target ack from the server.");
  194. FSTQueryView *queryView = [[FSTQueryView alloc] initWithQuery:queryData.query
  195. targetID:queryData.targetID
  196. resumeToken:queryData.resumeToken
  197. view:view];
  198. self.queryViewsByQuery[queryData.query] = queryView;
  199. _queryViewsByTarget[queryData.targetID] = queryView;
  200. HARD_ASSERT(viewChange.snapshot.has_value(),
  201. "applyChangesToDocuments for new view should always return a snapshot");
  202. return viewChange.snapshot.value();
  203. }
  204. - (void)stopListeningToQuery:(FSTQuery *)query {
  205. [self assertDelegateExistsForSelector:_cmd];
  206. FSTQueryView *queryView = self.queryViewsByQuery[query];
  207. HARD_ASSERT(queryView, "Trying to stop listening to a query not found");
  208. [self.localStore releaseQuery:query];
  209. _remoteStore->StopListening(queryView.targetID);
  210. [self removeAndCleanupQuery:queryView];
  211. }
  212. - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations
  213. completion:(FSTVoidErrorBlock)completion {
  214. [self assertDelegateExistsForSelector:_cmd];
  215. FSTLocalWriteResult *result = [self.localStore locallyWriteMutations:std::move(mutations)];
  216. [self addMutationCompletionBlock:completion batchID:result.batchID];
  217. [self emitNewSnapshotsAndNotifyLocalStoreWithChanges:result.changes remoteEvent:absl::nullopt];
  218. _remoteStore->FillWritePipeline();
  219. }
  220. - (void)addMutationCompletionBlock:(FSTVoidErrorBlock)completion batchID:(BatchId)batchID {
  221. NSMutableDictionary<NSNumber *, FSTVoidErrorBlock> *completionBlocks =
  222. _mutationCompletionBlocks[_currentUser];
  223. if (!completionBlocks) {
  224. completionBlocks = [NSMutableDictionary dictionary];
  225. _mutationCompletionBlocks[_currentUser] = completionBlocks;
  226. }
  227. [completionBlocks setObject:completion forKey:@(batchID)];
  228. }
  229. /**
  230. * Takes an updateCallback in which a set of reads and writes can be performed atomically. In the
  231. * updateCallback, user code can read and write values using a transaction object. After the
  232. * updateCallback, all changes will be committed. If someone else has changed any of the data
  233. * referenced, then the updateCallback will be called again. If the updateCallback still fails after
  234. * the given number of retries, then the transaction will be rejected.
  235. *
  236. * The transaction object passed to the updateCallback contains methods for accessing documents
  237. * and collections. Unlike other firestore access, data accessed with the transaction will not
  238. * reflect local changes that have not been committed. For this reason, it is required that all
  239. * reads are performed before any writes. Transactions must be performed while online.
  240. */
  241. - (void)transactionWithRetries:(int)retries
  242. workerQueue:(const std::shared_ptr<AsyncQueue> &)workerQueue
  243. updateCallback:(core::TransactionUpdateCallback)updateCallback
  244. resultCallback:(core::TransactionResultCallback)resultCallback {
  245. workerQueue->VerifyIsCurrentQueue();
  246. HARD_ASSERT(retries >= 0, "Got negative number of retries for transaction");
  247. std::shared_ptr<Transaction> transaction = _remoteStore->CreateTransaction();
  248. updateCallback(transaction, [=](util::StatusOr<absl::any> maybe_result) {
  249. workerQueue->Enqueue(
  250. [self, retries, workerQueue, updateCallback, resultCallback, transaction, maybe_result] {
  251. if (!maybe_result.ok()) {
  252. resultCallback(std::move(maybe_result));
  253. return;
  254. }
  255. transaction->Commit([self, retries, workerQueue, updateCallback, resultCallback,
  256. maybe_result](Status status) {
  257. if (status.ok()) {
  258. resultCallback(std::move(maybe_result));
  259. return;
  260. }
  261. // TODO(b/35201829): Only retry on real transaction failures.
  262. if (retries == 0) {
  263. Status wrappedError =
  264. Status(FirestoreErrorCode::FailedPrecondition, "Transaction failed all retries.")
  265. .CausedBy(std::move(status));
  266. resultCallback(std::move(wrappedError));
  267. return;
  268. }
  269. workerQueue->VerifyIsCurrentQueue();
  270. return [self transactionWithRetries:(retries - 1)
  271. workerQueue:workerQueue
  272. updateCallback:updateCallback
  273. resultCallback:resultCallback];
  274. });
  275. });
  276. });
  277. }
  278. - (void)applyRemoteEvent:(const RemoteEvent &)remoteEvent {
  279. [self assertDelegateExistsForSelector:_cmd];
  280. // Update `receivedDocument` as appropriate for any limbo targets.
  281. for (const auto &entry : remoteEvent.target_changes()) {
  282. TargetId targetID = entry.first;
  283. const TargetChange &change = entry.second;
  284. const auto iter = _limboResolutionsByTarget.find(targetID);
  285. if (iter != _limboResolutionsByTarget.end()) {
  286. LimboResolution &limboResolution = iter->second;
  287. // Since this is a limbo resolution lookup, it's for a single document and it could be
  288. // added, modified, or removed, but not a combination.
  289. HARD_ASSERT(change.added_documents().size() + change.modified_documents().size() +
  290. change.removed_documents().size() <=
  291. 1,
  292. "Limbo resolution for single document contains multiple changes.");
  293. if (change.added_documents().size() > 0) {
  294. limboResolution.document_received = true;
  295. } else if (change.modified_documents().size() > 0) {
  296. HARD_ASSERT(limboResolution.document_received,
  297. "Received change for limbo target document without add.");
  298. } else if (change.removed_documents().size() > 0) {
  299. HARD_ASSERT(limboResolution.document_received,
  300. "Received remove for limbo target document without add.");
  301. limboResolution.document_received = false;
  302. } else {
  303. // This was probably just a CURRENT targetChange or similar.
  304. }
  305. }
  306. }
  307. MaybeDocumentMap changes = [self.localStore applyRemoteEvent:remoteEvent];
  308. [self emitNewSnapshotsAndNotifyLocalStoreWithChanges:changes remoteEvent:remoteEvent];
  309. }
  310. - (void)applyChangedOnlineState:(OnlineState)onlineState {
  311. __block std::vector<ViewSnapshot> newViewSnapshots;
  312. [self.queryViewsByQuery
  313. enumerateKeysAndObjectsUsingBlock:^(FSTQuery *query, FSTQueryView *queryView, BOOL *stop) {
  314. FSTViewChange *viewChange = [queryView.view applyChangedOnlineState:onlineState];
  315. HARD_ASSERT(viewChange.limboChanges.count == 0,
  316. "OnlineState should not affect limbo documents.");
  317. if (viewChange.snapshot.has_value()) {
  318. newViewSnapshots.push_back(std::move(viewChange.snapshot.value()));
  319. }
  320. }];
  321. [self.syncEngineDelegate handleViewSnapshots:std::move(newViewSnapshots)];
  322. [self.syncEngineDelegate applyChangedOnlineState:onlineState];
  323. }
  324. - (void)rejectListenWithTargetID:(const TargetId)targetID error:(NSError *)error {
  325. [self assertDelegateExistsForSelector:_cmd];
  326. const auto iter = _limboResolutionsByTarget.find(targetID);
  327. if (iter != _limboResolutionsByTarget.end()) {
  328. const DocumentKey limboKey = iter->second.key;
  329. // Since this query failed, we won't want to manually unlisten to it.
  330. // So go ahead and remove it from bookkeeping.
  331. _limboTargetsByKey.erase(limboKey);
  332. _limboResolutionsByTarget.erase(targetID);
  333. // TODO(dimond): Retry on transient errors?
  334. // It's a limbo doc. Create a synthetic event saying it was deleted. This is kind of a hack.
  335. // Ideally, we would have a method in the local store to purge a document. However, it would
  336. // be tricky to keep all of the local store's invariants with another method.
  337. FSTDeletedDocument *doc = [FSTDeletedDocument documentWithKey:limboKey
  338. version:SnapshotVersion::None()
  339. hasCommittedMutations:NO];
  340. DocumentKeySet limboDocuments = DocumentKeySet{doc.key};
  341. RemoteEvent event{SnapshotVersion::None(), /*target_changes=*/{}, /*target_mismatches=*/{},
  342. /*document_updates=*/{{limboKey, doc}}, std::move(limboDocuments)};
  343. [self applyRemoteEvent:event];
  344. } else {
  345. auto found = _queryViewsByTarget.find(targetID);
  346. HARD_ASSERT(found != _queryViewsByTarget.end(), "Unknown targetId: %s", targetID);
  347. FSTQueryView *queryView = found->second;
  348. FSTQuery *query = queryView.query;
  349. [self.localStore releaseQuery:query];
  350. [self removeAndCleanupQuery:queryView];
  351. if ([self errorIsInteresting:error]) {
  352. LOG_WARN("Listen for query at %s failed: %s", query.path.CanonicalString(),
  353. error.localizedDescription);
  354. }
  355. [self.syncEngineDelegate handleError:error forQuery:query];
  356. }
  357. }
  358. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult {
  359. [self assertDelegateExistsForSelector:_cmd];
  360. // The local store may or may not be able to apply the write result and raise events immediately
  361. // (depending on whether the watcher is caught up), so we raise user callbacks first so that they
  362. // consistently happen before listen events.
  363. [self processUserCallbacksForBatchID:batchResult.batch.batchID error:nil];
  364. MaybeDocumentMap changes = [self.localStore acknowledgeBatchWithResult:batchResult];
  365. [self emitNewSnapshotsAndNotifyLocalStoreWithChanges:changes remoteEvent:absl::nullopt];
  366. }
  367. - (void)rejectFailedWriteWithBatchID:(BatchId)batchID error:(NSError *)error {
  368. [self assertDelegateExistsForSelector:_cmd];
  369. MaybeDocumentMap changes = [self.localStore rejectBatchID:batchID];
  370. if (!changes.empty() && [self errorIsInteresting:error]) {
  371. const DocumentKey &minKey = changes.min()->first;
  372. LOG_WARN("Write at %s failed: %s", minKey.ToString(), error.localizedDescription);
  373. }
  374. // The local store may or may not be able to apply the write result and raise events immediately
  375. // (depending on whether the watcher is caught up), so we raise user callbacks first so that they
  376. // consistently happen before listen events.
  377. [self processUserCallbacksForBatchID:batchID error:error];
  378. [self emitNewSnapshotsAndNotifyLocalStoreWithChanges:changes remoteEvent:absl::nullopt];
  379. }
  380. - (void)processUserCallbacksForBatchID:(BatchId)batchID error:(NSError *_Nullable)error {
  381. NSMutableDictionary<NSNumber *, FSTVoidErrorBlock> *completionBlocks =
  382. _mutationCompletionBlocks[_currentUser];
  383. // NOTE: Mutations restored from persistence won't have completion blocks, so it's okay for
  384. // this (or the completion below) to be nil.
  385. if (completionBlocks) {
  386. NSNumber *boxedBatchID = @(batchID);
  387. FSTVoidErrorBlock completion = completionBlocks[boxedBatchID];
  388. if (completion) {
  389. completion(error);
  390. [completionBlocks removeObjectForKey:boxedBatchID];
  391. }
  392. }
  393. }
  394. - (void)assertDelegateExistsForSelector:(SEL)methodSelector {
  395. HARD_ASSERT(self.syncEngineDelegate, "Tried to call '%s' before delegate was registered.",
  396. NSStringFromSelector(methodSelector));
  397. }
  398. - (void)removeAndCleanupQuery:(FSTQueryView *)queryView {
  399. [self.queryViewsByQuery removeObjectForKey:queryView.query];
  400. _queryViewsByTarget.erase(queryView.targetID);
  401. DocumentKeySet limboKeys = _limboDocumentRefs.ReferencedKeys(queryView.targetID);
  402. _limboDocumentRefs.RemoveReferences(queryView.targetID);
  403. for (const DocumentKey &key : limboKeys) {
  404. if (!_limboDocumentRefs.ContainsKey(key)) {
  405. // We removed the last reference for this key.
  406. [self removeLimboTargetForKey:key];
  407. }
  408. }
  409. }
  410. /**
  411. * Computes a new snapshot from the changes and calls the registered callback with the new snapshot.
  412. */
  413. - (void)emitNewSnapshotsAndNotifyLocalStoreWithChanges:(const MaybeDocumentMap &)changes
  414. remoteEvent:(const absl::optional<RemoteEvent> &)
  415. maybeRemoteEvent {
  416. __block std::vector<ViewSnapshot> newSnapshots;
  417. NSMutableArray<FSTLocalViewChanges *> *documentChangesInAllViews = [NSMutableArray array];
  418. [self.queryViewsByQuery
  419. enumerateKeysAndObjectsUsingBlock:^(FSTQuery *query, FSTQueryView *queryView, BOOL *stop) {
  420. FSTView *view = queryView.view;
  421. FSTViewDocumentChanges *viewDocChanges = [view computeChangesWithDocuments:changes];
  422. if (viewDocChanges.needsRefill) {
  423. // The query has a limit and some docs were removed/updated, so we need to re-run the
  424. // query against the local store to make sure we didn't lose any good docs that had been
  425. // past the limit.
  426. DocumentMap docs = [self.localStore executeQuery:queryView.query];
  427. viewDocChanges = [view computeChangesWithDocuments:docs.underlying_map()
  428. previousChanges:viewDocChanges];
  429. }
  430. absl::optional<TargetChange> targetChange;
  431. if (maybeRemoteEvent.has_value()) {
  432. const RemoteEvent &remoteEvent = maybeRemoteEvent.value();
  433. auto it = remoteEvent.target_changes().find(queryView.targetID);
  434. if (it != remoteEvent.target_changes().end()) {
  435. targetChange = it->second;
  436. }
  437. }
  438. FSTViewChange *viewChange = [queryView.view applyChangesToDocuments:viewDocChanges
  439. targetChange:targetChange];
  440. [self updateTrackedLimboDocumentsWithChanges:viewChange.limboChanges
  441. targetID:queryView.targetID];
  442. if (viewChange.snapshot.has_value()) {
  443. newSnapshots.push_back(viewChange.snapshot.value());
  444. FSTLocalViewChanges *docChanges =
  445. [FSTLocalViewChanges changesForViewSnapshot:viewChange.snapshot.value()
  446. withTargetID:queryView.targetID];
  447. [documentChangesInAllViews addObject:docChanges];
  448. }
  449. }];
  450. [self.syncEngineDelegate handleViewSnapshots:std::move(newSnapshots)];
  451. [self.localStore notifyLocalViewChanges:documentChangesInAllViews];
  452. }
  453. /** Updates the limbo document state for the given targetID. */
  454. - (void)updateTrackedLimboDocumentsWithChanges:(NSArray<FSTLimboDocumentChange *> *)limboChanges
  455. targetID:(TargetId)targetID {
  456. for (FSTLimboDocumentChange *limboChange in limboChanges) {
  457. switch (limboChange.type) {
  458. case FSTLimboDocumentChangeTypeAdded:
  459. _limboDocumentRefs.AddReference(limboChange.key, targetID);
  460. [self trackLimboChange:limboChange];
  461. break;
  462. case FSTLimboDocumentChangeTypeRemoved:
  463. LOG_DEBUG("Document no longer in limbo: %s", limboChange.key.ToString());
  464. _limboDocumentRefs.RemoveReference(limboChange.key, targetID);
  465. if (!_limboDocumentRefs.ContainsKey(limboChange.key)) {
  466. // We removed the last reference for this key
  467. [self removeLimboTargetForKey:limboChange.key];
  468. }
  469. break;
  470. default:
  471. HARD_FAIL("Unknown limbo change type: %s", limboChange.type);
  472. }
  473. }
  474. }
  475. - (void)trackLimboChange:(FSTLimboDocumentChange *)limboChange {
  476. DocumentKey key{limboChange.key};
  477. if (_limboTargetsByKey.find(key) == _limboTargetsByKey.end()) {
  478. LOG_DEBUG("New document in limbo: %s", key.ToString());
  479. TargetId limboTargetID = _targetIdGenerator.NextId();
  480. FSTQuery *query = [FSTQuery queryWithPath:key.path()];
  481. FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
  482. targetID:limboTargetID
  483. listenSequenceNumber:kIrrelevantSequenceNumber
  484. purpose:FSTQueryPurposeLimboResolution];
  485. _limboResolutionsByTarget.emplace(limboTargetID, LimboResolution{key});
  486. _remoteStore->Listen(queryData);
  487. _limboTargetsByKey[key] = limboTargetID;
  488. }
  489. }
  490. - (void)removeLimboTargetForKey:(const DocumentKey &)key {
  491. const auto iter = _limboTargetsByKey.find(key);
  492. if (iter == _limboTargetsByKey.end()) {
  493. // This target already got removed, because the query failed.
  494. return;
  495. }
  496. TargetId limboTargetID = iter->second;
  497. _remoteStore->StopListening(limboTargetID);
  498. _limboTargetsByKey.erase(key);
  499. _limboResolutionsByTarget.erase(limboTargetID);
  500. }
  501. // Used for testing
  502. - (std::map<DocumentKey, TargetId>)currentLimboDocuments {
  503. // Return defensive copy
  504. return _limboTargetsByKey;
  505. }
  506. - (void)credentialDidChangeWithUser:(const firebase::firestore::auth::User &)user {
  507. BOOL userChanged = (_currentUser != user);
  508. _currentUser = user;
  509. if (userChanged) {
  510. // Notify local store and emit any resulting events from swapping out the mutation queue.
  511. MaybeDocumentMap changes = [self.localStore userDidChange:user];
  512. [self emitNewSnapshotsAndNotifyLocalStoreWithChanges:changes remoteEvent:absl::nullopt];
  513. }
  514. // Notify remote store so it can restart its streams.
  515. _remoteStore->HandleCredentialChange();
  516. }
  517. - (DocumentKeySet)remoteKeysForTarget:(TargetId)targetId {
  518. const auto iter = _limboResolutionsByTarget.find(targetId);
  519. if (iter != _limboResolutionsByTarget.end() && iter->second.document_received) {
  520. return DocumentKeySet{iter->second.key};
  521. } else {
  522. auto found = _queryViewsByTarget.find(targetId);
  523. FSTQueryView *queryView = found != _queryViewsByTarget.end() ? found->second : nil;
  524. return queryView ? queryView.view.syncedDocuments : DocumentKeySet{};
  525. }
  526. }
  527. /**
  528. * Decides if the error likely represents a developer mistake such as forgetting to create an index
  529. * or permission denied. Used to decide whether an error is worth automatically logging as a
  530. * warning.
  531. */
  532. - (BOOL)errorIsInteresting:(NSError *)error {
  533. if (error.domain == FIRFirestoreErrorDomain) {
  534. if (error.code == FIRFirestoreErrorCodeFailedPrecondition &&
  535. [error.localizedDescription containsString:@"requires an index"]) {
  536. return YES;
  537. } else if (error.code == FIRFirestoreErrorCodePermissionDenied) {
  538. return YES;
  539. }
  540. }
  541. return NO;
  542. }
  543. @end
  544. NS_ASSUME_NONNULL_END