/** * AriaEngine ANALYZE / VACUUM / REINDEX + EXPLAIN 测试 * 全部 MemoryBackend,零卡死 */ import { AriaEngine } from '../../src/engine/aria/index'; import { createSchema } from '../../src/table/schema'; import { QueryExecutor } from '../../src/query/executor'; import { parse } from '../../src/sql/parser'; describe('AriaEngine — ANALYZE/VACUUM/REINDEX + EXPLAIN', () => { let engine: AriaEngine; beforeEach(async () => { engine = new AriaEngine({ storageBackend: 'memory' }); await engine.open('maint-test', 1); await engine.createTable(createSchema('users', { id: { type: 'string', primaryKey: true }, name: { type: 'string', index: true }, age: { type: 'number', default: 0 }, })); await engine.insert('users', [ { id: '1', name: 'Alice', age: 30 }, { id: '2', name: 'Bob', age: 25 }, { id: '3', name: 'Charlie', age: 35 }, ]); }); afterEach(async () => { await engine.close(); }); // ---- ANALYZE ---- it('ANALYZE 返回表统计信息', async () => { const stats = await (engine as any).analyzeTable('users'); expect(stats.table).toBe('users'); expect(stats.rowCount).toBe(3); expect(stats.avgRowSize).toBeGreaterThan(0); expect(stats.indexDepth).toBeGreaterThanOrEqual(0); expect(stats.sstableCount).toBeGreaterThanOrEqual(0); }); it('ANALYZE 包含列基数统计', async () => { const stats = await (engine as any).analyzeTable('users'); expect(stats.columnStats).toBeDefined(); expect(stats.columnStats.name.distinctValues).toBe(3); expect(stats.columnStats.age.distinctValues).toBe(3); }); it('ANALYZE 空表返回 rowCount=0', async () => { await engine.createTable(createSchema('empty', { id: { type: 'string', primaryKey: true } })); const stats = await (engine as any).analyzeTable('empty'); expect(stats.rowCount).toBe(0); }); // ---- REINDEX ---- it('REINDEX 重建索引后查询正常', async () => { const count = await (engine as any).reindexTable('users'); expect(count).toBeGreaterThanOrEqual(1); const rows = await engine.find('users', { table: 'users', where: { name: 'Alice' } }); expect(rows).toHaveLength(1); }); it('REINDEX 不存在的表抛出错误', async () => { await expect((engine as any).reindexTable('ghost')).rejects.toThrow('does not exist'); }); // ---- VACUUM ---- it('VACUUM 返回压缩统计', async () => { const result = await (engine as any).vacuum(); expect(result).toHaveProperty('compactedLevels'); expect(result).toHaveProperty('gcVersions'); }); // ---- EXPLAIN ---- it('EXPLAIN SELECT 输出查询计划', async () => { const executor = new QueryExecutor(engine); const selectStmt = parse('SELECT * FROM users WHERE id = \'1\''); const explainStmt: any = { type: 'EXPLAIN', query: selectStmt }; const plan = await executor.execute(explainStmt); expect(plan.type).toBe('SELECT'); expect(plan.table).toBe('users'); expect(plan.usingIndex).toBeDefined(); expect(plan.actualTimeMs).toBeGreaterThanOrEqual(0); }); // ---- 查询优化器 ---- it('estimateQueryCost PK 等值 → pk_lookup', () => { const cost = (engine as any).estimateQueryCost('users', { table: 'users', where: { id: '1' } }); expect(cost.strategy).toBe('pk_lookup'); expect(cost.estimatedRows).toBe(1); }); it('estimateQueryCost 索引等值 → index_eq', () => { const cost = (engine as any).estimateQueryCost('users', { table: 'users', where: { name: 'Alice' } }); expect(cost.strategy).toBe('index_eq:name'); }); it('estimateQueryCost 无索引 → full_scan', () => { const cost = (engine as any).estimateQueryCost('users', { table: 'users', where: { age: { $gt: 20 } } }); expect(cost.strategy).toMatch(/index_range|full_scan/); }); // ---- 在线备份 ---- it('backup 导出全库一致性快照', async () => { const backup = await (engine as any).backup(); expect(backup.users).toHaveLength(3); expect(backup.users[0]).toHaveProperty('id'); expect(backup.users[0]).toHaveProperty('name'); }); });