Skip to content

Commit 2cc698e

Browse files
Cleanup
1 parent 4992aa0 commit 2cc698e

File tree

1 file changed

+107
-78
lines changed

1 file changed

+107
-78
lines changed

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 107 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,33 @@ import { encode, EncodedResourcePath } from './encoded_resource_path';
2424

2525
export const SCHEMA_VERSION = 2;
2626

27+
/**
28+
* Performs database creation and schema migrations from and up to the specified
29+
* version.
30+
*/
31+
export function createOrUpgradeDb(
32+
db: IDBDatabase,
33+
fromVersion: number,
34+
toVersion: number
35+
): void {
36+
assert(
37+
fromVersion < toVersion && fromVersion >= 0 && toVersion <= 2,
38+
'Unexpected schema upgrade from v${fromVersion} to v{toVersion}.'
39+
);
40+
41+
if (toVersion === 2) {
42+
createInstanceMetadataStore(db);
43+
createTargetChangeStore(db);
44+
}
45+
46+
if (fromVersion == 0 && toVersion >= 1) {
47+
createOwnerStore(db);
48+
createMutationQueue(db);
49+
createQueryCache(db);
50+
createRemoteDocumentCache(db);
51+
}
52+
}
53+
2754
// TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their types.
2855
// https://github.com/Microsoft/TypeScript/issues/14322
2956
type KeyPath = any; // tslint:disable-line:no-any
@@ -73,20 +100,24 @@ export class DbMutationQueue {
73100
/** Keys are automatically assigned via the userId property. */
74101
static keyPath = 'userId';
75102

103+
76104
constructor(
77105
/**
78-
* The normalized user ID to which this queue belongs.
106+
* @param userId - The normalized user ID to which this queue belongs.
79107
*/
80108
public userId: string,
109+
81110
/**
82-
* An identifier for the highest numbered batch that has been acknowledged
83-
* by the server. All MutationBatches in this queue with batchIds less
84-
* than or equal to this value are considered to have been acknowledged by
85-
* the server.
111+
* @param lastAcknowledgedBatchId - An identifier for the highest numbered
112+
* batch that has been acknowledged by the server. All MutationBatches in
113+
* this queue with batchIds less than or equal to this value are considered
114+
* to have been acknowledged by the server.
86115
*/
87116
public lastAcknowledgedBatchId: number,
117+
88118
/**
89-
* A stream token that was previously sent by the server.
119+
* @param lastStreamToken - A stream token that was previously sent by the
120+
* server.
90121
*
91122
* See StreamingWriteRequest in datastore.proto for more details about
92123
* usage.
@@ -95,10 +126,11 @@ export class DbMutationQueue {
95126
* only a single stream token is retained.
96127
*/
97128
public lastStreamToken: string,
129+
98130
/**
99-
* An identifier for the highest numbered batch in the mutation queue. This
100-
* allows for efficient insert of new batches without relying on in-memory
101-
* state.
131+
* @param highestPendingBatchId - An identifier for the highest numbered
132+
* batch in the mutation queue. This allows for efficient insert of new
133+
* batches without relying on in-memory state.
102134
*
103135
* PORTING NOTE: iOS and Android clients keep this value in-memory.
104136
*/
@@ -125,21 +157,25 @@ export class DbMutationBatch {
125157

126158
constructor(
127159
/**
128-
* The normalized user ID to which this batch belongs.
160+
* @userId userId - The normalized user ID to which this batch belongs.
129161
*/
130162
public userId: string,
163+
131164
/**
132-
* An identifier for this batch, allocated by the mutation queue in a
133-
* monotonically increasing manner.
165+
* @batchId batchId - An identifier for this batch, allocated by the
166+
* mutation queue in a monotonically increasing manner.
134167
*/
135168
public batchId: BatchId,
169+
136170
/**
137-
* The local write time of the batch, stored as milliseconds since the
138-
* epoch.
171+
* @param localWriteTimeMs - The local write time of the batch, stored as
172+
* milliseconds since the epoch.
139173
*/
140174
public localWriteTimeMs: number,
175+
141176
/**
142-
* A list of mutations to apply. All mutations will be applied atomically.
177+
* @param mutations - A list of mutations to apply. All mutations will be
178+
* applied atomically.
143179
*
144180
* Mutations are serialized via JsonProtoSerializer.toMutation().
145181
*/
@@ -251,13 +287,14 @@ export class DbRemoteDocument {
251287

252288
constructor(
253289
/**
254-
* Set to an instance of a DbNoDocument if it is known that no document
255-
* exists.
290+
* @param noDocument - Set to an instance of a DbNoDocument if it is known
291+
* that no document exists.
256292
*/
257293
public noDocument: DbNoDocument | null,
294+
258295
/**
259-
* Set to an instance of a Document if there's a cached version of the
260-
* document.
296+
* @param document - Set to an instance of a Document if there's a cached
297+
* version of the document.
261298
*/
262299
public document: api.Document | null
263300
) {}
@@ -302,29 +339,36 @@ export class DbTarget {
302339

303340
constructor(
304341
/**
305-
* An auto-generated sequential numeric identifier for the query.
342+
* @param targetId - An auto-generated sequential numeric identifier for the
343+
* query.
306344
*
307345
* Queries are stored using their canonicalId as the key, but these
308346
* canonicalIds can be quite long so we additionally assign a unique
309347
* queryId which can be used by referenced data structures (e.g.
310348
* indexes) to minimize the on-disk cost.
311349
*/
312350
public targetId: TargetId,
351+
313352
/**
314-
* The canonical string representing this query. This is not unique.
353+
* @param canonicalId - The canonical string representing this query. This
354+
* is not unique.
315355
*/
316356
public canonicalId: string,
357+
317358
/**
318-
* The last readTime received from the Watch Service for this query.
359+
* @param readTime - The last readTime received from the Watch Service for
360+
* this query.
319361
*
320362
* This is the same value as TargetChange.read_time in the protos.
321363
*/
322364
public readTime: DbTimestamp,
365+
323366
/**
324-
* An opaque, server-assigned token that allows watching a query to be
325-
* resumed after disconnecting without retransmitting all the data
326-
* that matches the query. The resume token essentially identifies a
327-
* point in time from which the server should resume sending results.
367+
* @param resumeToken - An opaque, server-assigned token that allows
368+
* watching a query to be resumed after disconnecting without retransmitting
369+
* all the data that matches the query. The resume token essentially
370+
* identifies a point in time from which the server should resume sending
371+
* results.
328372
*
329373
* This is related to the snapshotVersion in that the resumeToken
330374
* effectively also encodes that value, but the resumeToken is opaque
@@ -338,9 +382,11 @@ export class DbTarget {
338382
* This is the same value as TargetChange.resume_token in the protos.
339383
*/
340384
public resumeToken: string,
385+
341386
/**
342-
* A sequence number representing the last time this query was
343-
* listened to, used for garbage collection purposes.
387+
* @param lastListenSequenceNumber - A sequence number representing the
388+
* last time this query was listened to, used for garbage collection
389+
* purposes.
344390
*
345391
* Conventionally this would be a timestamp value, but device-local
346392
* clocks are unreliable and they must be able to create new listens
@@ -353,8 +399,9 @@ export class DbTarget {
353399
* listened to.
354400
*/
355401
public lastListenSequenceNumber: number,
402+
356403
/**
357-
* The query for this target.
404+
* @param query - The query for this target.
358405
*
359406
* Because canonical ids are not unique we must store the actual query. We
360407
* use the proto to have an object we can persist without having to
@@ -390,11 +437,11 @@ export class DbTargetDocument {
390437

391438
constructor(
392439
/**
393-
* The targetId identifying a target.
440+
* @param targetId - The targetId identifying a target.
394441
*/
395442
public targetId: TargetId,
396443
/**
397-
* The path to the document, as encoded in the key.
444+
* @param path - The path to the document, as encoded in the key.
398445
*/
399446
public path: EncodedResourcePath
400447
) {}
@@ -421,24 +468,29 @@ export class DbTargetGlobal {
421468

422469
constructor(
423470
/**
424-
* The highest numbered target id across all targets.
471+
* @param highestTargetId - The highest numbered target id across all
472+
* targets.
425473
*
426474
* See DbTarget.targetId.
427475
*/
428476
public highestTargetId: TargetId,
477+
429478
/**
430-
* The highest numbered lastListenSequenceNumber across all targets.
479+
* @param highestListenSequenceNumber - The highest numbered
480+
* lastListenSequenceNumber across all targets.
431481
*
432482
* See DbTarget.lastListenSequenceNumber.
433483
*/
434484
public highestListenSequenceNumber: number,
485+
435486
/**
436-
* A global snapshot version representing the last consistent snapshot we
437-
* received from the backend. This is monotonically increasing and any
438-
* snapshots received from the backend prior to this version (e.g. for
439-
* targets resumed with a resumeToken) should be suppressed (buffered)
440-
* until the backend has caught up to this snapshot version again. This
441-
* prevents our cache from ever going backwards in time.
487+
* @param lastRemoteSnapshotVersion - A global snapshot version representing
488+
* the last consistent snapshot we received from the backend. This is
489+
* monotonically increasing and any snapshots received from the backend
490+
* prior to this version (e.g. for targets resumed with a resumeToken)
491+
* should be suppressed (buffered) until the backend has caught up to this
492+
* snapshot version again. This prevents our cache from ever going backwards
493+
* in time.
442494
*/
443495
public lastRemoteSnapshotVersion: DbTimestamp
444496
) {}
@@ -484,15 +536,17 @@ export class DbTargetChange {
484536

485537
constructor(
486538
/**
487-
* The targetId identifying a target.
539+
* @param targetId - The targetId identifying a target.
488540
*/
489541
public targetId: TargetId,
542+
490543
/**
491-
* The snapshot version, as encoded in UTC milliseconds.
544+
* @param snapshotVersion - The snapshot version for this change.
492545
*/
493546
public snapshotVersion: DbTimestamp,
547+
494548
/**
495-
* The keys of the changed documents in this snapshot.
549+
* @param changes - The keys of the changed documents in this snapshot.
496550
*/
497551
public changes: {
498552
added?: EncodedResourcePath[];
@@ -514,56 +568,31 @@ function createTargetChangeStore(db: IDBDatabase): void {
514568
* PORTING NOTE: This is used for primary-tab selection for multi-tab
515569
* persistence and does not need to be ported to iOS or Android.
516570
*/
517-
export class DbInstance {
571+
export class DbInstanceMetadata {
518572
/** Name of the IndexedDb object store. */
519-
static store = 'instanceState';
573+
static store = 'instanceMetadata';
520574

521575
/** Keys are automatically assigned via the userId and instanceKey properties. */
522576
static keyPath = ['userId', 'instanceKey'];
523577

524578
constructor(
525-
/** The normalized user ID to which this batch belongs.*/
579+
/** userId - The normalized user ID to which this batch belongs.*/
526580
public userId: string,
527-
/** The auto-generated instance key assigned at client startup. */
581+
582+
/** instanceKey - The auto-generated instance key assigned at client startup. */
528583
public instanceKey: string,
529-
/** The last time this state was updated. */
584+
585+
/** updateTimeMs - The last time this state was updated. */
530586
public updateTimeMs: DbTimestamp
531587
) {}
532588
}
533589

534-
function createInstanceStore(db: IDBDatabase): void {
535-
db.createObjectStore(DbInstance.store, {
536-
keyPath: DbInstance.keyPath as KeyPath
590+
function createInstanceMetadataStore(db: IDBDatabase): void {
591+
db.createObjectStore(DbInstanceMetadata.store, {
592+
keyPath: DbInstanceMetadata.keyPath as KeyPath
537593
});
538594
}
539595

540-
/**
541-
* Runs any migrations needed to bring the given database up to the specified
542-
* schema version.
543-
*/
544-
export function createOrUpgradeDb(
545-
db: IDBDatabase,
546-
fromVersion: number,
547-
toVersion: number
548-
): void {
549-
assert(
550-
fromVersion < toVersion && fromVersion >= 0 && toVersion <= 2,
551-
'Unexpected schema upgrade from v${fromVersion} to v{toVersion}.'
552-
);
553-
554-
if (toVersion === 2) {
555-
createInstanceStore(db);
556-
createTargetChangeStore(db);
557-
}
558-
559-
if (fromVersion == 0 && toVersion >= 1) {
560-
createOwnerStore(db);
561-
createMutationQueue(db);
562-
createQueryCache(db);
563-
createRemoteDocumentCache(db);
564-
}
565-
}
566-
567596
// Exported for testing.
568597
export const V1_STORES = [
569598
DbDocumentMutation.store,
@@ -576,7 +605,7 @@ export const V1_STORES = [
576605
DbTarget.store
577606
];
578607

579-
const SCHEMA_V2_STORES = [DbInstance.store, DbTargetChange.store];
608+
const SCHEMA_V2_STORES = [DbInstanceMetadata.store, DbTargetChange.store];
580609

581610
/**
582611
* The list of all IndexedDB stored used by the SDK. This is used when creating

0 commit comments

Comments
 (0)