Skip to content

Commit 414beec

Browse files
Review feedback
1 parent 1e626ee commit 414beec

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

packages/firestore/src/local/indexeddb_index_manager.ts

+33-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { decode, encode } from './encoded_resource_path';
2222
import { IndexManager } from './index_manager';
2323
import { IndexedDbPersistence } from './indexeddb_persistence';
2424
import { DbCollectionParent, DbCollectionParentKey } from './indexeddb_schema';
25+
import { MemoryCollectionParentIndex } from './memory_index_manager';
2526
import { PersistenceTransaction } from './persistence';
2627
import { PersistencePromise } from './persistence_promise';
2728
import { SimpleDbStore } from './simple_db';
@@ -30,17 +31,43 @@ import { SimpleDbStore } from './simple_db';
3031
* A persisted implementation of IndexManager.
3132
*/
3233
export class IndexedDbIndexManager implements IndexManager {
34+
/**
35+
* An in-memory copy of the index entries we've already written since the SDK
36+
* launched. Used to avoid re-writing the same entry repeatedly.
37+
*
38+
* This is *NOT* a complete cache of what's in persistence and so can never be used to
39+
* satisfy reads.
40+
*/
41+
private collectionParentsCache = new MemoryCollectionParentIndex();
42+
43+
/**
44+
* Adds a new entry to the collection parent index.
45+
*
46+
* Repeated calls for the same collectionPath should be avoided within a
47+
* transaction as IndexedDbIndexManager only caches writes once a transaction
48+
* has been committed.
49+
*/
3350
addToCollectionParentIndex(
3451
transaction: PersistenceTransaction,
3552
collectionPath: ResourcePath
3653
): PersistencePromise<void> {
3754
assert(collectionPath.length % 2 === 1, 'Expected a collection path.');
38-
const collectionId = collectionPath.lastSegment();
39-
const parentPath = collectionPath.popLast();
40-
return collectionParentsStore(transaction).put({
41-
collectionId,
42-
parent: encode(parentPath)
43-
});
55+
if (!this.collectionParentsCache.has(collectionPath)) {
56+
const collectionId = collectionPath.lastSegment();
57+
const parentPath = collectionPath.popLast();
58+
59+
transaction.addOnCommittedListener(() => {
60+
// Add the collection to the in memory cache only if the transaction was
61+
// successfully committed.
62+
this.collectionParentsCache.add(collectionPath);
63+
});
64+
65+
return collectionParentsStore(transaction).put({
66+
collectionId,
67+
parent: encode(parentPath)
68+
});
69+
}
70+
return PersistencePromise.resolve();
4471
}
4572

4673
getCollectionParents(

packages/firestore/src/local/indexeddb_persistence.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,10 @@ export class IndexedDbPersistence implements Persistence {
769769
this.listenSequence.next()
770770
);
771771

772-
if (mode === 'readwrite-primary'||
773-
mode === 'readwrite-primary-idempotent') {
772+
if (
773+
mode === 'readwrite-primary' ||
774+
mode === 'readwrite-primary-idempotent'
775+
) {
774776
// While we merely verify that we have (or can acquire) the lease
775777
// immediately, we wait to extend the primary lease until after
776778
// executing transactionOperation(). This ensures that even if the

packages/firestore/src/local/memory_index_manager.ts

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ export class MemoryCollectionParentIndex {
6969
return added;
7070
}
7171

72+
// Returns true if the entry already exists.
73+
has(collectionPath: ResourcePath): boolean {
74+
const collectionId = collectionPath.lastSegment();
75+
const parentPath = collectionPath.popLast();
76+
const existingParents = this.index[collectionId];
77+
return existingParents && existingParents.has(parentPath);
78+
}
79+
7280
getEntries(collectionId: string): ResourcePath[] {
7381
const parentPaths =
7482
this.index[collectionId] ||

0 commit comments

Comments
 (0)