/** * The blind index one way (coordinator for the owner, 22 Nov 2026: his search index was 0.55 GB of `blind` plus * 0.83 GB of `blind_rid`, 34.6 million tokens). `blind (tok, rid)` answers a search by token; `blind_rid` existed * only to find a row's tokens again when the row changes or goes. Each row now keeps its own tokens beside it, in * `blind_row.toks` (8 bytes a token, big-endian signed, sorted), and a row's tokens go one by one along the primary * key. Once every row holds its own, `blind_rid` is dropped. * * The move is per account (`blind_oneway`, `PUT /v1/admin/business`): off until set, then a step an alarm * (`moveStep`), resumable where it stopped (the rows whose `toks` is still null). A search answers the same before, * during and after: `blind` itself is never touched by the move. */ /** The column a row's own tokens live in, laid on an object made before it (`ready`). */ export function ready(sql: SqlStorage): void { const has = sql.exec<{ name: string }>("PRAGMA table_info(blind_row)").toArray().some((c) => c.name === "toks"); if (!has) sql.exec("ALTER TABLE blind_row ADD COLUMN toks BLOB"); } /** Tokens (decimal text of signed 64-bit numbers) packed sorted, 8 bytes each. */ export function pack(tokens: string[]): Uint8Array { const sorted = [...new Set(tokens)].map((t) => BigInt(t)).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)); const out = new Uint8Array(sorted.length * 8); const view = new DataView(out.buffer); sorted.forEach((t, i) => view.setBigInt64(i * 8, t)); return out; } /** A packed row's tokens, as decimal text. */ export function unpack(toks: ArrayBuffer | Uint8Array): string[] { const bytes = toks instanceof Uint8Array ? toks : new Uint8Array(toks); const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); const out: string[] = []; for (let i = 0; i + 8 <= bytes.byteLength; i += 8) out.push(view.getBigInt64(i).toString()); return out; } /** * A row's tokens out of `blind`: along the primary key from its own `toks`, or by `blind_rid` while it has none yet * (a row the move has not reached). Answers how many went. */ export function dropTokens(sql: SqlStorage, rid: number): number { const held = sql.exec<{ toks: ArrayBuffer | null }>("SELECT toks FROM blind_row WHERE rid = ?", rid).toArray()[0]; if (!held) return 0; if (held.toks === null) return sql.exec("DELETE FROM blind WHERE rid = ? RETURNING tok", rid).toArray().length; let gone = 0; for (const tok of unpack(held.toks)) gone += sql.exec("DELETE FROM blind WHERE tok = CAST(? AS INTEGER) AND rid = ? RETURNING tok", tok, rid).toArray().length; return gone; } /** Whether `blind_rid` is still there: the move is not done (cheap, read on every alarm). */ export function indexed(sql: SqlStorage): boolean { return sql.exec("SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = 'blind_rid'").toArray().length > 0; } /** How far the move is: rows still holding no tokens of their own, and whether `blind_rid` is still there. */ export function progress(sql: SqlStorage): { left: number; index: boolean } { const left = Number(sql.exec<{ n: number }>("SELECT COUNT(*) AS n FROM blind_row WHERE toks IS NULL").one().n); const index = sql.exec("SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = 'blind_rid'").toArray().length > 0; return { left, index }; } /** * The rows a move step reads (a sealed email holds about 360 tokens). 2,000 a step took up to 28.7 s of an alarm at * his size on the loaded box, too near the 30 s limit; 300 is about a seventh of that. */ export const MOVE_ROWS = 300; /** * One step of the move: up to `most` rows not yet holding their tokens get them, read along `blind_rid`; once none * is left, `blind_rid` is dropped. Answers whether more is left. */ export function moveStep(sql: SqlStorage, most = MOVE_ROWS): boolean { const rids = sql.exec<{ rid: number }>("SELECT rid FROM blind_row WHERE toks IS NULL LIMIT ?", most).toArray(); for (const { rid } of rids) { const tokens = sql.exec<{ tok: string }>("SELECT CAST(tok AS TEXT) AS tok FROM blind WHERE rid = ?", rid).toArray().map((r) => r.tok); sql.exec("UPDATE blind_row SET toks = ? WHERE rid = ?", pack(tokens), rid); } if (rids.length === most) return true; sql.exec("DROP INDEX IF EXISTS blind_rid"); return false; }