Skip to content

Commit 8dee3b1

Browse files
committed
Addressing remaining comments
1 parent 0059aec commit 8dee3b1

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

packages/firestore/src/core/query.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,38 +226,41 @@ export class Query {
226226
);
227227
}
228228

229-
// TODO(b/29183165): This is used to get a unique string from a query to, for
230-
// example, use as a dictionary key, but the implementation is subject to
231-
// collisions. Make it collision-free.
229+
/**
230+
* Returns a "canonical ID" for the query which is a stable string representation
231+
* of the query that can be stored as a key in dictionaries or persistence tables.
232+
* It is similar to a hash code (and like a hash code it need not be collision-free),
233+
* but since it is persisted to disk, we must be careful any future changes are
234+
* backwards-compatible.
235+
*/
232236
canonicalId(): string {
233-
const query = this.convertLimitToFirstIfNecessary();
234237
if (this.memoizedCanonicalId === null) {
235-
let canonicalId = query.path.canonicalString();
236-
if (query.isCollectionGroupQuery()) {
237-
canonicalId += '|cg:' + query.collectionGroup;
238+
let canonicalId = this.path.canonicalString();
239+
if (this.isCollectionGroupQuery()) {
240+
canonicalId += '|cg:' + this.collectionGroup;
238241
}
239242
canonicalId += '|f:';
240-
for (const filter of query.filters) {
243+
for (const filter of this.filters) {
241244
canonicalId += filter.canonicalId();
242245
canonicalId += ',';
243246
}
244247
canonicalId += '|ob:';
245248
// TODO(dimond): make this collision resistant
246-
for (const orderBy of query.orderBy) {
249+
for (const orderBy of this.orderBy) {
247250
canonicalId += orderBy.canonicalId();
248251
canonicalId += ',';
249252
}
250-
if (!isNullOrUndefined(query.limit)) {
253+
if (!isNullOrUndefined(this.limit)) {
251254
canonicalId += '|l:';
252-
canonicalId += query.limit!;
255+
canonicalId += this.limit!;
253256
}
254-
if (query.startAt) {
257+
if (this.startAt) {
255258
canonicalId += '|lb:';
256-
canonicalId += query.startAt.canonicalId();
259+
canonicalId += this.startAt.canonicalId();
257260
}
258-
if (query.endAt) {
261+
if (this.endAt) {
259262
canonicalId += '|ub:';
260-
canonicalId += query.endAt.canonicalId();
263+
canonicalId += this.endAt.canonicalId();
261264
}
262265
this.memoizedCanonicalId = canonicalId;
263266
}
@@ -419,7 +422,7 @@ export class Query {
419422
* Converts limitToLast queries to limitToFirst queries since the serializer
420423
* doesn't support them.
421424
*/
422-
convertLimitToFirstIfNecessary(): Query {
425+
private convertLimitToFirstIfNecessary(): Query {
423426
if (this.limitType === LimitType.First) {
424427
return this;
425428
} else {

packages/firestore/src/remote/serializer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,6 @@ export class JsonProtoSerializer {
11071107
endAt = this.fromCursor(query.endAt);
11081108
}
11091109

1110-
// TODO(limitToLast): Persist limitType somehow? :-/
11111110
return new Query(
11121111
path,
11131112
collectionGroup,

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,12 @@ describe('Query', () => {
470470
.addFilter(filter('bar', '>', 2))
471471
.addOrderBy(orderBy('bar'));
472472

473-
const q7a = Query.atPath(path('foo')).withLimitToFirst(10);
474-
const q8a = Query.atPath(path('foo')).withLimitToLast(10);
473+
const q7a = Query.atPath(path('foo'))
474+
.withLimitToFirst(10)
475+
.toTargetQuery();
476+
const q8a = Query.atPath(path('foo'))
477+
.withLimitToLast(10)
478+
.toTargetQuery();
475479

476480
const lip1a = bound([[DOCUMENT_KEY_NAME, 'coll/foo', 'asc']], true);
477481
const lip1b = bound([[DOCUMENT_KEY_NAME, 'coll/foo', 'asc']], false);

0 commit comments

Comments
 (0)