-
Notifications
You must be signed in to change notification settings - Fork 938
Change removeMutationBatch to remove a single batch #1148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,73 +328,45 @@ export class MemoryMutationQueue implements MutationQueue { | |
return result; | ||
} | ||
|
||
removeMutationBatches( | ||
removeMutationBatch( | ||
transaction: PersistenceTransaction, | ||
batches: MutationBatch[] | ||
batch: MutationBatch | ||
): PersistencePromise<void> { | ||
const batchCount = batches.length; | ||
assert(batchCount > 0, 'Should not remove mutations when none exist.'); | ||
|
||
const firstBatchId = batches[0].batchId; | ||
const queueCount = this.mutationQueue.length; | ||
|
||
// Find the position of the first batch for removal. This need not be the | ||
// first entry in the queue. | ||
const startIndex = this.indexOfExistingBatchId(firstBatchId, 'removed'); | ||
const batchIndex = this.indexOfExistingBatchId(batch.batchId, 'removed'); | ||
assert( | ||
this.mutationQueue[startIndex].batchId === firstBatchId, | ||
this.mutationQueue[batchIndex].batchId === batch.batchId, | ||
'Removed batches must exist in the queue' | ||
); | ||
|
||
// Check that removed batches are contiguous (while excluding tombstones). | ||
let batchIndex = 1; | ||
let queueIndex = startIndex + 1; | ||
while (batchIndex < batchCount && queueIndex < queueCount) { | ||
const batch = this.mutationQueue[queueIndex]; | ||
if (batch.isTombstone()) { | ||
queueIndex++; | ||
continue; | ||
} | ||
|
||
assert( | ||
batch.batchId === batches[batchIndex].batchId, | ||
'Removed batches must be contiguous in the queue' | ||
); | ||
batchIndex++; | ||
queueIndex++; | ||
} | ||
|
||
// Only actually remove batches if removing at the front of the queue. | ||
// Previously rejected batches may have left tombstones in the queue, so | ||
// expand the removal range to include any tombstones. | ||
if (startIndex === 0) { | ||
for (; queueIndex < queueCount; queueIndex++) { | ||
if (batchIndex === 0) { | ||
let queueIndex = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. endIndex? The queueIndex name was never any good and now it's even worse because we're not comparing the batch to remove against the queue contents. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
for (; queueIndex < this.mutationQueue.length; queueIndex++) { | ||
const batch = this.mutationQueue[queueIndex]; | ||
if (!batch.isTombstone()) { | ||
break; | ||
} | ||
} | ||
const length = queueIndex - startIndex; | ||
this.mutationQueue.splice(startIndex, length); | ||
this.mutationQueue.splice(0, queueIndex); | ||
} else { | ||
// Mark the tombstones | ||
for (let i = startIndex; i < queueIndex; i++) { | ||
this.mutationQueue[i] = this.mutationQueue[i].toTombstone(); | ||
} | ||
this.mutationQueue[batchIndex] = this.mutationQueue[ | ||
batchIndex | ||
].toTombstone(); | ||
} | ||
|
||
let references = this.batchesByDocumentKey; | ||
for (const batch of batches) { | ||
const batchId = batch.batchId; | ||
for (const mutation of batch.mutations) { | ||
const key = mutation.key; | ||
if (this.garbageCollector !== null) { | ||
this.garbageCollector.addPotentialGarbageKey(key); | ||
} | ||
|
||
const ref = new DocReference(key, batchId); | ||
references = references.delete(ref); | ||
for (const mutation of batch.mutations) { | ||
const key = mutation.key; | ||
if (this.garbageCollector !== null) { | ||
this.garbageCollector.addPotentialGarbageKey(key); | ||
} | ||
|
||
const ref = new DocReference(key, batch.batchId); | ||
references = references.delete(ref); | ||
} | ||
this.batchesByDocumentKey = references; | ||
return PersistencePromise.resolve(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this called in the loop? Couldn't this be called outside the loop on mutations in the batch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this should just be called once. Fixed.