mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 19:09:21 +00:00
A retained decode_android_resources result could only be read back whole.
It carries the full manifest text and every returned string, so on VLC
3.7.2 it was 180,996 bytes.
The android-resources view pages one retained decode. Facet strings lists
{locale, name, value} rows across the decoded tables, selected by locales
(null is the default table) and matched by exact names or name prefixes;
coverage.total counts every match, and each page carries the selected
tables' own counts, pages and coverage. A locale the decode did not return
is refused with the decoded tables named. Facet manifest pages the manifest
text by UTF-16 offset.
Pages stop at the analysis-view page bytes through a shared
selectByteBoundedViewRows: a row that does not fit ends the page with
byte_limited and next_offset after the last returned row, and a row that
cannot fit alone is refused naming the retained parent. Unfiltered pages
visit only their own rows, and the flattened string rows are built once
per retained decode. A decode recorded with include_strings false refuses
the strings facet instead of reporting an empty table.
The summary keeps the client, target, metadata, locales, decoded file
count and coverage, each table's counts and page without its strings, and
the manifest length without its text.
Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
import type { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
|
import type { JsonValue } from "../jsonValue.js";
|
|
import { jsonValueByteLength } from "../jsonValueByteLength.js";
|
|
import { ok, type Result } from "../result.js";
|
|
import {
|
|
ANALYSIS_VIEW_PAGE_BYTES as PAGE_BYTES,
|
|
analysisViewPageTooLarge,
|
|
} from "./analysisViewErrors.js";
|
|
|
|
/** Bytes the view digest adds after projection. */
|
|
const DIGEST_BYTES = jsonValueByteLength({ view_digest: "0".repeat(64) }) - 1;
|
|
|
|
/** The widest page coverage, for measuring a page envelope before its rows. */
|
|
export const PLACEHOLDER_COVERAGE = {
|
|
status: "page",
|
|
examined: Number.MAX_SAFE_INTEGER,
|
|
total: Number.MAX_SAFE_INTEGER,
|
|
next_offset: Number.MAX_SAFE_INTEGER,
|
|
exhausted: false,
|
|
} as const;
|
|
|
|
/** A page of rows, the number of matching rows, and whether bytes ended it early. */
|
|
export interface ByteBoundedRows<Row> {
|
|
readonly rows: Row[];
|
|
readonly total: number;
|
|
/** Rows after these fit `limit` but not the analysis-view page bytes. */
|
|
readonly byte_limited: boolean;
|
|
}
|
|
|
|
/** How a byte-bounded page names itself when one row cannot fit. */
|
|
export interface ByteBoundedPageOwner {
|
|
readonly parentEvidenceId: string;
|
|
/** The page result without its rows, measured once as the page's fixed cost. */
|
|
readonly envelope: JsonValue;
|
|
readonly facet: string;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* byte_limited, so the next offset continues after the last returned row; a
|
|
* first row that cannot fit alone is refused, naming the parent. Without
|
|
* `matches`, only the page's rows are visited, and `project` runs only on
|
|
* rows the page returns.
|
|
*/
|
|
export const selectByteBoundedViewRows = <Source, Row>(
|
|
rows: readonly Source[],
|
|
view: { readonly offset: number; readonly limit: number },
|
|
{
|
|
matches,
|
|
project,
|
|
}: {
|
|
readonly matches: ((row: Source, index: number) => boolean) | undefined;
|
|
readonly project: (row: Source, index: number) => Row;
|
|
},
|
|
owner: ByteBoundedPageOwner,
|
|
): Result<ByteBoundedRows<Row>, AnalysisResourceConstraintError> => {
|
|
const tooLarge = () =>
|
|
analysisViewPageTooLarge(
|
|
owner.parentEvidenceId,
|
|
`One ${owner.facet} row exceeds the analysis-view page bytes on its own. Complete parent Evidence remains retained; select complete detail or export the Evidence bundle to read it.`,
|
|
{ facet: owner.facet },
|
|
);
|
|
let bytes =
|
|
DIGEST_BYTES +
|
|
jsonValueByteLength(owner.envelope, PAGE_BYTES - DIGEST_BYTES);
|
|
if (bytes > PAGE_BYTES) return tooLarge();
|
|
const selected: Row[] = [];
|
|
let byteLimited = false;
|
|
/** Admit one projected row, or end the page; false refuses the page. */
|
|
const admit = (row: Row): boolean => {
|
|
const size =
|
|
jsonValueByteLength(row as unknown as JsonValue, PAGE_BYTES - bytes) +
|
|
Number(selected.length > 0);
|
|
if (bytes + size > PAGE_BYTES) {
|
|
if (selected.length === 0) return false;
|
|
byteLimited = true;
|
|
return true;
|
|
}
|
|
bytes += size;
|
|
selected.push(row);
|
|
return true;
|
|
};
|
|
if (matches === undefined) {
|
|
const start = Math.min(view.offset, rows.length);
|
|
const end = Math.min(rows.length, start + view.limit);
|
|
for (let index = start; index < end && !byteLimited; index += 1)
|
|
if (!admit(project(rows[index]!, index))) return tooLarge();
|
|
return ok({
|
|
rows: selected,
|
|
total: rows.length,
|
|
byte_limited: byteLimited,
|
|
});
|
|
}
|
|
let total = 0;
|
|
for (let index = 0; index < rows.length; index += 1) {
|
|
const row = rows[index]!;
|
|
if (!matches(row, index)) continue;
|
|
if (total >= view.offset && selected.length < view.limit && !byteLimited)
|
|
if (!admit(project(row, index))) return tooLarge();
|
|
total++;
|
|
}
|
|
return ok({ rows: selected, total, byte_limited: byteLimited });
|
|
};
|