Skip to content

Commit 600a3cb

Browse files
committed
set up migration
1 parent 9ec2ff7 commit 600a3cb

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,6 @@ export interface DbRemoteDocument {
211211
/** The document ID. */
212212
documentId: string;
213213

214-
/**
215-
* The type of the remote document.
216-
* 0: A normal document.
217-
* 1: A document representing a bundle.
218-
*
219-
* This is a schema migration starting at version 19. Documents written
220-
* prior to this version has this field unset.
221-
*/
222-
documentType?: number;
223-
224214
/** When the document was read from the backend. */
225215
readTime: DbTimestampKey;
226216

@@ -247,6 +237,19 @@ export interface DbRemoteDocument {
247237
* the write's commit version as their document version.
248238
*/
249239
hasCommittedMutations: boolean;
240+
/**
241+
* The type of the remote document.
242+
* 0: The type has not been determined, likely due to a schema migration from
243+
* and older version that lacked this property.
244+
* 1: NO_DOCUMENT: The `noDocument` property is set.
245+
* 2: FOUND_DOCUMENT: The `document` property is set to a real document.
246+
* 3: UNKNOWN_DOCUMENT: The `unknownDocument` property is set.
247+
* 4: INVALID_DOCUMENT: Should never happen, but here for completeness.
248+
*
249+
* This property was added in a schema migration at version 19. Documents
250+
* written prior to this version has this field set to 0 (zero).
251+
*/
252+
documentType: number;
250253
}
251254

252255
/**

packages/firestore/src/local/indexeddb_schema_converter.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ import {
7474
DbGlobalsStore,
7575
DbIndexConfigurationCollectionGroupIndex,
7676
DbIndexConfigurationCollectionGroupIndexPath,
77+
DbRemoteDocumentCollectionIndex,
78+
DbRemoteDocumentCollectionIndexPath,
7779
DbIndexConfigurationKeyPath,
7880
DbIndexConfigurationStore,
7981
DbIndexEntryDocumentKeyIndex,
@@ -296,26 +298,15 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
296298
}
297299

298300
if (fromVersion < 19 && toVersion >= 19) {
299-
p = p.next(() => this.backfillDocumentType(simpleDbTransaction));
301+
p = p.next(() => {
302+
createRemoteDocumentStoreDocumentTypeIndex(txn);
303+
return backfillRemoteDocumentStoreDocumentType(simpleDbTransaction);
304+
});
300305
}
301306

302307
return p;
303308
}
304309

305-
private backfillDocumentType(
306-
txn: SimpleDbTransaction
307-
): PersistencePromise<void> {
308-
const remoteDocumentStore = txn.store<
309-
DbRemoteDocumentKey,
310-
DbRemoteDocument
311-
>(DbRemoteDocumentStore);
312-
313-
return remoteDocumentStore.iterate((key, doc) => {
314-
doc.documentType = 0;
315-
return remoteDocumentStore.put(doc);
316-
});
317-
}
318-
319310
private addDocumentGlobal(
320311
txn: SimpleDbTransaction
321312
): PersistencePromise<void> {
@@ -519,7 +510,8 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
519510
unknownDocument: legacyDocument.unknownDocument,
520511
noDocument: legacyDocument.noDocument,
521512
document: legacyDocument.document,
522-
hasCommittedMutations: !!legacyDocument.hasCommittedMutations
513+
hasCommittedMutations: !!legacyDocument.hasCommittedMutations,
514+
documentType: 0
523515
};
524516
writes.push(remoteDocumentStore.put(dbRemoteDocument));
525517
})
@@ -813,3 +805,26 @@ function extractKey(remoteDoc: DbRemoteDocumentLegacy): DocumentKey {
813805
return fail(0x8faf, 'Unexpected DbRemoteDocument');
814806
}
815807
}
808+
809+
function createRemoteDocumentStoreDocumentTypeIndex(txn: IDBTransaction): void {
810+
txn
811+
.objectStore(DbRemoteDocumentStore)
812+
.createIndex(
813+
DbRemoteDocumentCollectionIndex,
814+
DbRemoteDocumentCollectionIndexPath,
815+
{ unique: false }
816+
);
817+
}
818+
819+
function backfillRemoteDocumentStoreDocumentType(
820+
txn: SimpleDbTransaction
821+
): PersistencePromise<void> {
822+
const remoteDocumentStore = txn.store<DbRemoteDocumentKey, DbRemoteDocument>(
823+
DbRemoteDocumentStore
824+
);
825+
826+
return remoteDocumentStore.iterate((key, doc) => {
827+
doc.documentType = 0;
828+
return remoteDocumentStore.put(doc);
829+
});
830+
}

packages/firestore/src/local/indexeddb_sentinels.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ export const DbRemoteDocumentCollectionGroupIndexPath = [
172172
'documentId'
173173
];
174174

175+
/**
176+
* An index that provides access to documents by collection, read time, and
177+
* document type.
178+
*
179+
* This index is used as an optimization in getDocumentsMatchingQuery() to
180+
* avoid visiting "tombstone" entries which are impossible to be part of the
181+
* query result anyway.
182+
*
183+
* See https://github.com/firebase/firebase-android-sdk/issues/7295.
184+
*/
185+
export const DbRemoteDocumentCollectionIndex = 'collectionIndex';
186+
187+
export const DbRemoteDocumentCollectionIndexPath = [
188+
'documentType',
189+
'prefixPath',
190+
'collectionGroup',
191+
'readTime'
192+
];
193+
175194
export const DbRemoteDocumentGlobalStore = 'remoteDocumentGlobal';
176195

177196
export const DbRemoteDocumentGlobalKey = 'remoteDocumentGlobalKey';

0 commit comments

Comments
 (0)