Skip to content

Commit 09aaec4

Browse files
Remove TargetIdGenerator
1 parent f1ddd70 commit 09aaec4

File tree

6 files changed

+27
-185
lines changed

6 files changed

+27
-185
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import { ListenSequence } from './listen_sequence';
5151
import { Query, LimitType } from './query';
5252
import { SnapshotVersion } from './snapshot_version';
5353
import { Target } from './target';
54-
import { TargetIdGenerator } from './target_id_generator';
5554
import { Transaction } from './transaction';
5655
import {
5756
BatchId,
@@ -161,7 +160,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
161160
};
162161
/** Stores user callbacks waiting for all pending writes to be acknowledged. */
163162
private pendingWritesCallbacks = new Map<BatchId, Array<Deferred<void>>>();
164-
private limboTargetIdGenerator = TargetIdGenerator.forSyncEngine();
163+
private nextLimboTargetId = 1;
165164

166165
// The primary state is set to `true` or `false` immediately after Firestore
167166
// startup. In the interim, a client should only be considered primary if
@@ -770,7 +769,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
770769
const key = limboChange.key;
771770
if (!this.limboTargetsByKey.get(key)) {
772771
logDebug(LOG_TAG, 'New document in limbo: ' + key);
773-
const limboTargetId = this.limboTargetIdGenerator.next();
772+
773+
// Target IDs in SyncEngine start at 1 and remain odd.
774+
const limboTargetId = this.nextLimboTargetId;
775+
this.nextLimboTargetId += 2;
776+
774777
const query = Query.atPath(key.path);
775778
this.limboResolutionsByTarget[limboTargetId] = new LimboResolution(key);
776779
this.remoteStore.listen(

packages/firestore/src/core/target_id_generator.ts

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

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ import { DocumentKey } from '../model/document_key';
2323
import { assert } from '../util/assert';
2424
import { immediateSuccessor } from '../util/misc';
2525

26-
import { TargetIdGenerator } from '../core/target_id_generator';
2726
import * as EncodedResourcePath from './encoded_resource_path';
2827
import {
2928
IndexedDbLruDelegate,
@@ -60,15 +59,12 @@ export class IndexedDbTargetCache implements TargetCache {
6059
// to IndexedDb whenever we need to read metadata. We can revisit if it turns
6160
// out to have a meaningful performance impact.
6261

63-
private targetIdGenerator = TargetIdGenerator.forTargetCache();
64-
6562
allocateTargetId(
6663
transaction: PersistenceTransaction
6764
): PersistencePromise<TargetId> {
6865
return this.retrieveMetadata(transaction).next(metadata => {
69-
metadata.highestTargetId = this.targetIdGenerator.after(
70-
metadata.highestTargetId
71-
);
66+
// Target IDs in persistence start at two and remain even.
67+
metadata.highestTargetId += 2;
7268
return this.saveMetadata(transaction, metadata).next(
7369
() => metadata.highestTargetId
7470
);

packages/firestore/src/local/memory_target_cache.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import { SnapshotVersion } from '../core/snapshot_version';
19-
import { TargetIdGenerator } from '../core/target_id_generator';
2019
import { ListenSequenceNumber, TargetId } from '../core/types';
2120
import { DocumentKeySet } from '../model/collections';
2221
import { DocumentKey } from '../model/document_key';
@@ -52,8 +51,6 @@ export class MemoryTargetCache implements TargetCache {
5251

5352
private targetCount = 0;
5453

55-
private targetIdGenerator = TargetIdGenerator.forTargetCache();
56-
5754
constructor(private readonly persistence: MemoryPersistence) {}
5855

5956
forEachTarget(
@@ -79,9 +76,9 @@ export class MemoryTargetCache implements TargetCache {
7976
allocateTargetId(
8077
transaction: PersistenceTransaction
8178
): PersistencePromise<TargetId> {
82-
const nextTargetId = this.targetIdGenerator.after(this.highestTargetId);
83-
this.highestTargetId = nextTargetId;
84-
return PersistencePromise.resolve(nextTargetId);
79+
// Target IDs in persistence start at two and remain even.
80+
this.highestTargetId += 2;
81+
return PersistencePromise.resolve(this.highestTargetId);
8582
}
8683

8784
setTargetsMetadata(

packages/firestore/test/unit/core/target_id_generator.test.ts

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

packages/firestore/test/unit/specs/spec_builder.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import { FieldFilter, Filter, Query } from '../../../src/core/query';
1919
import { Target } from '../../../src/core/target';
20-
import { TargetIdGenerator } from '../../../src/core/target_id_generator';
2120
import { TargetId } from '../../../src/core/types';
2221
import {
2322
Document,
@@ -82,8 +81,7 @@ export class ClientMemoryState {
8281
activeTargets: ActiveTargetMap = {};
8382
queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
8483
limboMapping: LimboMap = {};
85-
86-
limboIdGenerator: TargetIdGenerator = TargetIdGenerator.forSyncEngine();
84+
nextLimboTarget = 1;
8785

8886
constructor() {
8987
this.reset();
@@ -94,7 +92,13 @@ export class ClientMemoryState {
9492
this.queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
9593
this.limboMapping = {};
9694
this.activeTargets = {};
97-
this.limboIdGenerator = TargetIdGenerator.forSyncEngine();
95+
this.nextLimboTarget = 1;
96+
}
97+
98+
nextLimboTargetId(): TargetId {
99+
const nextLimboTargetId = this.nextLimboTarget;
100+
this.nextLimboTarget += 2;
101+
return nextLimboTargetId;
98102
}
99103

100104
/**
@@ -112,7 +116,7 @@ export class ClientMemoryState {
112116
class CachedTargetIdGenerator {
113117
// TODO(wuandy): rename this to targetMapping.
114118
private queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
115-
private targetIdGenerator = TargetIdGenerator.forTargetCache();
119+
private nextTargetId = 2;
116120

117121
/**
118122
* Returns a cached target ID for the provided Target, or a new ID if no
@@ -122,7 +126,8 @@ class CachedTargetIdGenerator {
122126
if (this.queryMapping.has(target)) {
123127
return this.queryMapping.get(target)!;
124128
}
125-
const targetId = this.targetIdGenerator.next();
129+
const targetId = this.nextTargetId;
130+
this.nextTargetId += 2;
126131
this.queryMapping.set(target, targetId);
127132
return targetId;
128133
}
@@ -170,8 +175,8 @@ export class SpecBuilder {
170175
return this.currentClientState;
171176
}
172177

173-
private get limboIdGenerator(): TargetIdGenerator {
174-
return this.clientState.limboIdGenerator;
178+
private nextLimboTargetId(): TargetId {
179+
return this.clientState.nextLimboTargetId();
175180
}
176181

177182
private get queryMapping(): ObjectMap<Target, TargetId> {
@@ -450,7 +455,7 @@ export class SpecBuilder {
450455
const path = key.path.canonicalString();
451456
// Create limbo target ID mapping if it was not in limbo yet
452457
if (!objUtils.contains(this.limboMapping, path)) {
453-
this.limboMapping[path] = this.limboIdGenerator.next();
458+
this.limboMapping[path] = this.nextLimboTargetId();
454459
}
455460
// Limbo doc queries are always without resume token
456461
this.addQueryToActiveTargets(

0 commit comments

Comments
 (0)