@@ -86,17 +86,6 @@ @interface FSTLocalStore ()
86
86
/* * Maps a targetID to data about its query. */
87
87
@property (nonatomic , strong ) NSMutableDictionary <NSNumber *, FSTQueryData *> *targetIDs;
88
88
89
- /* *
90
- * A heldBatchResult is a mutation batch result (from a write acknowledgement) that arrived before
91
- * the watch stream got notified of a snapshot that includes the write. So we "hold" it until
92
- * the watch stream catches up. It ensures that the local write remains visible (latency
93
- * compensation) and doesn't temporarily appear reverted because the watch stream is slower than
94
- * the write stream and so wasn't reflecting it.
95
- *
96
- * NOTE: Eventually we want to move this functionality into the remote store.
97
- */
98
- @property (nonatomic , strong ) NSMutableArray <FSTMutationBatchResult *> *heldBatchResults;
99
-
100
89
@end
101
90
102
91
@implementation FSTLocalStore {
@@ -117,7 +106,6 @@ - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
117
106
[_persistence.referenceDelegate addInMemoryPins: _localViewReferences];
118
107
119
108
_targetIDs = [NSMutableDictionary dictionary ];
120
- _heldBatchResults = [NSMutableArray array ];
121
109
122
110
_targetIDGenerator = TargetIdGenerator::LocalStoreTargetIdGenerator (0 );
123
111
}
@@ -131,27 +119,7 @@ - (void)start {
131
119
}
132
120
133
121
- (void )startMutationQueue {
134
- self.persistence .run (" Start MutationQueue" , [&]() {
135
- [self .mutationQueue start ];
136
-
137
- // If we have any leftover mutation batch results from a prior run, just drop them.
138
- // TODO(http://b/33446471): We probably need to repopulate heldBatchResults or similar instead,
139
- // but that is not straightforward since we're not persisting the write ack versions.
140
- [self .heldBatchResults removeAllObjects ];
141
-
142
- // TODO(mikelehen): This is the only usage of getAllMutationBatchesThroughBatchId:. Consider
143
- // removing it in favor of a getAcknowledgedBatches method.
144
- BatchId highestAck = [self .mutationQueue highestAcknowledgedBatchID ];
145
- if (highestAck != kFSTBatchIDUnknown ) {
146
- NSArray <FSTMutationBatch *> *batches =
147
- [self .mutationQueue allMutationBatchesThroughBatchID: highestAck];
148
- if (batches.count > 0 ) {
149
- // NOTE: This could be more efficient if we had a removeBatchesThroughBatchID, but this set
150
- // should be very small and this code should go away eventually.
151
- [self removeMutationBatches: batches];
152
- }
153
- }
154
- });
122
+ self.persistence .run (" Start MutationQueue" , [&]() { [self .mutationQueue start ]; });
155
123
}
156
124
157
125
- (FSTMaybeDocumentDictionary *)userDidChange : (const User &)user {
@@ -202,18 +170,12 @@ - (FSTMaybeDocumentDictionary *)acknowledgeBatchWithResult:(FSTMutationBatchResu
202
170
return self.persistence .run (" Acknowledge batch" , [&]() -> FSTMaybeDocumentDictionary * {
203
171
id <FSTMutationQueue> mutationQueue = self.mutationQueue ;
204
172
205
- [mutationQueue acknowledgeBatch: batchResult.batch streamToken: batchResult.streamToken];
206
-
207
- DocumentKeySet affected;
208
- if ([self shouldHoldBatchResultWithVersion: batchResult.commitVersion]) {
209
- [self .heldBatchResults addObject: batchResult];
210
- } else {
211
- affected = [self releaseBatchResults: @[ batchResult ]];
212
- }
213
-
173
+ FSTMutationBatch *batch = batchResult.batch ;
174
+ [mutationQueue acknowledgeBatch: batch streamToken: batchResult.streamToken];
175
+ [self applyBatchResult: batchResult];
214
176
[self .mutationQueue performConsistencyCheck ];
215
177
216
- return [self .localDocuments documentsForKeys: affected ];
178
+ return [self .localDocuments documentsForKeys: batch.keys ];
217
179
});
218
180
}
219
181
@@ -225,11 +187,10 @@ - (FSTMaybeDocumentDictionary *)rejectBatchID:(BatchId)batchID {
225
187
BatchId lastAcked = [self .mutationQueue highestAcknowledgedBatchID ];
226
188
HARD_ASSERT (batchID > lastAcked, " Acknowledged batches can't be rejected." );
227
189
228
- DocumentKeySet affected = [self removeMutationBatch: toReject];
229
-
190
+ [self .mutationQueue removeMutationBatch: toReject];
230
191
[self .mutationQueue performConsistencyCheck ];
231
192
232
- return [self .localDocuments documentsForKeys: affected ];
193
+ return [self .localDocuments documentsForKeys: toReject.keys ];
233
194
});
234
195
}
235
196
@@ -341,15 +302,7 @@ - (FSTMaybeDocumentDictionary *)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent {
341
302
[self .queryCache setLastRemoteSnapshotVersion: remoteVersion];
342
303
}
343
304
344
- DocumentKeySet releasedWriteKeys = [self releaseHeldBatchResults ];
345
-
346
- // Union the two key sets.
347
- DocumentKeySet keysToRecalc = changedDocKeys;
348
- for (const DocumentKey &key : releasedWriteKeys) {
349
- keysToRecalc = keysToRecalc.insert (key);
350
- }
351
-
352
- return [self .localDocuments documentsForKeys: keysToRecalc];
305
+ return [self .localDocuments documentsForKeys: changedDocKeys];
353
306
});
354
307
}
355
308
@@ -458,12 +411,6 @@ - (void)releaseQuery:(FSTQuery *)query {
458
411
[self .localViewReferences removeReferencesForID: targetID];
459
412
[self .targetIDs removeObjectForKey: boxedTargetID];
460
413
[self .persistence.referenceDelegate removeTarget: queryData];
461
-
462
- // If this was the last watch target, then we won't get any more watch snapshots, so we should
463
- // release any held batch results.
464
- if ([self .targetIDs count ] == 0 ) {
465
- [self releaseHeldBatchResults ];
466
- }
467
414
});
468
415
}
469
416
@@ -479,67 +426,6 @@ - (DocumentKeySet)remoteDocumentKeysForTarget:(TargetId)targetID {
479
426
});
480
427
}
481
428
482
- /* *
483
- * Releases all the held mutation batches up to the current remote version received, and
484
- * applies their mutations to the docs in the remote documents cache.
485
- *
486
- * @return the set of keys of docs that were modified by those writes.
487
- */
488
- - (DocumentKeySet)releaseHeldBatchResults {
489
- NSMutableArray <FSTMutationBatchResult *> *toRelease = [NSMutableArray array ];
490
- for (FSTMutationBatchResult *batchResult in self.heldBatchResults ) {
491
- if (![self isRemoteUpToVersion: batchResult.commitVersion]) {
492
- break ;
493
- }
494
- [toRelease addObject: batchResult];
495
- }
496
-
497
- if (toRelease.count == 0 ) {
498
- return DocumentKeySet{};
499
- } else {
500
- [self .heldBatchResults removeObjectsInRange: NSMakeRange (0 , toRelease.count)];
501
- return [self releaseBatchResults: toRelease];
502
- }
503
- }
504
-
505
- - (BOOL )isRemoteUpToVersion : (const SnapshotVersion &)version {
506
- // If there are no watch targets, then we won't get remote snapshots, and are always "up-to-date."
507
- return version <= self.queryCache .lastRemoteSnapshotVersion || self.targetIDs .count == 0 ;
508
- }
509
-
510
- - (BOOL )shouldHoldBatchResultWithVersion : (const SnapshotVersion &)version {
511
- // Check if watcher isn't up to date or prior results are already held.
512
- return ![self isRemoteUpToVersion: version] || self.heldBatchResults .count > 0 ;
513
- }
514
-
515
- - (DocumentKeySet)releaseBatchResults : (NSArray <FSTMutationBatchResult *> *)batchResults {
516
- NSMutableArray <FSTMutationBatch *> *batches = [NSMutableArray array ];
517
- for (FSTMutationBatchResult *batchResult in batchResults) {
518
- [self applyBatchResult: batchResult];
519
- [batches addObject: batchResult.batch];
520
- }
521
-
522
- return [self removeMutationBatches: batches];
523
- }
524
-
525
- - (DocumentKeySet)removeMutationBatch : (FSTMutationBatch *)batch {
526
- return [self removeMutationBatches: @[ batch ]];
527
- }
528
-
529
- /* * Removes all the mutation batches named in the given array. */
530
- - (DocumentKeySet)removeMutationBatches : (NSArray <FSTMutationBatch *> *)batches {
531
- DocumentKeySet affectedDocs;
532
- for (FSTMutationBatch *batch in batches) {
533
- for (FSTMutation *mutation in batch.mutations ) {
534
- const DocumentKey &key = mutation.key ;
535
- affectedDocs = affectedDocs.insert (key);
536
- }
537
- [self .mutationQueue removeMutationBatch: batch];
538
- }
539
-
540
- return affectedDocs;
541
- }
542
-
543
429
- (void )applyBatchResult : (FSTMutationBatchResult *)batchResult {
544
430
FSTMutationBatch *batch = batchResult.batch ;
545
431
DocumentKeySet docKeys = batch.keys ;
@@ -562,6 +448,8 @@ - (void)applyBatchResult:(FSTMutationBatchResult *)batchResult {
562
448
}
563
449
}
564
450
}
451
+
452
+ [self .mutationQueue removeMutationBatch: batch];
565
453
}
566
454
567
455
@end
0 commit comments