Skip to content

Update REST and wire protocol query params for startAfter, endBefore #6706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 18, 2022
62 changes: 52 additions & 10 deletions packages/database-compat/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,34 +375,76 @@ describe('Query Tests', () => {
expect(queryId(path)).to.equal('default');

expect(queryId(path.startAt('pri', 'name'))).to.equal(
'{"sn":"name","sp":"pri"}'
'{"sin":true,"sn":"name","sp":"pri"}'
);
expect(queryId(path.startAfter('pri', 'name'))).to.equal(
'{"sn":"name-","sp":"pri"}'
'{"sin":false,"sn":"name","sp":"pri"}'
);
expect(queryId(path.endAt('pri', 'name'))).to.equal(
'{"ein":true,"en":"name","ep":"pri"}'
);
expect(queryId(path.endBefore('pri', 'name'))).to.equal(
'{"ein":false,"en":"name","ep":"pri"}'
);

expect(queryId(path.startAt('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAt('spri').endBefore('epri'))).to.equal(
'{"ein":false,"en":"[MIN_NAME]","ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sn":"[MAX_NAME]","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":false,"sn":"[MAX_NAME]","sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endBefore('epri'))).to.equal(
'{"ein":false,"en":"[MIN_NAME]","ep":"epri","sin":false,"sn":"[MAX_NAME]","sp":"spri"}'
);

expect(
queryId(path.startAt('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAt('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname-","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);

expect(queryId(path.startAt('pri').limitToFirst(100))).to.equal(
'{"l":100,"sp":"pri","vf":"l"}'
'{"l":100,"sin":true,"sp":"pri","vf":"l"}'
);
expect(queryId(path.startAfter('pri').limitToFirst(100))).to.equal(
'{"l":100,"sn":"[MAX_NAME]","sp":"pri","vf":"l"}'
'{"l":100,"sin":false,"sn":"[MAX_NAME]","sp":"pri","vf":"l"}'
);
expect(queryId(path.endAt('pri').limitToLast(100))).to.equal(
'{"ein":true,"ep":"pri","l":100,"vf":"r"}'
);
expect(queryId(path.endBefore('pri').limitToLast(100))).to.equal(
'{"ein":false,"en":"[MIN_NAME]","ep":"pri","l":100,"vf":"r"}'
);

expect(queryId(path.startAt('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sp":"bar"}'
'{"i":"foo","sin":true,"sp":"bar"}'
);
expect(queryId(path.startAfter('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sn":"[MAX_NAME]","sp":"bar"}'
'{"i":"foo","sin":false,"sn":"[MAX_NAME]","sp":"bar"}'
);
expect(queryId(path.endAt('bar').orderByChild('foo'))).to.equal(
'{"ein":true,"ep":"bar","i":"foo"}'
);
expect(queryId(path.endBefore('bar').orderByChild('foo'))).to.equal(
'{"ein":false,"en":"[MIN_NAME]","ep":"bar","i":"foo"}'
);
});

Expand Down
65 changes: 26 additions & 39 deletions packages/database/src/core/view/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { KEY_INDEX } from '../snap/indexes/KeyIndex';
import { PathIndex } from '../snap/indexes/PathIndex';
import { PRIORITY_INDEX, PriorityIndex } from '../snap/indexes/PriorityIndex';
import { VALUE_INDEX } from '../snap/indexes/ValueIndex';
import { predecessor, successor } from '../util/NextPushId';
import { MAX_NAME, MIN_NAME } from '../util/util';

import { IndexedFilter } from './filter/IndexedFilter';
Expand All @@ -36,8 +35,10 @@ import { RangedFilter } from './filter/RangedFilter';
const enum WIRE_PROTOCOL_CONSTANTS {
INDEX_START_VALUE = 'sp',
INDEX_START_NAME = 'sn',
INDEX_START_IS_INCLUSIVE = 'sin',
INDEX_END_VALUE = 'ep',
INDEX_END_NAME = 'en',
INDEX_END_IS_INCLUSIVE = 'ein',
LIMIT = 'l',
VIEW_FROM = 'vf',
VIEW_FROM_LEFT = 'l',
Expand All @@ -53,8 +54,10 @@ const enum REST_QUERY_CONSTANTS {
PRIORITY_INDEX = '$priority',
VALUE_INDEX = '$value',
KEY_INDEX = '$key',
START_AFTER = 'startAfter',
START_AT = 'startAt',
END_AT = 'endAt',
END_BEFORE = 'endBefore',
LIMIT_TO_FIRST = 'limitToFirst',
LIMIT_TO_LAST = 'limitToLast'
}
Expand All @@ -70,10 +73,10 @@ export class QueryParams {
limitSet_ = false;
startSet_ = false;
startNameSet_ = false;
startAfterSet_ = false;
startAfterSet_ = false; // can only be true if startSet_ is true
endSet_ = false;
endNameSet_ = false;
endBeforeSet_ = false;
endBeforeSet_ = false; // can only be true if endSet_ is true
limit_ = 0;
viewFrom_ = '';
indexStartValue_: unknown | null = null;
Expand All @@ -86,14 +89,6 @@ export class QueryParams {
return this.startSet_;
}

hasStartAfter(): boolean {
return this.startAfterSet_;
}

hasEndBefore(): boolean {
return this.endBeforeSet_;
}

/**
* @returns True if it would return from left.
*/
Expand Down Expand Up @@ -191,10 +186,12 @@ export class QueryParams {
copy.limitSet_ = this.limitSet_;
copy.limit_ = this.limit_;
copy.startSet_ = this.startSet_;
copy.startAfterSet_ = this.startAfterSet_;
copy.indexStartValue_ = this.indexStartValue_;
copy.startNameSet_ = this.startNameSet_;
copy.indexStartName_ = this.indexStartName_;
copy.endSet_ = this.endSet_;
copy.endBeforeSet_ = this.endBeforeSet_;
copy.indexEndValue_ = this.indexEndValue_;
copy.endNameSet_ = this.endNameSet_;
copy.indexEndName_ = this.indexEndName_;
Expand Down Expand Up @@ -274,19 +271,10 @@ export function queryParamsStartAfter(
key?: string | null
): QueryParams {
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = successor(indexValue as string);
}
if (queryParams.index_ === KEY_INDEX || !!key) {
params = queryParamsStartAt(queryParams, indexValue, key);
} else {
let childKey: string;
if (key == null) {
childKey = MAX_NAME;
} else {
childKey = successor(key);
}
params = queryParamsStartAt(queryParams, indexValue, childKey);
params = queryParamsStartAt(queryParams, indexValue, MAX_NAME);
}
params.startAfterSet_ = true;
return params;
Expand Down Expand Up @@ -318,20 +306,11 @@ export function queryParamsEndBefore(
indexValue: unknown,
key?: string | null
): QueryParams {
let childKey: string;
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = predecessor(indexValue as string);
}
if (queryParams.index_ === KEY_INDEX || !!key) {
params = queryParamsEndAt(queryParams, indexValue, key);
} else {
if (key == null) {
childKey = MIN_NAME;
} else {
childKey = predecessor(key);
}
params = queryParamsEndAt(queryParams, indexValue, childKey);
params = queryParamsEndAt(queryParams, indexValue, MIN_NAME);
}
params.endBeforeSet_ = true;
return params;
Expand Down Expand Up @@ -374,18 +353,22 @@ export function queryParamsToRestQueryStringParameters(
qs[REST_QUERY_CONSTANTS.ORDER_BY] = stringify(orderBy);

if (queryParams.startSet_) {
qs[REST_QUERY_CONSTANTS.START_AT] = stringify(queryParams.indexStartValue_);
const startParam = queryParams.startAfterSet_
? REST_QUERY_CONSTANTS.START_AFTER
: REST_QUERY_CONSTANTS.START_AT;
qs[startParam] = stringify(queryParams.indexStartValue_);
if (queryParams.startNameSet_) {
qs[REST_QUERY_CONSTANTS.START_AT] +=
',' + stringify(queryParams.indexStartName_);
qs[startParam] += ',' + stringify(queryParams.indexStartName_);
}
}

if (queryParams.endSet_) {
qs[REST_QUERY_CONSTANTS.END_AT] = stringify(queryParams.indexEndValue_);
const endParam = queryParams.endBeforeSet_
? REST_QUERY_CONSTANTS.END_BEFORE
: REST_QUERY_CONSTANTS.END_AT;
qs[endParam] = stringify(queryParams.indexEndValue_);
if (queryParams.endNameSet_) {
qs[REST_QUERY_CONSTANTS.END_AT] +=
',' + stringify(queryParams.indexEndName_);
qs[endParam] += ',' + stringify(queryParams.indexEndName_);
}
}

Expand All @@ -411,12 +394,16 @@ export function queryParamsGetQueryObject(
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_START_NAME] =
queryParams.indexStartName_;
}
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_START_IS_INCLUSIVE] =
!queryParams.startAfterSet_;
}
if (queryParams.endSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_VALUE] = queryParams.indexEndValue_;
if (queryParams.endNameSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_NAME] = queryParams.indexEndName_;
}
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_IS_INCLUSIVE] =
!queryParams.endBeforeSet_;
}
if (queryParams.limitSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.LIMIT] = queryParams.limit_;
Expand Down
17 changes: 13 additions & 4 deletions packages/database/src/core/view/filter/RangedFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ export class RangedFilter implements NodeFilter {

private endPost_: NamedNode;

private startIsInclusive_: boolean;

private endIsInclusive_: boolean;

constructor(params: QueryParams) {
this.indexedFilter_ = new IndexedFilter(params.getIndex());
this.index_ = params.getIndex();
this.startPost_ = RangedFilter.getStartPost_(params);
this.endPost_ = RangedFilter.getEndPost_(params);
this.startIsInclusive_ = !params.startAfterSet_;
this.endIsInclusive_ = !params.endBeforeSet_;
}

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

matches(node: NamedNode): boolean {
return (
this.index_.compare(this.getStartPost(), node) <= 0 &&
this.index_.compare(node, this.getEndPost()) <= 0
);
const isWithinStart = this.startIsInclusive_
? this.index_.compare(this.getStartPost(), node) <= 0
: this.index_.compare(this.getStartPost(), node) < 0;
const isWithinEnd = this.endIsInclusive_
? this.index_.compare(node, this.getEndPost()) <= 0
: this.index_.compare(node, this.getEndPost()) < 0;
return isWithinStart && isWithinEnd;
}
updateChild(
snap: Node,
Expand Down
92 changes: 92 additions & 0 deletions packages/database/test/helpers/syncPointSpec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4179,6 +4179,98 @@
}
]
},
{
"name": "Queries with startAfter and endBefore work",
"steps":
[
{
"type": "listen",
"path": "",
"params": {
"tag": 1,
"orderBy": "index",
"startAfter": { "index": 1 },
"endBefore": { "index": 10 }
},
"events": []
},
{
".comment": "update from server sends all data",
"type": "serverUpdate",
"path": "",
"data": {
"a": { "index": 0, "value": "a" },
"b": { "index": 2, "value": "b" },
"c": { "index": 9, "value": "c" },
"d": { "index": 11, "value": "d" }
},
"events": [
{
"path": "",
"type": "child_added",
"name": "b",
"prevName": null,
"data": { "index": 2, "value": "b" }
},
{
"path": "",
"type": "child_added",
"name": "c",
"prevName": null,
"data": { "index": 9, "value": "c" }
},
{
"path": "",
"type": "value",
"data": {
"b": { "index": 2, "value": "b" },
"c": { "index": 9, "value": "c" }
}
}
]
},
{
".comment": "update from server to move child c out of query",
"type": "serverUpdate",
"path": "c/index",
"data": 10,
"events": [
{
"path": "",
"type": "child_removed",
"name": "c",
"data": { "index": 9, "value": "c" }
},
{
"path": "",
"type": "value",
"data": {
"b": { "index": 2, "value": "b" }
}
}
]
},
{
".comment": "update from server to move child b out of window",
"type": "serverUpdate",
"path": "b/index",
"data": 1,
"events": [
{
"path": "",
"type": "child_removed",
"name": "b",
"data": { "index": 2, "value": "b" }
},
{
"path": "",
"type": "value",
"data": {}
}
]
}
]
},
{
"name": "Update to single child that moves out of window",
"steps":
Expand Down
Loading