From f122cf7ce802972bd2fea45acac3deae2affcafa Mon Sep 17 00:00:00 2001 From: Michael Lehenbauer Date: Mon, 12 Mar 2018 11:53:26 -0700 Subject: Fix MutationQueue issue resulting in re-sending acknowledged writes. (#909) Port of: https://github.com/firebase/firebase-js-sdk/pull/559 Should address #772 once released. getNextMutationBatchAfterBatchId() was not respecting highestAcknowledgedBatchId and therefore we were resending writes after the network was disabled / re-enabled. --- Firestore/Source/Local/FSTLevelDBMutationQueue.mm | 9 +++++++-- Firestore/Source/Local/FSTMemoryMutationQueue.mm | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'Firestore/Source') diff --git a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm index 575e98d..d7b5eca 100644 --- a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm +++ b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm @@ -315,7 +315,12 @@ static ReadOptions StandardReadOptions() { } - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID { - std::string key = [self mutationKeyForBatchID:batchID + 1]; + // All batches with batchID <= self.metadata.lastAcknowledgedBatchId have been acknowledged so + // the first unacknowledged batch after batchID will have a batchID larger than both of these + // values. + FSTBatchID nextBatchID = MAX(batchID, self.metadata.lastAcknowledgedBatchId) + 1; + + std::string key = [self mutationKeyForBatchID:nextBatchID]; std::unique_ptr it(_db->NewIterator(StandardReadOptions())); it->Seek(key); @@ -336,7 +341,7 @@ static ReadOptions StandardReadOptions() { return nil; } - FSTAssert(rowKey.batchID > batchID, @"Should have found mutation after %d", batchID); + FSTAssert(rowKey.batchID >= nextBatchID, @"Should have found mutation after %d", nextBatchID); return [self decodedMutationBatch:it->value()]; } diff --git a/Firestore/Source/Local/FSTMemoryMutationQueue.mm b/Firestore/Source/Local/FSTMemoryMutationQueue.mm index 5b2fca6..7e5cc02 100644 --- a/Firestore/Source/Local/FSTMemoryMutationQueue.mm +++ b/Firestore/Source/Local/FSTMemoryMutationQueue.mm @@ -192,10 +192,10 @@ static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, // All batches with batchID <= self.highestAcknowledgedBatchID have been acknowledged so the // first unacknowledged batch after batchID will have a batchID larger than both of these values. - batchID = MAX(batchID + 1, self.highestAcknowledgedBatchID); + FSTBatchID nextBatchID = MAX(batchID, self.highestAcknowledgedBatchID) + 1; // The requested batchID may still be out of range so normalize it to the start of the queue. - NSInteger rawIndex = [self indexOfBatchID:batchID]; + NSInteger rawIndex = [self indexOfBatchID:nextBatchID]; NSUInteger index = rawIndex < 0 ? 0 : (NSUInteger)rawIndex; // Finally return the first non-tombstone batch. -- cgit v1.2.3