@@ -24,6 +24,12 @@ import { encode, EncodedResourcePath } from './encoded_resource_path';
24
24
import { SimpleDbTransaction } from './simple_db' ;
25
25
import { PersistencePromise } from './persistence_promise' ;
26
26
import { SnapshotVersion } from '../core/snapshot_version' ;
27
+ import { BATCHID_UNKNOWN } from '../model/mutation_batch' ;
28
+ import { IndexedDbMutationQueue } from './indexeddb_mutation_queue' ;
29
+ import { LocalSerializer } from './local_serializer' ;
30
+ import { JsonProtoSerializer } from '../remote/serializer' ;
31
+ import { IndexedDbTransaction } from './indexeddb_persistence' ;
32
+ import { DatabaseId } from '../core/database_info' ;
27
33
28
34
/**
29
35
* Schema Version for the Web client:
@@ -35,8 +41,11 @@ import { SnapshotVersion } from '../core/snapshot_version';
35
41
* to limbo resolution. Addresses
36
42
* https://github.com/firebase/firebase-ios-sdk/issues/1548
37
43
* 4. Multi-Tab Support.
44
+ * 5. Removal of held write acks (not yet active).
38
45
*/
39
46
export const SCHEMA_VERSION = 4 ;
47
+ // TODO(mrschmidt): As SCHEMA_VERSION becomes 5, uncomment the assert in
48
+ // `createOrUpgradeDb`.
40
49
41
50
/**
42
51
* Performs database creation and schema upgrades.
@@ -47,14 +56,15 @@ export const SCHEMA_VERSION = 4;
47
56
*/
48
57
export function createOrUpgradeDb (
49
58
db : IDBDatabase ,
59
+ databaseId : DatabaseId ,
50
60
txn : SimpleDbTransaction ,
51
61
fromVersion : number ,
52
62
toVersion : number
53
63
) : PersistencePromise < void > {
54
- assert (
55
- fromVersion < toVersion && fromVersion >= 0 && toVersion <= SCHEMA_VERSION ,
56
- ' Unexpected schema upgrade from v${fromVersion} to v{toVersion}.'
57
- ) ;
64
+ // assert(
65
+ // fromVersion < toVersion && fromVersion >= 0 && toVersion <= SCHEMA_VERSION,
66
+ // ` Unexpected schema upgrade from v${fromVersion} to v{toVersion}.`
67
+ // );
58
68
59
69
if ( fromVersion < 1 && toVersion >= 1 ) {
60
70
createPrimaryClientStore ( db ) ;
@@ -94,6 +104,10 @@ export function createOrUpgradeDb(
94
104
} ) ;
95
105
}
96
106
107
+ if ( fromVersion < 5 && toVersion >= 5 ) {
108
+ p = p . next ( ( ) => removeAcknowledgedMutations ( db , databaseId , txn ) ) ;
109
+ }
110
+
97
111
return p ;
98
112
}
99
113
@@ -295,6 +309,62 @@ function upgradeMutationBatchSchemaAndMigrateData(
295
309
} ) ;
296
310
}
297
311
312
+ function removeAcknowledgedMutations (
313
+ db : IDBDatabase ,
314
+ databaseId : DatabaseId ,
315
+ txn : SimpleDbTransaction
316
+ ) : PersistencePromise < void > {
317
+ const queuesStore = txn . store < DbMutationQueueKey , DbMutationQueue > (
318
+ DbMutationQueue . store
319
+ ) ;
320
+ const mutationsStore = txn . store < DbMutationBatchKey , DbMutationBatch > (
321
+ DbMutationBatch . store
322
+ ) ;
323
+ const serializer = new LocalSerializer (
324
+ new JsonProtoSerializer ( databaseId , {
325
+ useProto3Json : true
326
+ } )
327
+ ) ;
328
+
329
+ const indexedDbTransaction = new IndexedDbTransaction ( txn ) ;
330
+ return queuesStore . loadAll ( ) . next ( queues => {
331
+ let p = PersistencePromise . resolve ( ) ;
332
+ for ( const queue of queues ) {
333
+ p = p . next ( ( ) => {
334
+ const mutationQueue = new IndexedDbMutationQueue (
335
+ queue . userId ,
336
+ serializer
337
+ ) ;
338
+ const range = IDBKeyRange . bound (
339
+ [ queue . userId , BATCHID_UNKNOWN ] ,
340
+ [ queue . userId , queue . lastAcknowledgedBatchId ]
341
+ ) ;
342
+
343
+ return mutationsStore
344
+ . loadAll ( DbMutationBatch . userMutationsIndex , range )
345
+ . next ( dbBatches => {
346
+ let removeP = PersistencePromise . resolve ( ) ;
347
+ for ( const dbBatch of dbBatches ) {
348
+ assert (
349
+ dbBatch . userId === queue . userId ,
350
+ `Cannot process batch ${ dbBatch . batchId } from unexpected user`
351
+ ) ;
352
+ const batch = serializer . fromDbMutationBatch ( dbBatch ) ;
353
+ removeP = removeP . next ( ( ) =>
354
+ mutationQueue . removeMutationBatch ( indexedDbTransaction , batch )
355
+ ) ;
356
+ }
357
+ return removeP ;
358
+ } )
359
+ . next ( ( ) =>
360
+ mutationQueue . performConsistencyCheck ( indexedDbTransaction )
361
+ ) ;
362
+ } ) ;
363
+ }
364
+ return p ;
365
+ } ) ;
366
+ }
367
+
298
368
/**
299
369
* An object to be stored in the 'documentMutations' store in IndexedDb.
300
370
*
0 commit comments