Skip to content

Commit 6cbd7ed

Browse files
Merge 8ec3ba9 into 23e8971
2 parents 23e8971 + 8ec3ba9 commit 6cbd7ed

File tree

4 files changed

+123
-94
lines changed

4 files changed

+123
-94
lines changed

packages/firestore/lite/test/dependencies.json

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@
7171
"assertUint8ArrayAvailable",
7272
"binaryStringFromUint8Array",
7373
"blobEquals",
74+
"boundEquals",
7475
"canonicalId",
7576
"canonifyArray",
77+
"canonifyBound",
7678
"canonifyByteString",
7779
"canonifyGeoPoint",
7880
"canonifyMap",
@@ -151,6 +153,8 @@
151153
"primitiveComparator",
152154
"refValue",
153155
"registerFirestore",
156+
"sortsBeforeDocument",
157+
"stringifyTarget",
154158
"targetEquals",
155159
"timestampEquals",
156160
"toBytes",
@@ -238,7 +242,7 @@
238242
],
239243
"variables": []
240244
},
241-
"sizeInBytes": 97396
245+
"sizeInBytes": 97991
242246
},
243247
"DocumentReference": {
244248
"dependencies": {
@@ -774,7 +778,7 @@
774778
],
775779
"variables": []
776780
},
777-
"sizeInBytes": 88429
781+
"sizeInBytes": 87202
778782
},
779783
"QueryDocumentSnapshot": {
780784
"dependencies": {
@@ -1274,8 +1278,10 @@
12741278
"assertUint8ArrayAvailable",
12751279
"binaryStringFromUint8Array",
12761280
"blobEquals",
1281+
"boundEquals",
12771282
"canonicalId",
12781283
"canonifyArray",
1284+
"canonifyBound",
12791285
"canonifyByteString",
12801286
"canonifyGeoPoint",
12811287
"canonifyMap",
@@ -1363,6 +1369,8 @@
13631369
"refValue",
13641370
"registerFirestore",
13651371
"serverTimestamp",
1372+
"sortsBeforeDocument",
1373+
"stringifyTarget",
13661374
"targetEquals",
13671375
"timestampEquals",
13681376
"toBytes",
@@ -1466,7 +1474,7 @@
14661474
],
14671475
"variables": []
14681476
},
1469-
"sizeInBytes": 107674
1477+
"sizeInBytes": 108269
14701478
},
14711479
"arrayRemove": {
14721480
"dependencies": {
@@ -1697,8 +1705,10 @@
16971705
"assertUint8ArrayAvailable",
16981706
"binaryStringFromUint8Array",
16991707
"blobEquals",
1708+
"boundEquals",
17001709
"canonicalId",
17011710
"canonifyArray",
1711+
"canonifyBound",
17021712
"canonifyByteString",
17031713
"canonifyGeoPoint",
17041714
"canonifyMap",
@@ -1778,6 +1788,8 @@
17781788
"primitiveComparator",
17791789
"refValue",
17801790
"registerFirestore",
1791+
"sortsBeforeDocument",
1792+
"stringifyTarget",
17811793
"targetEquals",
17821794
"timestampEquals",
17831795
"toBytes",
@@ -1866,7 +1878,7 @@
18661878
],
18671879
"variables": []
18681880
},
1869-
"sizeInBytes": 98024
1881+
"sizeInBytes": 98619
18701882
},
18711883
"collectionGroup": {
18721884
"dependencies": {
@@ -1877,8 +1889,10 @@
18771889
"assertUint8ArrayAvailable",
18781890
"binaryStringFromUint8Array",
18791891
"blobEquals",
1892+
"boundEquals",
18801893
"canonicalId",
18811894
"canonifyArray",
1895+
"canonifyBound",
18821896
"canonifyByteString",
18831897
"canonifyGeoPoint",
18841898
"canonifyMap",
@@ -1958,6 +1972,8 @@
19581972
"primitiveComparator",
19591973
"refValue",
19601974
"registerFirestore",
1975+
"sortsBeforeDocument",
1976+
"stringifyTarget",
19611977
"targetEquals",
19621978
"timestampEquals",
19631979
"toBytes",
@@ -2044,7 +2060,7 @@
20442060
],
20452061
"variables": []
20462062
},
2047-
"sizeInBytes": 97455
2063+
"sizeInBytes": 98050
20482064
},
20492065
"deleteDoc": {
20502066
"dependencies": {
@@ -2235,8 +2251,10 @@
22352251
"assertUint8ArrayAvailable",
22362252
"binaryStringFromUint8Array",
22372253
"blobEquals",
2254+
"boundEquals",
22382255
"canonicalId",
22392256
"canonifyArray",
2257+
"canonifyBound",
22402258
"canonifyByteString",
22412259
"canonifyGeoPoint",
22422260
"canonifyMap",
@@ -2317,6 +2335,8 @@
23172335
"randomBytes",
23182336
"refValue",
23192337
"registerFirestore",
2338+
"sortsBeforeDocument",
2339+
"stringifyTarget",
23202340
"targetEquals",
23212341
"timestampEquals",
23222342
"toBytes",
@@ -2406,7 +2426,7 @@
24062426
],
24072427
"variables": []
24082428
},
2409-
"sizeInBytes": 98845
2429+
"sizeInBytes": 99440
24102430
},
24112431
"documentId": {
24122432
"dependencies": {
@@ -2828,7 +2848,7 @@
28282848
],
28292849
"variables": []
28302850
},
2831-
"sizeInBytes": 94239
2851+
"sizeInBytes": 93121
28322852
},
28332853
"increment": {
28342854
"dependencies": {
@@ -3005,8 +3025,10 @@
30053025
"assertUint8ArrayAvailable",
30063026
"binaryStringFromUint8Array",
30073027
"blobEquals",
3028+
"boundEquals",
30083029
"canonicalId",
30093030
"canonifyArray",
3031+
"canonifyBound",
30103032
"canonifyByteString",
30113033
"canonifyGeoPoint",
30123034
"canonifyMap",
@@ -3086,6 +3108,8 @@
30863108
"primitiveComparator",
30873109
"refValue",
30883110
"registerFirestore",
3111+
"sortsBeforeDocument",
3112+
"stringifyTarget",
30893113
"targetEquals",
30903114
"timestampEquals",
30913115
"toBytes",
@@ -3173,7 +3197,7 @@
31733197
],
31743198
"variables": []
31753199
},
3176-
"sizeInBytes": 97739
3200+
"sizeInBytes": 98334
31773201
},
31783202
"queryEqual": {
31793203
"dependencies": {
@@ -3342,7 +3366,7 @@
33423366
],
33433367
"variables": []
33443368
},
3345-
"sizeInBytes": 88633
3369+
"sizeInBytes": 87406
33463370
},
33473371
"refEqual": {
33483372
"dependencies": {
@@ -3353,8 +3377,10 @@
33533377
"assertUint8ArrayAvailable",
33543378
"binaryStringFromUint8Array",
33553379
"blobEquals",
3380+
"boundEquals",
33563381
"canonicalId",
33573382
"canonifyArray",
3383+
"canonifyBound",
33583384
"canonifyByteString",
33593385
"canonifyGeoPoint",
33603386
"canonifyMap",
@@ -3434,6 +3460,8 @@
34343460
"refEqual",
34353461
"refValue",
34363462
"registerFirestore",
3463+
"sortsBeforeDocument",
3464+
"stringifyTarget",
34373465
"targetEquals",
34383466
"timestampEquals",
34393467
"toBytes",
@@ -3521,7 +3549,7 @@
35213549
],
35223550
"variables": []
35233551
},
3524-
"sizeInBytes": 97681
3552+
"sizeInBytes": 98276
35253553
},
35263554
"runTransaction": {
35273555
"dependencies": {
@@ -4142,7 +4170,7 @@
41424170
],
41434171
"variables": []
41444172
},
4145-
"sizeInBytes": 89366
4173+
"sizeInBytes": 88139
41464174
},
41474175
"terminate": {
41484176
"dependencies": {

packages/firestore/src/core/query.ts

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ export class Query {
452452
* Makes sure a document is within the bounds, if provided.
453453
*/
454454
private matchesBounds(doc: Document): boolean {
455-
if (this.startAt && !this.startAt.sortsBeforeDocument(this.orderBy, doc)) {
455+
if (this.startAt && !sortsBeforeDocument(this.startAt, this.orderBy, doc)) {
456456
return false;
457457
}
458-
if (this.endAt && this.endAt.sortsBeforeDocument(this.orderBy, doc)) {
458+
if (this.endAt && sortsBeforeDocument(this.endAt, this.orderBy, doc)) {
459459
return false;
460460
}
461461
return true;
@@ -732,73 +732,80 @@ export const enum Direction {
732732
*/
733733
export class Bound {
734734
constructor(readonly position: api.Value[], readonly before: boolean) {}
735+
}
735736

736-
canonicalId(): string {
737-
// TODO(b/29183165): Make this collision robust.
738-
return `${this.before ? 'b' : 'a'}:${this.position
739-
.map(p => canonicalId(p))
740-
.join(',')}`;
741-
}
737+
export function canonifyBound(bound: Bound): string {
738+
// TODO(b/29183165): Make this collision robust.
739+
return `${bound.before ? 'b' : 'a'}:${bound.position
740+
.map(p => canonicalId(p))
741+
.join(',')}`;
742+
}
742743

743-
/**
744-
* Returns true if a document sorts before a bound using the provided sort
745-
* order.
746-
*/
747-
sortsBeforeDocument(orderBy: OrderBy[], doc: Document): boolean {
748-
debugAssert(
749-
this.position.length <= orderBy.length,
750-
"Bound has more components than query's orderBy"
751-
);
752-
let comparison = 0;
753-
for (let i = 0; i < this.position.length; i++) {
754-
const orderByComponent = orderBy[i];
755-
const component = this.position[i];
756-
if (orderByComponent.field.isKeyField()) {
757-
debugAssert(
758-
isReferenceValue(component),
759-
'Bound has a non-key value where the key path is being used.'
760-
);
761-
comparison = DocumentKey.comparator(
762-
DocumentKey.fromName(component.referenceValue),
763-
doc.key
764-
);
765-
} else {
766-
const docValue = doc.field(orderByComponent.field);
767-
debugAssert(
768-
docValue !== null,
769-
'Field should exist since document matched the orderBy already.'
770-
);
771-
comparison = valueCompare(component, docValue);
772-
}
773-
if (orderByComponent.dir === Direction.DESCENDING) {
774-
comparison = comparison * -1;
775-
}
776-
if (comparison !== 0) {
777-
break;
778-
}
744+
/**
745+
* Returns true if a document sorts before a bound using the provided sort
746+
* order.
747+
*/
748+
export function sortsBeforeDocument(
749+
bound: Bound,
750+
orderBy: OrderBy[],
751+
doc: Document
752+
): boolean {
753+
debugAssert(
754+
bound.position.length <= orderBy.length,
755+
"Bound has more components than query's orderBy"
756+
);
757+
let comparison = 0;
758+
for (let i = 0; i < bound.position.length; i++) {
759+
const orderByComponent = orderBy[i];
760+
const component = bound.position[i];
761+
if (orderByComponent.field.isKeyField()) {
762+
debugAssert(
763+
isReferenceValue(component),
764+
'Bound has a non-key value where the key path is being used.'
765+
);
766+
comparison = DocumentKey.comparator(
767+
DocumentKey.fromName(component.referenceValue),
768+
doc.key
769+
);
770+
} else {
771+
const docValue = doc.field(orderByComponent.field);
772+
debugAssert(
773+
docValue !== null,
774+
'Field should exist since document matched the orderBy already.'
775+
);
776+
comparison = valueCompare(component, docValue);
777+
}
778+
if (orderByComponent.dir === Direction.DESCENDING) {
779+
comparison = comparison * -1;
780+
}
781+
if (comparison !== 0) {
782+
break;
779783
}
780-
return this.before ? comparison <= 0 : comparison < 0;
781784
}
785+
return bound.before ? comparison <= 0 : comparison < 0;
786+
}
782787

783-
isEqual(other: Bound | null): boolean {
784-
if (other === null) {
785-
return false;
786-
}
787-
if (
788-
this.before !== other.before ||
789-
this.position.length !== other.position.length
790-
) {
788+
export function boundEquals(left: Bound | null, right: Bound | null): boolean {
789+
if (left === null) {
790+
return right === null;
791+
} else if (right === null) {
792+
return false;
793+
}
794+
795+
if (
796+
left.before !== right.before ||
797+
left.position.length !== right.position.length
798+
) {
799+
return false;
800+
}
801+
for (let i = 0; i < left.position.length; i++) {
802+
const leftPosition = left.position[i];
803+
const rightPosition = right.position[i];
804+
if (!valueEquals(leftPosition, rightPosition)) {
791805
return false;
792806
}
793-
for (let i = 0; i < this.position.length; i++) {
794-
const thisPosition = this.position[i];
795-
const otherPosition = other.position[i];
796-
if (!valueEquals(thisPosition, otherPosition)) {
797-
return false;
798-
}
799-
}
800-
return true;
801807
}
808+
return true;
802809
}
803810

804811
/**

0 commit comments

Comments
 (0)