Skip to content

Commit c153684

Browse files
committed
add back the utf-8 string comparison
1 parent f598475 commit c153684

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,9 @@ export function dbKeyComparator(l: DocumentKey, r: DocumentKey): number {
655655
return cmp;
656656
}
657657

658+
// TODO(b/329441702): Document IDs should be sorted by UTF-8 encoded byte
659+
// order, but IndexedDB sorts strings lexicographically. Document ID
660+
// comparison here still relies on primitive comparison to avoid mismatches
661+
// observed in snapshot listeners with Unicode characters in documentIds
658662
return primitiveComparator(left[left.length - 1], right[right.length - 1]);
659663
}

packages/firestore/src/model/path.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Integer } from '@firebase/webchannel-wrapper/bloom-blob';
1919

2020
import { debugAssert, fail } from '../util/assert';
2121
import { Code, FirestoreError } from '../util/error';
22+
import { compareUtf8Strings, primitiveComparator } from '../util/misc';
2223

2324
export const DOCUMENT_KEY_NAME = '__name__';
2425

@@ -181,7 +182,7 @@ abstract class BasePath<B extends BasePath<B>> {
181182
return comparison;
182183
}
183184
}
184-
return Math.sign(p1.length - p2.length);
185+
return primitiveComparator(p1.length, p2.length);
185186
}
186187

187188
private static compareSegments(lhs: string, rhs: string): number {
@@ -201,13 +202,7 @@ abstract class BasePath<B extends BasePath<B>> {
201202
);
202203
} else {
203204
// both non-numeric
204-
if (lhs < rhs) {
205-
return -1;
206-
}
207-
if (lhs > rhs) {
208-
return 1;
209-
}
210-
return 0;
205+
return compareUtf8Strings(lhs, rhs);
211206
}
212207
}
213208

packages/firestore/src/model/values.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
Value
2626
} from '../protos/firestore_proto_api';
2727
import { fail } from '../util/assert';
28-
import { arrayEquals, primitiveComparator } from '../util/misc';
28+
import {
29+
arrayEquals,
30+
compareUtf8Strings,
31+
primitiveComparator
32+
} from '../util/misc';
2933
import { forEach, objectSize } from '../util/obj';
3034
import { isNegativeZero } from '../util/types';
3135

@@ -251,7 +255,7 @@ export function valueCompare(left: Value, right: Value): number {
251255
getLocalWriteTime(right)
252256
);
253257
case TypeOrder.StringValue:
254-
return primitiveComparator(left.stringValue!, right.stringValue!);
258+
return compareUtf8Strings(left.stringValue!, right.stringValue!);
255259
case TypeOrder.BlobValue:
256260
return compareBlobs(left.bytesValue!, right.bytesValue!);
257261
case TypeOrder.RefValue:
@@ -400,7 +404,7 @@ function compareMaps(left: MapValue, right: MapValue): number {
400404
rightKeys.sort();
401405

402406
for (let i = 0; i < leftKeys.length && i < rightKeys.length; ++i) {
403-
const keyCompare = primitiveComparator(leftKeys[i], rightKeys[i]);
407+
const keyCompare = compareUtf8Strings(leftKeys[i], rightKeys[i]);
404408
if (keyCompare !== 0) {
405409
return keyCompare;
406410
}

packages/firestore/src/util/misc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function compareUtf8Strings(left: string, right: string): number {
107107
}
108108

109109
// Increment by 2 for surrogate pairs, 1 otherwise
110-
i += leftCodePoint > 0xffff ? 2 : 1;
110+
i += leftCodePoint > 0xffff ? 2 : 1;
111111
}
112112

113113
// Compare lengths if all characters are equal

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2451,7 +2451,7 @@ apiDescribe('Database', persistence => {
24512451
'h': { value: '你好' },
24522452
'i': { value: '你顥' },
24532453
'j': { value: '😁' },
2454-
'k': { value: '😀' },
2454+
'k': { value: '😀' }
24552455
};
24562456

24572457
return withTestCollection(persistence, testDocs, async collectionRef => {

0 commit comments

Comments
 (0)