Skip to content

Commit 3c5f931

Browse files
committed
finish schema migration
1 parent 600a3cb commit 3c5f931

File tree

3 files changed

+76
-30
lines changed

3 files changed

+76
-30
lines changed

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,26 @@ export interface DbUnknownDocument {
185185
version: DbTimestamp;
186186
}
187187

188+
/**
189+
* The "type" of a document stored in a `DbRemoteDocument`.
190+
*/
191+
export enum DbRemoteDocumentType {
192+
/**
193+
* The `noDocument` property of the `DbRemoteDocument` is set.
194+
*/
195+
NoDocument = 1,
196+
197+
/**
198+
* The `document` property of the `DbRemoteDocument` is set.
199+
*/
200+
FoundDocument = 2,
201+
202+
/**
203+
* The `unknownDocument` property of the `DbRemoteDocument` is set.
204+
*/
205+
UnknownDocument = 3
206+
}
207+
188208
/**
189209
* An object to be stored in the 'remoteDocuments' store in IndexedDb.
190210
* It represents either:
@@ -238,18 +258,17 @@ export interface DbRemoteDocument {
238258
*/
239259
hasCommittedMutations: boolean;
240260
/**
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.
261+
* The "type" of document stored in this object.
262+
*
263+
* The value of this property _must_ be consistent with the semantics of the
264+
* `unknownDocument`, `noDocument`, and `document` properties. This property
265+
* is provided as an optimization to allow skipping entries in result sets
266+
* whose documents are not of the desired "type".
248267
*
249268
* This property was added in a schema migration at version 19. Documents
250269
* written prior to this version has this field set to 0 (zero).
251270
*/
252-
documentType: number;
271+
documentType: DbRemoteDocumentType;
253272
}
254273

