FSTSyncEngine.mm 26 KB

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