mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
perf(environment): streamline recorded list query processing (#13840)
This commit is contained in:
1 parent
4707f45ac2
commit
e31ccfd858
3 files changed
+105
-106
No files matched your search
@@ -330,11 +330,12 @@ const listReproduces = (
|
||||
readonly whole: ReadonlySet<number>;
|
||||
readonly query: ListQuery | undefined;
|
||||
};
|
||||
readonly queried: ReadonlySet<number>;
|
||||
readonly spellings?: ClockSpellings;
|
||||
},
|
||||
record: StateRecordFacts,
|
||||
): boolean => {
|
||||
const { view, spellings } = check;
|
||||
const { view, queried, spellings } = check;
|
||||
const field = context.idField(plan.type);
|
||||
const collection = collectionPathOf(record, false);
|
||||
return context.withBody(record.ordinal, (body) => {
|
||||
@@ -344,13 +345,14 @@ const listReproduces = (
|
||||
const observed = new Map(
|
||||
items.map((item) => [scalarText(ownMember(item, field)) ?? "", item]),
|
||||
);
|
||||
const computed = view.query?.record_ordinals.includes(record.ordinal)
|
||||
? evaluateQuery(
|
||||
view.query.parameters,
|
||||
singleQueryValues(record.query) ?? new Map(),
|
||||
context.inCollection(plan.type, collection),
|
||||
)
|
||||
: undefined;
|
||||
const computed =
|
||||
view.query !== undefined && queried.has(record.ordinal)
|
||||
? evaluateQuery(
|
||||
view.query.parameters,
|
||||
singleQueryValues(record.query) ?? new Map(),
|
||||
context.inCollection(plan.type, collection),
|
||||
)
|
||||
: undefined;
|
||||
const members = view.whole.has(record.ordinal)
|
||||
? context.inCollection(plan.type, collection)
|
||||
: (computed?.page ??
|
||||
@@ -409,25 +411,27 @@ export const checkListPlan = (
|
||||
whole,
|
||||
};
|
||||
let query = listQuery(context, plan, whole);
|
||||
const queried = new Set(query?.record_ordinals);
|
||||
// A line the query explains but whose envelope or fields the state does not reproduce keeps its recorded members.
|
||||
if (query !== undefined) {
|
||||
const failing = plan.latest.filter(
|
||||
(record) =>
|
||||
query!.record_ordinals.includes(record.ordinal) &&
|
||||
!listReproduces(context, plan, { view: { ...base, query } }, record),
|
||||
);
|
||||
const kept = query.record_ordinals.filter(
|
||||
(ordinal) => !failing.some((record) => record.ordinal === ordinal),
|
||||
);
|
||||
query = kept.length === 0 ? undefined : { ...query, record_ordinals: kept };
|
||||
const check = { view: { ...base, query }, queried };
|
||||
for (const record of plan.latest)
|
||||
if (
|
||||
queried.has(record.ordinal) &&
|
||||
!listReproduces(context, plan, check, record)
|
||||
)
|
||||
queried.delete(record.ordinal);
|
||||
query =
|
||||
queried.size === 0
|
||||
? undefined
|
||||
: { ...query, record_ordinals: [...queried] };
|
||||
}
|
||||
const view = { ...base, query };
|
||||
const all = plan.latest.every((record) => whole.has(record.ordinal));
|
||||
const spellings = new ClockSpellings(context.retain);
|
||||
const check = { view, queried, spellings };
|
||||
if (
|
||||
!plan.latest.every((record) =>
|
||||
listReproduces(context, plan, { view, spellings }, record),
|
||||
)
|
||||
!plan.latest.every((record) => listReproduces(context, plan, check, record))
|
||||
)
|
||||
return recorded(plan, "list-differs");
|
||||
return {
|
||||
|
||||
@@ -93,12 +93,6 @@ describe("explaining a recorded line", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("searches the fields the term matched, ignoring case, and leaves out URLs that merely repeat it", () => {
|
||||
expect(explainLine(line("?q=TIDE", [1, 3]))).toEqual({
|
||||
q: { role: "search", fields: ["name"] },
|
||||
});
|
||||
});
|
||||
|
||||
it("sorts by the field a value names, with a direction parameter, prefix or suffix", () => {
|
||||
expect(
|
||||
explainLine(line("?orderBy=name&orderDirection=desc", [3, 1, 2, 4])),
|
||||
@@ -125,6 +119,13 @@ describe("explaining a recorded line", () => {
|
||||
"2",
|
||||
"4",
|
||||
]);
|
||||
// The collection has the sort field even when none of the matches do.
|
||||
expect(
|
||||
ids("?sort=url&shelf=b", {
|
||||
sort: { role: "sort" },
|
||||
shelf: { role: "filter", field: "shelf" },
|
||||
}),
|
||||
).toEqual(["2"]);
|
||||
});
|
||||
|
||||
it("pages by limit and offset, or by page number from the first page it explains", () => {
|
||||
@@ -144,13 +145,16 @@ describe("explaining a recorded line", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("takes a parameter that changes nothing as inert, for its recorded values only", () => {
|
||||
const explained = explainLine(line("?includeDeleted=true", [1, 2, 3, 4]));
|
||||
expect(explained).toEqual({
|
||||
includeDeleted: { role: "inert", values: ["true"] },
|
||||
});
|
||||
expect(ids("?includeDeleted=false", explained!)).toBeUndefined();
|
||||
});
|
||||
it.each(["includeDeleted", "active"])(
|
||||
"treats %s as inert for its observed value when it excludes nothing",
|
||||
(parameter) => {
|
||||
const explained = explainLine(line(`?${parameter}=true`, [1, 2, 3, 4]));
|
||||
expect(explained).toEqual({
|
||||
[parameter]: { role: "inert", values: ["true"] },
|
||||
});
|
||||
expect(ids(`?${parameter}=false`, explained!)).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
it("leaves ambiguous page conventions unknown rather than preferring the first candidate", () => {
|
||||
expect(explainLine(line("?page=100&limit=1", []))).toBeUndefined();
|
||||
@@ -187,12 +191,6 @@ describe("explaining a recorded line", () => {
|
||||
expect(explainLine(line("?offset=unknown", [1, 2, 3, 4]))).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps a filter that excludes nothing inert, as a flag", () => {
|
||||
expect(explainLine(line("?active=true", [1, 2, 3, 4]))).toEqual({
|
||||
active: { role: "inert", values: ["true"] },
|
||||
});
|
||||
});
|
||||
|
||||
it("explains nothing that the collection cannot reproduce", () => {
|
||||
expect(explainLine(line("?shelf=a", [3, 1]))).toBeUndefined();
|
||||
});
|
||||
@@ -203,18 +201,21 @@ describe("learning a list's query", () => {
|
||||
const learned = learnQuery([
|
||||
line("?shelf=a", [1, 3], 10),
|
||||
line("?shelf=b", [2], 11),
|
||||
line("?q=kelp", [4], 12),
|
||||
line("?q=TIDE", [1, 3], 12),
|
||||
line("?q=oil", [2], 13),
|
||||
line("?variant=green", [1, 2, 3, 4], 15),
|
||||
line("?variant=blue", [1, 2, 3, 4], 14),
|
||||
]);
|
||||
expect(learned).toEqual({
|
||||
parameters: {
|
||||
shelf: { role: "filter", field: "shelf" },
|
||||
// The URL on Kelp also contains TIDE. Only the names explained the recording.
|
||||
q: { role: "search", fields: ["name"] },
|
||||
variant: { role: "inert", values: ["blue", "green"] },
|
||||
},
|
||||
record_ordinals: [10, 11, 12, 13],
|
||||
record_ordinals: [10, 11, 12, 13, 15, 14],
|
||||
});
|
||||
// A value no line recorded is evaluated the same way.
|
||||
expect(ids("?q=TIDE&shelf=a", learned!.parameters)).toEqual(["1", "3"]);
|
||||
expect(ids("?q=tidew&shelf=a", learned!.parameters)).toEqual(["3"]);
|
||||
});
|
||||
|
||||
it("refuses a value a parameter cannot apply, or a parameter it never learned", () => {
|
||||
@@ -231,8 +232,6 @@ describe("learning a list's query", () => {
|
||||
size: { role: "inert", values: ["2"] },
|
||||
limit: { role: "limit" },
|
||||
});
|
||||
// Serving supplies a call's values in name order.
|
||||
expect(ids("?limit=1&size=2", learned!.parameters)).toEqual(["1"]);
|
||||
});
|
||||
|
||||
it("adopts a filter on a field named unlike its parameter only once lines with two values agree on it", () => {
|
||||
@@ -271,52 +270,47 @@ describe("queries the state can always answer", () => {
|
||||
"9007199254740992",
|
||||
])
|
||||
expect(queryTotalCount(value)).toBeUndefined();
|
||||
const view = {
|
||||
pointer: "/items",
|
||||
counters: [],
|
||||
query: { totals: ["total"] },
|
||||
};
|
||||
expect(renderQueryList(view, { items: [], total: "3" }, [], 2)).toEqual({
|
||||
items: [],
|
||||
total: "2",
|
||||
});
|
||||
expect(renderQueryList(view, { items: [], total: "03" }, [], 2)).toEqual({
|
||||
items: [],
|
||||
total: "03",
|
||||
});
|
||||
});
|
||||
|
||||
it("projects query totals only into envelopes that actually contain a known count", () => {
|
||||
const view = {
|
||||
pointer: "/items",
|
||||
counters: ["numberOfElements"],
|
||||
query: { totals: ["total"] },
|
||||
};
|
||||
const selected = [{ id: 1 }];
|
||||
expect(
|
||||
renderQueryList(view, { items: [], numberOfElements: 0 }, selected, 7),
|
||||
).toEqual({ items: selected, numberOfElements: 1 });
|
||||
expect(
|
||||
renderQueryList(view, { items: [], total: null }, selected, 7),
|
||||
).toEqual({ items: selected, total: null });
|
||||
expect(renderQueryList(view, { items: [], total: 3 }, selected, 7)).toEqual(
|
||||
{ items: selected, total: 7 },
|
||||
);
|
||||
const items = [{ id: 1 }];
|
||||
for (const [recorded, rendered] of [
|
||||
[{}, {}],
|
||||
[{ total: null }, { total: null }],
|
||||
[{ total: 3 }, { total: 7 }],
|
||||
[{ total: "3" }, { total: "7" }],
|
||||
[{ total: "03" }, { total: "03" }],
|
||||
] as const)
|
||||
expect(
|
||||
renderQueryList(
|
||||
view,
|
||||
{ ...recorded, items: [], numberOfElements: 0 },
|
||||
items,
|
||||
7,
|
||||
),
|
||||
).toEqual({ ...rendered, items, numberOfElements: 1 });
|
||||
});
|
||||
|
||||
it("sorts case-only ties in state order, ignoring case", () => {
|
||||
const named = [
|
||||
entity({ id: 10, name: "alpha" }),
|
||||
entity({ id: 11, name: "Alpha" }),
|
||||
];
|
||||
const order = (entities: readonly StateEntity[]) =>
|
||||
evaluateQuery(
|
||||
{ sort: { role: "sort" } },
|
||||
singleQueryValues("?sort=name")!,
|
||||
entities,
|
||||
)?.page.map((entry) => entry.id);
|
||||
expect(order(named)).toEqual(["10", "11"]);
|
||||
expect(order([...named].reverse())).toEqual(["11", "10"]);
|
||||
it("sorts names and preserves state order and positional evidence for case-only ties", () => {
|
||||
const first = entity({ id: 10, name: "alpha" });
|
||||
const second = entity({ id: 11, name: "Alpha" });
|
||||
const later = entity({ id: 12, name: "Beta" });
|
||||
for (const ties of [
|
||||
[first, second],
|
||||
[second, first],
|
||||
]) {
|
||||
const input = [later, ...ties];
|
||||
expect(
|
||||
evaluateQuery(
|
||||
{ sort: { role: "sort" } },
|
||||
singleQueryValues("?sort=name")!,
|
||||
input,
|
||||
),
|
||||
).toEqual({ page: [...ties, later], total: 3, positional: ties });
|
||||
expect(input).toEqual([later, ...ties]);
|
||||
}
|
||||
});
|
||||
|
||||
it("answers a sort of an emptied collection with no entities rather than giving up", () => {
|
||||
|
||||
@@ -215,7 +215,7 @@ const readCall = (
|
||||
|
||||
/** Entities in the order a sort value names, ties in state order, and those whose place state order decides: all without a sort, else those tied with a neighbour; undefined when it names no field. */
|
||||
const sorted = (
|
||||
matched: readonly StateEntity[],
|
||||
matched: StateEntity[],
|
||||
entities: readonly StateEntity[],
|
||||
call: QueryCall,
|
||||
):
|
||||
@@ -226,28 +226,26 @@ const sorted = (
|
||||
| undefined => {
|
||||
// Nothing to order: an emptied collection answers no entities rather than a recording.
|
||||
if (call.sort === undefined || matched.length === 0)
|
||||
return { entities: [...matched], positional: "all" };
|
||||
const names = [
|
||||
...new Set(entities.flatMap((entity) => Object.keys(entity.fields))),
|
||||
];
|
||||
const key = sortKey(call.sort, names);
|
||||
return { entities: matched, positional: "all" };
|
||||
const names = new Set<string>();
|
||||
for (const entity of entities)
|
||||
for (const name of Object.keys(entity.fields)) names.add(name);
|
||||
const key = sortKey(call.sort, [...names]);
|
||||
if (key === undefined) return undefined;
|
||||
const sign = (
|
||||
call.direction === undefined ? key.descending : call.direction === "desc"
|
||||
)
|
||||
? -1
|
||||
: 1;
|
||||
const ordered = matched
|
||||
.map((entity, index) => ({ entity, index }))
|
||||
.sort(
|
||||
(left, right) =>
|
||||
sign *
|
||||
ascending(
|
||||
ownMember(left.entity.fields, key.field),
|
||||
ownMember(right.entity.fields, key.field),
|
||||
) || left.index - right.index,
|
||||
)
|
||||
.map(({ entity }) => entity);
|
||||
// The filter owns this array. Stable sort retains state order at ties.
|
||||
matched.sort(
|
||||
(left, right) =>
|
||||
sign *
|
||||
ascending(
|
||||
ownMember(left.fields, key.field),
|
||||
ownMember(right.fields, key.field),
|
||||
),
|
||||
);
|
||||
const tied = (
|
||||
left: StateEntity | undefined,
|
||||
right: StateEntity | undefined,
|
||||
@@ -259,10 +257,10 @@ const sorted = (
|
||||
ownMember(right.fields, key.field),
|
||||
) === 0;
|
||||
return {
|
||||
entities: ordered,
|
||||
positional: ordered.filter(
|
||||
entities: matched,
|
||||
positional: matched.filter(
|
||||
(entity, index) =>
|
||||
tied(ordered[index - 1], entity) || tied(entity, ordered[index + 1]),
|
||||
tied(matched[index - 1], entity) || tied(entity, matched[index + 1]),
|
||||
),
|
||||
};
|
||||
};
|
||||
@@ -498,8 +496,11 @@ const mergeRoles = (
|
||||
): Record<string, QueryParameter> => {
|
||||
const roles = new Map<string, QueryParameter[]>();
|
||||
for (const parameters of explained)
|
||||
for (const [name, role] of Object.entries(parameters))
|
||||
roles.set(name, [...(roles.get(name) ?? []), role]);
|
||||
for (const [name, role] of Object.entries(parameters)) {
|
||||
const seen = roles.get(name);
|
||||
if (seen === undefined) roles.set(name, [role]);
|
||||
else seen.push(role);
|
||||
}
|
||||
// Entries become own members even for names such as `__proto__`, which assignment would treat as the prototype.
|
||||
const merged: [string, QueryParameter][] = [];
|
||||
for (const [name, seen] of roles) {
|
||||
|
||||
Reference in new issue
Block a user