Skip to content

Commit d69f79c

Browse files
Remove TargetIdGenerator (#2818)
1 parent 7199846 commit d69f79c

File tree

5 files changed

+21
-126
lines changed

5 files changed

+21
-126
lines changed

packages/firestore/src/core/target_id_generator.ts

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,43 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { assert } from '../util/assert';
1918
import { TargetId } from './types';
2019

21-
const RESERVED_BITS = 1;
22-
23-
const enum GeneratorIds {
24-
QueryCache = 0, // The target IDs for user-issued queries are even (end in 0).
25-
SyncEngine = 1 // The target IDs for limbo detection are odd (end in 1).
26-
}
20+
/** Offset to ensure non-overlapping target ids. */
21+
const OFFSET = 2;
2722

2823
/**
2924
* Generates monotonically increasing target IDs for sending targets to the
3025
* watch stream.
3126
*
32-
* The client constructs two generators, one for the query cache (via
33-
* forQueryCache()), and one for limbo documents (via forSyncEngine()). These
34-
* two generators produce non-overlapping IDs (by using even and odd IDs
27+
* The client constructs two generators, one for the target cache, and one for
28+
* for the sync engine (to generate limbo documents targets). These
29+
* generators produce non-overlapping IDs (by using even and odd IDs
3530
* respectively).
3631
*
3732
* By separating the target ID space, the query cache can generate target IDs
3833
* that persist across client restarts, while sync engine can independently
3934
* generate in-memory target IDs that are transient and can be reused after a
4035
* restart.
4136
*/
42-
// TODO(mrschmidt): Explore removing this class in favor of generating these IDs
43-
// directly in SyncEngine and LocalStore.
4437
export class TargetIdGenerator {
45-
// Initialized in the constructor via call to seek().
46-
private nextId!: TargetId;
47-
48-
/**
49-
* Instantiates a new TargetIdGenerator. If a seed is provided, the generator
50-
* will use the seed value as the next target ID.
51-
*/
52-
constructor(private generatorId: number, seed?: number) {
53-
assert(
54-
(generatorId & RESERVED_BITS) === generatorId,
55-
`Generator ID ${generatorId} contains more than ${RESERVED_BITS} reserved bits`
56-
);
57-
this.seek(seed !== undefined ? seed : this.generatorId);
58-
}
38+
constructor(private lastId: number) {}
5939

6040
next(): TargetId {
61-
const nextId = this.nextId;
62-
this.nextId += 1 << RESERVED_BITS;
63-
return nextId;
64-
}
65-
66-
/**
67-
* Returns the ID that follows the given ID. Subsequent calls to `next()`
68-
* use the newly returned target ID as their base.
69-
*/
70-
// PORTING NOTE: Multi-tab only.
71-
after(targetId: TargetId): TargetId {
72-
this.seek(targetId + (1 << RESERVED_BITS));
73-
return this.next();
74-
}
75-
76-
private seek(targetId: TargetId): void {
77-
assert(
78-
(targetId & RESERVED_BITS) === this.generatorId,
79-
'Cannot supply target ID from different generator ID'
80-
);
81-
this.nextId = targetId;
41+
this.lastId += OFFSET;
42+
return this.lastId;
8243
}
8344

8445
static forTargetCache(): TargetIdGenerator {
85-
// We seed the query cache generator to return '2' as its first ID, as there
86-
// is no differentiation in the protocol layer between an unset number and
87-
// the number '0'. If we were to sent a target with target ID '0', the
88-
// backend would consider it unset and replace it with its own ID.
89-
const targetIdGenerator = new TargetIdGenerator(GeneratorIds.QueryCache, 2);
90-
return targetIdGenerator;
46+
// The target cache generator must return '2' in its first call to `next()`
47+
// as there is no differentiation in the protocol layer between an unset
48+
// number and the number '0'. If we were to sent a target with target ID
49+
// '0', the backend would consider it unset and replace it with its own ID.
50+
return new TargetIdGenerator(2 - OFFSET);
9151
}
9252

9353
static forSyncEngine(): TargetIdGenerator {
9454
// Sync engine assigns target IDs for limbo document detection.
95-
return new TargetIdGenerator(GeneratorIds.SyncEngine);
55+
return new TargetIdGenerator(1 - OFFSET);
9656
}
9757
}

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@ export class IndexedDbTargetCache implements TargetCache {
6262
// to IndexedDb whenever we need to read metadata. We can revisit if it turns
6363
// out to have a meaningful performance impact.
6464

65-
private targetIdGenerator = TargetIdGenerator.forTargetCache();
66-
6765
allocateTargetId(
6866
transaction: PersistenceTransaction
6967
): PersistencePromise<TargetId> {
7068
return this.retrieveMetadata(transaction).next(metadata => {
71-
metadata.highestTargetId = this.targetIdGenerator.after(
72-
metadata.highestTargetId
73-
);
69+
const targetIdGenerator = new TargetIdGenerator(metadata.highestTargetId);
70+
metadata.highestTargetId = targetIdGenerator.next();
7471
return this.saveMetadata(transaction, metadata).next(
7572
() => metadata.highestTargetId
7673
);

packages/firestore/src/local/local_serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class LocalSerializer {
170170
mutations
171171
);
172172
}
173-
173+
174174
/** Decodes a DbTarget into TargetData */
175175
fromDbTarget(dbTarget: DbTarget): TargetData {
176176
const version = this.fromDbTimestamp(dbTarget.readTime);

packages/firestore/src/local/memory_target_cache.ts

Lines changed: 4 additions & 4 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.
@@ -79,9 +79,8 @@ export class MemoryTargetCache implements TargetCache {
7979
allocateTargetId(
8080
transaction: PersistenceTransaction
8181
): PersistencePromise<TargetId> {
82-
const nextTargetId = this.targetIdGenerator.after(this.highestTargetId);
83-
this.highestTargetId = nextTargetId;
84-
return PersistencePromise.resolve(nextTargetId);
82+
this.highestTargetId = this.targetIdGenerator.next();
83+
return PersistencePromise.resolve(this.highestTargetId);
8584
}
8685

8786
setTargetsMetadata(
@@ -102,6 +101,7 @@ export class MemoryTargetCache implements TargetCache {
102101
this.targets.set(targetData.target, targetData);
103102
const targetId = targetData.targetId;
104103
if (targetId > this.highestTargetId) {
104+
this.targetIdGenerator = new TargetIdGenerator(targetId);
105105
this.highestTargetId = targetId;
106106
}
107107
if (targetData.sequenceNumber > this.highestSequenceNumber) {

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

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

0 commit comments

Comments
 (0)