feat: v0.2.0 AriaEngine 自研存储引擎
- 新增 AriaEngine: LSM-Tree 页面式存储引擎,19 个模块,~3500 行 TS - page/: Slotted Page 格式 (header/slot/tuple/format) + CRC32 - buffer/: Buffer Pool (LRU 缓存 + 驱逐策略) - index/: LSM-Tree (MemTable 红黑树 + SSTable + Bloom Filter + Merge Iterator) - wal/: WAL 日志 (二进制格式) + Checkpoint 管理 - transaction/: MVCC 版本链 + 快照隔离 - store/: IndexedDB / Memory 双后端抽象 - compression/: LZ4 页面压缩 - 完整持久化: Schema 自动保存、SSTable 元数据管理、WAL 恢复 - 事务感知 CRUD: insert/update/delete 在事务中缓冲到 snapshot - mode: 'aria' 激活自研引擎 - 新增 7 个测试文件,测试数 318 → 524,套件 20 → 27 - aria-page.test.ts (32 tests): Page 格式单元测试 - aria-index.test.ts (26 tests): Bloom Filter + MemTable - aria-sstable.test.ts (9 tests): SSTable Builder + Reader - aria-buffer.test.ts (25 tests): LRU + Eviction + Buffer Pool - aria-wal-mvcc.test.ts (22 tests): WAL 编解码 + MVCC 事务 - aria-compress.test.ts (11 tests): LZ4 + Merge Iterator - aria.test.ts (80 tests): AriaEngine 集成 + 边界测试 - Bug 修复: LRUList size 跟踪、WAL 缓冲区越界、ColumnEncoding 导入 - 全面更新 README.md + site/ 站点文件 (index/docs/demo)
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
/**
|
||||
* AriaEngine WAL + MVCC 单元测试
|
||||
*/
|
||||
import { WAL, type WALStore } from '../../src/engine/aria/wal/log';
|
||||
import { WALRecordType, type WALRecord } from '../../src/engine/aria/types';
|
||||
import { CheckpointManager, type Flushable } from '../../src/engine/aria/wal/checkpoint';
|
||||
import { MVCCManager } from '../../src/engine/aria/transaction/mvcc';
|
||||
|
||||
// ===================================================================
|
||||
// WAL 存储 Mock
|
||||
// ===================================================================
|
||||
class MockWALStore implements WALStore {
|
||||
chunks: Uint8Array[] = [];
|
||||
async append(data: Uint8Array) { this.chunks.push(data); }
|
||||
async readAll(): Promise<Uint8Array> {
|
||||
const total = this.chunks.reduce((s, c) => s + c.byteLength, 0);
|
||||
const combined = new Uint8Array(total);
|
||||
let off = 0;
|
||||
for (const c of this.chunks) { combined.set(c, off); off += c.byteLength; }
|
||||
return combined;
|
||||
}
|
||||
async truncate() { this.chunks = []; }
|
||||
async exists() { return this.chunks.length > 0; }
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// WAL 测试
|
||||
// ===================================================================
|
||||
describe('AriaEngine — WAL', () => {
|
||||
it('append 记录后可恢复', async () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, true, 'full');
|
||||
|
||||
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 'users', key: '1', data: { name: 'Alice' } });
|
||||
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 'users', key: '2', data: { name: 'Bob' } });
|
||||
|
||||
const records: WALRecord[] = [];
|
||||
await wal.recover((r) => records.push(r));
|
||||
expect(records).toHaveLength(2);
|
||||
expect(records[0].tableName).toBe('users');
|
||||
expect(records[0].key).toBe('1');
|
||||
});
|
||||
|
||||
it('batch 模式缓冲后 flush', async () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, true, 'batch');
|
||||
|
||||
wal.append({ type: WALRecordType.UPDATE, txnId: 2, tableName: 'items', key: 'a', data: { v: 1 } });
|
||||
wal.append({ type: WALRecordType.DELETE, txnId: 2, tableName: 'items', key: 'b' });
|
||||
|
||||
// 未 flush 前无法恢复
|
||||
let records: WALRecord[] = [];
|
||||
await wal.recover((r) => records.push(r));
|
||||
expect(records).toHaveLength(0);
|
||||
|
||||
await wal.flush();
|
||||
records = [];
|
||||
await wal.recover((r) => records.push(r));
|
||||
expect(records).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('none 模式不记录', async () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, false, 'none');
|
||||
wal.append({ type: WALRecordType.INSERT, txnId: 3, tableName: 'x', key: 'y', data: {} });
|
||||
expect(store.chunks).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('checkpoint 清空 WAL', async () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, true, 'full');
|
||||
|
||||
wal.append({ type: WALRecordType.CREATE_TABLE, txnId: 0, tableName: 't', key: '' });
|
||||
expect(await store.exists()).toBe(true);
|
||||
|
||||
await wal.checkpoint();
|
||||
expect(await store.exists()).toBe(false);
|
||||
});
|
||||
|
||||
it('getLSN 跟踪序列号', () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, true, 'full');
|
||||
expect(wal.getLSN()).toBe(0);
|
||||
wal.append({ type: WALRecordType.BEGIN, txnId: 10, tableName: '', key: '' });
|
||||
expect(wal.getLSN()).toBe(1);
|
||||
wal.append({ type: WALRecordType.COMMIT, txnId: 10, tableName: '', key: '' });
|
||||
expect(wal.getLSN()).toBe(2);
|
||||
});
|
||||
|
||||
it('isEnabled 反映配置', () => {
|
||||
expect(new WAL(new MockWALStore(), true).isEnabled()).toBe(true);
|
||||
expect(new WAL(new MockWALStore(), false).isEnabled()).toBe(false);
|
||||
});
|
||||
|
||||
it('多种记录类型编解码', async () => {
|
||||
const store = new MockWALStore();
|
||||
const wal = new WAL(store, true, 'full');
|
||||
|
||||
wal.append({ type: WALRecordType.BEGIN, txnId: 100, tableName: '', key: '' });
|
||||
wal.append({ type: WALRecordType.INSERT, txnId: 100, tableName: 'users', key: '1', data: { x: 'hello' } });
|
||||
wal.append({ type: WALRecordType.UPDATE, txnId: 100, tableName: 'users', key: '1', data: { x: 'world' } });
|
||||
wal.append({ type: WALRecordType.COMMIT, txnId: 100, tableName: '', key: '' });
|
||||
|
||||
const records: WALRecord[] = [];
|
||||
await wal.recover((r) => records.push(r));
|
||||
expect(records).toHaveLength(4);
|
||||
expect(records[0].type).toBe(WALRecordType.BEGIN);
|
||||
expect(records[1].type).toBe(WALRecordType.INSERT);
|
||||
expect(records[2].type).toBe(WALRecordType.UPDATE);
|
||||
expect(records[3].type).toBe(WALRecordType.COMMIT);
|
||||
});
|
||||
|
||||
it('getBufferedCount 返回缓冲数', () => {
|
||||
const wal = new WAL(new MockWALStore(), true, 'batch');
|
||||
expect(wal.getBufferedCount()).toBe(0);
|
||||
wal.append({ type: WALRecordType.INSERT, txnId: 0, tableName: 't', key: 'k' });
|
||||
expect(wal.getBufferedCount()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// Checkpoint 测试
|
||||
// ===================================================================
|
||||
describe('AriaEngine — CheckpointManager', () => {
|
||||
class MockFlushable implements Flushable { flushed = false; async flushAll() { this.flushed = true; } }
|
||||
|
||||
it('tick 未达间隔不触发 checkpoint', async () => {
|
||||
const flushable = new MockFlushable();
|
||||
const cm = new CheckpointManager(null as any, null as any, flushable, 100);
|
||||
await cm.tick();
|
||||
await cm.tick();
|
||||
expect(cm.getOpCount()).toBe(2);
|
||||
expect(flushable.flushed).toBe(false);
|
||||
});
|
||||
|
||||
it('setInterval 修改间隔', async () => {
|
||||
const cm = new CheckpointManager(null as any, null as any, null, 1000);
|
||||
cm.setInterval(2);
|
||||
await cm.tick();
|
||||
await cm.tick();
|
||||
expect(cm.getOpCount()).toBe(0); // reset after checkpoint
|
||||
});
|
||||
});
|
||||
|
||||
// ===================================================================
|
||||
// MVCC 测试
|
||||
// ===================================================================
|
||||
describe('AriaEngine — MVCC', () => {
|
||||
let mvcc: MVCCManager;
|
||||
|
||||
beforeEach(() => { mvcc = new MVCCManager(); });
|
||||
|
||||
it('beginTransaction 分配唯一 ID', () => {
|
||||
const id1 = mvcc.beginTransaction();
|
||||
const id2 = mvcc.beginTransaction();
|
||||
expect(id1).not.toBe(id2);
|
||||
expect(mvcc.isActive(id1)).toBe(true);
|
||||
expect(mvcc.isActive(id2)).toBe(true);
|
||||
});
|
||||
|
||||
it('commit 后 isActive 返回 false', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.commitTransaction(txnId);
|
||||
expect(mvcc.isActive(txnId)).toBe(false);
|
||||
});
|
||||
|
||||
it('rollback 后 isActive 返回 false', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.rollbackTransaction(txnId);
|
||||
expect(mvcc.isActive(txnId)).toBe(false);
|
||||
});
|
||||
|
||||
it('writeVersion + readVersion 往返', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Alice', age: 30 }, txnId);
|
||||
const val = mvcc.readVersion('users', '1', txnId);
|
||||
expect(val).not.toBeNull();
|
||||
expect(val!.name).toBe('Alice');
|
||||
});
|
||||
|
||||
it('未提交版本对其他事务不可见', () => {
|
||||
const txn1 = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Alice' }, txn1);
|
||||
|
||||
const txn2 = mvcc.beginTransaction();
|
||||
const val = mvcc.readVersion('users', '1', txn2);
|
||||
expect(val).toBeNull(); // txn1's write not yet committed
|
||||
});
|
||||
|
||||
it('commit 后新事务可见', () => {
|
||||
const txn1 = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Alice' }, txn1);
|
||||
mvcc.commitTransaction(txn1);
|
||||
|
||||
const txn2 = mvcc.beginTransaction();
|
||||
const val = mvcc.readVersion('users', '1', txn2);
|
||||
expect(val).not.toBeNull();
|
||||
expect(val!.name).toBe('Alice');
|
||||
});
|
||||
|
||||
it('rollback 移除写入的版本', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Temp' }, txnId);
|
||||
mvcc.rollbackTransaction(txnId);
|
||||
|
||||
const txn2 = mvcc.beginTransaction();
|
||||
expect(mvcc.readVersion('users', '1', txn2)).toBeNull();
|
||||
});
|
||||
|
||||
it('deleteVersion 创建墓碑', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||||
mvcc.commitTransaction(txnId);
|
||||
// delete
|
||||
const txn2 = mvcc.beginTransaction();
|
||||
mvcc.deleteVersion('users', '1', txn2);
|
||||
mvcc.commitTransaction(txn2);
|
||||
// 删除后读取
|
||||
const txn3 = mvcc.beginTransaction();
|
||||
const val = mvcc.readVersion('users', '1', txn3);
|
||||
expect(val).not.toBeNull();
|
||||
expect((val! as any).__mvcc_tombstone).toBe(true);
|
||||
});
|
||||
|
||||
it('getLatestCommittedVersions 返回最新已提交', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||||
mvcc.writeVersion('users', '2', { name: 'Bob' }, txnId);
|
||||
mvcc.commitTransaction(txnId);
|
||||
|
||||
const result = mvcc.getLatestCommittedVersions('users');
|
||||
expect(result['1'].name).toBe('Alice');
|
||||
expect(result['2'].name).toBe('Bob');
|
||||
});
|
||||
|
||||
it('clearTable 清理指定表', () => {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { name: 'A' }, txnId);
|
||||
mvcc.writeVersion('users', '2', { name: 'B' }, txnId);
|
||||
mvcc.writeVersion('orders', 'o1', { amt: 100 }, txnId);
|
||||
mvcc.commitTransaction(txnId);
|
||||
|
||||
mvcc.clearTable('users');
|
||||
const txn2 = mvcc.beginTransaction();
|
||||
expect(mvcc.readVersion('users', '1', txn2)).toBeNull();
|
||||
expect(mvcc.readVersion('users', '2', txn2)).toBeNull();
|
||||
expect(mvcc.readVersion('orders', 'o1', txn2)).not.toBeNull();
|
||||
});
|
||||
|
||||
it('getActiveTxnCount 返回活跃事务数', () => {
|
||||
expect(mvcc.getActiveTxnCount()).toBe(0);
|
||||
mvcc.beginTransaction();
|
||||
mvcc.beginTransaction();
|
||||
expect(mvcc.getActiveTxnCount()).toBe(2);
|
||||
});
|
||||
|
||||
it('gc 清理过旧版本', () => {
|
||||
// 创建很多版本后 gc
|
||||
for (let i = 0; i < 200; i++) {
|
||||
const txnId = mvcc.beginTransaction();
|
||||
mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
||||
mvcc.commitTransaction(txnId);
|
||||
}
|
||||
mvcc.gc(100);
|
||||
// gc 后应可继续操作
|
||||
const txnId = mvcc.beginTransaction();
|
||||
const val = mvcc.readVersion('users', '1', txnId);
|
||||
expect(val).not.toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user