@@ -18,6 +18,7 @@ import { Timestamp } from '../api/timestamp';
18
18
import { User } from '../auth/user' ;
19
19
import { Query } from '../core/query' ;
20
20
import { BatchId , ProtoByteString } from '../core/types' ;
21
+ import { DocumentKeySet } from '../model/collections' ;
21
22
import { DocumentKey } from '../model/document_key' ;
22
23
import { Mutation } from '../model/mutation' ;
23
24
import { BATCHID_UNKNOWN , MutationBatch } from '../model/mutation_batch' ;
@@ -362,6 +363,39 @@ export class IndexedDbMutationQueue implements MutationQueue {
362
363
. next ( ( ) => results ) ;
363
364
}
364
365
366
+ getAllMutationBatchesAffectingDocumentKeys (
367
+ transaction : PersistenceTransaction ,
368
+ documentKeys : DocumentKeySet
369
+ ) : PersistencePromise < MutationBatch [ ] > {
370
+ const minKey = DbDocumentMutation . prefixForPath (
371
+ this . userId ,
372
+ documentKeys . first ( ) . path
373
+ ) ;
374
+ const maxKey = DbDocumentMutation . prefixForPath (
375
+ this . userId ,
376
+ documentKeys . first ( ) . path
377
+ ) ;
378
+ const keyRange = IDBKeyRange . bound ( minKey , maxKey ) ;
379
+ let uniqueBatchIDs = new SortedSet < BatchId > ( primitiveComparator ) ;
380
+
381
+ const results : MutationBatch [ ] = [ ] ;
382
+ return documentMutationsStore ( transaction )
383
+ . iterate ( { range : keyRange } , ( indexKey , _ , control ) => {
384
+ const [ userID , encodedPath , batchID ] = indexKey ;
385
+ const path = EncodedResourcePath . decode ( encodedPath ) ;
386
+ if ( userID !== this . userId ) {
387
+ control . done ( ) ;
388
+ return ;
389
+ }
390
+
391
+ if ( ! documentKeys . has ( new DocumentKey ( path ) ) ) {
392
+ return ;
393
+ }
394
+ uniqueBatchIDs = uniqueBatchIDs . add ( batchID ) ;
395
+ } )
396
+ . next ( ( ) => this . lookupMutationBatches ( transaction , uniqueBatchIDs ) ) ;
397
+ }
398
+
365
399
getAllMutationBatchesAffectingQuery (
366
400
transaction : PersistenceTransaction ,
367
401
query : Query
@@ -413,29 +447,33 @@ export class IndexedDbMutationQueue implements MutationQueue {
413
447
}
414
448
uniqueBatchIDs = uniqueBatchIDs . add ( batchID ) ;
415
449
} )
416
- . next ( ( ) => {
417
- const results : MutationBatch [ ] = [ ] ;
418
- const promises : Array < PersistencePromise < void > > = [ ] ;
419
- // TODO(rockwood): Implement this using iterate.
420
- uniqueBatchIDs . forEach ( batchID => {
421
- const mutationKey = this . keyForBatchId ( batchID ) ;
422
- promises . push (
423
- mutationsStore ( transaction )
424
- . get ( mutationKey )
425
- . next ( mutation => {
426
- if ( mutation === null ) {
427
- fail (
428
- 'Dangling document-mutation reference found, ' +
429
- 'which points to ' +
430
- mutationKey
431
- ) ;
432
- }
433
- results . push ( this . serializer . fromDbMutationBatch ( mutation ! ) ) ;
434
- } )
435
- ) ;
436
- } ) ;
437
- return PersistencePromise . waitFor ( promises ) . next ( ( ) => results ) ;
438
- } ) ;
450
+ . next ( ( ) => this . lookupMutationBatches ( transaction , uniqueBatchIDs ) ) ;
451
+ }
452
+
453
+ private lookupMutationBatches (
454
+ transaction : PersistenceTransaction ,
455
+ batchIDs : SortedSet < BatchId > ) : PersistencePromise < MutationBatch [ ] > {
456
+ const results : MutationBatch [ ] = [ ] ;
457
+ const promises : Array < PersistencePromise < void > > = [ ] ;
458
+ // TODO(rockwood): Implement this using iterate.
459
+ batchIDs . forEach ( batchID => {
460
+ const mutationKey = this . keyForBatchId ( batchID ) ;
461
+ promises . push (
462
+ mutationsStore ( transaction )
463
+ . get ( mutationKey )
464
+ . next ( mutation => {
465
+ if ( mutation === null ) {
466
+ fail (
467
+ 'Dangling document-mutation reference found, ' +
468
+ 'which points to ' +
469
+ mutationKey
470
+ ) ;
471
+ }
472
+ results . push ( this . serializer . fromDbMutationBatch ( mutation ! ) ) ;
473
+ } )
474
+ ) ;
475
+ } ) ;
476
+ return PersistencePromise . waitFor ( promises ) . next ( ( ) => results ) ;
439
477
}
440
478
441
479
removeMutationBatches (
0 commit comments