Skip to content

Commit 28911b5

Browse files
committed
Extend syncpoint spec protocol
1 parent 4e3106c commit 28911b5

File tree

4 files changed

+125
-16
lines changed

4 files changed

+125
-16
lines changed

packages/database/src/core/view/QueryParams.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export class QueryParams {
7373
limitSet_ = false;
7474
startSet_ = false;
7575
startNameSet_ = false;
76-
startAfterSet_ = false;
76+
startAfterSet_ = false; // can only be true if startSet_ is true
7777
endSet_ = false;
7878
endNameSet_ = false;
79-
endBeforeSet_ = false;
79+
endBeforeSet_ = false; // can only be true if endSet_ is true
8080
limit_ = 0;
8181
viewFrom_ = '';
8282
indexStartValue_: unknown | null = null;
@@ -89,14 +89,6 @@ export class QueryParams {
8989
return this.startSet_;
9090
}
9191

92-
hasStartAfter(): boolean {
93-
return this.startAfterSet_;
94-
}
95-
96-
hasEndBefore(): boolean {
97-
return this.endBeforeSet_;
98-
}
99-
10092
/**
10193
* @returns True if it would return from left.
10294
*/
@@ -278,7 +270,12 @@ export function queryParamsStartAfter(
278270
indexValue: unknown,
279271
key?: string | null
280272
): QueryParams {
281-
const params: QueryParams = queryParamsStartAt(queryParams, indexValue, key);
273+
let params: QueryParams;
274+
if (queryParams.index_ === KEY_INDEX || !!key) {
275+
params = queryParamsStartAt(queryParams, indexValue, key);
276+
} else {
277+
params = queryParamsStartAt(queryParams, indexValue, MAX_NAME);
278+
}
282279
params.startAfterSet_ = true;
283280
return params;
284281
}
@@ -309,7 +306,12 @@ export function queryParamsEndBefore(
309306
indexValue: unknown,
310307
key?: string | null
311308
): QueryParams {
312-
const params: QueryParams = queryParamsEndAt(queryParams, indexValue, key);
309+
let params: QueryParams;
310+
if (queryParams.index_ === KEY_INDEX || !!key) {
311+
params = queryParamsEndAt(queryParams, indexValue, key);
312+
} else {
313+
params = queryParamsEndAt(queryParams, indexValue, MIN_NAME);
314+
}
313315
params.endBeforeSet_ = true;
314316
return params;
315317
}

packages/database/src/core/view/filter/RangedFilter.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ export class RangedFilter implements NodeFilter {
3939

4040
private endPost_: NamedNode;
4141

42+
private startIsInclusive_: boolean;
43+
44+
private endIsInclusive_: boolean;
45+
4246
constructor(params: QueryParams) {
4347
this.indexedFilter_ = new IndexedFilter(params.getIndex());
4448
this.index_ = params.getIndex();
4549
this.startPost_ = RangedFilter.getStartPost_(params);
4650
this.endPost_ = RangedFilter.getEndPost_(params);
51+
this.startIsInclusive_ = !params.startAfterSet_;
52+
this.endIsInclusive_ = !params.endBeforeSet_;
4753
}
4854

4955
getStartPost(): NamedNode {
@@ -55,10 +61,13 @@ export class RangedFilter implements NodeFilter {
5561
}
5662

5763
matches(node: NamedNode): boolean {
58-
return (
59-
this.index_.compare(this.getStartPost(), node) <= 0 &&
60-
this.index_.compare(node, this.getEndPost()) <= 0
61-
);
64+
const isWithinStart = this.startIsInclusive_
65+
? this.index_.compare(this.getStartPost(), node) <= 0
66+
: this.index_.compare(this.getStartPost(), node) < 0;
67+
const isWithinEnd = this.endIsInclusive_
68+
? this.index_.compare(node, this.getEndPost()) <= 0
69+
: this.index_.compare(node, this.getEndPost()) < 0;
70+
return isWithinStart && isWithinEnd;
6271
}
6372
updateChild(
6473
snap: Node,

packages/database/test/helpers/syncPointSpec.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4179,6 +4179,98 @@
41794179
}
41804180
]
41814181
},
4182+
{
4183+
"name": "Queries with startAfter and endBefore work",
4184+
"steps":
4185+
[
4186+
{
4187+
"type": "listen",
4188+
"path": "",
4189+
"params": {
4190+
"tag": 1,
4191+
"orderBy": "index",
4192+
"startAfter": { "index": 1 },
4193+
"endBefore": { "index": 10 }
4194+
},
4195+
"events": []
4196+
},
4197+
{
4198+
".comment": "update from server sends all data",
4199+
"type": "serverUpdate",
4200+
"path": "",
4201+
"data": {
4202+
"a": { "index": 0, "value": "a" },
4203+
"b": { "index": 2, "value": "b" },
4204+
"c": { "index": 9, "value": "c" },
4205+
"d": { "index": 11, "value": "d" }
4206+
},
4207+
"events": [
4208+
{
4209+
"path": "",
4210+
"type": "child_added",
4211+
"name": "b",
4212+
"prevName": null,
4213+
"data": { "index": 2, "value": "b" }
4214+
},
4215+
{
4216+
"path": "",
4217+
"type": "child_added",
4218+
"name": "c",
4219+
"prevName": null,
4220+
"data": { "index": 9, "value": "c" }
4221+
},
4222+
{
4223+
"path": "",
4224+
"type": "value",
4225+
"data": {
4226+
"b": { "index": 2, "value": "b" },
4227+
"c": { "index": 9, "value": "c" }
4228+
}
4229+
}
4230+
]
4231+
},
4232+
{
4233+
".comment": "update from server to move child c out of query",
4234+
"type": "serverUpdate",
4235+
"path": "c/index",
4236+
"data": 10,
4237+
"events": [
4238+
{
4239+
"path": "",
4240+
"type": "child_removed",
4241+
"name": "c",
4242+
"data": { "index": 9, "value": "c" }
4243+
},
4244+
{
4245+
"path": "",
4246+
"type": "value",
4247+
"data": {
4248+
"b": { "index": 2, "value": "b" }
4249+
}
4250+
}
4251+
]
4252+
},
4253+
{
4254+
".comment": "update from server to move child b out of window",
4255+
"type": "serverUpdate",
4256+
"path": "b/index",
4257+
"data": 1,
4258+
"events": [
4259+
{
4260+
"path": "",
4261+
"type": "child_removed",
4262+
"name": "b",
4263+
"data": { "index": 2, "value": "b" }
4264+
},
4265+
{
4266+
"path": "",
4267+
"type": "value",
4268+
"data": {}
4269+
}
4270+
]
4271+
}
4272+
]
4273+
},
41824274
{
41834275
"name": "Update to single child that moves out of window",
41844276
"steps":

packages/database/test/helpers/syncpoint-util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import {
3131
ref,
3232
limitToFirst,
3333
limitToLast,
34+
startAfter,
3435
startAt,
3536
equalTo,
3637
endAt,
38+
endBefore,
3739
orderByChild,
3840
orderByKey,
3941
orderByPriority
@@ -366,10 +368,14 @@ export class SyncPointTestParser {
366368
q = query(q, limitToFirst(paramValue));
367369
} else if (paramName === 'limitToLast') {
368370
q = query(q, limitToLast(paramValue));
371+
} else if (paramName === 'startAfter') {
372+
q = query(q, startAfter(paramValue.index, paramValue.name));
369373
} else if (paramName === 'startAt') {
370374
q = query(q, startAt(paramValue.index, paramValue.name));
371375
} else if (paramName === 'endAt') {
372376
q = query(q, endAt(paramValue.index, paramValue.name));
377+
} else if (paramName === 'endBefore') {
378+
q = query(q, endBefore(paramValue.index, paramValue.name));
373379
} else if (paramName === 'equalTo') {
374380
q = query(q, equalTo(paramValue.index, paramValue.name));
375381
} else if (paramName === 'orderBy') {

0 commit comments

Comments
 (0)