@@ -71,6 +71,10 @@ export class MemoryMutationQueue implements MutationQueue {
71
71
) ;
72
72
73
73
const batchIndex = this . indexOfExistingBatchId ( batchId , 'acknowledged' ) ;
74
+ assert (
75
+ batchIndex === 0 ,
76
+ 'Can only acknowledge the first batch in the mutation queue'
77
+ ) ;
74
78
75
79
// Verify that the batch in the queue is the one to be acknowledged.
76
80
const check = this . mutationQueue [ batchIndex ] ;
@@ -81,10 +85,6 @@ export class MemoryMutationQueue implements MutationQueue {
81
85
', got batch ' +
82
86
check . batchId
83
87
) ;
84
- assert (
85
- ! check . isTombstone ( ) ,
86
- "Can't acknowledge a previously removed batch"
87
- ) ;
88
88
89
89
this . highestAcknowledgedBatchId = batchId ;
90
90
this . lastStreamToken = streamToken ;
@@ -149,17 +149,15 @@ export class MemoryMutationQueue implements MutationQueue {
149
149
) : PersistencePromise < DocumentKeySet | null > {
150
150
const mutationBatch = this . findMutationBatch ( batchId ) ;
151
151
assert ( mutationBatch != null , 'Failed to find local mutation batch.' ) ;
152
- return PersistencePromise . resolve (
153
- ! mutationBatch ! . isTombstone ( ) ? mutationBatch ! . keys ( ) : null
152
+ return PersistencePromise . resolve < DocumentKeySet | null > (
153
+ mutationBatch ! . keys ( )
154
154
) ;
155
155
}
156
156
157
157
getNextMutationBatchAfterBatchId (
158
158
transaction : PersistenceTransaction ,
159
159
batchId : BatchId
160
160
) : PersistencePromise < MutationBatch | null > {
161
- const size = this . mutationQueue . length ;
162
-
163
161
// All batches with batchId <= this.highestAcknowledgedBatchId have been
164
162
// acknowledged so the first unacknowledged batch after batchID will have a
165
163
// batchID larger than both of these values.
@@ -168,24 +166,16 @@ export class MemoryMutationQueue implements MutationQueue {
168
166
// The requested batchId may still be out of range so normalize it to the
169
167
// start of the queue.
170
168
const rawIndex = this . indexOfBatchId ( nextBatchId ) ;
171
- let index = rawIndex < 0 ? 0 : rawIndex ;
172
-
173
- // Finally return the first non-tombstone batch.
174
- for ( ; index < size ; index ++ ) {
175
- const batch = this . mutationQueue [ index ] ;
176
- if ( ! batch . isTombstone ( ) ) {
177
- return PersistencePromise . resolve < MutationBatch | null > ( batch ) ;
178
- }
179
- }
180
- return PersistencePromise . resolve < MutationBatch | null > ( null ) ;
169
+ const index = rawIndex < 0 ? 0 : rawIndex ;
170
+ return PersistencePromise . resolve (
171
+ this . mutationQueue . length > index ? this . mutationQueue [ index ] : null
172
+ ) ;
181
173
}
182
174
183
175
getAllMutationBatches (
184
176
transaction : PersistenceTransaction
185
177
) : PersistencePromise < MutationBatch [ ] > {
186
- return PersistencePromise . resolve (
187
- this . getAllLiveMutationBatchesBeforeIndex ( this . mutationQueue . length )
188
- ) ;
178
+ return PersistencePromise . resolve ( this . mutationQueue . slice ( ) ) ;
189
179
}
190
180
191
181
getAllMutationBatchesAffectingDocumentKey (
@@ -298,27 +288,10 @@ export class MemoryMutationQueue implements MutationQueue {
298
288
// first entry in the queue.
299
289
const batchIndex = this . indexOfExistingBatchId ( batch . batchId , 'removed' ) ;
300
290
assert (
301
- this . mutationQueue [ batchIndex ] . batchId === batch . batchId ,
302
- 'Removed batches must exist in the queue'
291
+ batchIndex === 0 ,
292
+ 'Can only remove the first entry of the mutation queue'
303
293
) ;
304
-
305
- // Only actually remove batches if removing at the front of the queue.
306
- // Previously rejected batches may have left tombstones in the queue, so
307
- // expand the removal range to include any tombstones.
308
- if ( batchIndex === 0 ) {
309
- let endIndex = 1 ;
310
- for ( ; endIndex < this . mutationQueue . length ; endIndex ++ ) {
311
- const batch = this . mutationQueue [ endIndex ] ;
312
- if ( ! batch . isTombstone ( ) ) {
313
- break ;
314
- }
315
- }
316
- this . mutationQueue . splice ( 0 , endIndex ) ;
317
- } else {
318
- this . mutationQueue [ batchIndex ] = this . mutationQueue [
319
- batchIndex
320
- ] . toTombstone ( ) ;
321
- }
294
+ this . mutationQueue . shift ( ) ;
322
295
323
296
let references = this . batchesByDocumentKey ;
324
297
return PersistencePromise . forEach ( batch . mutations , mutation => {
@@ -358,26 +331,6 @@ export class MemoryMutationQueue implements MutationQueue {
358
331
return PersistencePromise . resolve ( ) ;
359
332
}
360
333
361
- /**
362
- * A private helper that collects all the mutations batches in the queue up to
363
- * but not including the given endIndex. All tombstones in the queue are
364
- * excluded.
365
- */
366
- private getAllLiveMutationBatchesBeforeIndex (
367
- endIndex : number
368
- ) : MutationBatch [ ] {
369
- const result : MutationBatch [ ] = [ ] ;
370
-
371
- for ( let i = 0 ; i < endIndex ; i ++ ) {
372
- const batch = this . mutationQueue [ i ] ;
373
- if ( ! batch . isTombstone ( ) ) {
374
- result . push ( batch ) ;
375
- }
376
- }
377
-
378
- return result ;
379
- }
380
-
381
334
/**
382
335
* Finds the index of the given batchId in the mutation queue and asserts that
383
336
* the resulting index is within the bounds of the queue.
@@ -430,6 +383,6 @@ export class MemoryMutationQueue implements MutationQueue {
430
383
431
384
const batch = this . mutationQueue [ index ] ;
432
385
assert ( batch . batchId === batchId , 'If found batch must match' ) ;
433
- return batch . isTombstone ( ) ? null : batch ;
386
+ return batch ;
434
387
}
435
388
}
0 commit comments