From afea8d5aacf474b57b4364feda08be9ca195594b Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 12 Jun 2018 10:58:35 -0700 Subject: Refactor Remote Event (#1396) --- Firestore/Source/Core/FSTSyncEngine.mm | 45 +++++++++++++++------------------- Firestore/Source/Core/FSTView.mm | 30 ++++++++--------------- 2 files changed, 30 insertions(+), 45 deletions(-) (limited to 'Firestore/Source/Core') diff --git a/Firestore/Source/Core/FSTSyncEngine.mm b/Firestore/Source/Core/FSTSyncEngine.mm index 7d0c1a3..89cb774 100644 --- a/Firestore/Source/Core/FSTSyncEngine.mm +++ b/Firestore/Source/Core/FSTSyncEngine.mm @@ -295,21 +295,6 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1; - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent { [self assertDelegateExistsForSelector:_cmd]; - // Make sure limbo documents are deleted if there were no results. - // Filter out document additions to targets that they already belong to. - [remoteEvent.targetChanges enumerateKeysAndObjectsUsingBlock:^( - FSTBoxedTargetID *_Nonnull targetID, - FSTTargetChange *_Nonnull targetChange, BOOL *_Nonnull stop) { - const auto iter = self->_limboKeysByTarget.find([targetID intValue]); - if (iter == self->_limboKeysByTarget.end()) { - FSTQueryView *qv = self.queryViewsByTarget[targetID]; - HARD_ASSERT(qv, "Missing queryview for non-limbo query: %s", [targetID intValue]); - [targetChange.mapping filterUpdatesUsingExistingKeys:qv.view.syncedDocuments]; - } else { - [remoteEvent synthesizeDeleteForLimboTargetChange:targetChange key:iter->second]; - } - }]; - FSTMaybeDocumentDictionary *changes = [self.localStore applyRemoteEvent:remoteEvent]; [self emitNewSnapshotsWithChanges:changes remoteEvent:remoteEvent]; } @@ -345,18 +330,16 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1; // It's a limbo doc. Create a synthetic event saying it was deleted. This is kind of a hack. // Ideally, we would have a method in the local store to purge a document. However, it would // be tricky to keep all of the local store's invariants with another method. - NSMutableDictionary *targetChanges = - [NSMutableDictionary dictionary]; FSTDeletedDocument *doc = [FSTDeletedDocument documentWithKey:limboKey version:SnapshotVersion::None()]; DocumentKeySet limboDocuments = DocumentKeySet{doc.key}; - FSTRemoteEvent *event = - [[FSTRemoteEvent alloc] initWithSnapshotVersion:SnapshotVersion::None() - targetChanges:targetChanges - documentUpdates:{ - { limboKey, doc } - } - limboDocuments:std::move(limboDocuments)]; + FSTRemoteEvent *event = [[FSTRemoteEvent alloc] initWithSnapshotVersion:SnapshotVersion::None() + targetChanges:{} + targetMismatches:{} + documentUpdates:{ + { limboKey, doc } + } + limboDocuments:std::move(limboDocuments)]; [self applyRemoteEvent:event]; } else { FSTQueryView *queryView = self.queryViewsByTarget[@(targetID)]; @@ -439,7 +422,14 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1; FSTDocumentDictionary *docs = [self.localStore executeQuery:queryView.query]; viewDocChanges = [view computeChangesWithDocuments:docs previousChanges:viewDocChanges]; } - FSTTargetChange *_Nullable targetChange = remoteEvent.targetChanges[@(queryView.targetID)]; + + FSTTargetChange *_Nullable targetChange = nil; + if (remoteEvent) { + auto it = remoteEvent.targetChanges.find(queryView.targetID); + if (it != remoteEvent.targetChanges.end()) { + targetChange = it->second; + } + } FSTViewChange *viewChange = [queryView.view applyChangesToDocuments:viewDocChanges targetChange:targetChange]; @@ -531,6 +521,11 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1; [self.remoteStore userDidChange:user]; } +- (firebase::firestore::model::DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId { + FSTQueryView *queryView = self.queryViewsByTarget[targetId]; + return queryView ? queryView.view.syncedDocuments : DocumentKeySet{}; +} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Core/FSTView.mm b/Firestore/Source/Core/FSTView.mm index 63efd4e..f954731 100644 --- a/Firestore/Source/Core/FSTView.mm +++ b/Firestore/Source/Core/FSTView.mm @@ -401,28 +401,18 @@ static NSComparisonResult FSTCompareDocumentViewChangeTypes(FSTDocumentViewChang */ - (void)applyTargetChange:(nullable FSTTargetChange *)targetChange { if (targetChange) { - FSTTargetMapping *targetMapping = targetChange.mapping; - if ([targetMapping isKindOfClass:[FSTResetMapping class]]) { - _syncedDocuments = ((FSTResetMapping *)targetMapping).documents; - } else if ([targetMapping isKindOfClass:[FSTUpdateMapping class]]) { - for (const DocumentKey &key : ((FSTUpdateMapping *)targetMapping).addedDocuments) { - _syncedDocuments = _syncedDocuments.insert(key); - } - for (const DocumentKey &key : ((FSTUpdateMapping *)targetMapping).removedDocuments) { - _syncedDocuments = _syncedDocuments.erase(key); - } + for (const DocumentKey &key : targetChange.addedDocuments) { + _syncedDocuments = _syncedDocuments.insert(key); } - - switch (targetChange.currentStatusUpdate) { - case FSTCurrentStatusUpdateMarkCurrent: - self.current = YES; - break; - case FSTCurrentStatusUpdateMarkNotCurrent: - self.current = NO; - break; - case FSTCurrentStatusUpdateNone: - break; + for (const DocumentKey &key : targetChange.modifiedDocuments) { + HARD_ASSERT(_syncedDocuments.find(key) != _syncedDocuments.end(), + "Modified document %s not found in view.", key.ToString()); } + for (const DocumentKey &key : targetChange.removedDocuments) { + _syncedDocuments = _syncedDocuments.erase(key); + } + + self.current = targetChange.current; } } -- cgit v1.2.3