refactor(views): share oversized singleton paging (#16832)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 04:07:34 +08:00
1 parent bbc2266465
commit 0ad9672589
4 files changed
+62 -88

No files matched your search

+9 -38
View File
@@ -1,8 +1,4 @@
import type { JsonValue } from "../jsonValue.js";
import {
PLACEHOLDER_COVERAGE,
selectByteBoundedViewRows,
} from "./byteBoundedViewRows.js";
import { selectViewPageWithSingleRowFallback } from "./byteBoundedViewRows.js";
import { viewParentIdentity } from "./viewParentIdentity.js";
import { z } from "zod";
import {
@@ -25,7 +21,6 @@ import {
boundedViewLimitations,
boundedViewMessages,
completeWithinViewCoverage,
pageViewCoverage,
} from "./analysisViewCoverage.js";
import { invalidAnalysisView } from "./analysisViewErrors.js";
import {
@@ -41,7 +36,7 @@ import {
analysisTextPage,
analysisTextPageItemSchema,
} from "./analysisViewText.js";
import { selectViewRows, filterSet } from "./selectViewRows.js";
import { filterSet } from "./selectViewRows.js";
const OPERATION = "batch_decompile" as const;
@@ -372,39 +367,15 @@ export const projectBatchDecompileView = (
...shared,
...context,
};
const page = selectByteBoundedViewRows(
batch.items,
view,
{
matches,
project: projectItem,
candidateIndices: indices,
},
{
parentEvidenceId: parent.evidenceId,
envelope: {
...envelope,
items: [],
coverage: PLACEHOLDER_COVERAGE,
} as unknown as JsonValue,
facet: "batch decompile item",
},
);
// A body may exceed the view allowance yet fit the transport alone.
// Keep the transport's existing single-body recovery if it cannot fit.
const { rows, total } = page.ok
? page.value
: selectViewRows(
batch.items,
{ ...view, limit: 1 },
matches,
projectItem,
indices,
);
return ok({
...envelope,
items: rows,
coverage: pageViewCoverage(view.offset, rows.length, total),
...selectViewPageWithSingleRowFallback(
batch.items,
view,
{ matches, project: projectItem, candidateIndices: indices },
envelope,
"batch decompile item",
),
});
}
const item = batch.items[view.index];
+41 -11
View File
@@ -10,8 +10,8 @@ import {
viewPageBytesWithDigest,
WIDEST_VIEW_PAGE_COVERAGE,
} from "./analysisViewPageBudget.js";
import { pageViewCoverage } from "./analysisViewCoverage.js";
import { orderedViewRowIndices } from "./selectViewRows.js";
import { pageViewCoverage, selectedViewPage } from "./analysisViewCoverage.js";
import { orderedViewRowIndices, selectViewRows } from "./selectViewRows.js";
/** These row pages reserve only the page status and full-width counters. */
export const PLACEHOLDER_COVERAGE = {
@@ -50,6 +50,12 @@ export const byteBoundedItemsPageOwner = (
facet,
});
type ByteBoundedRowSelection<Source, Row> = {
readonly matches: ((row: Source, index: number) => boolean) | undefined;
readonly project: (row: Source, index: number) => Row;
readonly candidateIndices?: ReadonlySet<number> | undefined;
};
/**
* Select rows from `offset` up to `limit` that fit the analysis-view page
* bytes beside `owner.envelope`. A row that does not fit ends the page with
@@ -61,15 +67,7 @@ export const byteBoundedItemsPageOwner = (
export const selectByteBoundedViewRows = <Source, Row>(
rows: readonly Source[],
view: { readonly offset: number; readonly limit: number },
{
matches,
project,
candidateIndices,
}: {
readonly matches: ((row: Source, index: number) => boolean) | undefined;
readonly project: (row: Source, index: number) => Row;
readonly candidateIndices?: ReadonlySet<number> | undefined;
},
{ matches, project, candidateIndices }: ByteBoundedRowSelection<Source, Row>,
owner: ByteBoundedPageOwner,
): Result<ByteBoundedRows<Row>, AnalysisResourceConstraintError> => {
const tooLarge = () =>
@@ -126,6 +124,38 @@ export const selectByteBoundedViewRows = <Source, Row>(
return ok({ rows: selected, total, byte_limited: byteLimited });
};
/**
* When the first row exceeds the view allowance, leave its delivery to the
* transport. Preserve the original match total and source indices.
*/
export const selectViewPageWithSingleRowFallback = <Source, Row>(
rows: readonly Source[],
view: { readonly offset: number; readonly limit: number },
selection: ByteBoundedRowSelection<Source, Row>,
context: { readonly parent_evidence_id: string },
facet: string,
) => {
const page = selectByteBoundedViewRows(rows, view, selection, {
parentEvidenceId: context.parent_evidence_id,
envelope: {
...context,
items: [],
coverage: PLACEHOLDER_COVERAGE,
} as unknown as JsonValue,
facet,
});
const selected = page.ok
? page.value
: selectViewRows(
rows,
{ ...view, limit: 1 },
selection.matches,
selection.project,
selection.candidateIndices,
);
return selectedViewPage(view.offset, selected);
};
/** Page one named facet with the enclosing result included in its byte budget. */
export const createByteBoundedFacetSelector =
<Envelope extends { readonly parent_evidence_id: string }>(
+8 -35
View File
@@ -1,10 +1,6 @@
import { viewParentIdentity } from "./viewParentIdentity.js";
import { indexedRow, selectViewRows } from "./selectViewRows.js";
import type { JsonValue } from "../jsonValue.js";
import {
PLACEHOLDER_COVERAGE,
selectByteBoundedViewRows,
} from "./byteBoundedViewRows.js";
import { selectViewPageWithSingleRowFallback } from "./byteBoundedViewRows.js";
import { z } from "zod";
import { devicetreeSchema } from "../firmware/devicetree.js";
import { ok } from "../result.js";
@@ -19,7 +15,6 @@ import { createAnalysisViewResultReader } from "./analysisViewParent.js";
import {
boundedViewLimitations,
completeWithinViewCoverage,
pageViewCoverage,
selectedViewPage,
} from "./analysisViewCoverage.js";
@@ -137,39 +132,17 @@ export const projectDevicetreeView = (
view,
devicetree: { ...context, alias_count: r.summary.aliases.length },
};
if (view.collection === "properties" && node !== undefined) {
const page = selectByteBoundedViewRows(
node.properties,
view,
{ matches: undefined, project: indexedRow },
{
parentEvidenceId: parent.evidenceId,
envelope: {
...base,
items: [],
coverage: PLACEHOLDER_COVERAGE,
} as unknown as JsonValue,
facet: "devicetree property",
},
);
// Keep individually deliverable properties available even when they exceed
// the view allowance. The adapter applies its own limit to this singleton.
const first = node.properties[view.offset];
const items = page.ok
? page.value.rows
: first === undefined
? []
: [indexedRow(first, view.offset)];
if (view.collection === "properties" && node !== undefined)
return ok({
...base,
items,
coverage: pageViewCoverage(
view.offset,
items.length,
node.properties.length,
...selectViewPageWithSingleRowFallback(
node.properties,
view,
{ matches: undefined, project: indexedRow },
base,
"devicetree property",
),
});
}
const selected =
view.collection === "nodes"
? selectViewRows(
@@ -1028,8 +1028,8 @@ it.each([
it("visits only selected indices while continuing byte-limited batch pages", async () => {
const reads: number[][] = [];
const select = byteBoundedRows.selectByteBoundedViewRows;
const observeSelection: typeof select = (rows, view, selection, owner) => {
const select = byteBoundedRows.selectViewPageWithSingleRowFallback;
const observeSelection: typeof select = (rows, ...args) => {
const visited: number[] = [];
reads.push(visited);
const observed = new Proxy(rows, {
@@ -1039,10 +1039,10 @@ it("visits only selected indices while continuing byte-limited batch pages", asy
return Reflect.get(target, property, receiver);
},
});
return select(observed, view, selection, owner);
return select(observed, ...args);
};
const spy = vi
.spyOn(byteBoundedRows, "selectByteBoundedViewRows")
.spyOn(byteBoundedRows, "selectViewPageWithSingleRowFallback")
.mockImplementation(observeSelection);
onTestFinished(() => spy.mockRestore());
const items = Array.from({ length: 100 }, (_, index) => ({