pager->nPages === 0) return []; $this->tree(1, null); foreach ($this->schema->tables as $t) { if (($t['root'] ?? 0) > 1) $this->tree($t['root'], null); } foreach ($this->schema->indexes as $idx) $this->tree($idx['root'], $idx); $this->freelist(); $lock = intdiv(0x40000000, $this->pager->pageSize) + 1; for ($p = 1; $p <= $this->pager->nPages; $p++) { if (!isset($this->used[$p]) && $p !== $lock) $this->msgs[] = "ページ{$p}は使われていません"; } if (empty($this->msgs)) $this->indexes(); return array_slice($this->msgs, 0, 100); } private function mark(int $p, string $what): bool { if ($p < 1 || $p > $this->pager->nPages) { $this->msgs[] = "{$what}:不正なページ番号{$p}"; return false; } if (isset($this->used[$p])) { $this->msgs[] = "ページ{$p}が2回使われています({$this->used[$p]}、{$what})"; return false; } $this->used[$p] = $what; return true; } private function tree(int $root, ?array $idx): void { $depths = []; $this->page($root, $idx, null, null, 0, $depths, $root); if (count(array_unique($depths)) > 1) $this->msgs[] = "ツリー{$root}:葉の深さが違います"; } private function page(int $pgno, ?array $idx, mixed $lo, mixed $hi, int $depth, array &$depths, int $root): void { if (!$this->mark($pgno, "ツリー{$root}")) return; try { $p = $this->bt->page($pgno); } catch (\Throwable $e) { $this->msgs[] = "ページ{$pgno}:".$e->getMessage(); return; } if ($p->isTable() !== ($idx === null)) { $this->msgs[] = "ページ{$pgno}:ツリーの種類が違います"; return; } $n = count($p->cells); if ($n === 0 && $pgno !== $root && $p->isLeaf()) $this->msgs[] = "ページ{$pgno}:空の葉"; $prev = $lo; for ($i = 0; $i <= $n; $i++) { $key = null; if ($i < $n) { try { $key = $p->isTable() ? $p->keys[$i] : $this->bt->key($p, $i); } catch (\Throwable $e) { $this->msgs[] = "ページ{$pgno}のセル{$i}:".$e->getMessage(); continue; } $this->overflow($p, $i, $root); } if (!$p->isLeaf()) $this->page($i < $n ? $p->left[$i] : $p->right, $idx, $prev, $i < $n ? $key : $hi, $depth + 1, $depths, $root); if ($i === $n) break; if ($p->isTable()) { if ($prev !== null && ($p->isLeaf() ? $key <= $prev : $key < $prev)) $this->msgs[] = "ページ{$pgno}のセル{$i}:rowidの順番"; if ($hi !== null && $key > $hi) $this->msgs[] = "ページ{$pgno}のセル{$i}:rowidが親より大きい"; } else { if ($prev !== null && Record::compareKeys($key, $prev, $idx['colls'], $idx['desc']) <= 0) $this->msgs[] = "ページ{$pgno}のセル{$i}:キーの順番"; if ($hi !== null && Record::compareKeys($key, $hi, $idx['colls'], $idx['desc']) >= 0) $this->msgs[] = "ページ{$pgno}のセル{$i}:キーが親より大きい"; } $prev = $key; } if ($p->isLeaf()) $depths[] = $depth; } private function overflow(Page $p, int $i, int $root): void { if (!$p->isLeaf() && $p->isTable()) return; $c = $p->cells[$i]; $pos = 0; $len = Record::getVarint($c, $pos); if ($p->isTable()) Record::getVarint($c, $pos); $local = $this->bt->localSize($len, $p->isTable()); if ($len <= $local) return; $next = unpack('N', $c, $pos + $local)[1]; $need = intdiv($len - $local + $this->pager->usable - 5, $this->pager->usable - 4); for ($k = 0; $k < $need; $k++) { if (!$this->mark($next, "オーバーフロー(ツリー{$root})")) return; $next = unpack('N', $this->pager->read($next))[1]; } if ($next !== 0) $this->msgs[] = "ツリー{$root}:オーバーフローが長過ぎます"; } private function freelist(): void { $t = $this->pager->hdr['freeTrunk']; $count = 0; while ($t !== 0) { if (!$this->mark($t, 'フリーリスト')) return; $count++; $raw = $this->pager->read($t); $n = unpack('N', $raw, 4)[1]; if ($n > intdiv($this->pager->usable, 4) - 2) { $this->msgs[] = "フリーリストのページ{$t}:数が不正です"; return; } for ($i = 0; $i < $n; $i++) { $this->mark(unpack('N', $raw, 8 + $i * 4)[1], 'フリーリスト'); $count++; } $t = unpack('N', $raw)[1]; } if ($count !== $this->pager->hdr['freeCount']) $this->msgs[] = "フリーリストのページ数:{$count}(ヘッダー:{$this->pager->hdr['freeCount']})"; } /** インデックスとテーブルの行が合っているか */ private function indexes(): void { foreach ($this->schema->indexes as $idx) { $t = $this->schema->tables[$idx['table']] ?? null; if (!$t || isset($t['unsupported'])) continue; $n = 0; foreach ($this->bt->indexScan($idx) as $key) { $n++; $rowid = end($key); $rec = $this->bt->get($t['root'], $rowid); if ($rec === null) { $this->msgs[] = "インデックス{$idx['name']}:行{$rowid}がテーブルにありません"; continue; } $vals = Record::decode($rec); foreach ($idx['cols'] as $k => $ci) { $v = $ci === $t['ipk'] ? $rowid : ($vals[$ci] ?? null); if (Record::compare($v, $key[$k], 'BINARY') !== 0 && $vals[$ci] !== null) { $this->msgs[] = "インデックス{$idx['name']}:行{$rowid}の値が違います"; break; } } } $rows = $this->bt->count($t['root']); if ($n !== $rows) $this->msgs[] = "インデックス{$idx['name']}:{$n}行(テーブルは{$rows}行)"; } } }