@@ -24,6 +24,33 @@ import { encode, EncodedResourcePath } from './encoded_resource_path';
24
24
25
25
export const SCHEMA_VERSION = 2 ;
26
26
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
+
27
54
// TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their types.
28
55
// https://github.com/Microsoft/TypeScript/issues/14322
29
56
type KeyPath = any ; // tslint:disable-line:no-any
@@ -73,20 +100,24 @@ export class DbMutationQueue {
73
100
/** Keys are automatically assigned via the userId property. */
74
101
static keyPath = 'userId' ;
75
102
103
+
76
104
constructor (
77
105
/**
78
- * The normalized user ID to which this queue belongs.
106
+ * @param userId - The normalized user ID to which this queue belongs.
79
107
*/
80
108
public userId : string ,
109
+
81
110
/**
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.
86
115
*/
87
116
public lastAcknowledgedBatchId : number ,
117
+
88
118
/**
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.
90
121
*
91
122
* See StreamingWriteRequest in datastore.proto for more details about
92
123
* usage.
@@ -95,10 +126,11 @@ export class DbMutationQueue {
95
126
* only a single stream token is retained.
96
127
*/
97
128
public lastStreamToken : string ,
129
+
98
130
/**
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.
102
134
*
103
135
* PORTING NOTE: iOS and Android clients keep this value in-memory.
104
136
*/
@@ -125,21 +157,25 @@ export class DbMutationBatch {
125
157
126
158
constructor (
127
159
/**
128
- * The normalized user ID to which this batch belongs.
160
+ * @userId userId - The normalized user ID to which this batch belongs.
129
161
*/
130
162
public userId : string ,
163
+
131
164
/**
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.
134
167
*/
135
168
public batchId : BatchId ,
169
+
136
170
/**
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.
139
173
*/
140
174
public localWriteTimeMs : number ,
175
+
141
176
/**
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.
143
179
*
144
180
* Mutations are serialized via JsonProtoSerializer.toMutation().
145
181
*/
@@ -251,13 +287,14 @@ export class DbRemoteDocument {
251
287
252
288
constructor (
253
289
/**
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.
256
292
*/
257
293
public noDocument : DbNoDocument | null ,
294
+
258
295
/**
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.
261
298
*/
262
299
public document : api . Document | null
263
300
) { }
@@ -302,29 +339,36 @@ export class DbTarget {
302
339
303
340
constructor (
304
341
/**
305
- * An auto-generated sequential numeric identifier for the query.
342
+ * @param targetId - An auto-generated sequential numeric identifier for the
343
+ * query.
306
344
*
307
345
* Queries are stored using their canonicalId as the key, but these
308
346
* canonicalIds can be quite long so we additionally assign a unique
309
347
* queryId which can be used by referenced data structures (e.g.
310
348
* indexes) to minimize the on-disk cost.
311
349
*/
312
350
public targetId : TargetId ,
351
+
313
352
/**
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.
315
355
*/
316
356
public canonicalId : string ,
357
+
317
358
/**
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.
319
361
*
320
362
* This is the same value as TargetChange.read_time in the protos.
321
363
*/
322
364
public readTime : DbTimestamp ,
365
+
323
366
/**
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.
328
372
*
329
373
* This is related to the snapshotVersion in that the resumeToken
330
374
* effectively also encodes that value, but the resumeToken is opaque
@@ -338,9 +382,11 @@ export class DbTarget {
338
382
* This is the same value as TargetChange.resume_token in the protos.
339
383
*/
340
384
public resumeToken : string ,
385
+
341
386
/**
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.
344
390
*
345
391
* Conventionally this would be a timestamp value, but device-local
346
392
* clocks are unreliable and they must be able to create new listens
@@ -353,8 +399,9 @@ export class DbTarget {
353
399
* listened to.
354
400
*/
355
401
public lastListenSequenceNumber : number ,
402
+
356
403
/**
357
- * The query for this target.
404
+ * @param query - The query for this target.
358
405
*
359
406
* Because canonical ids are not unique we must store the actual query. We
360
407
* use the proto to have an object we can persist without having to
@@ -390,11 +437,11 @@ export class DbTargetDocument {
390
437
391
438
constructor (
392
439
/**
393
- * The targetId identifying a target.
440
+ * @param targetId - The targetId identifying a target.
394
441
*/
395
442
public targetId : TargetId ,
396
443
/**
397
- * The path to the document, as encoded in the key.
444
+ * @param path - The path to the document, as encoded in the key.
398
445
*/
399
446
public path : EncodedResourcePath
400
447
) { }
@@ -421,24 +468,29 @@ export class DbTargetGlobal {
421
468
422
469
constructor (
423
470
/**
424
- * The highest numbered target id across all targets.
471
+ * @param highestTargetId - The highest numbered target id across all
472
+ * targets.
425
473
*
426
474
* See DbTarget.targetId.
427
475
*/
428
476
public highestTargetId : TargetId ,
477
+
429
478
/**
430
- * The highest numbered lastListenSequenceNumber across all targets.
479
+ * @param highestListenSequenceNumber - The highest numbered
480
+ * lastListenSequenceNumber across all targets.
431
481
*
432
482
* See DbTarget.lastListenSequenceNumber.
433
483
*/
434
484
public highestListenSequenceNumber : number ,
485
+
435
486
/**
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.
442
494
*/
443
495
public lastRemoteSnapshotVersion : DbTimestamp
444
496
) { }
@@ -484,15 +536,17 @@ export class DbTargetChange {
484
536
485
537
constructor (
486
538
/**
487
- * The targetId identifying a target.
539
+ * @param targetId - The targetId identifying a target.
488
540
*/
489
541
public targetId : TargetId ,
542
+
490
543
/**
491
- * The snapshot version, as encoded in UTC milliseconds .
544
+ * @param snapshotVersion - The snapshot version for this change .
492
545
*/
493
546
public snapshotVersion : DbTimestamp ,
547
+
494
548
/**
495
- * The keys of the changed documents in this snapshot.
549
+ * @param changes - The keys of the changed documents in this snapshot.
496
550
*/
497
551
public changes : {
498
552
added ?: EncodedResourcePath [ ] ;
@@ -514,56 +568,31 @@ function createTargetChangeStore(db: IDBDatabase): void {
514
568
* PORTING NOTE: This is used for primary-tab selection for multi-tab
515
569
* persistence and does not need to be ported to iOS or Android.
516
570
*/
517
- export class DbInstance {
571
+ export class DbInstanceMetadata {
518
572
/** Name of the IndexedDb object store. */
519
- static store = 'instanceState ' ;
573
+ static store = 'instanceMetadata ' ;
520
574
521
575
/** Keys are automatically assigned via the userId and instanceKey properties. */
522
576
static keyPath = [ 'userId' , 'instanceKey' ] ;
523
577
524
578
constructor (
525
- /** The normalized user ID to which this batch belongs.*/
579
+ /** userId - The normalized user ID to which this batch belongs.*/
526
580
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. */
528
583
public instanceKey : string ,
529
- /** The last time this state was updated. */
584
+
585
+ /** updateTimeMs - The last time this state was updated. */
530
586
public updateTimeMs : DbTimestamp
531
587
) { }
532
588
}
533
589
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
537
593
} ) ;
538
594
}
539
595
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
-
567
596
// Exported for testing.
568
597
export const V1_STORES = [
569
598
DbDocumentMutation . store ,
@@ -576,7 +605,7 @@ export const V1_STORES = [
576
605
DbTarget . store
577
606
] ;
578
607
579
- const SCHEMA_V2_STORES = [ DbInstance . store , DbTargetChange . store ] ;
608
+ const SCHEMA_V2_STORES = [ DbInstanceMetadata . store , DbTargetChange . store ] ;
580
609
581
610
/**
582
611
* The list of all IndexedDB stored used by the SDK. This is used when creating
0 commit comments