Skip to content

Commit cce601e

Browse files
author
Greg Soltis
authored
Implement Eager Reference Delegate for Memory Persistence, drop old GC (#1256)
* Implement eager reference delegate for memory * Removing old garbage collector and fixing tests * Remove sequential forEach, replace with parallel forEach
1 parent c873f5d commit cce601e

30 files changed

+403
-915
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
import { CredentialsProvider } from '../api/credentials';
1818
import { User } from '../auth/user';
19-
import { EagerGarbageCollector } from '../local/eager_garbage_collector';
20-
import { GarbageCollector } from '../local/garbage_collector';
2119
import { IndexedDbPersistence } from '../local/indexeddb_persistence';
2220
import { LocalStore } from '../local/local_store';
2321
import { MemoryPersistence } from '../local/memory_persistence';
24-
import { NoOpGarbageCollector } from '../local/no_op_garbage_collector';
2522
import { Persistence } from '../local/persistence';
2623
import {
2724
DocumentKeySet,
@@ -83,7 +80,6 @@ export class FirestoreClient {
8380
// with the types rather than littering the code with '!' or unnecessary
8481
// undefined checks.
8582
private eventMgr: EventManager;
86-
private garbageCollector: GarbageCollector;
8783
private persistence: Persistence;
8884
private localStore: LocalStore;
8985
private remoteStore: RemoteStore;
@@ -292,7 +288,6 @@ export class FirestoreClient {
292288

293289
// TODO(http://b/33384523): For now we just disable garbage collection
294290
// when persistence is enabled.
295-
this.garbageCollector = new NoOpGarbageCollector();
296291
const storagePrefix = IndexedDbPersistence.buildStoragePrefix(
297292
this.databaseInfo
298293
);
@@ -347,8 +342,7 @@ export class FirestoreClient {
347342
* @returns A promise that will successfully resolve.
348343
*/
349344
private startMemoryPersistence(): Promise<void> {
350-
this.garbageCollector = new EagerGarbageCollector();
351-
this.persistence = new MemoryPersistence(this.clientId);
345+
this.persistence = MemoryPersistence.createEagerPersistence(this.clientId);
352346
this.sharedClientState = new MemorySharedClientState();
353347
return Promise.resolve();
354348
}
@@ -363,11 +357,7 @@ export class FirestoreClient {
363357
return this.platform
364358
.loadConnection(this.databaseInfo)
365359
.then(async connection => {
366-
this.localStore = new LocalStore(
367-
this.persistence,
368-
user,
369-
this.garbageCollector
370-
);
360+
this.localStore = new LocalStore(this.persistence, user);
371361
const serializer = this.platform.newSerializer(
372362
this.databaseInfo.databaseId
373363
);

packages/firestore/src/core/sync_engine.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
324324
this.remoteStore.unlisten(queryView.targetId);
325325
return this.removeAndCleanupQuery(queryView);
326326
})
327-
.then(() => this.localStore.collectGarbage())
328327
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
329328
}
330329
} else {
@@ -583,7 +582,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
583582
// NOTE: Both these methods are no-ops for batches that originated from
584583
// other clients.
585584
this.processUserCallback(batchId, error ? error : null);
586-
587585
this.localStore.removeCachedMutationBatchMetadata(batchId);
588586
} else {
589587
fail(`Unknown batchState: ${batchState}`);
@@ -820,12 +818,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
820818

821819
await Promise.all(queriesProcessed);
822820
this.syncEngineListener!.onWatchChange(newSnaps);
823-
this.localStore.notifyLocalViewChanges(docChangesInAllViews);
824-
if (this.isPrimary) {
825-
await this.localStore
826-
.collectGarbage()
827-
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
828-
}
821+
await this.localStore.notifyLocalViewChanges(docChangesInAllViews);
829822
}
830823

831824
/**

packages/firestore/src/local/eager_garbage_collector.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

packages/firestore/src/local/garbage_collector.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

packages/firestore/src/local/garbage_source.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { primitiveComparator } from '../util/misc';
2828
import { SortedSet } from '../util/sorted_set';
2929

3030
import * as EncodedResourcePath from './encoded_resource_path';
31-
import { GarbageCollector } from './garbage_collector';
3231
import {
3332
IndexedDbPersistence,
3433
IndexedDbTransaction
@@ -43,7 +42,7 @@ import {
4342
} from './indexeddb_schema';
4443
import { LocalSerializer } from './local_serializer';
4544
import { MutationQueue } from './mutation_queue';
46-
import { PersistenceTransaction } from './persistence';
45+
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
4746
import { PersistencePromise } from './persistence_promise';
4847
import { SimpleDbStore, SimpleDbTransaction } from './simple_db';
4948

@@ -63,15 +62,14 @@ export class IndexedDbMutationQueue implements MutationQueue {
6362
// PORTING NOTE: Multi-tab only.
6463
private documentKeysByBatchId = {} as { [batchId: number]: DocumentKeySet };
6564

66-
private garbageCollector: GarbageCollector | null = null;
67-
6865
constructor(
6966
/**
7067
* The normalized userId (e.g. null UID => "" userId) used to store /
7168
* retrieve mutations.
7269
*/
7370
private userId: string,
74-
private serializer: LocalSerializer
71+
private serializer: LocalSerializer,
72+
private readonly referenceDelegate: ReferenceDelegate
7573
) {}
7674

7775
/**
@@ -81,15 +79,16 @@ export class IndexedDbMutationQueue implements MutationQueue {
8179
*/
8280
static forUser(
8381
user: User,
84-
serializer: LocalSerializer
82+
serializer: LocalSerializer,
83+
referenceDelegate: ReferenceDelegate
8584
): IndexedDbMutationQueue {
8685
// TODO(mcg): Figure out what constraints there are on userIDs
8786
// In particular, are there any reserved characters? are empty ids allowed?
8887
// For the moment store these together in the same mutations table assuming
8988
// that empty userIDs aren't allowed.
9089
assert(user.uid !== '', 'UserID must not be an empty string.');
9190
const userId = user.isAuthenticated() ? user.uid! : '';
92-
return new IndexedDbMutationQueue(userId, serializer);
91+
return new IndexedDbMutationQueue(userId, serializer, referenceDelegate);
9392
}
9493

9594
start(transaction: PersistenceTransaction): PersistencePromise<void> {
@@ -470,12 +469,9 @@ export class IndexedDbMutationQueue implements MutationQueue {
470469
batch
471470
).next(removedDocuments => {
472471
this.removeCachedMutationKeys(batch.batchId);
473-
if (this.garbageCollector !== null) {
474-
for (const key of removedDocuments) {
475-
// TODO(gsoltis): tell reference delegate that mutation was ack'd
476-
this.garbageCollector.addPotentialGarbageKey(key);
477-
}
478-
}
472+
return PersistencePromise.forEach(removedDocuments, key => {
473+
return this.referenceDelegate.removeMutationReference(transaction, key);
474+
});
479475
});
480476
}
481477

@@ -519,10 +515,6 @@ export class IndexedDbMutationQueue implements MutationQueue {
519515
});
520516
}
521517

522-
setGarbageCollector(gc: GarbageCollector | null): void {
523-
this.garbageCollector = gc;
524-
}
525-
526518
containsKey(
527519
txn: PersistenceTransaction,
528520
key: DocumentKey

0 commit comments

Comments
 (0)