refactor(views): reuse indexed load-image paging (#15628)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 21:17:59 +00:00
1 parent 5b5d66dcfc
commit e6edca0403
3 files changed
+50 -59

No files matched your search

@@ -3,6 +3,7 @@ import { createEvidence } from "../../domain/evidence.js";
import { inspectAnalysisViewInputSchema } from "../../domain/analysisView/analysisViewRequest.js";
import { analysisViewResultSchema } from "../../domain/analysisView/analysisViewResult.js";
import { projectNativeLoadImageView } from "../../domain/analysisView/nativeLoadImageView.js";
import { selectViewRows } from "../../domain/analysisView/selectViewRows.js";
import type { NativeLoadImage } from "../../domain/native/nativeLoadImage.js";
import {
nativeDosLoadImageFixture,
@@ -426,3 +427,26 @@ it("keeps the segments identity facts in a summary and pages its checks", async
items: { collection: "checks", rows: [{ source_index: 0 }] },
});
});
it("reads only the requested source rows on an unfiltered indexed page", () => {
const reads: number[] = [];
const rows = [0, 1, 2, 3, 4];
for (const index of rows.slice())
Object.defineProperty(rows, index, {
get: () => {
reads.push(index);
return index;
},
});
const page = selectViewRows(
rows,
{ offset: 1, limit: 1 },
undefined,
(value, source_index) => ({ value, source_index }),
new Set([4, 1, 8, 2, -1]),
);
expect(page).toEqual({ rows: [{ value: 2, source_index: 2 }], total: 3 });
expect(reads).toEqual([2]);
});
+13 -23
View File
@@ -1,5 +1,9 @@
import { viewParentIdentity } from "./viewParentIdentity.js";
import { filterSet } from "./selectViewRows.js";
import {
filterSet,
pickSourceOrderedFields,
selectViewRows,
} from "./selectViewRows.js";
import { analysisViewPageFields } from "./analysisViewPageFields.js";
import { z } from "zod";
import {
@@ -196,30 +200,16 @@ const selectRows = (source: readonly (Row | string)[], view: PageRequest) => {
return {
source_index,
record:
fields === undefined
? record
: Object.fromEntries(
Object.entries(record).filter(([key]) => fields.has(key)),
),
fields === undefined ? record : pickSourceOrderedFields(record, fields),
};
};
if (view.indices === undefined)
return {
total: source.length,
rows: source
.slice(view.offset, view.offset + view.limit)
.map((value, index) => project(value, view.offset + index)),
};
const indices = [...new Set(view.indices)]
.filter((index) => index < source.length)
.sort((left, right) => left - right);
return {
total: indices.length,
// The format owner validated a dense collection; these indices are in range.
rows: indices
.slice(view.offset, view.offset + view.limit)
.map((index) => project(source[index]!, index)),
};
return selectViewRows(
source,
view,
undefined,
project,
filterSet(view.indices),
);
};
/** Query the owner's parsed observations without reopening the target. */
+13 -36
View File
@@ -23,25 +23,24 @@ export const selectViewRows = <Source, Row>(
project: (row: Source, index: number) => Row,
candidateIndices?: ReadonlySet<number>,
): { rows: Row[]; total: number } => {
if (candidateIndices !== undefined)
return selectIndexedViewRows(
rows,
candidateIndices,
view,
matches,
project,
);
const ordered =
candidateIndices === undefined
? undefined
: orderedViewRowIndices(rows.length, candidateIndices);
const count = ordered?.length ?? rows.length;
const selected: Row[] = [];
if (matches === undefined) {
const start = Math.max(0, view.offset);
const end = Math.min(rows.length, start + Math.max(0, view.limit));
const selected: Row[] = [];
for (let index = start; index < end; index += 1)
const end = Math.min(count, start + Math.max(0, view.limit));
for (let position = start; position < end; position += 1) {
const index = ordered?.[position] ?? position;
selected.push(project(rows[index]!, index));
return { rows: selected, total: rows.length };
}
return { rows: selected, total: count };
}
const selected: Row[] = [];
let total = 0;
for (let index = 0; index < rows.length; index += 1) {
for (let position = 0; position < count; position += 1) {
const index = ordered?.[position] ?? position;
const row = rows[index]!;
if (!matches(row, index)) continue;
if (total >= view.offset && selected.length < view.limit)
@@ -51,28 +50,6 @@ export const selectViewRows = <Source, Row>(
return { rows: selected, total };
};
/** Select only from an explicit set of candidate indices in ascending order. */
const selectIndexedViewRows = <Source, Row>(
rows: readonly Source[],
candidateIndices: ReadonlySet<number>,
view: { readonly offset: number; readonly limit: number },
matches: ((row: Source, index: number) => boolean) | undefined,
project: (row: Source, index: number) => Row,
): { rows: Row[]; total: number } => {
const ordered = orderedViewRowIndices(rows.length, candidateIndices);
const selected: Row[] = [];
let total = 0;
for (let position = 0; position < ordered.length; position += 1) {
const index = ordered[position]!;
const row = rows[index]!;
if (matches !== undefined && !matches(row, index)) continue;
if (total >= view.offset && selected.length < view.limit)
selected.push(project(row, index));
total++;
}
return { rows: selected, total };
};
/** Copy only own properties named in `fields` without allocating `Object.entries(row)`. */
export const pickFields = <Row extends Record<string, unknown>>(
row: Row,