Skip to content

Commit aad3532

Browse files
committed
Remove the index that is not necessary.
1 parent b63ea07 commit aad3532

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

packages/firestore/src/local/document_overlay_cache.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { DocumentKeySet } from '../model/collections';
1819
import { DocumentKey } from '../model/document_key';
1920
import { Mutation } from '../model/mutation';
2021
import { Overlay } from '../model/overlay';
@@ -53,9 +54,10 @@ export interface DocumentOverlayCache {
5354
overlays: Map<DocumentKey, Mutation>
5455
): PersistencePromise<void>;
5556

56-
/** Removes the overlay whose largest-batch-id equals the given ID. */
57+
/** Removes overlays for the given document keys and batch ID. */
5758
removeOverlaysForBatchId(
5859
transaction: PersistenceTransaction,
60+
documentKeys: DocumentKeySet,
5961
batchId: number
6062
): PersistencePromise<void>;
6163

packages/firestore/src/local/indexeddb_document_overlay_cache.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { User } from '../auth/user';
19+
import { DocumentKeySet } from '../model/collections';
1920
import { DocumentKey } from '../model/document_key';
2021
import { Mutation } from '../model/mutation';
2122
import { Overlay } from '../model/overlay';
@@ -85,18 +86,32 @@ export class IndexedDbDocumentOverlayCache implements DocumentOverlayCache {
8586

8687
removeOverlaysForBatchId(
8788
transaction: PersistenceTransaction,
89+
documentKeys: DocumentKeySet,
8890
batchId: number
8991
): PersistencePromise<void> {
90-
const range = IDBKeyRange.bound(
91-
[this.userId, batchId],
92-
[this.userId, batchId + 1],
93-
/*lowerOpen=*/ false,
94-
/*upperOpen=*/ true
95-
);
96-
return documentOverlayStore(transaction).deleteAll(
97-
DbDocumentOverlay.batchIdOverlayIndex,
98-
range
92+
const collectionPaths = new Set<string>();
93+
94+
// Get the set of unique collection paths.
95+
documentKeys.forEach(key =>
96+
collectionPaths.add(encodeResourcePath(key.getCollectionPath()))
9997
);
98+
99+
const promises: Array<PersistencePromise<void>> = [];
100+
collectionPaths.forEach(collectionPath => {
101+
const range = IDBKeyRange.bound(
102+
[this.userId, collectionPath, batchId],
103+
[this.userId, collectionPath, batchId + 1],
104+
/*lowerOpen=*/ false,
105+
/*upperOpen=*/ true
106+
);
107+
promises.push(
108+
documentOverlayStore(transaction).deleteAll(
109+
DbDocumentOverlay.collectionPathOverlayIndex,
110+
range
111+
)
112+
);
113+
});
114+
return PersistencePromise.waitFor(promises);
100115
}
101116

102117
getOverlaysForCollection(

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,7 @@ export class DbDocumentOverlay {
810810

811811
static keyPath = ['userId', 'collectionPath', 'documentId'];
812812

813-
// TODO(overlays): Remove this index if the other indexes suffice.
814-
static batchIdOverlayIndex = 'batchIdOverlayIndex';
815-
static batchIdOverlayIndexPath = ['userId', 'largestBatchId'];
816-
817-
static collectionPathOverlayIndex = 'collectionPathBatchIdIndex';
813+
static collectionPathOverlayIndex = 'collectionPathOverlayIndex';
818814
static collectionPathOverlayIndexPath = [
819815
'userId',
820816
'collectionPath',

packages/firestore/src/local/indexeddb_schema_converter.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,6 @@ function createDocumentOverlayStore(db: IDBDatabase): void {
538538
const documentOverlayStore = db.createObjectStore(DbDocumentOverlay.store, {
539539
keyPath: DbDocumentOverlay.keyPath
540540
});
541-
documentOverlayStore.createIndex(
542-
DbDocumentOverlay.batchIdOverlayIndex,
543-
DbDocumentOverlay.batchIdOverlayIndexPath,
544-
{ unique: false }
545-
);
546541
documentOverlayStore.createIndex(
547542
DbDocumentOverlay.collectionPathOverlayIndex,
548543
DbDocumentOverlay.collectionPathOverlayIndexPath,

packages/firestore/src/local/memory_document_overlay_cache.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { DocumentKeySet } from '../model/collections';
1819
import { DocumentKey } from '../model/document_key';
1920
import { Mutation } from '../model/mutation';
2021
import { Overlay } from '../model/overlay';
@@ -57,6 +58,7 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
5758

5859
removeOverlaysForBatchId(
5960
transaction: PersistenceTransaction,
61+
documentKeys: DocumentKeySet,
6062
batchId: number
6163
): PersistencePromise<void> {
6264
const keys = this.overlayByBatchId.get(batchId);

packages/firestore/src/model/document_key.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export class DocumentKey {
5656
return this.path.get(this.path.length - 2);
5757
}
5858

59+
/** Returns the fully qualified path to the parent collection. */
60+
getCollectionPath(): ResourcePath {
61+
return this.path.popLast();
62+
}
63+
5964
isEqual(other: DocumentKey | null): boolean {
6065
return (
6166
other !== null && ResourcePath.comparator(this.path, other.path) === 0

0 commit comments

Comments
 (0)