FSTSyncEngine.mm 25 KB

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