Skip to content

Commit 9323c9a

Browse files
Review
1 parent d251a62 commit 9323c9a

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { SortedSet } from '../util/sorted_set';
5050
import { ListenSequence } from './listen_sequence';
5151
import { Query, LimitType } from './query';
5252
import { SnapshotVersion } from './snapshot_version';
53-
import { Target } from './target';
53+
import { generateNextTargetId, Target, TARGET_ID_OFFSET_SYNC_ENGINE } from './target';
5454
import { Transaction } from './transaction';
5555
import {
5656
BatchId,
@@ -173,7 +173,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
173173
};
174174
/** Stores user callbacks waiting for all pending writes to be acknowledged. */
175175
private pendingWritesCallbacks = new Map<BatchId, Array<Deferred<void>>>();
176-
private nextLimboTargetId = 1;
176+
private nextLimboTargetId = TARGET_ID_OFFSET_SYNC_ENGINE;
177177

178178
// The primary state is set to `true` or `false` immediately after Firestore
179179
// startup. In the interim, a client should only be considered primary if
@@ -808,17 +808,15 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
808808
this.activeLimboTargetsByKey.size < this.maxConcurrentLimboResolutions
809809
) {
810810
const key = this.enqueuedLimboResolutions.shift()!;
811-
// Target IDs in SyncEngine start at 1 and remain odd.
812811
const limboTargetId = this.nextLimboTargetId;
813-
this.nextLimboTargetId += 2;
812+
this.nextLimboTargetId = generateNextTargetId(limboTargetId);
814813
this.activeLimboResolutionsByTarget[limboTargetId] = new LimboResolution(
815814
key
816815
);
817816
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.insert(
818817
key,
819818
limboTargetId
820819
);
821-
>>>>>>> master
822820
this.remoteStore.listen(
823821
new TargetData(
824822
Query.atPath(key.path).toTarget(),

packages/firestore/src/core/target.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DocumentKey } from '../model/document_key';
1919
import { ResourcePath } from '../model/path';
2020
import { isNullOrUndefined } from '../util/types';
2121
import { Bound, Filter, OrderBy } from './query';
22+
import { TargetId } from './types';
2223

2324
/**
2425
* A Target represents the WatchTarget representation of a Query, which is used
@@ -153,3 +154,20 @@ export class Target {
153154
);
154155
}
155156
}
157+
158+
/** Target IDs for Limbo resolution are odd. */
159+
export const TARGET_ID_OFFSET_SYNC_ENGINE: TargetId = 1;
160+
161+
/** Target IDs for Queries start are even. */
162+
export const TARGET_ID_OFFSET_LOCAL_STORE: TargetId = 2;
163+
164+
/**
165+
* Generates the next target ID given the last target ID.
166+
*
167+
* If TARGET_ID_OFFSET_SYNC_ENGINE/TARGET_ID_OFFSET_LOCAL_STORE are used as
168+
* initial target ID, then the IDs generated by `generateNextTargetId` never
169+
* collide.
170+
*/
171+
export function generateNextTargetId(lastTargetId: TargetId): number {
172+
return lastTargetId + 2;
173+
}

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { PersistencePromise } from './persistence_promise';
4444
import { TargetCache } from './target_cache';
4545
import { TargetData } from './target_data';
4646
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
47-
import { Target } from '../core/target';
47+
import { generateNextTargetId, Target } from '../core/target';
4848

4949
export class IndexedDbTargetCache implements TargetCache {
5050
constructor(
@@ -63,8 +63,7 @@ export class IndexedDbTargetCache implements TargetCache {
6363
transaction: PersistenceTransaction
6464
): PersistencePromise<TargetId> {
6565
return this.retrieveMetadata(transaction).next(metadata => {
66-
// Target IDs in persistence start at two and remain even.
67-
metadata.highestTargetId += 2;
66+
metadata.highestTargetId = generateNextTargetId(metadata.highestTargetId);
6867
return this.saveMetadata(transaction, metadata).next(
6968
() => metadata.highestTargetId
7069
);

packages/firestore/src/local/memory_target_cache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { PersistencePromise } from './persistence_promise';
2929
import { ReferenceSet } from './reference_set';
3030
import { TargetCache } from './target_cache';
3131
import { TargetData } from './target_data';
32-
import { Target } from '../core/target';
32+
import { generateNextTargetId, Target } from '../core/target';
3333

3434
export class MemoryTargetCache implements TargetCache {
3535
/**
@@ -76,8 +76,7 @@ export class MemoryTargetCache implements TargetCache {
7676
allocateTargetId(
7777
transaction: PersistenceTransaction
7878
): PersistencePromise<TargetId> {
79-
// Target IDs in persistence start at two and remain even.
80-
this.highestTargetId += 2;
79+
this.highestTargetId = generateNextTargetId(this.highestTargetId);
8180
return PersistencePromise.resolve(this.highestTargetId);
8281
}
8382

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
*/
1717

1818
import { FieldFilter, Filter, Query } from '../../../src/core/query';
19-
import { Target } from '../../../src/core/target';
19+
import {
20+
generateNextTargetId,
21+
Target,
22+
TARGET_ID_OFFSET_LOCAL_STORE,
23+
TARGET_ID_OFFSET_SYNC_ENGINE
24+
} from '../../../src/core/target';
2025
import { TargetId } from '../../../src/core/types';
2126
import {
2227
Document,
@@ -81,7 +86,7 @@ export class ClientMemoryState {
8186
activeTargets: ActiveTargetMap = {};
8287
queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
8388
limboMapping: LimboMap = {};
84-
nextLimboTarget = 1;
89+
nextLimboTargetId = TARGET_ID_OFFSET_SYNC_ENGINE;
8590

8691
constructor() {
8792
this.reset();
@@ -92,12 +97,12 @@ export class ClientMemoryState {
9297
this.queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
9398
this.limboMapping = {};
9499
this.activeTargets = {};
95-
this.nextLimboTarget = 1;
100+
this.nextLimboTargetId = TARGET_ID_OFFSET_SYNC_ENGINE;
96101
}
97102

98-
nextLimboTargetId(): TargetId {
99-
const nextLimboTargetId = this.nextLimboTarget;
100-
this.nextLimboTarget += 2;
103+
generateLimboTargetId(): TargetId {
104+
const nextLimboTargetId = this.nextLimboTargetId;
105+
this.nextLimboTargetId = generateNextTargetId(this.nextLimboTargetId);
101106
return nextLimboTargetId;
102107
}
103108

@@ -116,7 +121,7 @@ export class ClientMemoryState {
116121
class CachedTargetIdGenerator {
117122
// TODO(wuandy): rename this to targetMapping.
118123
private queryMapping = new ObjectMap<Target, TargetId>(t => t.canonicalId());
119-
private nextTargetId = 2;
124+
private nextTargetId = TARGET_ID_OFFSET_LOCAL_STORE;
120125

121126
/**
122127
* Returns a cached target ID for the provided Target, or a new ID if no
@@ -127,7 +132,7 @@ class CachedTargetIdGenerator {
127132
return this.queryMapping.get(target)!;
128133
}
129134
const targetId = this.nextTargetId;
130-
this.nextTargetId += 2;
135+
this.nextTargetId = generateNextTargetId(this.nextTargetId);
131136
this.queryMapping.set(target, targetId);
132137
return targetId;
133138
}
@@ -175,8 +180,8 @@ export class SpecBuilder {
175180
return this.currentClientState;
176181
}
177182

178-
private nextLimboTargetId(): TargetId {
179-
return this.clientState.nextLimboTargetId();
183+
private generateLimboTargetId(): TargetId {
184+
return this.clientState.generateLimboTargetId();
180185
}
181186

182187
private get queryMapping(): ObjectMap<Target, TargetId> {
@@ -463,7 +468,7 @@ export class SpecBuilder {
463468
const path = key.path.canonicalString();
464469
// Create limbo target ID mapping if it was not in limbo yet
465470
if (!objUtils.contains(this.limboMapping, path)) {
466-
this.limboMapping[path] = this.nextLimboTargetId();
471+
this.limboMapping[path] = this.generateLimboTargetId();
467472
}
468473
// Limbo doc queries are always without resume token
469474
this.addQueryToActiveTargets(

packages/firestore/test/util/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ export function byteStringFromString(value: string): ByteString {
512512
return ByteString.fromBase64String(base64);
513513
}
514514

515-
/**
515+
/**
516516
* Decodes a base 64 decoded string.
517517
*
518518
* Note that this is typed to accept Uint8Arrays to match the types used

0 commit comments

Comments
 (0)