@oh-my-pi/pi-utils 18.12.1 → 18.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/types/sqlite.d.ts +11 -0
- package/package.json +2 -2
- package/src/sqlite.ts +69 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [18.12.2] - 2026-10-18
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Added `readSalvageableRows()` to read the rows of a damaged SQLite table that are still readable, skipping only the damaged pages ([#15751](https://github.com/can1357/oh-my-pi/pull/15751) by [@Shadorain](https://github.com/Shadorain))
|
|
10
|
+
|
|
5
11
|
## [18.12.0] - 2026-10-17
|
|
6
12
|
|
|
7
13
|
### Added
|
package/dist/types/sqlite.d.ts
CHANGED
|
@@ -42,6 +42,17 @@ export declare function openSqliteDatabaseSync<T>(dbPath: string, initialize: (d
|
|
|
42
42
|
export declare function annotateSqliteError(error: unknown, dbPath: string): Error;
|
|
43
43
|
/** Checkpoints committed WAL frames without waiting for concurrent readers. */
|
|
44
44
|
export declare function checkpointWal(db: Database): void;
|
|
45
|
+
/**
|
|
46
|
+
* Reads every row of a rowid table that SQLite can still decode, to salvage a
|
|
47
|
+
* damaged store. A full scan aborts at the first unreadable page, so this walks
|
|
48
|
+
* rowids with indexed seeks instead: when a seek hits damage it doubles its
|
|
49
|
+
* stride past it, then bisects back to the first rowid from which seeking
|
|
50
|
+
* works again, losing only rows on the damaged pages. Cross-linked pages can
|
|
51
|
+
* still decode rows with foreign column types, so callers validate types;
|
|
52
|
+
* rows returned out of rowid order count as damage. Only corruption errors are
|
|
53
|
+
* treated as damage; anything else is thrown.
|
|
54
|
+
*/
|
|
55
|
+
export declare function readSalvageableRows<T extends object>(db: Database, table: string, columns: string): T[];
|
|
45
56
|
/**
|
|
46
57
|
* SQLite's busy result-code family — base `SQLITE_BUSY` plus the extended
|
|
47
58
|
* variants `SQLITE_BUSY_RECOVERY` (concurrent WAL recovery), `SQLITE_BUSY_SNAPSHOT`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oh-my-pi/pi-utils",
|
|
3
|
-
"version": "18.12.
|
|
3
|
+
"version": "18.12.2",
|
|
4
4
|
"description": "Shared utilities for pi packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"fmt": "oxfmt --no-error-on-unmatched-pattern 'src/**/*.{ts,tsx}' '{test,bench,examples,scripts}/**/*.ts' '*.ts'"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@oh-my-pi/pi-natives": "18.12.
|
|
60
|
+
"@oh-my-pi/pi-natives": "18.12.2"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@types/bun": "^1.3.14"
|
package/src/sqlite.ts
CHANGED
|
@@ -343,6 +343,75 @@ export function checkpointWal(db: Database): void {
|
|
|
343
343
|
db.run("PRAGMA wal_checkpoint(PASSIVE)");
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
+
/** Damaged regions one {@link readSalvageableRows} scan skips before abandoning the rest of the table. */
|
|
347
|
+
const SALVAGE_MAX_DAMAGED_REGIONS = 1000;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Reads every row of a rowid table that SQLite can still decode, to salvage a
|
|
351
|
+
* damaged store. A full scan aborts at the first unreadable page, so this walks
|
|
352
|
+
* rowids with indexed seeks instead: when a seek hits damage it doubles its
|
|
353
|
+
* stride past it, then bisects back to the first rowid from which seeking
|
|
354
|
+
* works again, losing only rows on the damaged pages. Cross-linked pages can
|
|
355
|
+
* still decode rows with foreign column types, so callers validate types;
|
|
356
|
+
* rows returned out of rowid order count as damage. Only corruption errors are
|
|
357
|
+
* treated as damage; anything else is thrown.
|
|
358
|
+
*/
|
|
359
|
+
export function readSalvageableRows<T extends object>(db: Database, table: string, columns: string): T[] {
|
|
360
|
+
type Row = T & { salvage_rowid: number };
|
|
361
|
+
using seek = db.prepare<Row, [number]>(
|
|
362
|
+
`SELECT rowid AS salvage_rowid, ${columns} FROM ${table} WHERE rowid > ? ORDER BY rowid LIMIT 1`,
|
|
363
|
+
);
|
|
364
|
+
using exact = db.prepare<Row, [number]>(`SELECT rowid AS salvage_rowid, ${columns} FROM ${table} WHERE rowid = ?`);
|
|
365
|
+
// The row found, null when there is none, or undefined when the read hit damage.
|
|
366
|
+
const probe = (after: number, at?: number): Row | null | undefined => {
|
|
367
|
+
try {
|
|
368
|
+
const row = at === undefined ? seek.get(after) : exact.get(at);
|
|
369
|
+
if (!row) return null;
|
|
370
|
+
const valid = Number.isSafeInteger(row.salvage_rowid) && row.salvage_rowid === (at ?? row.salvage_rowid);
|
|
371
|
+
return valid && row.salvage_rowid > after ? row : undefined;
|
|
372
|
+
} catch (error) {
|
|
373
|
+
if (isSqliteCorruptionError(error)) return undefined;
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const rows: T[] = [];
|
|
379
|
+
const keep = (row: Row): void => {
|
|
380
|
+
const { salvage_rowid: _, ...values } = row;
|
|
381
|
+
rows.push(values as unknown as T);
|
|
382
|
+
};
|
|
383
|
+
let after = Number.MIN_SAFE_INTEGER;
|
|
384
|
+
let damaged = 0;
|
|
385
|
+
while (after < Number.MAX_SAFE_INTEGER) {
|
|
386
|
+
const row = probe(after);
|
|
387
|
+
if (row === null) break;
|
|
388
|
+
if (row) {
|
|
389
|
+
keep(row);
|
|
390
|
+
after = row.salvage_rowid;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (++damaged > SALVAGE_MAX_DAMAGED_REGIONS) break;
|
|
394
|
+
// An end-of-table result counts as clear: the gap below it may still hold readable rows.
|
|
395
|
+
let bad = after;
|
|
396
|
+
let clear = Number.MAX_SAFE_INTEGER;
|
|
397
|
+
for (let step = 1; after + step < clear; step *= 2) {
|
|
398
|
+
if (probe(after + step) !== undefined) clear = after + step;
|
|
399
|
+
else bad = after + step;
|
|
400
|
+
}
|
|
401
|
+
while (clear - bad > 1) {
|
|
402
|
+
const mid = bad + Math.floor((clear - bad) / 2);
|
|
403
|
+
if (probe(mid) === undefined) bad = mid;
|
|
404
|
+
else clear = mid;
|
|
405
|
+
}
|
|
406
|
+
// An interior key bounds its left subtree inclusively, so seeking past the last
|
|
407
|
+
// damaged rowid descends into the damage again and `clear` itself is skipped.
|
|
408
|
+
const boundary = probe(bad, clear);
|
|
409
|
+
if (boundary) keep(boundary);
|
|
410
|
+
after = clear;
|
|
411
|
+
}
|
|
412
|
+
return rows;
|
|
413
|
+
}
|
|
414
|
+
|
|
346
415
|
/**
|
|
347
416
|
* SQLite's busy result-code family — base `SQLITE_BUSY` plus the extended
|
|
348
417
|
* variants `SQLITE_BUSY_RECOVERY` (concurrent WAL recovery), `SQLITE_BUSY_SNAPSHOT`,
|