aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Core
diff options
context:
space:
mode:
authorGravatar Sebastian Schmidt <mrschmidt@google.com>2018-06-12 10:58:35 -0700
committerGravatar GitHub <noreply@github.com>2018-06-12 10:58:35 -0700
commitafea8d5aacf474b57b4364feda08be9ca195594b (patch)
treed43c39ae9f71e88d256012f4467cd2b707fc7ddd /Firestore/Source/Core
parent0db8ef7dbe6c8c191252d33090dbb88b98735148 (diff)
Refactor Remote Event (#1396)
Diffstat (limited to 'Firestore/Source/Core')
-rw-r--r--Firestore/Source/Core/FSTSyncEngine.mm45
-rw-r--r--Firestore/Source/Core/FSTView.mm30
2 files changed, 30 insertions, 45 deletions
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<NSNumber *, FSTTargetChange *> *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;
}
}