255274
/**

packages/firestore/src/local/indexeddb_schema_converter.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ import {
4444
DbMutationQueue,
4545
DbRemoteDocument,
4646
DbRemoteDocumentGlobal,
47+
DbRemoteDocumentType,
4748
DbTarget,
4849
DbTargetDocument,
4950
DbTargetGlobal,
5051
SCHEMA_VERSION
5152
} from './indexeddb_schema';
5253
import {
5354
DbRemoteDocument as DbRemoteDocumentLegacy,
54-
DbRemoteDocumentStore as DbRemoteDocumentStoreLegacy,
55-
DbRemoteDocumentKey as DbRemoteDocumentKeyLegacy
55+
DbRemoteDocumentKey as DbRemoteDocumentKeyLegacy,
56+
DbRemoteDocumentStore as DbRemoteDocumentStoreLegacy
5657
} from './indexeddb_schema_legacy';
5758
import {
5859
DbBundleKeyPath,
@@ -74,8 +75,6 @@ import {
7475
DbGlobalsStore,
7576
DbIndexConfigurationCollectionGroupIndex,
7677
DbIndexConfigurationCollectionGroupIndexPath,
77-
DbRemoteDocumentCollectionIndex,
78-
DbRemoteDocumentCollectionIndexPath,
7978
DbIndexConfigurationKeyPath,
8079
DbIndexConfigurationStore,
8180
DbIndexEntryDocumentKeyIndex,
@@ -99,6 +98,8 @@ import {
9998
DbPrimaryClientStore,
10099
DbRemoteDocumentCollectionGroupIndex,
101100
DbRemoteDocumentCollectionGroupIndexPath,
101+
DbRemoteDocumentCollectionIndex,
102+
DbRemoteDocumentCollectionIndexPath,
102103
DbRemoteDocumentDocumentKeyIndex,
103104
DbRemoteDocumentDocumentKeyIndexPath,
104105
DbRemoteDocumentGlobalKey,
@@ -502,6 +503,9 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
502503
>(DbRemoteDocumentStore);
503504

504505
const path = extractKey(legacyDocument).path.toArray();
506+
507+
// Note: omit the "documentType" property because it will be populated
508+
// by schema migration 19.
505509
const dbRemoteDocument = {
506510
prefixPath: path.slice(0, path.length - 2),
507511
collectionGroup: path[path.length - 2],
@@ -510,9 +514,8 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
510514
unknownDocument: legacyDocument.unknownDocument,
511515
noDocument: legacyDocument.noDocument,
512516
document: legacyDocument.document,
513-
hasCommittedMutations: !!legacyDocument.hasCommittedMutations,
514-
documentType: 0
515-
};
517+
hasCommittedMutations: !!legacyDocument.hasCommittedMutations
518+
} satisfies Omit<DbRemoteDocument, 'documentType'> as DbRemoteDocument;
516519
writes.push(remoteDocumentStore.put(dbRemoteDocument));
517520
})
518521
.next(() => PersistencePromise.waitFor(writes));
@@ -824,7 +827,12 @@ function backfillRemoteDocumentStoreDocumentType(
824827
);
825828

826829
return remoteDocumentStore.iterate((key, doc) => {
827-
doc.documentType = 0;
830+
doc.documentType = doc.noDocument
831+
? DbRemoteDocumentType.NoDocument
832+
: doc.unknownDocument
833+
? DbRemoteDocumentType.UnknownDocument
834+
: DbRemoteDocumentType.FoundDocument;
835+
828836
return remoteDocumentStore.put(doc);
829837
});
830838
}

packages/firestore/src/local/local_serializer.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
DbNamedQuery,
6767
DbQuery,
6868
DbRemoteDocument,
69+
DbRemoteDocumentType,
6970
DbTarget,
7071
DbTimestamp
7172
} from './indexeddb_schema';
@@ -117,30 +118,48 @@ export function toDbRemoteDocument(
117118
document: MutableDocument
118119
): DbRemoteDocument {
119120
const key = document.key;
120-
const remoteDoc: DbRemoteDocument = {
121+
return {
121122
prefixPath: key.getCollectionPath().popLast().toArray(),
122123
collectionGroup: key.collectionGroup,
123124
documentId: key.path.lastSegment(),
124125
readTime: toDbTimestampKey(document.readTime),
125-
hasCommittedMutations: document.hasCommittedMutations
126+
hasCommittedMutations: document.hasCommittedMutations,
127+
...toDbRemoteDocumentDocumentSpecificComponents(localSerializer, document)
126128
};
129+
}
127130

131+
function toDbRemoteDocumentDocumentSpecificComponents(
132+
localSerializer: LocalSerializer,
133+
document: MutableDocument
134+
): Pick<
135+
DbRemoteDocument,
136+
'documentType' | 'document' | 'noDocument' | 'unknownDocument'
137+
> {
128138
if (document.isFoundDocument()) {
129-
remoteDoc.document = toDocument(localSerializer.remoteSerializer, document);
130-
} else if (document.isNoDocument()) {
131-
remoteDoc.noDocument = {
132-
path: key.path.toArray(),
133-
readTime: toDbTimestamp(document.version)
139+
return {
140+
documentType: DbRemoteDocumentType.FoundDocument,
141+
document: toDocument(localSerializer.remoteSerializer, document)
134142
};
135-
} else if (document.isUnknownDocument()) {
136-
remoteDoc.unknownDocument = {
137-
path: key.path.toArray(),
138-
version: toDbTimestamp(document.version)
143+
}
144+
if (document.isNoDocument()) {
145+
return {
146+
documentType: DbRemoteDocumentType.NoDocument,
147+
noDocument: {
148+
path: document.key.path.toArray(),
149+
readTime: toDbTimestamp(document.version)
150+
}
151+
};
152+
}
153+
if (document.isUnknownDocument()) {
154+
return {
155+
documentType: DbRemoteDocumentType.UnknownDocument,
156+
unknownDocument: {
157+
path: document.key.path.toArray(),
158+
version: toDbTimestamp(document.version)
159+
}
139160
};
140-
} else {
141-
return fail(0xe230, 'Unexpected Document', { document });
142161
}
143-
return remoteDoc;
162+
fail(0xe230, 'Unexpected Document', { document });
144163
}
145164

146165
export function toDbTimestampKey(

0 commit comments

Comments
 (0)