Skip to content

Commit 81b1597

Browse files
authored
Merge 2cf4c72 into bdd3eae
2 parents bdd3eae + 2cf4c72 commit 81b1597

File tree

3 files changed

+42
-90
lines changed

3 files changed

+42
-90
lines changed

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export const INDEXING_SCHEMA_VERSION = 15;
5454
* 12. Add document overlays.
5555
* 13. Rewrite the keys of the remote document cache to allow for efficient
5656
* document lookup via `getAll()`.
57-
* 14. Add indexing support.
57+
* 14. Add overlays.
58+
* 15. Add indexing support.
5859
*/
5960

6061
export const SCHEMA_VERSION = INDEXING_ENABLED ? INDEXING_SCHEMA_VERSION : 14;

packages/firestore/src/local/indexeddb_schema_converter.ts

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { User } from '../auth/user';
1919
import { ListenSequence } from '../core/listen_sequence';
2020
import { SnapshotVersion } from '../core/snapshot_version';
21-
import { documentKeySet } from '../model/collections';
21+
import { DocumentKeySet, documentKeySet } from '../model/collections';
2222
import { DocumentKey } from '../model/document_key';
2323
import { ResourcePath } from '../model/path';
2424
import { debugAssert, fail, hardAssert } from '../util/assert';
@@ -471,9 +471,6 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
471471
db: IDBDatabase,
472472
transaction: SimpleDbTransaction
473473
): PersistencePromise<void> {
474-
const queuesStore = transaction.store<DbMutationQueueKey, DbMutationQueue>(
475-
DbMutationQueueStore
476-
);
477474
const mutationsStore = transaction.store<
478475
DbMutationBatchKey,
479476
DbMutationBatch
@@ -482,78 +479,56 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
482479
const remoteDocumentCache = newIndexedDbRemoteDocumentCache(
483480
this.serializer
484481
);
482+
const memoryPersistence = new MemoryPersistence(
483+
MemoryEagerDelegate.factory,
484+
this.serializer.remoteSerializer
485+
);
485486

486487
const promises: Array<PersistencePromise<void>> = [];
487-
let userIds = new Set<string>();
488+
const userToDocumentSet = new Map<string, DocumentKeySet>();
488489

489-
return queuesStore
490+
return mutationsStore
490491
.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) => {
499503
const user = new User(userId);
500504
const documentOverlayCache = IndexedDbDocumentOverlayCache.forUser(
501505
this.serializer,
502506
user
503507
);
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
508524
);
509525
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+
)
555530
);
556-
}
531+
});
557532
})
558533
.next(() => PersistencePromise.waitFor(promises));
559534
}

packages/firestore/test/unit/local/indexeddb_persistence.test.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,7 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
11381138
DbDocumentMutationKey,
11391139
DbDocumentMutation
11401140
>(DbDocumentMutationStore);
1141-
const mutationQueuesStore = txn.store<
1142-
DbMutationQueueKey,
1143-
DbMutationQueue
1144-
>(DbMutationQueueStore);
1145-
// Manually populate the mutation queue and create all indicies.
1141+
// Manually populate the mutations.
11461142
return PersistencePromise.forEach(
11471143
testMutations,
11481144
(testMutation: DbMutationBatch) => {
@@ -1163,25 +1159,6 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
11631159
);
11641160
});
11651161
}
1166-
).next(() =>
1167-
// Populate the mutation queues' metadata
1168-
PersistencePromise.waitFor([
1169-
mutationQueuesStore.put({
1170-
userId: 'user1',
1171-
lastAcknowledgedBatchId: -1,
1172-
lastStreamToken: ''
1173-
}),
1174-
mutationQueuesStore.put({
1175-
userId: 'user2',
1176-
lastAcknowledgedBatchId: -1,
1177-
lastStreamToken: ''
1178-
}),
1179-
mutationQueuesStore.put({
1180-
userId: 'user3',
1181-
lastAcknowledgedBatchId: -1,
1182-
lastStreamToken: ''
1183-
})
1184-
])
11851162
);
11861163
}
11871164
);
@@ -1202,7 +1179,6 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
12021179
// We should have a total of 4 overlays:
12031180
// For user1: testWriteFoo
12041181
// For user2: testWriteBar, testWriteBaz, and testWritePending
1205-
// For user3: NO OVERLAYS!
12061182
let p = documentOverlayStore.count().next(count => {
12071183
expect(count).to.equal(4);
12081184
});

0 commit comments

Comments
 (0)