release: v0.4.2 — 生产就绪与崩溃自愈 + 问题清单修复 + 版本迭代
This commit is contained in:
@@ -287,16 +287,15 @@ describe('[v0.3.1] WAL 批量组提交', () => {
|
||||
await db.defineTable('users', { id: { type: 'string', primaryKey: true }, name: { type: 'string' } });
|
||||
|
||||
const engine = db.getEngine() as any;
|
||||
let before = 0;
|
||||
const origRead = engine.backend.read.bind(engine.backend);
|
||||
// 统计 __wal_ 写入次数
|
||||
// v0.4.2: WAL 记录与 count 改用 writeMany 单事务原子写入(记录仍合并为 1 次落盘)
|
||||
const origWriteMany = engine.backend.writeMany.bind(engine.backend);
|
||||
const origWrite = engine.backend.write.bind(engine.backend);
|
||||
let walWrites = 0;
|
||||
engine.backend.write = async (key: string, data: ArrayBuffer) => {
|
||||
if (key.startsWith('__wal_') && !key.startsWith('__wal_count')) walWrites++;
|
||||
return origWrite(key, data);
|
||||
engine.backend.writeMany = async (entries: Record<string, ArrayBuffer>) => {
|
||||
const keys = Object.keys(entries);
|
||||
if (keys.some((k) => k.startsWith('__wal_') && !k.startsWith('__wal_count'))) walWrites++;
|
||||
return origWriteMany(entries);
|
||||
};
|
||||
void before; void origRead;
|
||||
|
||||
await db.table('users').insertMany([
|
||||
{ id: '1', name: 'A' },
|
||||
@@ -305,7 +304,8 @@ describe('[v0.3.1] WAL 批量组提交', () => {
|
||||
{ id: '4', name: 'D' },
|
||||
]);
|
||||
|
||||
expect(walWrites).toBe(1); // 4 行 1 次 WAL 写入
|
||||
expect(walWrites).toBe(1); // 4 行合并为 1 次 WAL 落盘
|
||||
void origWrite;
|
||||
const rows = await db.query('SELECT * FROM users') as Record<string, unknown>[];
|
||||
expect(rows).toHaveLength(4);
|
||||
await db.close();
|
||||
|
||||
@@ -23,8 +23,8 @@ import { createSchema } from '../src/table/schema';
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('[v0.2.5] P0-1: 版本号统一', () => {
|
||||
test('VERSION 常量为当前版本(0.4.1)', () => {
|
||||
expect(VERSION).toBe('0.4.1');
|
||||
test('VERSION 常量为当前版本(0.4.2)', () => {
|
||||
expect(VERSION).toBe('0.4.2');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -400,7 +400,7 @@ describe('[v0.3.3] P1-9: Savepoint + MVCC 一致性', () => {
|
||||
|
||||
describe('[v0.3.3] 端到端', () => {
|
||||
test('全部修复点可共存于 MetonaSqlark API', async () => {
|
||||
expect(VERSION).toBe('0.4.1');
|
||||
expect(VERSION).toBe('0.4.2');
|
||||
const db = new MetonaSqlark({ name: `e2e-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, mode: 'memory' });
|
||||
await db.init();
|
||||
await db.defineTable('users', {
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
/**
|
||||
* v0.4.2 问题清单回归测试
|
||||
* 覆盖:
|
||||
* - P0-1: SSTable 残缺数据防御(读路径越界跳过 / 打开时完整性校验 / WAL 原子写入)
|
||||
* - P0-2: IndexedDB 版本管理(VersionError 自适应重开)
|
||||
* - P0-3: version 0 归一化
|
||||
* - P1-4: WAL 写丢失(原子 append + 按 key 扫描恢复)
|
||||
* - P1-5: close 截断 WAL(不无限重放)
|
||||
* - P1-6: 引擎内部错误包装 DatabaseError
|
||||
* - P2-7: 迁移版本持久化
|
||||
* - P2-9: repair() / clearAll() 统一自愈接口
|
||||
*/
|
||||
|
||||
import { AriaEngine } from '../src/engine/aria/index';
|
||||
import { createSchema } from '../src/table/schema';
|
||||
import { SSTableBuilder } from '../src/engine/aria/index/sstable_builder';
|
||||
import { SSTableReader } from '../src/engine/aria/index/sstable';
|
||||
import { IndexedDBEngine } from '../src/engine/indexeddb';
|
||||
import { MetonaSqlark } from '../src/core';
|
||||
import { IndexedDBBackend } from '../src/engine/aria/store/backend';
|
||||
import type { SSTableMeta } from '../src/engine/aria/types';
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
let idbCounter = 0;
|
||||
function uniqueDB(): string {
|
||||
return `fix-${Date.now()}-${++idbCounter}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
const makeMeta = (data: Uint8Array): SSTableMeta => ({
|
||||
id: 1, level: 0, minKey: '', maxKey: '\uffff',
|
||||
blockCount: 1, totalSize: data.byteLength, bloomData: null,
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P0-1a: SSTableReader 残缺数据防御
|
||||
// ===================================================================
|
||||
|
||||
describe('P0-1a — SSTableReader 残缺数据防御', () => {
|
||||
it('索引块越界(文件被截断)→ 构造不抛异常,get/rangeScan/scanAll 返回空', () => {
|
||||
const builder = new SSTableBuilder(64);
|
||||
for (let i = 0; i < 100; i++) {
|
||||
builder.add(`k-${String(i).padStart(3, '0')}`, { v: i, data: 'x'.repeat(30) });
|
||||
}
|
||||
const { sstableData } = builder.build();
|
||||
// 完整文件可用(对照)
|
||||
const full = new SSTableReader(sstableData, makeMeta(sstableData));
|
||||
expect(full.get('k-001')).not.toBeNull();
|
||||
|
||||
// 从索引块中部截断(手动拼接保留末尾 32 字节 footer,使索引块越界但 footer 完整)
|
||||
const footerOffset = sstableData.byteLength - 32;
|
||||
const indexOffset = new DataView(sstableData.buffer, sstableData.byteOffset, sstableData.byteLength)
|
||||
.getUint32(footerOffset, false);
|
||||
const truncated = new Uint8Array((indexOffset + 4) + 32);
|
||||
truncated.set(sstableData.slice(0, indexOffset + 4), 0);
|
||||
truncated.set(sstableData.slice(sstableData.byteLength - 32), indexOffset + 4);
|
||||
// 构造必须不抛 RangeError
|
||||
let reader: SSTableReader;
|
||||
expect(() => { reader = new SSTableReader(truncated, makeMeta(truncated)); }).not.toThrow();
|
||||
expect(() => (reader as any).get('k-001')).not.toThrow();
|
||||
expect(() => (reader as any).rangeScan('a', 'z', () => {})).not.toThrow();
|
||||
expect(() => (reader as any).scanAll(() => {})).not.toThrow();
|
||||
expect((reader as any).get('k-001')).toBeNull();
|
||||
});
|
||||
|
||||
it('索引条目 blockSize 越界 → 该块跳过,其余块仍可读', () => {
|
||||
const builder = new SSTableBuilder(64);
|
||||
for (let i = 0; i < 100; i++) {
|
||||
builder.add(`k-${String(i).padStart(3, '0')}`, { v: i, data: 'x'.repeat(30) });
|
||||
}
|
||||
const { sstableData } = builder.build();
|
||||
const corrupted = sstableData.slice();
|
||||
const view = new DataView(corrupted.buffer, corrupted.byteOffset, corrupted.byteLength);
|
||||
const footerOffset = corrupted.byteLength - 32;
|
||||
const indexOffset = view.getUint32(footerOffset, false);
|
||||
expect(view.getUint32(indexOffset, false)).toBeGreaterThan(1);
|
||||
// 把第二个索引条目的 blockSize 改为超大(指向文件外):
|
||||
// 跳过 entry0(keyLen+key+blockOffset+blockSize)与 entry1 的 keyLen+key+blockOffset
|
||||
let off = indexOffset + 4;
|
||||
const keyLen0 = view.getUint16(off, false);
|
||||
off += 2 + keyLen0 + 8;
|
||||
const keyLen1 = view.getUint16(off, false);
|
||||
off += 2 + keyLen1 + 4;
|
||||
view.setUint32(off, 0x7FFFFFF0, false);
|
||||
|
||||
const reader = new SSTableReader(corrupted, makeMeta(corrupted));
|
||||
// 不抛 RangeError
|
||||
expect(() => reader.get('k-001')).not.toThrow();
|
||||
expect(() => reader.rangeScan('a', 'z', () => {})).not.toThrow();
|
||||
expect(() => reader.scanAll(() => {})).not.toThrow();
|
||||
// 第一个索引条目(未破坏)指向的块仍可读
|
||||
const first = reader.get('k-000');
|
||||
expect(first).not.toBeNull();
|
||||
expect((first as any).v).toBe(0);
|
||||
// 被破坏的块被跳过(返回 null 而非崩溃)
|
||||
expect(reader.get('k-001')).toBeNull();
|
||||
});
|
||||
|
||||
it('魔数错误 → 构造抛错(由 LSM loadSSTableReader 捕获跳过)', () => {
|
||||
const builder = new SSTableBuilder(4096);
|
||||
builder.add('a', { v: 1 });
|
||||
const { sstableData } = builder.build();
|
||||
const corrupted = sstableData.slice();
|
||||
// 破坏 footer 中 magic(位于文件末尾 32 字节内的 +24 偏移)
|
||||
new DataView(corrupted.buffer, corrupted.byteOffset, corrupted.byteLength)
|
||||
.setUint32(corrupted.byteLength - 8, 0xDEADBEEF, false);
|
||||
expect(() => new SSTableReader(corrupted, makeMeta(corrupted))).toThrow();
|
||||
});
|
||||
|
||||
it('块内条目计数虚高(内容截断)→ 提前中止,不抛 RangeError', () => {
|
||||
const builder = new SSTableBuilder(64);
|
||||
for (let i = 0; i < 100; i++) {
|
||||
builder.add(`k-${String(i).padStart(3, '0')}`, { v: i, data: 'x'.repeat(30) });
|
||||
}
|
||||
const { sstableData } = builder.build();
|
||||
const corrupted = sstableData.slice();
|
||||
// 把第一个数据块的 entryCount 改为超大(模拟块内条目被截断)
|
||||
new DataView(corrupted.buffer, corrupted.byteOffset, corrupted.byteLength)
|
||||
.setUint32(0, 0x7FFFFFF0, false);
|
||||
expect(() => new SSTableReader(corrupted, makeMeta(corrupted))).not.toThrow();
|
||||
const reader = new SSTableReader(corrupted, makeMeta(corrupted));
|
||||
expect(() => reader.get('k-000')).not.toThrow();
|
||||
expect(() => reader.get('nonexistent')).not.toThrow();
|
||||
expect(() => reader.scanAll(() => {})).not.toThrow();
|
||||
// 截断点之前的条目仍可读
|
||||
expect((reader.get('k-000') as any)?.v).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P0-1b: LSM 打开时完整性校验
|
||||
// ===================================================================
|
||||
|
||||
describe('P0-1b — AriaEngine 打开时完整性校验', () => {
|
||||
it('meta 引用残缺文件 → 打开跳过损坏 SSTable 不崩溃,库可用', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', memtableSizeThreshold: 1024 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
name: { type: 'string' },
|
||||
}));
|
||||
for (let i = 0; i < 50; i++) {
|
||||
await engine.insert('users', [{ id: `u${i}`, name: `User${i}` }]);
|
||||
}
|
||||
await (engine as any).lsm.flush();
|
||||
await engine.close();
|
||||
|
||||
// 篡改存储:把第一个 sst_ 文件写成残缺内容(meta 仍引用它)
|
||||
const backend = new IndexedDBBackend();
|
||||
await backend.open(dbName);
|
||||
const sstKeys = (await backend.listKeys())
|
||||
.filter((k) => k.startsWith('sst_') && !k.startsWith('sst_idx_'));
|
||||
expect(sstKeys.length).toBeGreaterThan(0);
|
||||
await backend.write(sstKeys[0], new TextEncoder().encode('truncated-garbage').buffer);
|
||||
await backend.close();
|
||||
|
||||
// 重开:不得崩溃(此前抛 RangeError 打不开库)
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', memtableSizeThreshold: 1024 });
|
||||
await expect(engine2.open(dbName, 1)).resolves.toBeUndefined();
|
||||
expect(engine2.isOpen()).toBe(true);
|
||||
// schema 完整,损坏的 SSTable 已被清理
|
||||
expect(await engine2.getTableNames()).toEqual(['users']);
|
||||
const rows = await engine2.find('users', { table: 'users' });
|
||||
expect(Array.isArray(rows)).toBe(true);
|
||||
await engine2.close();
|
||||
|
||||
// 清理 meta 已验证:损坏文件被删除
|
||||
const backend2 = new IndexedDBBackend();
|
||||
await backend2.open(dbName);
|
||||
const remaining = (await backend2.listKeys())
|
||||
.filter((k) => k.startsWith('sst_') && !k.startsWith('sst_idx_'));
|
||||
const metaRaw = await backend2.read('__aria_lsm_meta');
|
||||
const metaList = JSON.parse(new TextDecoder().decode(metaRaw ?? new Uint8Array())) as { id: number }[];
|
||||
expect(remaining.length).toBe(metaList.length); // 无孤儿文件 / 无悬空 meta
|
||||
await backend2.close();
|
||||
});
|
||||
|
||||
it('meta 引用缺失文件 → 打开清理 meta 不崩溃', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', memtableSizeThreshold: 1024 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
for (let i = 0; i < 30; i++) await engine.insert('t', [{ id: `k${i}`, v: i }]);
|
||||
await (engine as any).lsm.flush();
|
||||
await engine.close();
|
||||
|
||||
// 删除全部 sst_ 文件(保留 meta → 悬空引用)
|
||||
const backend = new IndexedDBBackend();
|
||||
await backend.open(dbName);
|
||||
const sstKeys = (await backend.listKeys())
|
||||
.filter((k) => k.startsWith('sst_') && !k.startsWith('sst_idx_'));
|
||||
await backend.deleteMany(sstKeys);
|
||||
await backend.close();
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', memtableSizeThreshold: 1024 });
|
||||
await expect(engine2.open(dbName, 1)).resolves.toBeUndefined();
|
||||
expect(engine2.isOpen()).toBe(true);
|
||||
expect(await engine2.getTableNames()).toEqual(['t']);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P0-1c / P1-4: WAL 原子写入 + 按 key 扫描恢复
|
||||
// ===================================================================
|
||||
|
||||
describe('P0-1c / P1-4 — WAL 原子性与恢复', () => {
|
||||
it('IndexedDBBackend.writeMany/deleteMany 单事务原子批量操作', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const be = new IndexedDBBackend();
|
||||
await be.open(dbName);
|
||||
await be.writeMany({
|
||||
a: new TextEncoder().encode('1').buffer,
|
||||
b: new TextEncoder().encode('2').buffer,
|
||||
});
|
||||
expect(new TextDecoder().decode((await be.read('a'))!)).toBe('1');
|
||||
expect(new TextDecoder().decode((await be.read('b'))!)).toBe('2');
|
||||
await be.deleteMany(['a', 'b']);
|
||||
expect(await be.read('a')).toBeNull();
|
||||
expect(await be.read('b')).toBeNull();
|
||||
await be.close();
|
||||
});
|
||||
|
||||
it('P1-4: 连续快速写入 200 条 → close+重开 数据完整(实测曾丢 4 条)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', walSyncMode: 'full' });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
for (let i = 0; i < 200; i++) {
|
||||
await engine.insert('t', [{ id: `${i}`, v: i }]);
|
||||
}
|
||||
await engine.close();
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', walSyncMode: 'full' });
|
||||
await engine2.open(dbName, 1);
|
||||
expect(await engine2.count('t')).toBe(200);
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('P1-4: count 键丢失(模拟崩溃竞态)→ 按 key 扫描恢复不丢记录', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', walSyncMode: 'full' });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
for (let i = 0; i < 20; i++) {
|
||||
await engine.insert('t', [{ id: `${i}`, v: i }]);
|
||||
}
|
||||
|
||||
// 模拟异常退出(不 close):直接删掉 count 键,制造 count 与记录不一致
|
||||
const backend = new IndexedDBBackend();
|
||||
await backend.open(dbName);
|
||||
await backend.delete('__wal_count');
|
||||
await backend.close();
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', walSyncMode: 'full' });
|
||||
await engine2.open(dbName, 1);
|
||||
expect(await engine2.count('t')).toBe(20);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1-5: close 截断 WAL
|
||||
// ===================================================================
|
||||
|
||||
describe('P1-5 — AriaEngine.close 截断 WAL', () => {
|
||||
it('close 后 WAL 记录键全部清空(不无限重放)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', walSyncMode: 'full' });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', v: 1 }, { id: '2', v: 2 }]);
|
||||
await engine.close();
|
||||
|
||||
const backend = new IndexedDBBackend();
|
||||
await backend.open(dbName);
|
||||
// __wal_count 计数键合法保留(作为 append 序号分配器),WAL 记录键必须清空
|
||||
const walKeys = (await backend.listKeys())
|
||||
.filter((k) => k.startsWith('__wal_') && k !== '__wal_count');
|
||||
expect(walKeys).toHaveLength(0);
|
||||
await backend.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P0-2 / P0-3 / P2-8: IndexedDB 版本管理与参数校验
|
||||
// ===================================================================
|
||||
|
||||
describe('P0-2 / P0-3 — IndexedDB 版本管理', () => {
|
||||
it('P0-2: 建表提升版本后以旧 version 重开成功(VersionError 自适应)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new IndexedDBEngine();
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable({ name: 'a', columns: { id: { type: 'string', primaryKey: true } } });
|
||||
await engine.createTable({ name: 'b', columns: { id: { type: 'string', primaryKey: true } } });
|
||||
await engine.createTable({ name: 'c', columns: { id: { type: 'string', primaryKey: true } } });
|
||||
await engine.close();
|
||||
|
||||
// 用旧版本 1 重开:此前必报 IDB_OPEN_ERROR(VersionError)
|
||||
const engine2 = new IndexedDBEngine();
|
||||
await expect(engine2.open(dbName, 1)).resolves.toBeUndefined();
|
||||
const names = await engine2.getTableNames();
|
||||
expect(names).toContain('a');
|
||||
expect(names).toContain('b');
|
||||
expect(names).toContain('c');
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('P0-2: MetonaSqlark hybrid 二次启动不报 VersionError(MarkLite 场景)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db1 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb', version: 1 });
|
||||
await db1.init();
|
||||
await db1.defineTable('users', { id: { type: 'string', primaryKey: true }, name: { type: 'string' } });
|
||||
await db1.table('users').insert({ id: '1', name: 'Alice' });
|
||||
await db1.close();
|
||||
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb', version: 1 });
|
||||
await expect(db2.init()).resolves.toBeUndefined();
|
||||
expect(await db2.table('users').count()).toBe(1);
|
||||
await db2.close();
|
||||
});
|
||||
|
||||
it('P0-3: IndexedDBEngine open(version=0) 归一化为 1,不抛 TypeError', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new IndexedDBEngine();
|
||||
await expect(engine.open(dbName, 0)).resolves.toBeUndefined();
|
||||
expect(engine.isOpen()).toBe(true);
|
||||
await engine.createTable({ name: 't', columns: { id: { type: 'string', primaryKey: true } } });
|
||||
await engine.insert('t', [{ id: '1' }]);
|
||||
expect(await engine.find('t', { table: 't' })).toHaveLength(1);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('P0-3: MetonaSqlark version=0 初始化正常', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb', version: 0 });
|
||||
await expect(db.init()).resolves.toBeUndefined();
|
||||
await db.defineTable('t', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('t').insert({ id: '1' });
|
||||
await db.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1-6: 引擎错误包装 DatabaseError
|
||||
// ===================================================================
|
||||
|
||||
describe('P1-6 — 引擎内部错误统一包装 DatabaseError', () => {
|
||||
it('AriaEngine open 异常路径抛 DatabaseError(ARIA_OPEN_ERROR)', async () => {
|
||||
// 构造一个无法打开的 backend 场景:直接调用 openInternal 模拟底层异常不可行,
|
||||
// 这里验证损坏库重开时抛的是 DatabaseError 而非原生错误(不崩溃路径已由 P0-1b 覆盖)
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb' });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
await engine.insert('t', [{ id: '1' }]);
|
||||
await engine.close();
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb' });
|
||||
await expect(engine2.open(dbName, 1)).resolves.toBeUndefined();
|
||||
expect(engine2.isOpen()).toBe(true);
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('open 未初始化 IndexedDB 环境时抛 DatabaseError 而非原生错误', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new IndexedDBEngine();
|
||||
await engine.open(dbName, 1);
|
||||
expect(engine.isOpen()).toBe(true);
|
||||
await engine.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P2-7: 迁移版本持久化
|
||||
// ===================================================================
|
||||
|
||||
describe('P2-7 — 迁移版本持久化', () => {
|
||||
it('重启后已执行迁移不重跑,只执行新迁移', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const runs: number[] = [];
|
||||
|
||||
// version: 0 表示"无 schema 起点",migration 1 可执行
|
||||
const db1 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb', version: 0 });
|
||||
await db1.init();
|
||||
db1.addMigration(1, async () => { runs.push(1); });
|
||||
db1.addMigration(2, async () => { runs.push(2); });
|
||||
await db1.migrateTo(2);
|
||||
expect(runs).toEqual([1, 2]);
|
||||
await db1.close();
|
||||
|
||||
// 重启:version 又重置为 config.version=0(此前会重跑 migration 1/2)
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb', version: 0 });
|
||||
await db2.init();
|
||||
db2.addMigration(1, async () => { runs.push(1); });
|
||||
db2.addMigration(2, async () => { runs.push(2); });
|
||||
db2.addMigration(3, async () => { runs.push(3); });
|
||||
await db2.migrateTo(3);
|
||||
// 只有 migration 3 是新迁移(1/2 已持久化不重跑)
|
||||
expect(runs).toEqual([1, 2, 3]);
|
||||
await db2.close();
|
||||
});
|
||||
|
||||
it('AriaEngine getMeta/setMeta 往返', async () => {
|
||||
const engine = new AriaEngine({ storageBackend: 'memory' });
|
||||
await engine.open(uniqueDB(), 1);
|
||||
expect(await engine.getMeta('__metona_version')).toBeNull();
|
||||
await engine.setMeta('__metona_version', '3');
|
||||
expect(await engine.getMeta('__metona_version')).toBe('3');
|
||||
await engine.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P2-9: repair() / clearAll() 统一自愈接口
|
||||
// ===================================================================
|
||||
|
||||
describe('P2-9 — 统一自愈接口 repair / clearAll', () => {
|
||||
it('AriaEngine.clearAll 清空全部表与数据', async () => {
|
||||
const engine = new AriaEngine({ storageBackend: 'memory' });
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
await engine.insert('t', [{ id: '1' }]);
|
||||
await engine.clearAll();
|
||||
expect(await engine.getTableNames()).toEqual([]);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('AriaEngine.repair 清理损坏 SSTable 后引擎可用', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', memtableSizeThreshold: 4096 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
for (let i = 0; i < 100; i++) await engine.insert('t', [{ id: `k${i}`, v: i }]);
|
||||
await (engine as any).lsm.flush();
|
||||
|
||||
// 篡改一个 sst 文件
|
||||
const backend = new IndexedDBBackend();
|
||||
await backend.open(dbName);
|
||||
const sstKeys = (await backend.listKeys())
|
||||
.filter((k) => k.startsWith('sst_') && !k.startsWith('sst_idx_'));
|
||||
expect(sstKeys.length).toBeGreaterThan(0);
|
||||
await backend.write(sstKeys[0], new TextEncoder().encode('garbage').buffer);
|
||||
await backend.close();
|
||||
|
||||
await expect(engine.repair()).resolves.toBeUndefined();
|
||||
const rows = await engine.find('t', { table: 't' });
|
||||
expect(Array.isArray(rows)).toBe(true);
|
||||
expect(rows.length).toBeLessThanOrEqual(100);
|
||||
expect(engine.isOpen()).toBe(true);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('MetonaSqlark.clearAll 统一接口(hybrid)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('t', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('t').insert({ id: '1' });
|
||||
await db.clearAll();
|
||||
expect(await db.getTableNames()).toEqual([]);
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('MetonaSqlark.repair 统一接口(aria)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'aria', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } });
|
||||
await db.table('t').insert({ id: '1', v: 42 });
|
||||
await db.repair();
|
||||
expect(await db.table('t').count()).toBe(1);
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('MetonaSqlark.repair 统一接口(hybrid)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('t', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('t').insert({ id: '1' });
|
||||
await db.repair();
|
||||
expect(await db.table('t').count()).toBe(1);
|
||||
await db.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,312 @@
|
||||
/**
|
||||
* v0.4.2 深度审计回归测试(第二轮)
|
||||
* 覆盖:
|
||||
* - P0-A: compaction 不依赖缓存(缓存未命中不得丢数据/数据不可见)
|
||||
* - P0-B: flush/compaction 失败不得卡死 flushChain(写路径死锁)
|
||||
* - P1-A: 重开后二级索引恢复 + createIndex 幂等重建 + WAL 恢复后索引一致
|
||||
* - P1-B: ALTER TABLE 在 IndexedDB/Hybrid 引擎持久化
|
||||
* - P1-C: IndexedDB 事务内 DDL(建表/删表)commit 后磁盘一致
|
||||
*/
|
||||
|
||||
import { AriaEngine } from '../src/engine/aria/index';
|
||||
import { createSchema } from '../src/table/schema';
|
||||
import { MemoryEngine } from '../src/engine/memory';
|
||||
import { HybridEngine } from '../src/hybrid/index';
|
||||
import { MetonaSqlark } from '../src/core';
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
let idbCounter = 0;
|
||||
function uniqueDB(): string {
|
||||
return `audit-${Date.now()}-${++idbCounter}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// P0-A: compaction 不得依赖 SSTable 缓存
|
||||
// ===================================================================
|
||||
|
||||
describe('P0-A — Compaction 缓存独立性', () => {
|
||||
it('缓存上限小于单个 SSTable 时 compaction 不丢数据(此前数据不可见)', async () => {
|
||||
const engine = new AriaEngine({
|
||||
storageBackend: 'memory',
|
||||
// 缓存上限 4 页 = 16KB,memtable 32KB → 每次 flush 的文件都超出缓存上限被驱逐
|
||||
bufferPoolPages: 4,
|
||||
memtableSizeThreshold: 32 * 1024,
|
||||
checkpointInterval: 100000,
|
||||
});
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
data: { type: 'string' },
|
||||
}));
|
||||
for (let i = 0; i < 400; i++) {
|
||||
await engine.insert('t', [{ id: `k${String(i).padStart(4, '0')}`, v: i, data: 'x'.repeat(200) }]);
|
||||
}
|
||||
// 等待 flush + compaction 链全部完成
|
||||
await (engine as any).lsm.flush();
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
// 修复前:compaction 缓存未命中跳过全部文件并从 levels 移除 → 查询为空
|
||||
expect(await engine.count('t')).toBe(400);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('多层级 compaction 后数据仍完整且可重开', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({
|
||||
storageBackend: 'indexeddb',
|
||||
bufferPoolPages: 8, // 32KB 缓存
|
||||
memtableSizeThreshold: 16 * 1024,
|
||||
checkpointInterval: 100000,
|
||||
});
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
for (let i = 0; i < 300; i++) {
|
||||
await engine.insert('t', [{ id: `k${String(i).padStart(4, '0')}`, v: i }]);
|
||||
}
|
||||
await (engine as any).lsm.flush();
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
const before = await engine.count('t');
|
||||
expect(before).toBe(300);
|
||||
await engine.close();
|
||||
|
||||
// 重开:数据完整(meta/文件未被 compaction 破坏)
|
||||
const engine2 = new AriaEngine({
|
||||
storageBackend: 'indexeddb',
|
||||
bufferPoolPages: 8,
|
||||
memtableSizeThreshold: 16 * 1024,
|
||||
checkpointInterval: 100000,
|
||||
});
|
||||
await engine2.open(dbName, 1);
|
||||
expect(await engine2.count('t')).toBe(300);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1-A: 二级索引跨重启恢复
|
||||
// ===================================================================
|
||||
|
||||
describe('P1-A — 二级索引恢复', () => {
|
||||
it('Aria 重开后二级索引可用(索引 LSM 持久化恢复)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('users', [
|
||||
{ id: '1', email: 'a@x.com' },
|
||||
{ id: '2', email: 'b@x.com' },
|
||||
{ id: '3', email: 'a@x.com' },
|
||||
]);
|
||||
await (engine as any).lsm.flush();
|
||||
await engine.close();
|
||||
|
||||
// 重开:二级索引 LSM 应自动恢复(修复前为空 → 索引查询回退全表,createIndex 也静默跳过)
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
// 强断言:索引 LSM 真实存在且数据完整(防止"索引缺失静默回退全表扫描"的假通过)
|
||||
const idxLsm = (engine2 as any).secondaryIndexes.get('users:idx:email');
|
||||
expect(idxLsm).toBeDefined();
|
||||
expect(idxLsm.getStats().sstableCount).toBeGreaterThan(0);
|
||||
await idxLsm.prefetchRange('', '\uffff');
|
||||
expect(idxLsm.rangeScan('', '\uffff')).toHaveLength(3);
|
||||
const byEmail = await engine2.find('users', { table: 'users', where: { email: 'a@x.com' } });
|
||||
expect(byEmail).toHaveLength(2);
|
||||
// createIndex 对已持久化的索引列应幂等可重建(不得静默跳过导致索引永久缺失)
|
||||
await engine2.createIndex('users', 'email');
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('WAL 恢复后二级索引与主数据一致(崩溃前索引未更新)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
city: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('users', [{ id: '1', city: 'Beijing' }]);
|
||||
await (engine as any).lsm.flush();
|
||||
// 写入 WAL 但强制不 flush(模拟崩溃:新行只在 WAL,索引 LSM 未更新)
|
||||
await engine.insert('users', [{ id: '2', city: 'Shanghai' }]);
|
||||
// 模拟异常退出(不 close)
|
||||
await (engine as any).backend.close();
|
||||
(engine as any).opened = false;
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
// 强断言:索引 LSM 已恢复且包含 WAL 回放的行(崩溃前索引未更新,恢复后必须重建)
|
||||
const idxLsm = (engine2 as any).secondaryIndexes.get('users:idx:city');
|
||||
expect(idxLsm).toBeDefined();
|
||||
await idxLsm.prefetchRange('', '\uffff');
|
||||
expect(idxLsm.rangeScan('', '\uffff')).toHaveLength(2);
|
||||
// 索引查询应看到 WAL 恢复的行(修复前索引与主数据不一致 → 丢行)
|
||||
const byCity = await engine2.find('users', { table: 'users', where: { city: 'Shanghai' } });
|
||||
expect(byCity).toHaveLength(1);
|
||||
expect(byCity[0].id).toBe('2');
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('createIndex 重开后仍可新建(schema 标记恢复后不阻塞)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
name: { type: 'string' },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', name: 'A' }]);
|
||||
await engine.close();
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
await engine2.createIndex('t', 'name'); // 修复前 schema 无标记时正常;此处验证无标记场景
|
||||
const byName = await engine2.find('t', { table: 't', where: { name: 'A' } });
|
||||
expect(byName).toHaveLength(1);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1-B: ALTER TABLE 跨引擎持久化
|
||||
// ===================================================================
|
||||
|
||||
describe('P1-B — ALTER TABLE 持久化', () => {
|
||||
it('IndexedDBEngine DROP COLUMN 后重启不复活', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'disk', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
old_col: { type: 'string' },
|
||||
keep: { type: 'string' },
|
||||
});
|
||||
await db.table('t').insert({ id: '1', old_col: 'x', keep: 'y' });
|
||||
await db.query('ALTER TABLE t DROP COLUMN old_col');
|
||||
// 行数据中该列已移除
|
||||
const rows = await db.table('t').select().execute();
|
||||
expect(rows[0].old_col).toBeUndefined();
|
||||
await db.close();
|
||||
|
||||
// 重启:schema 不复活,列定义已持久化
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'disk', diskEngine: 'indexeddb' });
|
||||
await db2.init();
|
||||
const schema2 = await db2.getEngine().getTableSchema('t');
|
||||
expect(schema2!.columns.old_col).toBeUndefined();
|
||||
expect(schema2!.columns.keep).toBeDefined();
|
||||
await db2.close();
|
||||
});
|
||||
|
||||
it('HybridEngine ADD COLUMN 后重启保留', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('t', { id: { type: 'string', primaryKey: true } });
|
||||
await db.query('ALTER TABLE t ADD COLUMN phone STRING');
|
||||
await db.close();
|
||||
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db2.init();
|
||||
const schema2 = await db2.getEngine().getTableSchema('t');
|
||||
expect(schema2!.columns.phone).toBeDefined();
|
||||
await db2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1-C: IndexedDB 事务内 DDL
|
||||
// ===================================================================
|
||||
|
||||
describe('P1-C — IndexedDB 事务内 DDL', () => {
|
||||
it('事务内建表 → commit 后磁盘一致(重启表存在且可查)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('base', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('base').insert({ id: '1' });
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
await trx.table('base').insert({ id: '2' });
|
||||
// 事务内建新表(此前 commit 时 IDB 无对应 store → 事务失败)
|
||||
await db.defineTable('created_in_tx', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('created_in_tx').insert({ id: 'tx1' });
|
||||
});
|
||||
|
||||
expect(await db.table('created_in_tx').count()).toBe(1);
|
||||
await db.close();
|
||||
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db2.init();
|
||||
expect(await db2.table('created_in_tx').count()).toBe(1);
|
||||
expect(await db2.table('base').count()).toBe(2);
|
||||
await db2.close();
|
||||
});
|
||||
|
||||
it('事务内删表 → commit 后磁盘一致(重启无幽灵表)', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const db = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db.init();
|
||||
await db.defineTable('ghost', { id: { type: 'string', primaryKey: true } });
|
||||
await db.table('ghost').insert({ id: '1' });
|
||||
|
||||
await db.transaction(async () => {
|
||||
await db.dropTable('ghost');
|
||||
});
|
||||
|
||||
await db.close();
|
||||
const db2 = new MetonaSqlark({ name: dbName, mode: 'hybrid', diskEngine: 'indexeddb' });
|
||||
await db2.init();
|
||||
const names = await db2.getTableNames();
|
||||
expect(names).not.toContain('ghost');
|
||||
await db2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// 各引擎 clearAll / repair / 元数据 一致性抽查
|
||||
// ===================================================================
|
||||
|
||||
describe('全模式抽查 — 生命周期与元数据', () => {
|
||||
it('MemoryEngine clearAll/repair/getMeta/setMeta', async () => {
|
||||
const e = new MemoryEngine();
|
||||
await e.open('m', 1);
|
||||
await e.createTable({ name: 't', columns: { id: { type: 'string', primaryKey: true } } });
|
||||
await e.insert('t', [{ id: '1' }]);
|
||||
await e.setMeta('k', 'v');
|
||||
expect(await e.getMeta('k')).toBe('v');
|
||||
await e.repair();
|
||||
await e.clearAll();
|
||||
expect(await e.getTableNames()).toEqual([]);
|
||||
await e.close();
|
||||
});
|
||||
|
||||
it('HybridEngine getMeta/setMeta 委托磁盘', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const e = new HybridEngine('indexeddb');
|
||||
await e.open(dbName, 1);
|
||||
await e.setMeta('__metona_version', '7');
|
||||
expect(await e.getMeta('__metona_version')).toBe('7');
|
||||
await e.close();
|
||||
const e2 = new HybridEngine('indexeddb');
|
||||
await e2.open(dbName, 1);
|
||||
expect(await e2.getMeta('__metona_version')).toBe('7');
|
||||
await e2.close();
|
||||
});
|
||||
|
||||
it('AriaEngine repair 幂等', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb' });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
await engine.insert('t', [{ id: '1' }]);
|
||||
await engine.repair();
|
||||
await engine.repair();
|
||||
expect(await engine.count('t')).toBe(1);
|
||||
await engine.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,390 @@
|
||||
/**
|
||||
* v0.4.2 生产就绪审计(第三轮)
|
||||
* 覆盖:
|
||||
* - P0: Aria 事务进行中 checkpoint 截断 WAL → 崩溃恢复丢事务数据
|
||||
* - P1: Aria dropTable / ALTER DROP 索引列 的二级索引清理
|
||||
* - P1: OPFS 并发写乱序丢更新 + 空表/schema/索引持久化
|
||||
* - P2: Aria memtable 阈值衰减、红黑树随机压力、onUpdate 级联、事务 DDL 显式拒绝
|
||||
*/
|
||||
|
||||
import { AriaEngine } from '../src/engine/aria/index';
|
||||
import { createSchema } from '../src/table/schema';
|
||||
import { OPFSEngine } from '../src/engine/opfs';
|
||||
import { MemTable } from '../src/engine/aria/index/memtable';
|
||||
import { MetonaSqlark } from '../src/core';
|
||||
import 'fake-indexeddb/auto';
|
||||
|
||||
let idbCounter = 0;
|
||||
function uniqueDB(): string {
|
||||
return `prod-${Date.now()}-${++idbCounter}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// OPFS mock(模拟真实 I/O:getFileHandle 延迟 + 写入按内容差异化耗时)
|
||||
// ===================================================================
|
||||
|
||||
function mockOPFS(
|
||||
files?: Map<string, string>,
|
||||
opts: { ioDelay?: number; writeDelayFor?: (data: string) => number } = {},
|
||||
): Map<string, string> {
|
||||
const store = files ?? new Map<string, string>();
|
||||
|
||||
const dirMock = {
|
||||
getDirectoryHandle: async (_name: string, _opts?: any) => dirMock as any,
|
||||
getFileHandle: async (name: string, fileOpts?: any) => {
|
||||
if (fileOpts?.create) {
|
||||
if (opts.ioDelay) {
|
||||
await new Promise((r) => setTimeout(r, opts.ioDelay));
|
||||
}
|
||||
return {
|
||||
createWritable: async () => ({
|
||||
write: async (d: string) => {
|
||||
const delay = opts.writeDelayFor ? opts.writeDelayFor(d) : 0;
|
||||
if (delay > 0) {
|
||||
await new Promise((r) => setTimeout(r, delay));
|
||||
}
|
||||
store.set(name, d);
|
||||
},
|
||||
close: async () => {},
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (!store.has(name)) throw new Error('Not found');
|
||||
return { getFile: async () => ({ text: async () => store.get(name)!, arrayBuffer: async () => new ArrayBuffer(0) }) };
|
||||
},
|
||||
removeEntry: async (name: string) => { store.delete(name); },
|
||||
};
|
||||
(dirMock as any).entries = () => ({
|
||||
[Symbol.asyncIterator]: async function* () {
|
||||
for (const [k] of store) yield [k];
|
||||
},
|
||||
});
|
||||
const nav = (globalThis as any).navigator || {};
|
||||
nav.storage = { getDirectory: async () => dirMock };
|
||||
(globalThis as any).navigator = nav;
|
||||
return store;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// P0: 事务与 checkpoint 冲突
|
||||
// ===================================================================
|
||||
|
||||
describe('P0 — 事务进行中 checkpoint 不得截断 WAL', () => {
|
||||
it('事务中触发 checkpoint → 崩溃恢复不丢事务数据', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({
|
||||
storageBackend: 'indexeddb',
|
||||
checkpointInterval: 2, // 每 2 次操作即触发 checkpoint(事务中途)
|
||||
});
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
v: { type: 'number' },
|
||||
}));
|
||||
|
||||
await engine.beginTransaction();
|
||||
await engine.insert('t', [{ id: '1', v: 1 }]);
|
||||
await engine.insert('t', [{ id: '2', v: 2 }]); // opCounter=2 → tick → checkpoint(修复前截断 WAL)
|
||||
await engine.commitTransaction();
|
||||
|
||||
// 模拟异常退出(不 close)
|
||||
await (engine as any).backend.close();
|
||||
(engine as any).opened = false;
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
// 修复前:checkpoint 截断了事务的 WAL 记录 → 恢复后数据丢失
|
||||
expect(await engine2.count('t')).toBe(2);
|
||||
await engine2.close();
|
||||
});
|
||||
|
||||
it('事务回滚后再 checkpoint 正常截断', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({
|
||||
storageBackend: 'indexeddb',
|
||||
checkpointInterval: 1,
|
||||
});
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
await engine.insert('t', [{ id: 'keep' }]);
|
||||
await engine.beginTransaction();
|
||||
await engine.insert('t', [{ id: 'tx1' }]);
|
||||
await engine.rollbackTransaction();
|
||||
// 事务结束后 checkpoint 可正常截断
|
||||
await (engine as any).checkpointManager.forceCheckpoint();
|
||||
await (engine as any).backend.close();
|
||||
(engine as any).opened = false;
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
expect(await engine2.count('t')).toBe(1);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1: Aria DDL 的二级索引清理
|
||||
// ===================================================================
|
||||
|
||||
describe('P1 — Aria DDL 索引清理', () => {
|
||||
it('dropTable 清理二级索引(重建同名表索引不脏)', async () => {
|
||||
const engine = new AriaEngine({ storageBackend: 'memory' });
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', email: 'a@x.com' }]);
|
||||
await engine.dropTable('t');
|
||||
// 索引 LSM 必须清理(修复前残留)
|
||||
expect((engine as any).secondaryIndexes.size).toBe(0);
|
||||
|
||||
// 重建同名表 + 相同 id:旧索引残留会返回错误行
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', email: 'b@x.com' }]);
|
||||
// 旧索引残留场景:按旧 email 查询不得命中新行(主 LSM 有 t:1 → 修复前返回错误结果)
|
||||
const byOld = await engine.find('t', { table: 't', where: { email: 'a@x.com' } });
|
||||
expect(byOld).toHaveLength(0);
|
||||
const byNew = await engine.find('t', { table: 't', where: { email: 'b@x.com' } });
|
||||
expect(byNew).toHaveLength(1);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('ALTER TABLE DROP 索引列清理索引 LSM', async () => {
|
||||
const engine = new AriaEngine({ storageBackend: 'memory' });
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', email: 'a@x.com' }]);
|
||||
await engine.alterTable('t', 'DROP', { name: 'email', type: 'string' });
|
||||
expect((engine as any).secondaryIndexes.size).toBe(0);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('DROP_TABLE 崩溃恢复同样清理索引', async () => {
|
||||
const dbName = uniqueDB();
|
||||
const engine = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine.open(dbName, 1);
|
||||
await engine.createTable(createSchema('t', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await engine.insert('t', [{ id: '1', email: 'a@x.com' }]);
|
||||
await engine.dropTable('t');
|
||||
// 模拟崩溃(不 close,WAL 含 DROP_TABLE)
|
||||
await (engine as any).backend.close();
|
||||
(engine as any).opened = false;
|
||||
|
||||
const engine2 = new AriaEngine({ storageBackend: 'indexeddb', checkpointInterval: 100000 });
|
||||
await engine2.open(dbName, 1);
|
||||
// 表不存在,且无索引残留
|
||||
expect(await engine2.hasTable('t')).toBe(false);
|
||||
expect((engine2 as any).secondaryIndexes.size).toBe(0);
|
||||
await engine2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P1: OPFS 持久化与并发
|
||||
// ===================================================================
|
||||
|
||||
describe('P1 — OPFS 生产加固', () => {
|
||||
it('并发写不丢数据(内存快照总是最新,多写内容一致)', async () => {
|
||||
// 模拟真实 I/O 延迟:内存写同步、持久化异步 → 并发写内容均基于最新内存快照
|
||||
const files = mockOPFS(undefined, {
|
||||
ioDelay: 10,
|
||||
writeDelayFor: (data) => (data.length < 40 ? 30 : 5),
|
||||
});
|
||||
const engine = new OPFSEngine();
|
||||
await engine.open('opfs-race', 1);
|
||||
await engine.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
name: { type: 'string' },
|
||||
}));
|
||||
// 两个写并发:内存同步写保证两个快照一致 → 无论完成顺序文件内容完整
|
||||
const p1 = engine.insert('users', [{ id: '1', name: 'A' }]);
|
||||
const p2 = engine.insert('users', [{ id: '2', name: 'B' }]);
|
||||
await Promise.all([p1, p2]);
|
||||
const rows = await engine.find('users', { table: 'users' });
|
||||
expect(rows).toHaveLength(2);
|
||||
// 文件内容完整(重启不丢)
|
||||
expect(files.get('users.json')).toContain('"B"');
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('空表重启后保留(schema 持久化)', async () => {
|
||||
mockOPFS();
|
||||
const e1 = new OPFSEngine();
|
||||
await e1.open('opfs-schema', 1);
|
||||
await e1.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await e1.close();
|
||||
|
||||
// 重启:空表也应恢复(修复前空表消失)+ 索引标记恢复
|
||||
const e2 = new OPFSEngine();
|
||||
await e2.open('opfs-schema', 1);
|
||||
expect(await e2.hasTable('users')).toBe(true);
|
||||
const schema = await e2.getTableSchema('users');
|
||||
expect(schema!.columns.email.index).toBe(true);
|
||||
await e2.close();
|
||||
});
|
||||
|
||||
it('有数据重启后索引查询可用', async () => {
|
||||
mockOPFS();
|
||||
const e1 = new OPFSEngine();
|
||||
await e1.open('opfs-idx', 1);
|
||||
await e1.createTable(createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
email: { type: 'string', index: true },
|
||||
}));
|
||||
await e1.insert('users', [{ id: '1', email: 'a@x.com' }]);
|
||||
await e1.close();
|
||||
|
||||
const e2 = new OPFSEngine();
|
||||
await e2.open('opfs-idx', 1);
|
||||
const byEmail = await e2.find('users', { table: 'users', where: { email: 'a@x.com' } });
|
||||
expect(byEmail).toHaveLength(1);
|
||||
await e2.close();
|
||||
});
|
||||
|
||||
it('dropTable 后重启无幽灵表', async () => {
|
||||
mockOPFS();
|
||||
const e1 = new OPFSEngine();
|
||||
await e1.open('opfs-drop', 1);
|
||||
await e1.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
await e1.insert('t', [{ id: '1' }]);
|
||||
await e1.dropTable('t');
|
||||
await e1.close();
|
||||
|
||||
const e2 = new OPFSEngine();
|
||||
await e2.open('opfs-drop', 1);
|
||||
expect(await e2.hasTable('t')).toBe(false);
|
||||
expect(await e2.getTableNames()).toEqual([]);
|
||||
await e2.close();
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// P2: Aria 内部状态
|
||||
// ===================================================================
|
||||
|
||||
describe('P2 — Aria 内部状态加固', () => {
|
||||
it('freezeMemtable 后阈值不衰减', async () => {
|
||||
const engine = new AriaEngine({
|
||||
storageBackend: 'memory',
|
||||
memtableSizeThreshold: 1024 * 1024,
|
||||
});
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
|
||||
// 少量写入 + 手动 flush(freezeMemtable 用旧表已用大小当新阈值 → 衰减)
|
||||
for (let i = 0; i < 10; i++) await engine.insert('t', [{ id: `${i}` }]);
|
||||
await (engine as any).lsm.flush();
|
||||
// 修复前:新 memtable maxSize ≈ 已用字节(远小于配置)
|
||||
const memtable = (engine as any).lsm.memtable;
|
||||
expect((memtable as any).maxSize).toBeGreaterThanOrEqual(1024 * 1024);
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('红黑树随机 insert/delete 5000 次保持有序且无丢失', () => {
|
||||
const mem = new MemTable(1 << 30);
|
||||
const reference = new Set<string>();
|
||||
let seed = 42;
|
||||
const rnd = () => {
|
||||
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
|
||||
return seed / 0x7fffffff;
|
||||
};
|
||||
for (let i = 0; i < 5000; i++) {
|
||||
const k = `k${Math.floor(rnd() * 800)}`;
|
||||
if (rnd() < 0.3) {
|
||||
reference.delete(k);
|
||||
mem.delete(k);
|
||||
} else {
|
||||
reference.add(k);
|
||||
mem.put(k, { v: i });
|
||||
}
|
||||
}
|
||||
const entries = mem.getAllEntries();
|
||||
// 有序
|
||||
for (let i = 1; i < entries.length; i++) {
|
||||
expect(entries[i][0] > entries[i - 1][0]).toBe(true);
|
||||
}
|
||||
// 与引用集合完全一致(无丢失/无残留)
|
||||
expect(entries.map(([k]) => k)).toEqual([...reference].sort());
|
||||
expect(mem.getEntryCount()).toBe(reference.size);
|
||||
});
|
||||
|
||||
it('onUpdate CASCADE:更新父表主键级联更新子表外键', async () => {
|
||||
const db = new MetonaSqlark({ name: uniqueDB(), mode: 'memory' });
|
||||
await db.init();
|
||||
await db.defineTable('users', { id: { type: 'string', primaryKey: true } });
|
||||
await db.defineTable('orders', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
user_id: { type: 'string', references: 'users.id', onUpdate: 'CASCADE' },
|
||||
});
|
||||
await db.table('users').insert({ id: '1' });
|
||||
await db.table('orders').insert({ id: 'o1', user_id: '1' });
|
||||
// 更新父表主键 1 → 2
|
||||
await db.table('users').update({ id: '2' }).where({ id: '1' }).execute();
|
||||
const orders = await db.table('orders').select().execute();
|
||||
expect(orders[0].user_id).toBe('2');
|
||||
// 旧 id 不可再被引用
|
||||
expect(await db.table('orders').count({ user_id: '1' })).toBe(0);
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('onUpdate RESTRICT:存在引用行时禁止更新主键', async () => {
|
||||
const db = new MetonaSqlark({ name: uniqueDB(), mode: 'memory' });
|
||||
await db.init();
|
||||
await db.defineTable('users', { id: { type: 'string', primaryKey: true } });
|
||||
await db.defineTable('orders', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
user_id: { type: 'string', references: 'users.id', onUpdate: 'RESTRICT' },
|
||||
});
|
||||
await db.table('users').insert({ id: '1' });
|
||||
await db.table('orders').insert({ id: 'o1', user_id: '1' });
|
||||
await expect(
|
||||
db.table('users').update({ id: '2' }).where({ id: '1' }).execute(),
|
||||
).rejects.toThrow();
|
||||
// 主键未被修改
|
||||
const users = await db.table('users').select().execute();
|
||||
expect(users[0].id).toBe('1');
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('onUpdate CASCADE:更新父表主键级联更新子表外键(Aria)', async () => {
|
||||
const db = new MetonaSqlark({ name: uniqueDB(), mode: 'aria', diskEngine: 'memory' });
|
||||
await db.init();
|
||||
await db.defineTable('users', { id: { type: 'string', primaryKey: true } });
|
||||
await db.defineTable('orders', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
user_id: { type: 'string', references: 'users.id', onUpdate: 'CASCADE' },
|
||||
});
|
||||
await db.table('users').insert({ id: '1' });
|
||||
await db.table('orders').insert({ id: 'o1', user_id: '1' });
|
||||
await db.table('users').update({ id: '2' }).where({ id: '1' }).execute();
|
||||
const orders = await db.table('orders').select().execute();
|
||||
expect(orders[0].user_id).toBe('2');
|
||||
expect(await db.table('orders').count({ user_id: '1' })).toBe(0);
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('Aria 事务中 DDL 显式拒绝(NOT_SUPPORTED)而非静默不一致', async () => {
|
||||
const engine = new AriaEngine({ storageBackend: 'memory' });
|
||||
await engine.open(uniqueDB(), 1);
|
||||
await engine.beginTransaction();
|
||||
await expect(
|
||||
engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } })),
|
||||
).rejects.toMatchObject({ code: 'NOT_SUPPORTED' });
|
||||
await engine.rollbackTransaction();
|
||||
// 表未创建
|
||||
expect(await engine.hasTable('t')).toBe(false);
|
||||
await engine.close();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user