FSTSyncEngine.mm 27 KB

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