/** * An invented object of the personal object's shape (21 Nov 2026: about * 800,000 rows, most of them in a few big tables of sealed rows with large * bodies, tombstones among them, and many small tables), written straight * into `rows` a few thousand at a time, so a test can watch what a statement * costs on an object this size. The normal suite builds it at a scale that * proves the method; `BIG_OBJECT=1` (an env var the pool passes as a binding) * builds it at full size. */ import { env } from "cloudflare:test"; export const BIG = (env as unknown as { BIG_OBJECT?: string }).BIG_OBJECT === "1"; export interface Shape { table: string; rows: number; /** The date column the table's rows carry, if any. */ dated?: string; /** Bytes of the sealed body each row carries. */ body: number; /** One row in this many is a tombstone. */ deadEvery: number; } /** The personal object's shape, scaled: `scale` 1 is about 800,000 rows. */ export function personalShape(scale: number): Shape[] { const n = (rows: number): number => Math.max(1, Math.round(rows * scale)); const shape: Shape[] = [ { table: "message", rows: n(300_000), dated: "sent_at", body: 1200, deadEvery: 12 }, { table: "email", rows: n(150_000), dated: "received_at", body: 3000, deadEvery: 20 }, { table: "photo", rows: n(120_000), dated: "taken_at", body: 400, deadEvery: 30 }, { table: "person_handle", rows: n(80_000), body: 300, deadEvery: 50 }, { table: "message_contact_handle", rows: n(60_000), body: 120, deadEvery: 40 }, { table: "event", rows: n(40_000), dated: "starts_at", body: 500, deadEvery: 10 }, { table: "thread", rows: n(20_000), body: 200, deadEvery: 25 }, ]; for (let i = 0; i < 25; i++) shape.push({ table: `small_${String(i).padStart(2, "0")}`, rows: n(1_200), dated: i % 3 === 0 ? "on" : undefined, body: 300, deadEvery: 7 }); return shape; } /** Write `shape` into the object's `rows` (versions 1… in order), returning the last version. */ export function writeShape(sql: SqlStorage, shape: Shape[]): number { let version = 0; for (const t of shape) { for (let start = 1; start <= t.rows; start += 10_000) { const end = Math.min(t.rows, start + 9_999); const dated = t.dated ? `'${t.dated}', printf('20%02d-%02d-%02dT08:00:00Z', 20 + x % 7, 1 + x % 12, 1 + x % 28), ` : ""; sql.exec( `WITH RECURSIVE c(x) AS (SELECT ? UNION ALL SELECT x + 1 FROM c WHERE x < ?) INSERT INTO rows (table_id, id, data, owner, version, deleted, created_at, updated_at) SELECT ?, printf('%s-%08d', ?, x), CASE WHEN x % ? = 0 THEN '{}' ELSE json_object(${dated}'thread_id', printf('t%05d', x % 3000), 'sealed_enc', 's1:' || hex(randomblob(?)), 'dek_enc', 'd1:' || hex(randomblob(30)), 'seal_v', 1) END, NULL, ? + x, x % ? = 0, '2025-01-01T00:00:00Z', '2025-06-01T00:00:00Z' FROM c`, start, end, t.table, t.table, t.deadEvery, Math.ceil(t.body / 2), version, t.deadEvery, ); } version += t.rows; } return version; } /** Each statement run through `sql` while `run` runs, with the rows it read (SqlStorageCursor.rowsRead, read once the cursor is drained). */ export function watchStatements(sql: SqlStorage, run: () => T): { result: T; statements: { query: string; args: SqlStorageValue[]; rowsRead: number }[] } { const statements: { query: string; args: SqlStorageValue[]; cursor: SqlStorageCursor> }[] = []; const exec = sql.exec.bind(sql); (sql as unknown as { exec: typeof sql.exec }).exec = ((query: string, ...args: SqlStorageValue[]) => { const cursor = exec(query, ...args); statements.push({ query, args, cursor }); return cursor; }) as typeof sql.exec; try { const result = run(); return { result, statements: statements.map((s) => ({ query: s.query, args: s.args, rowsRead: s.cursor.rowsRead })) }; } finally { (sql as unknown as { exec: typeof sql.exec }).exec = exec; } } /** The statements among `statements` that read `rows` and sort or group in a temporary B-tree (EXPLAIN QUERY PLAN), with their plans. */ export function sortsOfRows(sql: SqlStorage, statements: { query: string; args: SqlStorageValue[] }[]): string[] { const sorted: string[] = []; for (const s of statements) { if (!/\bFROM rows\b/.test(s.query) || !/^\s*SELECT/i.test(s.query)) continue; const plan = sql.exec<{ detail: string }>(`EXPLAIN QUERY PLAN ${s.query}`, ...s.args).toArray().map((r) => r.detail); if (plan.some((d) => /TEMP B-TREE/.test(d))) sorted.push(`${s.query.replace(/\s+/g, " ").slice(0, 160)} :: ${plan.join(" | ")}`); } return sorted; }