release: v0.4.2 — 生产就绪与崩溃自愈 + 问题清单修复 + 版本迭代
CI / test (18.x) (push) Successful in 10m7s
CI / test (20.x) (push) Successful in 10m6s
CI / test (22.x) (push) Successful in 10m2s
CI / test (24.x) (push) Successful in 10m0s

This commit is contained in:
thzxx
2026-08-09 19:15:37 +08:00
parent a1e4f5071c
commit 22b0b1fad4
34 changed files with 6626 additions and 565 deletions
+67 -4
View File
@@ -87,6 +87,49 @@ export class HybridEngine implements IStorageEngine {
return this.memoryEngine.isOpen() && this.diskEngine.isOpen();
}
// ---- v0.4.2-fix: 自愈 / 重置 / 元数据(委托双引擎) ----
/** 自愈:修复磁盘引擎后重载内存缓存 */
async repair(): Promise<void> {
if (typeof this.diskEngine.repair === 'function') {
await this.diskEngine.repair();
}
await this.reloadMemoryFromDisk();
}
/** 清空全部数据与表结构 */
async clearAll(): Promise<void> {
if (typeof this.diskEngine.clearAll === 'function') {
await this.diskEngine.clearAll();
} else {
const names = await this.diskEngine.getTableNames();
for (const name of names) {
await this.diskEngine.dropTable(name);
}
}
if (typeof this.memoryEngine.clearAll === 'function') {
await this.memoryEngine.clearAll();
} else {
const names = await this.memoryEngine.getTableNames();
for (const name of names) {
await this.memoryEngine.dropTable(name);
}
}
}
async getMeta(key: string): Promise<string | null> {
if (typeof this.diskEngine.getMeta === 'function') {
return this.diskEngine.getMeta(key);
}
return null;
}
async setMeta(key: string, value: string): Promise<void> {
if (typeof this.diskEngine.setMeta === 'function') {
await this.diskEngine.setMeta(key, value);
}
}
// ---- 表管理 ----
async createTable(schema: TableSchema): Promise<void> {
@@ -111,6 +154,22 @@ export class HybridEngine implements IStorageEngine {
return this.memoryEngine.getTableSchema(tableName);
}
/** v0.4.2-fix: 引擎级 ALTER TABLE — 双引擎同步(磁盘持久化 + 内存引用) */
async alterTable(
tableName: string,
action: 'ADD' | 'DROP',
column: import('../constants').ColumnDef & { name: string },
): Promise<void> {
await this.memoryEngine.alterTable(tableName, action, column);
if (typeof this.diskEngine.alterTable === 'function') {
await this.diskEngine.alterTable(tableName, action, column);
} else {
// 磁盘引擎无引擎级实现 → 从磁盘重建内存 schema(disk 引擎 schema 以自身为准)
const schema = await this.diskEngine.getTableSchema(tableName);
if (schema && action === 'DROP') delete schema.columns[column.name];
}
}
// ---- CRUDwrite-through 策略) ----
async insert(tableName: string, rows: Record<string, unknown>[]): Promise<string[]> {
@@ -181,10 +240,14 @@ export class HybridEngine implements IStorageEngine {
await this.diskEngine.commitTransaction();
try {
await this.memoryEngine.commitTransaction();
} catch {
// 内存提交失败时回滚磁盘
await this.diskEngine.rollbackTransaction();
throw new DatabaseError('Hybrid commit failed: memory engine error after disk commit', 'TX_COMMIT_ERROR');
} catch (error) {
// v0.4.2-fix: 磁盘已提交无法回滚(此前调 diskEngine.rollbackTransaction()
// 会抛 TX_NONE 掩盖原错误)。如实上报内存提交失败,磁盘数据保持已提交状态。
throw new DatabaseError(
'Hybrid commit failed: memory engine error after disk commit (disk data is committed)',
'TX_COMMIT_ERROR',
error,
);
}
}