Skip to content

Commit 1f414db

Browse files
Merge
1 parent 43b64b5 commit 1f414db

14 files changed

+211
-461
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,13 @@ import { Query } from './query';
4545
import { SnapshotVersion } from './snapshot_version';
4646
import { TargetIdGenerator } from './target_id_generator';
4747
import { Transaction } from './transaction';
48-
<<<<<<< HEAD
4948
import {
5049
BatchId,
5150
MutationBatchState,
5251
OnlineState,
5352
OnlineStateSource,
54-
ProtoByteString,
5553
TargetId
5654
} from './types';
57-
=======
58-
import { BatchId, OnlineState, TargetId } from './types';
59-
>>>>>>> master
6055
import {
6156
AddedLimboDocument,
6257
LimboDocumentChange,
@@ -272,13 +267,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
272267
'applyChanges for new view should always return a snapshot'
273268
);
274269

275-
<<<<<<< HEAD
276-
const data = new QueryView(
277-
query,
278-
queryData.targetId,
279-
queryData.resumeToken,
280-
view
281-
);
270+
const data = new QueryView(query, queryData.targetId, view);
282271
this.queryViewsByQuery.set(query, data);
283272
this.queryViewsByTarget[queryData.targetId] = data;
284273
return viewChange.snapshot!;
@@ -309,17 +298,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
309298
);
310299
}
311300
return viewSnapshot;
312-
=======
313-
const data = new QueryView(query, queryData.targetId, view);
314-
this.queryViewsByQuery.set(query, data);
315-
this.queryViewsByTarget[queryData.targetId] = data;
316-
this.viewHandler!([viewChange.snapshot!]);
317-
this.remoteStore.listen(queryData);
318-
});
319-
})
320-
.then(() => {
321-
return queryData.targetId;
322-
>>>>>>> master
323301
});
324302
});
325303
}

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ import { LocalSerializer } from './local_serializer';
4141
import { MutationQueue } from './mutation_queue';
4242
import { PersistenceTransaction } from './persistence';
4343
import { PersistencePromise } from './persistence_promise';
44-
<<<<<<< HEAD
45-
import { SimpleDb, SimpleDbStore } from './simple_db';
46-
import { DocumentKeySet } from '../model/collections';
47-
=======
4844
import { SimpleDbStore } from './simple_db';
4945
import { IndexedDbPersistence } from './indexeddb_persistence';
50-
>>>>>>> master
5146

5247
/** A mutation queue for a specific user, backed by IndexedDB. */
5348
export class IndexedDbMutationQueue implements MutationQueue {
@@ -443,36 +438,6 @@ export class IndexedDbMutationQueue implements MutationQueue {
443438
}
444439
uniqueBatchIDs = uniqueBatchIDs.add(batchID);
445440
})
446-
<<<<<<< HEAD
447-
.next(() => {
448-
const results: MutationBatch[] = [];
449-
const promises: Array<PersistencePromise<void>> = [];
450-
// TODO(rockwood): Implement this using iterate.
451-
uniqueBatchIDs.forEach(batchId => {
452-
promises.push(
453-
mutationsStore(transaction)
454-
.get(batchId)
455-
.next(mutation => {
456-
if (!mutation) {
457-
fail(
458-
'Dangling document-mutation reference found, ' +
459-
'which points to ' +
460-
batchId
461-
);
462-
}
463-
assert(
464-
mutation.userId === this.userId,
465-
`Unexpected user '${
466-
mutation.userId
467-
}' for mutation batch ${batchId}`
468-
);
469-
results.push(this.serializer.fromDbMutationBatch(mutation!));
470-
})
471-
);
472-
});
473-
return PersistencePromise.waitFor(promises).next(() => results);
474-
});
475-
=======
476441
.next(() => this.lookupMutationBatches(transaction, uniqueBatchIDs));
477442
}
478443

