From e6edca0403d44d0167fa59e16c5bec7f43c7d741 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 05:17:59 +0800 Subject: [PATCH] refactor(views): reuse indexed load-image paging (#15628) --- .../analysisView/NativeLoadImageViews.test.ts | 24 +++++++++ .../analysisView/nativeLoadImageView.ts | 36 +++++--------- src/domain/analysisView/selectViewRows.ts | 49 +++++-------------- 3 files changed, 50 insertions(+), 59 deletions(-) diff --git a/src/application/analysisView/NativeLoadImageViews.test.ts b/src/application/analysisView/NativeLoadImageViews.test.ts index 952034690e8..dc62575e2a4 100644 --- a/src/application/analysisView/NativeLoadImageViews.test.ts +++ b/src/application/analysisView/NativeLoadImageViews.test.ts @@ -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]); +}); diff --git a/src/domain/analysisView/nativeLoadImageView.ts b/src/domain/analysisView/nativeLoadImageView.ts index cfa3bb58c39..3f65f6cc2ab 100644 --- a/src/domain/analysisView/nativeLoadImageView.ts +++ b/src/domain/analysisView/nativeLoadImageView.ts @@ -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. */ diff --git a/src/domain/analysisView/selectViewRows.ts b/src/domain/analysisView/selectViewRows.ts index d43def2e15b..f43cd1c017b 100644 --- a/src/domain/analysisView/selectViewRows.ts +++ b/src/domain/analysisView/selectViewRows.ts @@ -23,25 +23,24 @@ export const selectViewRows = ( project: (row: Source, index: number) => Row, candidateIndices?: ReadonlySet, ): { 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 = ( return { rows: selected, total }; }; -/** Select only from an explicit set of candidate indices in ascending order. */ -const selectIndexedViewRows = ( - rows: readonly Source[], - candidateIndices: ReadonlySet, - 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: Row,