Skip to content

Commit 06e6c50

Browse files
Use Read-Time Index for Multi-Tab (#2128)
1 parent 998152d commit 06e6c50

14 files changed

+287
-414
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import { Deferred } from '../util/promise';
4141
import { SortedMap } from '../util/sorted_map';
4242

4343
import { ignoreIfPrimaryLeaseLoss } from '../local/indexeddb_persistence';
44-
import { isDocumentChangeMissingError } from '../local/indexeddb_remote_document_cache';
4544
import { ClientId, SharedClientState } from '../local/shared_client_state';
4645
import {
4746
QueryTargetState,
@@ -983,29 +982,16 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
983982
switch (state) {
984983
case 'current':
985984
case 'not-current': {
986-
try {
987-
const changes = await this.localStore.getNewDocumentChanges();
988-
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForCurrentChange(
989-
targetId,
990-
state === 'current'
991-
);
992-
await this.emitNewSnapsAndNotifyLocalStore(
993-
changes,
994-
synthesizedRemoteEvent
995-
);
996-
break;
997-
// Catch errors thrown by getNewDocumentchanges().
998-
} catch (error) {
999-
if (isDocumentChangeMissingError(error)) {
1000-
const activeTargets: TargetId[] = [];
1001-
objUtils.forEachNumber(this.queryViewsByTarget, target =>
1002-
activeTargets.push(target)
1003-
);
1004-
await this.synchronizeQueryViewsAndRaiseSnapshots(activeTargets);
1005-
} else {
1006-
throw error;
1007-
}
1008-
}
985+
const changes = await this.localStore.getNewDocumentChanges();
986+
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForCurrentChange(
987+
targetId,
988+
state === 'current'
989+
);
990+
await this.emitNewSnapsAndNotifyLocalStore(
991+
changes,
992+
synthesizedRemoteEvent
993+
);
994+
break;
1009995
}
1010996
case 'rejected': {
1011997
const queryView = this.queryViewsByTarget[targetId];

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ const LOG_TAG = 'IndexedDbPersistence';
7575

7676
/**
7777
* Oldest acceptable age in milliseconds for client metadata before the client
78-
* is considered inactive and its associated data (such as the remote document
79-
* cache changelog) is garbage collected.
78+
* is considered inactive and its associated data is garbage collected.
8079
*/
8180
const MAX_CLIENT_AGE_MS = 30 * 60 * 1000; // 30 minutes
8281

@@ -259,7 +258,7 @@ export class IndexedDbPersistence implements Persistence {
259258
/** The client metadata refresh task. */
260259
private clientMetadataRefresher: CancelablePromise<void> | null = null;
261260

262-
/** The last time we garbage collected the Remote Document Changelog. */
261+
/** The last time we garbage collected the client metadata object store. */
263262
private lastGarbageCollectionTime = Number.NEGATIVE_INFINITY;
264263

265264
/** Whether to allow shared multi-tab access to the persistence layer. */
@@ -304,8 +303,7 @@ export class IndexedDbPersistence implements Persistence {
304303
this.indexManager = new IndexedDbIndexManager();
305304
this.remoteDocumentCache = new IndexedDbRemoteDocumentCache(
306305
this.serializer,
307-
this.indexManager,
308-
/*keepDocumentChangeLog=*/ this.allowTabSynchronization
306+
this.indexManager
309307
);
310308
if (platform.window && platform.window.localStorage) {
311309
this.window = platform.window;
@@ -430,8 +428,7 @@ export class IndexedDbPersistence implements Persistence {
430428
this.clientId,
431429
Date.now(),
432430
this.networkEnabled,
433-
this.inForeground,
434-
this.remoteDocumentCache.lastProcessedDocumentChangeId
431+
this.inForeground
435432
)
436433
)
437434
.next(() => {
@@ -506,46 +503,22 @@ export class IndexedDbPersistence implements Persistence {
506503
DbClientMetadata
507504
>(txn, DbClientMetadata.store);
508505

509-
return metadataStore
510-
.loadAll()
511-
.next(existingClients => {
512-
activeClients = this.filterActiveClients(
513-
existingClients,
514-
MAX_CLIENT_AGE_MS
515-
);
516-
inactiveClients = existingClients.filter(
517-
client => activeClients.indexOf(client) === -1
518-
);
519-
})
520-
.next(() =>
521-
// Delete metadata for clients that are no longer considered active.
522-
PersistencePromise.forEach(
523-
inactiveClients,
524-
(inactiveClient: DbClientMetadata) =>
525-
metadataStore.delete(inactiveClient.clientId)
526-
)
527-
)
528-
.next(() => {
529-
// Retrieve the minimum change ID from the set of active clients.
530-
531-
// The primary client doesn't read from the document change log,
532-
// and hence we exclude it when we determine the minimum
533-
// `lastProcessedDocumentChangeId`.
534-
activeClients = activeClients.filter(
535-
client => client.clientId !== this.clientId
536-
);
506+
return metadataStore.loadAll().next(existingClients => {
507+
activeClients = this.filterActiveClients(
508+
existingClients,
509+
MAX_CLIENT_AGE_MS
510+
);
511+
inactiveClients = existingClients.filter(
512+
client => activeClients.indexOf(client) === -1
513+
);
537514

538-
if (activeClients.length > 0) {
539-
const processedChangeIds = activeClients.map(
540-
client => client.lastProcessedDocumentChangeId || 0
541-
);
542-
const oldestChangeId = Math.min(...processedChangeIds);
543-
return this.remoteDocumentCache.removeDocumentChangesThroughChangeId(
544-
txn,
545-
oldestChangeId
546-
);
547-
}
548-
});
515+
// Delete metadata for clients that are no longer considered active.
516+
return PersistencePromise.forEach(
517+
inactiveClients,
518+
(inactiveClient: DbClientMetadata) =>
519+
metadataStore.delete(inactiveClient.clientId)
520+
);
521+
});
549522
}
550523
);
551524

0 commit comments

Comments
 (0)