@@ -483,25 +448,29 @@ export class IndexedDbMutationQueue implements MutationQueue {
483448
const results: MutationBatch[] = [];
484449
const promises: Array<PersistencePromise<void>> = [];
485450
// TODO(rockwood): Implement this using iterate.
486-
batchIDs.forEach(batchID => {
487-
const mutationKey = this.keyForBatchId(batchID);
451+
batchIDs.forEach(batchId => {
488452
promises.push(
489453
mutationsStore(transaction)
490-
.get(mutationKey)
454+
.get(batchId)
491455
.next(mutation => {
492456
if (mutation === null) {
493457
fail(
494458
'Dangling document-mutation reference found, ' +
495459
'which points to ' +
496-
mutationKey
460+
batchId
497461
);
498462
}
463+
assert(
464+
mutation.userId === this.userId,
465+
`Unexpected user '${
466+
mutation.userId
467+
}' for mutation batch ${batchId}`
468+
);
499469
results.push(this.serializer.fromDbMutationBatch(mutation!));
500470
})
501471
);
502472
});
503473
return PersistencePromise.waitFor(promises).next(() => results);
504-
>>>>>>> master
505474
}
506475

507476
removeMutationBatches(

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 37 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,19 @@ import {
3535
} from './indexeddb_schema';
3636
import { LocalSerializer } from './local_serializer';
3737
import { MutationQueue } from './mutation_queue';
38-
<<<<<<< HEAD
3938
import {
4039
Persistence,
4140
PersistenceTransaction,
4241
PrimaryStateListener
4342
} from './persistence';
44-
=======
45-
import { Persistence, PersistenceTransaction } from './persistence';
46-
>>>>>>> master
4743
import { PersistencePromise } from './persistence_promise';
4844
import { QueryCache } from './query_cache';
4945
import { RemoteDocumentCache } from './remote_document_cache';
5046
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
51-
<<<<<<< HEAD
5247
import { Platform } from '../platform/platform';
5348
import { AsyncQueue, TimerId } from '../util/async_queue';
5449
import { ClientId } from './shared_client_state';
5550
import { CancelablePromise } from '../util/promise';
56-
=======
57-
>>>>>>> master
5851

5952
const LOG_TAG = 'IndexedDbPersistence';
6053

@@ -86,17 +79,15 @@ const UNSUPPORTED_PLATFORM_ERROR_MSG =
8679
' IndexedDB or is known to have an incomplete implementation. Offline' +
8780
' persistence has been disabled.';
8881

89-
<<<<<<< HEAD
9082
// The format of the LocalStorage key that stores zombied client is:
9183
// firestore_zombie_<persistence_prefix>_<instance_key>
9284
const ZOMBIED_CLIENTS_KEY_PREFIX = 'firestore_zombie';
93-
=======
85+
9486
export class IndexedDbTransaction extends PersistenceTransaction {
9587
constructor(readonly simpleDbTransaction: SimpleDbTransaction) {
9688
super();
9789
}
9890
}
99-
>>>>>>> master
10091

10192
/**
10293
* An IndexedDB-backed instance of Persistence. Data is stored persistently
@@ -494,14 +485,10 @@ export class IndexedDbPersistence implements Persistence {
494485

495486
runTransaction<T>(
496487
action: string,
497-
<<<<<<< HEAD
498488
requirePrimaryLease: boolean,
499489
transactionOperation: (
500-
transaction: PersistenceTransaction
490+
transaction: IndexedDbTransaction
501491
) => PersistencePromise<T>
502-
=======
503-
operation: (transaction: IndexedDbTransaction) => PersistencePromise<T>
504-
>>>>>>> master
505492
): Promise<T> {
506493
// TODO(multitab): Consider removing `requirePrimaryLease` and exposing
507494
// three different write modes (readonly, readwrite, readwrite_primary).
@@ -513,52 +500,47 @@ export class IndexedDbPersistence implements Persistence {
513500

514501
// Do all transactions as readwrite against all object stores, since we
515502
// are the only reader/writer.
516-
<<<<<<< HEAD
517-
return this.simpleDb.runTransaction('readwrite', ALL_STORES, txn => {
518-
if (requirePrimaryLease) {
519-
// While we merely verify that we have (or can acquire) the lease
520-
// immediately, we wait to extend the primary lease until after
521-
// executing transactionOperation(). This ensures that even if the
522-
// transactionOperation takes a long time, we'll use a recent
523-
// leaseTimestampMs in the extended (or newly acquired) lease.
524-
return this.canActAsPrimary(txn)
525-
.next(canActAsPrimary => {
526-
if (!canActAsPrimary) {
527-
// TODO(multitab): Handle this gracefully and transition back to
528-
// secondary state.
529-
log.error(
530-
`Failed to obtain primary lease for action '${action}'.`
531-
);
532-
this.isPrimary = false;
533-
this.queue.enqueue(() => this.primaryStateListener(false));
534-
throw new FirestoreError(
535-
Code.FAILED_PRECONDITION,
536-
PRIMARY_LEASE_LOST_ERROR_MSG
537-
);
538-
}
539-
return transactionOperation(txn);
540-
})
541-
.next(result => {
542-
return this.acquireOrExtendPrimaryLease(txn).next(() => result);
543-
});
544-
} else {
545-
return this.verifyAllowTabSynchronization(txn).next(() =>
546-
transactionOperation(txn)
547-
);
548-
}
549-
});
550-
=======
551503
return this.simpleDb.runTransaction(
552504
'readwrite',
553505
ALL_STORES,
554506
simpleDbTxn => {
555-
// Verify that we still have the owner lease as part of every transaction.
556-
return this.ensureOwnerLease(simpleDbTxn).next(() =>
557-
operation(new IndexedDbTransaction(simpleDbTxn))
558-
);
507+
if (requirePrimaryLease) {
508+
// While we merely verify that we have (or can acquire) the lease
509+
// immediately, we wait to extend the primary lease until after
510+
// executing transactionOperation(). This ensures that even if the
511+
// transactionOperation takes a long time, we'll use a recent
512+
// leaseTimestampMs in the extended (or newly acquired) lease.
513+
return this.canActAsPrimary(simpleDbTxn)
514+
.next(canActAsPrimary => {
515+
if (!canActAsPrimary) {
516+
// TODO(multitab): Handle this gracefully and transition back to
517+
// secondary state.
518+
log.error(
519+
`Failed to obtain primary lease for action '${action}'.`
520+
);
521+
this.isPrimary = false;
522+
this.queue.enqueue(() => this.primaryStateListener(false));
523+
throw new FirestoreError(
524+
Code.FAILED_PRECONDITION,
525+
PRIMARY_LEASE_LOST_ERROR_MSG
526+
);
527+
}
528+
return transactionOperation(
529+
new IndexedDbTransaction(simpleDbTxn)
530+
);
531+
})
532+
.next(result => {
533+
return this.acquireOrExtendPrimaryLease(simpleDbTxn).next(
534+
() => result
535+
);
536+
});
537+
} else {
538+
return this.verifyAllowTabSynchronization(simpleDbTxn).next(() =>
539+
transactionOperation(new IndexedDbTransaction(simpleDbTxn))
540+
);
541+
}
559542
}
560543
);
561-
>>>>>>> master
562544
}
563545

564546
/**
@@ -659,7 +641,6 @@ export class IndexedDbPersistence implements Persistence {
659641
return true;
660642
}
661643

662-
<<<<<<< HEAD
663644
private attachVisibilityHandler(): void {
664645
if (
665646
this.document !== null &&
@@ -671,27 +652,6 @@ export class IndexedDbPersistence implements Persistence {
671652
return this.updateClientMetadataAndTryBecomePrimary();
672653
});
673654
};
674-
=======
675-
/**
676-
* Schedules a recurring timer to update the owner lease timestamp to prevent
677-
* other tabs from taking the lease.
678-
*/
679-
private scheduleOwnerLeaseRefreshes(): void {
680-
// NOTE: This doesn't need to be scheduled on the async queue and doing so
681-
// would increase the chances of us not refreshing on time if the queue is
682-
// backed up for some reason.
683-
this.ownerLeaseRefreshHandle = setInterval(() => {
684-
const txResult = this.simpleDb.runTransaction(
685-
'readwrite',
686-
ALL_STORES,
687-
txn => {
688-
// NOTE: We don't need to validate the current owner contents, since
689-
// runTransaction does that automatically.
690-
const store = txn.store<DbOwnerKey, DbOwner>(DbOwner.store);
691-
return store.put('owner', new DbOwner(this.ownerId, Date.now()));
692-
}
693-
);
694-
>>>>>>> master
695655

696656
this.document.addEventListener(
697657
'visibilitychange',

packages/firestore/src/local/indexeddb_query_cache.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,13 @@ import { PersistenceTransaction } from './persistence';
3838
import { PersistencePromise } from './persistence_promise';
3939
import { QueryCache } from './query_cache';
4040
import { QueryData } from './query_data';
41-
<<<<<<< HEAD
42-
import { SimpleDb, SimpleDbStore } from './simple_db';
4341
import { TargetIdGenerator } from '../core/target_id_generator';
44-
=======
4542
import { SimpleDbStore } from './simple_db';
4643
import { IndexedDbPersistence } from './indexeddb_persistence';
47-
>>>>>>> master
4844

4945
export class IndexedDbQueryCache implements QueryCache {
5046
constructor(private serializer: LocalSerializer) {}
5147

52-
<<<<<<< HEAD
53-
=======
54-
/**
55-
* The last received snapshot version. We store this separately from the
56-
* metadata to avoid the extra conversion to/from DbTimestamp.
57-
*/
58-
private lastRemoteSnapshotVersion = SnapshotVersion.MIN;
59-
60-
/**
61-
* A cached copy of the metadata for the query cache.
62-
*/
63-
private metadata: DbTargetGlobal = null;
64-
65-
>>>>>>> master
6648
/** The garbage collector to notify about potential garbage keys. */
6749
private garbageCollector: GarbageCollector | null = null;
6850

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,20 @@ import {
2525
import { Document, MaybeDocument, NoDocument } from '../model/document';
2626
import { DocumentKey } from '../model/document_key';
2727

28-
<<<<<<< HEAD
2928
import {
3029
DbRemoteDocument,
3130
DbRemoteDocumentKey,
3231
DbRemoteDocumentChanges,
3332
DbRemoteDocumentChangesKey
3433
} from './indexeddb_schema';
35-
=======
36-
import { DbRemoteDocument, DbRemoteDocumentKey } from './indexeddb_schema';
3734
import { IndexedDbPersistence } from './indexeddb_persistence';
38-
>>>>>>> master
3935
import { LocalSerializer } from './local_serializer';
4036
import { PersistenceTransaction } from './persistence';
4137
import { PersistencePromise } from './persistence_promise';
4238
import { RemoteDocumentCache } from './remote_document_cache';
43-
<<<<<<< HEAD
44-
import { SimpleDb, SimpleDbStore } from './simple_db';
4539
import { SnapshotVersion } from '../core/snapshot_version';
4640
import { assert } from '../util/assert';
47-
=======
4841
import { SimpleDbStore } from './simple_db';
49-
>>>>>>> master
5042

5143
export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
5244
/** The last id read by `getNewDocumentChanges()`. */
@@ -213,10 +205,10 @@ function remoteDocumentsStore(
213205
function documentChangesStore(
214206
txn: PersistenceTransaction
215207
): SimpleDbStore<DbRemoteDocumentChangesKey, DbRemoteDocumentChanges> {
216-
return SimpleDb.getStore<DbRemoteDocumentChangesKey, DbRemoteDocumentChanges>(
217-
txn,
218-
DbRemoteDocumentChanges.store
219-
);
208+
return IndexedDbPersistence.getStore<
209+
DbRemoteDocumentChangesKey,
210+
DbRemoteDocumentChanges
211+
>(txn, DbRemoteDocumentChanges.store);
220212
}
221213

222214
function dbKey(docKey: DocumentKey): DbRemoteDocumentKey {

0 commit comments

Comments
 (0)