18
18
import { User } from '../auth/user' ;
19
19
import { ListenSequence } from '../core/listen_sequence' ;
20
20
import { SnapshotVersion } from '../core/snapshot_version' ;
21
- import { documentKeySet } from '../model/collections' ;
21
+ import { DocumentKeySet , documentKeySet } from '../model/collections' ;
22
22
import { DocumentKey } from '../model/document_key' ;
23
23
import { ResourcePath } from '../model/path' ;
24
24
import { debugAssert , fail , hardAssert } from '../util/assert' ;
@@ -471,9 +471,6 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
471
471
db : IDBDatabase ,
472
472
transaction : SimpleDbTransaction
473
473
) : PersistencePromise < void > {
474
- const queuesStore = transaction . store < DbMutationQueueKey , DbMutationQueue > (
475
- DbMutationQueueStore
476
- ) ;
477
474
const mutationsStore = transaction . store <
478
475
DbMutationBatchKey ,
479
476
DbMutationBatch
@@ -482,78 +479,56 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
482
479
const remoteDocumentCache = newIndexedDbRemoteDocumentCache (
483
480
this . serializer
484
481
) ;
482
+ const memoryPersistence = new MemoryPersistence (
483
+ MemoryEagerDelegate . factory ,
484
+ this . serializer . remoteSerializer
485
+ ) ;
485
486
486
487
const promises : Array < PersistencePromise < void > > = [ ] ;
487
- let userIds = new Set < string > ( ) ;
488
+ const userToDocumentSet = new Map < string , DocumentKeySet > ( ) ;
488
489
489
- return queuesStore
490
+ return mutationsStore
490
491
. loadAll ( )
491
- . next ( queues => {
492
- for ( const queue of queues ) {
493
- const userId = queue . userId ;
494
- if ( userIds . has ( userId ) ) {
495
- // We have already processed this user.
496
- continue ;
497
- }
498
- userIds = userIds . add ( userId ) ;
492
+ . next ( dbBatches => {
493
+ dbBatches . forEach ( dbBatch => {
494
+ let documentSet =
495
+ userToDocumentSet . get ( dbBatch . userId ) ?? documentKeySet ( ) ;
496
+ const batch = fromDbMutationBatch ( this . serializer , dbBatch ) ;
497
+ batch . keys ( ) . forEach ( key => ( documentSet = documentSet . add ( key ) ) ) ;
498
+ userToDocumentSet . set ( dbBatch . userId , documentSet ) ;
499
+ } ) ;
500
+ } )
501
+ . next ( ( ) => {
502
+ userToDocumentSet . forEach ( ( allDocumentKeysForUser , userId ) => {
499
503
const user = new User ( userId ) ;
500
504
const documentOverlayCache = IndexedDbDocumentOverlayCache . forUser (
501
505
this . serializer ,
502
506
user
503
507
) ;
504
- let allDocumentKeysForUser = documentKeySet ( ) ;
505
- const range = IDBKeyRange . bound (
506
- [ userId , BATCHID_UNKNOWN ] ,
507
- [ userId , Number . POSITIVE_INFINITY ]
508
+ // NOTE: The index manager and the reference delegate are
509
+ // irrelevant for the purpose of recalculating and saving
510
+ // overlays. We can therefore simply use the memory
511
+ // implementation.
512
+ const indexManager = memoryPersistence . getIndexManager ( user ) ;
513
+ const mutationQueue = IndexedDbMutationQueue . forUser (
514
+ user ,
515
+ this . serializer ,
516
+ indexManager ,
517
+ memoryPersistence . referenceDelegate
518
+ ) ;
519
+ const localDocumentsView = new LocalDocumentsView (
520
+ remoteDocumentCache ,
521
+ mutationQueue ,
522
+ documentOverlayCache ,
523
+ indexManager
508
524
) ;
509
525
promises . push (
510
- mutationsStore
511
- . loadAll ( DbMutationBatchUserMutationsIndex , range )
512
- . next ( dbBatches => {
513
- dbBatches . forEach ( dbBatch => {
514
- hardAssert (
515
- dbBatch . userId === userId ,
516
- `Cannot process batch ${ dbBatch . batchId } from unexpected user`
517
- ) ;
518
- const batch = fromDbMutationBatch ( this . serializer , dbBatch ) ;
519
- batch
520
- . keys ( )
521
- . forEach (
522
- key =>
523
- ( allDocumentKeysForUser =
524
- allDocumentKeysForUser . add ( key ) )
525
- ) ;
526
- } ) ;
527
- } )
528
- . next ( ( ) => {
529
- // NOTE: The index manager and the reference delegate are
530
- // irrelevant for the purpose of recalculating and saving
531
- // overlays. We can therefore simply use the memory
532
- // implementation.
533
- const memoryPersistence = new MemoryPersistence (
534
- MemoryEagerDelegate . factory ,
535
- this . serializer . remoteSerializer
536
- ) ;
537
- const indexManager = memoryPersistence . getIndexManager ( user ) ;
538
- const mutationQueue = IndexedDbMutationQueue . forUser (
539
- user ,
540
- this . serializer ,
541
- indexManager ,
542
- memoryPersistence . referenceDelegate
543
- ) ;
544
- const localDocumentsView = new LocalDocumentsView (
545
- remoteDocumentCache ,
546
- mutationQueue ,
547
- documentOverlayCache ,
548
- indexManager
549
- ) ;
550
- return localDocumentsView . recalculateAndSaveOverlaysForDocumentKeys (
551
- new IndexedDbTransaction ( transaction , ListenSequence . INVALID ) ,
552
- allDocumentKeysForUser
553
- ) ;
554
- } )
526
+ localDocumentsView . recalculateAndSaveOverlaysForDocumentKeys (
527
+ new IndexedDbTransaction ( transaction , ListenSequence . INVALID ) ,
528
+ allDocumentKeysForUser
529
+ )
555
530
) ;
556
- }
531
+ } ) ;
557
532
} )
558
533
. next ( ( ) => PersistencePromise . waitFor ( promises ) ) ;
559
534
}
0 commit comments