@@ -226,38 +226,41 @@ export class Query {
226
226
) ;
227
227
}
228
228
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
+ */
232
236
canonicalId ( ) : string {
233
- const query = this . convertLimitToFirstIfNecessary ( ) ;
234
237
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 ;
238
241
}
239
242
canonicalId += '|f:' ;
240
- for ( const filter of query . filters ) {
243
+ for ( const filter of this . filters ) {
241
244
canonicalId += filter . canonicalId ( ) ;
242
245
canonicalId += ',' ;
243
246
}
244
247
canonicalId += '|ob:' ;
245
248
// TODO(dimond): make this collision resistant
246
- for ( const orderBy of query . orderBy ) {
249
+ for ( const orderBy of this . orderBy ) {
247
250
canonicalId += orderBy . canonicalId ( ) ;
248
251
canonicalId += ',' ;
249
252
}
250
- if ( ! isNullOrUndefined ( query . limit ) ) {
253
+ if ( ! isNullOrUndefined ( this . limit ) ) {
251
254
canonicalId += '|l:' ;
252
- canonicalId += query . limit ! ;
255
+ canonicalId += this . limit ! ;
253
256
}
254
- if ( query . startAt ) {
257
+ if ( this . startAt ) {
255
258
canonicalId += '|lb:' ;
256
- canonicalId += query . startAt . canonicalId ( ) ;
259
+ canonicalId += this . startAt . canonicalId ( ) ;
257
260
}
258
- if ( query . endAt ) {
261
+ if ( this . endAt ) {
259
262
canonicalId += '|ub:' ;
260
- canonicalId += query . endAt . canonicalId ( ) ;
263
+ canonicalId += this . endAt . canonicalId ( ) ;
261
264
}
262
265
this . memoizedCanonicalId = canonicalId ;
263
266
}
@@ -419,7 +422,7 @@ export class Query {
419
422
* Converts limitToLast queries to limitToFirst queries since the serializer
420
423
* doesn't support them.
421
424
*/
422
- convertLimitToFirstIfNecessary ( ) : Query {
425
+ private convertLimitToFirstIfNecessary ( ) : Query {
423
426
if ( this . limitType === LimitType . First ) {
424
427
return this ;
425
428
} else {
0 commit comments