fix(P2): v0.6.3 原子性/一致性/资源治理 — 8 项修复 + 12 回归
- KVStore 混合写单记录原子:新增 writeBatch(put+delete 同一条日志记录), KVStoreEngine 全部混合写路径统一(兑现真原子宣称,崩溃无新旧行并存) - WAL full 模式写入失败抛错(此前 console.warn 吞错 → 崩溃即丢且无感知) - MemoryEngine SET NULL 级联索引残留:复用 removeIndexEntries(消除虚假 UNIQUE_VIOLATION) - delete 级联两阶段:先全量 RESTRICT 预检(沿 CASCADE 链递归)再执行,无部分级联 (Memory/Aria 对齐) - BufferPool 驱逐同步清理 pages Map(EvictionManager onRemove 回调,内存预算真实生效) - MVCC commit 清理已提交版本(版本链仅作事务内 undo,消除行数据双份常驻) - LSM.flush 重复入链修复(入链即置空 immutable)+ frozenMemtables 可见性时序 - rollbackToSavepoint 重建受影响表二级索引(消除过期索引条目) 测试 1114 → 1126(71 套件);行覆盖率 89.7%;版本 0.6.3
This commit is contained in:
@@ -2,6 +2,49 @@
|
|||||||
|
|
||||||
All notable changes to MetonaSqlark will be documented in this file.
|
All notable changes to MetonaSqlark will be documented in this file.
|
||||||
|
|
||||||
|
## [0.6.3] - 2026-08-13
|
||||||
|
|
||||||
|
### 原子性 / 一致性 / 资源治理
|
||||||
|
|
||||||
|
> 深度审计第二阶段:兑现"单记录真原子"宣称、补齐 WAL 失败可见性、
|
||||||
|
> 修复级联/索引残留/内存泄漏类问题。
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **KVStore 混合写非单记录原子(P2,宣称失实)** — delete/update 主键变更等路径
|
||||||
|
putMany 与 deleteMany 分两次调用 = 两条日志记录:崩溃在两条记录之间 → 新旧行并存
|
||||||
|
(主键变更产生重复行 / SET NULL 重写与父行删除不一致)。新增 `KVStore.writeBatch`
|
||||||
|
(put+delete 编码进同一条日志记录,真全有或全无),KVStoreEngine 全部混合写路径
|
||||||
|
(update/delete/dropTable/alterTable/clear/commitTransaction)统一走 writeBatch
|
||||||
|
- **WAL full 模式写入失败吞错(P2)** — append/appendBatch 失败仅 console.warn:
|
||||||
|
内存已提交而 WAL 缺失,崩溃即丢且调用方无感知。现在失败直接抛出(batch 模式
|
||||||
|
flush 早已抛错,行为对齐)
|
||||||
|
- **MemoryEngine SET NULL 级联索引残留(P2)** — 手动 `pks.get(v)?.delete(pk)` 遗留
|
||||||
|
空 Set → checkUniqueness 对旧值永久误报 UNIQUE_VIOLATION(外键列带 unique 约束时)。
|
||||||
|
统一复用 removeIndexEntries(空 Set 正确清理)+ updateIndexes
|
||||||
|
- **delete 级联部分执行(P2)** — 多行删除时第 N 行 RESTRICT 抛错 → 前 N-1 行的级联
|
||||||
|
子行已删除、父行未删(无事务下数据不一致)。改为两阶段:先对全部待删行做 RESTRICT
|
||||||
|
预检(沿 CASCADE 链递归),任何违规则整体拒绝;Memory/Aria 双引擎对齐
|
||||||
|
- **BufferPool 驱逐不清理 pages Map(P2)** — 驱逐仅清 LRU 链表,pages Map 保留全部
|
||||||
|
历史页面 → 内存无限增长(256 页 ≈ 1MB 预算形同虚设)。EvictionManager 增加
|
||||||
|
onRemove 回调,驱逐时同步从 pages Map 移除(容量约束真实生效,重载仍正确)
|
||||||
|
- **MVCC 已提交版本不清理(P2)** — commit 仅标记 committed,versionStore 随写入量
|
||||||
|
无限增长(行数据双份常驻)。快照读取已移除(v0.5.1),版本链仅作事务内 undo:
|
||||||
|
commit 时直接清理本事务版本(rollback/discardVersions 语义不变,gc 保留未提交链裁剪)
|
||||||
|
- **LSM.flush 重复入链(P2)** — freezeMemtable 会把同一张 immutable 再次入链 →
|
||||||
|
重复 SSTable(2 行 flush 出 3 个文件);且 frozenMemtables 在链执行前被清空 →
|
||||||
|
并发读短暂看不到冻结数据。修复:入链后立即置空 immutable(防重复),
|
||||||
|
frozenMemtables 保持到链排空后再清理
|
||||||
|
- **rollbackToSavepoint 不重建二级索引(P2)** — 事务内直写索引 LSM,savepoint 回滚
|
||||||
|
只还原快照 → savepoint 之后的过期索引条目残留。回滚后重建受影响表索引
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- 测试 1114 → **1126**(71 套件,+12 个 v0.6.3 回归);新增 `tests/v063-fixes.test.ts`;
|
||||||
|
MVCC 语义测试同步改写(commit 清理版本、gc 裁剪未提交链)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## [0.6.2] - 2026-08-13
|
## [0.6.2] - 2026-08-13
|
||||||
|
|
||||||
### 深度审计修复(数据正确性专项)
|
### 深度审计修复(数据正确性专项)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# MetonaSqlark
|
# MetonaSqlark
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://img.shields.io/badge/version-0.6.2-blue?style=flat-square" alt="version">
|
<img src="https://img.shields.io/badge/version-0.6.3-blue?style=flat-square" alt="version">
|
||||||
<img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="license">
|
<img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="license">
|
||||||
<img src="https://img.shields.io/badge/coverage-89.6%25-brightgreen?style=flat-square" alt="coverage">
|
<img src="https://img.shields.io/badge/coverage-89.7%25-brightgreen?style=flat-square" alt="coverage">
|
||||||
<img src="https://img.shields.io/badge/tests-1114%20passed-success?style=flat-square" alt="tests">
|
<img src="https://img.shields.io/badge/tests-1126%20passed-success?style=flat-square" alt="tests">
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
> 基于 TypeScript 的**前端关系型数据库**:完整 SQL + Query Builder 双 API,
|
> 基于 TypeScript 的**前端关系型数据库**:完整 SQL + Query Builder 双 API,
|
||||||
@@ -397,7 +397,7 @@ const { data, loading, error, refresh } = useSqlarkQuery(db, 'SELECT * FROM user
|
|||||||
npm install # 安装依赖
|
npm install # 安装依赖
|
||||||
npm run dev # 开发模式(localhost:3001)
|
npm run dev # 开发模式(localhost:3001)
|
||||||
npm run build # 生产构建(生成 dist/)
|
npm run build # 生产构建(生成 dist/)
|
||||||
npm test # 运行测试(1114 用例 · 70 套件)
|
npm test # 运行测试(1126 用例 · 71 套件)
|
||||||
npm run test:e2e # Playwright e2e(真实 Chromium + OPFS + 崩溃注入,需先 build)
|
npm run test:e2e # Playwright e2e(真实 Chromium + OPFS + 崩溃注入,需先 build)
|
||||||
npm run lint # 代码检查
|
npm run lint # 代码检查
|
||||||
npm run typecheck # 类型检查
|
npm run typecheck # 类型检查
|
||||||
@@ -409,9 +409,9 @@ npm run typecheck # 类型检查
|
|||||||
|
|
||||||
| 指标 | 数值 |
|
| 指标 | 数值 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| 测试用例 | 1114(+12 Playwright e2e) |
|
| 测试用例 | 1126(+12 Playwright e2e) |
|
||||||
| 测试套件 | 70 |
|
| 测试套件 | 70 |
|
||||||
| 行覆盖率 | 89.6% |
|
| 行覆盖率 | 89.7% |
|
||||||
| SQL 关键字 | 72 |
|
| SQL 关键字 | 72 |
|
||||||
| 存储引擎 | 5(Memory / KVStore / OPFS / Hybrid / Aria) |
|
| 存储引擎 | 5(Memory / KVStore / OPFS / Hybrid / Aria) |
|
||||||
| 运行时依赖 | 0 |
|
| 运行时依赖 | 0 |
|
||||||
|
|||||||
Vendored
+166
-53
@@ -34,7 +34,7 @@ class DatabaseError extends Error {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// 版本
|
// 版本
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
const VERSION = '0.6.2';
|
const VERSION = '0.6.3';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
||||||
@@ -477,7 +477,16 @@ class MemoryEngine {
|
|||||||
toDelete.push(pk);
|
toDelete.push(pk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 级联删除:检查引用此表的其他表
|
// v0.6.3-fix: 级联两阶段 —— 先对全部待删行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 任何一行违规则整体拒绝。此前逐行执行:第 N 行 RESTRICT 抛错时,前 N-1 行的
|
||||||
|
// 级联子行已被删除、父行未删 → 无事务下部分级联(数据不一致)
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pk of toDelete) {
|
||||||
|
const row = table.get(pk);
|
||||||
|
if (row)
|
||||||
|
this.checkCascadeRestrict(tableName, pk, restrictVisited);
|
||||||
|
}
|
||||||
|
// 级联删除:检查引用此表的其他表(RESTRICT 已预检通过,此阶段不再抛错)
|
||||||
let cascadeCount = 0;
|
let cascadeCount = 0;
|
||||||
for (const pk of toDelete) {
|
for (const pk of toDelete) {
|
||||||
const row = table.get(pk);
|
const row = table.get(pk);
|
||||||
@@ -488,6 +497,43 @@ class MemoryEngine {
|
|||||||
table.delete(pk);
|
table.delete(pk);
|
||||||
return toDelete.length + cascadeCount;
|
return toDelete.length + cascadeCount;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refTableData = this.tables.get(refTableName);
|
||||||
|
if (!refTableData)
|
||||||
|
continue;
|
||||||
|
const refPks = [];
|
||||||
|
for (const [refPk, refRow] of refTableData) {
|
||||||
|
if (String(refRow[colName]) === pkValue)
|
||||||
|
refPks.push(refPk);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && refPks.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
for (const refPk of refPks) {
|
||||||
|
this.checkCascadeRestrict(refTableName, refPk, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
this.ensureTable(tableName);
|
this.ensureTable(tableName);
|
||||||
const table = this.tables.get(tableName);
|
const table = this.tables.get(tableName);
|
||||||
@@ -782,14 +828,12 @@ class MemoryEngine {
|
|||||||
for (const refPk of toDelete) {
|
for (const refPk of toDelete) {
|
||||||
const refRow = refTableData.get(refPk);
|
const refRow = refTableData.get(refPk);
|
||||||
if (refRow) {
|
if (refRow) {
|
||||||
// v0.3.3: 外键列置空后同步更新索引
|
// v0.6.3-fix: 复用 removeIndexEntries 清理旧值索引 —— 此前手动
|
||||||
if (refRow[colName] !== undefined && refRow[colName] !== null) {
|
// `pks.get(v)?.delete(pk)` 后遗留空 Set → checkUniqueness 对旧值
|
||||||
const pks = this.indexes.get(refTableName)?.get(colName);
|
// 永久误报 UNIQUE_VIOLATION(外键列带 unique 约束时)
|
||||||
if (pks) {
|
this.removeIndexEntries(refTableName, refRow, refPk);
|
||||||
pks.get(refRow[colName])?.delete(refPk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
refRow[colName] = null;
|
refRow[colName] = null;
|
||||||
|
this.updateIndexes(refTableName, refRow, refPk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1577,6 +1621,19 @@ class KVStore {
|
|||||||
await this.appendRecord({}, keys);
|
await this.appendRecord({}, keys);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: 多 key 混合原子写(put + delete 编码进同一条日志记录)。
|
||||||
|
* 此前 KVStoreEngine 的 delete/update 主键变更等路径 putMany 与 deleteMany
|
||||||
|
* 分两次调用 = 两条记录:崩溃在两条记录之间 → 新旧行并存(重复行/脏数据),
|
||||||
|
* 与"一条日志记录 = 真原子"宣称不符。此方法保证混合操作全有或全无。
|
||||||
|
*/
|
||||||
|
async writeBatch(puts, deletes) {
|
||||||
|
if (Object.keys(puts).length === 0 && deletes.length === 0)
|
||||||
|
return;
|
||||||
|
await this.enqueue(async () => {
|
||||||
|
await this.appendRecord(puts, deletes);
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
||||||
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
||||||
@@ -1776,7 +1833,7 @@ class KVStore {
|
|||||||
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
||||||
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
||||||
* 主键变更/级联场景整表 diff;全部原子)
|
* 主键变更/级联场景整表 diff;全部原子)
|
||||||
* - 事务:内存快照 + commit 时受影响表原子 flush(putMany 单记录 = 真原子,
|
* - 事务:内存快照 + commit 时受影响表原子 flush(writeBatch 单记录 = 真原子,
|
||||||
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
||||||
*
|
*
|
||||||
* 数据布局(KVStore keys):
|
* 数据布局(KVStore keys):
|
||||||
@@ -1941,10 +1998,7 @@ class KVStoreEngine {
|
|||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
// 删除该表全部行(KV 中残留清理)
|
// 删除该表全部行(KV 中残留清理)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
async hasTable(tableName) {
|
async hasTable(tableName) {
|
||||||
this.ensureOpen();
|
this.ensureOpen();
|
||||||
@@ -1970,10 +2024,7 @@ class KVStoreEngine {
|
|||||||
if (action === 'DROP') {
|
if (action === 'DROP') {
|
||||||
// 重写存储行(移除该列)
|
// 重写存储行(移除该列)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ---- CRUD ----
|
// ---- CRUD ----
|
||||||
@@ -2055,10 +2106,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async delete(tableName, query) {
|
async delete(tableName, query) {
|
||||||
@@ -2080,10 +2128,7 @@ class KVStoreEngine {
|
|||||||
Object.assign(puts, diff.puts);
|
Object.assign(puts, diff.puts);
|
||||||
deletes.push(...diff.deletes);
|
deletes.push(...diff.deletes);
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
@@ -2098,10 +2143,7 @@ class KVStoreEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
// ---- 动态索引 ----
|
// ---- 动态索引 ----
|
||||||
async createIndex(tableName, column, unique) {
|
async createIndex(tableName, column, unique) {
|
||||||
@@ -2156,10 +2198,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
// 事务内 DDL 的 schema 一并持久化
|
// 事务内 DDL 的 schema 一并持久化
|
||||||
if (this.txSchemaChanged) {
|
if (this.txSchemaChanged) {
|
||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
@@ -2240,7 +2279,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
||||||
* 调用方合并到单次原子 putMany/deleteMany(多表操作真原子)。
|
* 调用方合并到单次原子 writeBatch(put+delete 同一条日志记录,多表操作真原子)。
|
||||||
*/
|
*/
|
||||||
async collectTableDiff(tableName) {
|
async collectTableDiff(tableName) {
|
||||||
const prefix = this.rowPrefix(tableName);
|
const prefix = this.rowPrefix(tableName);
|
||||||
@@ -4165,20 +4204,28 @@ class LSM {
|
|||||||
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
||||||
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
||||||
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
||||||
|
//
|
||||||
|
// v0.6.3-fix: ① 入链后立即置空 immutable —— 此前 freezeMemtable 会把同一张
|
||||||
|
// immutable 再次入链 → 重复 SSTable(2 行 flush 出 3 个文件);② frozenMemtables
|
||||||
|
// 保持到链排空后再清理 —— 此前入链后立即清空,链执行期间并发读看不到冻结数据
|
||||||
|
// (短暂数据不可见窗口)。
|
||||||
if (this.immutableMemtable) {
|
if (this.immutableMemtable) {
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
if (this.memtable.getEntryCount() > 0) {
|
if (this.memtable.getEntryCount() > 0) {
|
||||||
this.freezeMemtable();
|
this.freezeMemtable();
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
if (frozen) {
|
if (frozen) {
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.frozenMemtables = [];
|
|
||||||
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
||||||
await this.drainChain();
|
await this.drainChain();
|
||||||
|
// 清理空残留冻结表(flushImmutableAsync 完成时已自行移除已落盘的)
|
||||||
|
this.frozenMemtables = this.frozenMemtables.filter((f) => f.getEntryCount() > 0);
|
||||||
}
|
}
|
||||||
async clear() {
|
async clear() {
|
||||||
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
||||||
@@ -4405,15 +4452,11 @@ class WAL {
|
|||||||
};
|
};
|
||||||
const bytes = this.encodeRecord(fullRecord);
|
const bytes = this.encodeRecord(fullRecord);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 写入失败必须抛给调用方 —— 此前仅 console.warn 吞错:
|
||||||
|
// 内存已提交而 WAL 缺失,崩溃即丢且调用方无感知。
|
||||||
await this.store.append(bytes);
|
await this.store.append(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(bytes);
|
this.buffer.push(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
@@ -4431,15 +4474,10 @@ class WAL {
|
|||||||
}
|
}
|
||||||
const combined = this.mergeChunks(chunks);
|
const combined = this.mergeChunks(chunks);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 同 append —— 批量写入失败抛给调用方(不再吞错)
|
||||||
await this.store.append(combined);
|
await this.store.append(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append batch record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(combined);
|
this.buffer.push(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
@@ -5499,17 +5537,21 @@ class MVCCManager {
|
|||||||
throw new Error(`Transaction ${txnId} not found`);
|
throw new Error(`Transaction ${txnId} not found`);
|
||||||
txn.state = TransactionState.COMMITTED;
|
txn.state = TransactionState.COMMITTED;
|
||||||
this.globalCommitLsn++;
|
this.globalCommitLsn++;
|
||||||
// v0.4.2-fix: 仅标记本事务写入的版本(此前遍历全库 versionStore)
|
// v0.6.3-fix: 已提交版本直接清理 —— 快照读取已移除(v0.5.1),版本链仅作
|
||||||
|
// 事务内 undo 记录(rollback/savepoint 用),提交后 LSM 持有权威数据。
|
||||||
|
// 此前 commit 仅标记 committed → versionStore 随写入量无限增长(行数据双份常驻)。
|
||||||
const writeKeys = this.txnWriteKeys.get(txnId);
|
const writeKeys = this.txnWriteKeys.get(txnId);
|
||||||
if (writeKeys) {
|
if (writeKeys) {
|
||||||
for (const tableKey of writeKeys) {
|
for (const tableKey of writeKeys) {
|
||||||
const versions = this.versionStore.get(tableKey);
|
const versions = this.versionStore.get(tableKey);
|
||||||
if (!versions)
|
if (!versions)
|
||||||
continue;
|
continue;
|
||||||
for (const version of versions) {
|
const filtered = versions.filter((v) => v.txnId !== txnId);
|
||||||
if (version.txnId === txnId) {
|
if (filtered.length === 0) {
|
||||||
version.committed = true;
|
this.versionStore.delete(tableKey);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.versionStore.set(tableKey, filtered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5751,10 +5793,16 @@ class LRUList {
|
|||||||
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
||||||
*/
|
*/
|
||||||
class EvictionManager {
|
class EvictionManager {
|
||||||
constructor(capacity, onEvict) {
|
/**
|
||||||
|
* @param onEvict 驱逐脏页时的写回回调
|
||||||
|
* @param onRemove v0.6.3: 页面被驱逐时的回调(BufferPool 用于同步清理 pages Map;
|
||||||
|
* 此前驱逐只清 LRU 链表,pages Map 无限增长 → 1MB 内存预算形同虚设)
|
||||||
|
*/
|
||||||
|
constructor(capacity, onEvict, onRemove) {
|
||||||
this.lru = new LRUList();
|
this.lru = new LRUList();
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.onEvict = onEvict;
|
this.onEvict = onEvict;
|
||||||
|
this.onRemove = onRemove;
|
||||||
}
|
}
|
||||||
/** 访问页面,更新 LRU */
|
/** 访问页面,更新 LRU */
|
||||||
access(page) {
|
access(page) {
|
||||||
@@ -5787,6 +5835,7 @@ class EvictionManager {
|
|||||||
victim.dirty = false;
|
victim.dirty = false;
|
||||||
}
|
}
|
||||||
this.lru.remove(victim);
|
this.lru.remove(victim);
|
||||||
|
this.onRemove?.(victim);
|
||||||
evicted++;
|
evicted++;
|
||||||
}
|
}
|
||||||
return evicted;
|
return evicted;
|
||||||
@@ -5834,6 +5883,10 @@ class BufferPool {
|
|||||||
await this.pageIO.writePage(page.pageId, page.data);
|
await this.pageIO.writePage(page.pageId, page.data);
|
||||||
page.dirty = false;
|
page.dirty = false;
|
||||||
}
|
}
|
||||||
|
}, (page) => {
|
||||||
|
// v0.6.3-fix: 驱逐时同步从 pages Map 移除 —— 此前仅清 LRU 链表,
|
||||||
|
// pages Map 保留全部历史页面 → 内存无限增长(1MB 预算失效)
|
||||||
|
this.pages.delete(page.pageId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -6826,6 +6879,18 @@ class AriaEngine {
|
|||||||
const walRecords = [];
|
const walRecords = [];
|
||||||
// v0.4.1: 外键级联(环路保护)
|
// v0.4.1: 外键级联(环路保护)
|
||||||
const visited = new Set();
|
const visited = new Set();
|
||||||
|
// v0.6.3-fix: 级联两阶段 —— 先对全部匹配行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 否则第 N 行 RESTRICT 抛错时前 N-1 行的级联已执行 → 无事务部分级联(数据不一致)
|
||||||
|
const matchedPks = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
if (!query.where || Object.keys(query.where).length === 0 || matchWhere(row, query.where)) {
|
||||||
|
matchedPks.push(String(row[this.tablePKs.get(tableName)]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pkValue of matchedPks) {
|
||||||
|
await this.checkCascadeRestrict(tableName, pkValue, restrictVisited);
|
||||||
|
}
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const pkCol = this.tablePKs.get(tableName);
|
const pkCol = this.tablePKs.get(tableName);
|
||||||
const key = `${tableName}:${row[pkCol]}`;
|
const key = `${tableName}:${row[pkCol]}`;
|
||||||
@@ -6857,6 +6922,38 @@ class AriaEngine {
|
|||||||
this.trimAllCaches();
|
this.trimAllCaches();
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一,与 MemoryEngine 对齐)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
async checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refRows = await this.getAllRows(refTableName);
|
||||||
|
const matched = refRows.filter((r) => String(r[colName]) === pkValue);
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && matched.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
const refPkCol = this.tablePKs.get(refTableName);
|
||||||
|
for (const refRow of matched) {
|
||||||
|
await this.checkCascadeRestrict(refTableName, String(refRow[refPkCol]), visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
||||||
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
||||||
@@ -7249,6 +7346,16 @@ class AriaEngine {
|
|||||||
const sp = this.savepoints.get(name);
|
const sp = this.savepoints.get(name);
|
||||||
if (!sp)
|
if (!sp)
|
||||||
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
||||||
|
// v0.6.3-fix: 记录当前快照涉及的表(回滚后重建索引)—— 此前事务内直写
|
||||||
|
// 索引 LSM,savepoint 回滚只还原快照 → savepoint 之后的索引条目残留
|
||||||
|
const affectedTables = new Set();
|
||||||
|
if (this.txnSnapshot) {
|
||||||
|
for (const key of this.txnSnapshot.keys()) {
|
||||||
|
const idx = key.indexOf(':');
|
||||||
|
if (idx > 0)
|
||||||
|
affectedTables.add(key.slice(0, idx));
|
||||||
|
}
|
||||||
|
}
|
||||||
// 恢复到 savepoint 时的快照
|
// 恢复到 savepoint 时的快照
|
||||||
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
||||||
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
||||||
@@ -7264,6 +7371,12 @@ class AriaEngine {
|
|||||||
if (found)
|
if (found)
|
||||||
this.savepoints.delete(k);
|
this.savepoints.delete(k);
|
||||||
}
|
}
|
||||||
|
// v0.6.3-fix: 重建受影响表二级索引(消除 savepoint 之后的过期索引条目)
|
||||||
|
for (const tableName of affectedTables) {
|
||||||
|
if (this.schemas.has(tableName)) {
|
||||||
|
await this.reindexTable(tableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async releaseSavepoint(name) {
|
async releaseSavepoint(name) {
|
||||||
if (!this.savepoints.has(name))
|
if (!this.savepoints.has(name))
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+12
-2
@@ -164,7 +164,7 @@ interface MetonaPlugin {
|
|||||||
/** 销毁 */
|
/** 销毁 */
|
||||||
destroy(): void;
|
destroy(): void;
|
||||||
}
|
}
|
||||||
declare const VERSION = "0.6.2";
|
declare const VERSION = "0.6.3";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* metona-sqlark Plugin — 插件系统
|
* metona-sqlark Plugin — 插件系统
|
||||||
@@ -804,6 +804,11 @@ declare class MemoryEngine implements IStorageEngine {
|
|||||||
*/
|
*/
|
||||||
private applyUpdateCascade;
|
private applyUpdateCascade;
|
||||||
delete(tableName: string, query: QueryPlan): Promise<number>;
|
delete(tableName: string, query: QueryPlan): Promise<number>;
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
private checkCascadeRestrict;
|
||||||
count(tableName: string, query?: QueryPlan): Promise<number>;
|
count(tableName: string, query?: QueryPlan): Promise<number>;
|
||||||
clear(tableName: string): Promise<void>;
|
clear(tableName: string): Promise<void>;
|
||||||
createIndex(tableName: string, column: string, unique?: boolean): Promise<void>;
|
createIndex(tableName: string, column: string, unique?: boolean): Promise<void>;
|
||||||
@@ -940,7 +945,7 @@ declare class KVStoreEngine implements IStorageEngine {
|
|||||||
private persistSchema;
|
private persistSchema;
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
||||||
* 调用方合并到单次原子 putMany/deleteMany(多表操作真原子)。
|
* 调用方合并到单次原子 writeBatch(put+delete 同一条日志记录,多表操作真原子)。
|
||||||
*/
|
*/
|
||||||
private collectTableDiff;
|
private collectTableDiff;
|
||||||
}
|
}
|
||||||
@@ -1009,6 +1014,11 @@ declare class AriaEngine implements IStorageEngine {
|
|||||||
*/
|
*/
|
||||||
private applyForeignKeyUpdateRules;
|
private applyForeignKeyUpdateRules;
|
||||||
delete(tableName: string, query: QueryPlan): Promise<number>;
|
delete(tableName: string, query: QueryPlan): Promise<number>;
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一,与 MemoryEngine 对齐)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
private checkCascadeRestrict;
|
||||||
/**
|
/**
|
||||||
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
||||||
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
||||||
|
|||||||
Vendored
+166
-53
@@ -30,7 +30,7 @@ class DatabaseError extends Error {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// 版本
|
// 版本
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
const VERSION = '0.6.2';
|
const VERSION = '0.6.3';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
||||||
@@ -473,7 +473,16 @@ class MemoryEngine {
|
|||||||
toDelete.push(pk);
|
toDelete.push(pk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 级联删除:检查引用此表的其他表
|
// v0.6.3-fix: 级联两阶段 —— 先对全部待删行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 任何一行违规则整体拒绝。此前逐行执行:第 N 行 RESTRICT 抛错时,前 N-1 行的
|
||||||
|
// 级联子行已被删除、父行未删 → 无事务下部分级联(数据不一致)
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pk of toDelete) {
|
||||||
|
const row = table.get(pk);
|
||||||
|
if (row)
|
||||||
|
this.checkCascadeRestrict(tableName, pk, restrictVisited);
|
||||||
|
}
|
||||||
|
// 级联删除:检查引用此表的其他表(RESTRICT 已预检通过,此阶段不再抛错)
|
||||||
let cascadeCount = 0;
|
let cascadeCount = 0;
|
||||||
for (const pk of toDelete) {
|
for (const pk of toDelete) {
|
||||||
const row = table.get(pk);
|
const row = table.get(pk);
|
||||||
@@ -484,6 +493,43 @@ class MemoryEngine {
|
|||||||
table.delete(pk);
|
table.delete(pk);
|
||||||
return toDelete.length + cascadeCount;
|
return toDelete.length + cascadeCount;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refTableData = this.tables.get(refTableName);
|
||||||
|
if (!refTableData)
|
||||||
|
continue;
|
||||||
|
const refPks = [];
|
||||||
|
for (const [refPk, refRow] of refTableData) {
|
||||||
|
if (String(refRow[colName]) === pkValue)
|
||||||
|
refPks.push(refPk);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && refPks.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
for (const refPk of refPks) {
|
||||||
|
this.checkCascadeRestrict(refTableName, refPk, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
this.ensureTable(tableName);
|
this.ensureTable(tableName);
|
||||||
const table = this.tables.get(tableName);
|
const table = this.tables.get(tableName);
|
||||||
@@ -778,14 +824,12 @@ class MemoryEngine {
|
|||||||
for (const refPk of toDelete) {
|
for (const refPk of toDelete) {
|
||||||
const refRow = refTableData.get(refPk);
|
const refRow = refTableData.get(refPk);
|
||||||
if (refRow) {
|
if (refRow) {
|
||||||
// v0.3.3: 外键列置空后同步更新索引
|
// v0.6.3-fix: 复用 removeIndexEntries 清理旧值索引 —— 此前手动
|
||||||
if (refRow[colName] !== undefined && refRow[colName] !== null) {
|
// `pks.get(v)?.delete(pk)` 后遗留空 Set → checkUniqueness 对旧值
|
||||||
const pks = this.indexes.get(refTableName)?.get(colName);
|
// 永久误报 UNIQUE_VIOLATION(外键列带 unique 约束时)
|
||||||
if (pks) {
|
this.removeIndexEntries(refTableName, refRow, refPk);
|
||||||
pks.get(refRow[colName])?.delete(refPk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
refRow[colName] = null;
|
refRow[colName] = null;
|
||||||
|
this.updateIndexes(refTableName, refRow, refPk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1573,6 +1617,19 @@ class KVStore {
|
|||||||
await this.appendRecord({}, keys);
|
await this.appendRecord({}, keys);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: 多 key 混合原子写(put + delete 编码进同一条日志记录)。
|
||||||
|
* 此前 KVStoreEngine 的 delete/update 主键变更等路径 putMany 与 deleteMany
|
||||||
|
* 分两次调用 = 两条记录:崩溃在两条记录之间 → 新旧行并存(重复行/脏数据),
|
||||||
|
* 与"一条日志记录 = 真原子"宣称不符。此方法保证混合操作全有或全无。
|
||||||
|
*/
|
||||||
|
async writeBatch(puts, deletes) {
|
||||||
|
if (Object.keys(puts).length === 0 && deletes.length === 0)
|
||||||
|
return;
|
||||||
|
await this.enqueue(async () => {
|
||||||
|
await this.appendRecord(puts, deletes);
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
||||||
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
||||||
@@ -1772,7 +1829,7 @@ class KVStore {
|
|||||||
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
||||||
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
||||||
* 主键变更/级联场景整表 diff;全部原子)
|
* 主键变更/级联场景整表 diff;全部原子)
|
||||||
* - 事务:内存快照 + commit 时受影响表原子 flush(putMany 单记录 = 真原子,
|
* - 事务:内存快照 + commit 时受影响表原子 flush(writeBatch 单记录 = 真原子,
|
||||||
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
||||||
*
|
*
|
||||||
* 数据布局(KVStore keys):
|
* 数据布局(KVStore keys):
|
||||||
@@ -1937,10 +1994,7 @@ class KVStoreEngine {
|
|||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
// 删除该表全部行(KV 中残留清理)
|
// 删除该表全部行(KV 中残留清理)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
async hasTable(tableName) {
|
async hasTable(tableName) {
|
||||||
this.ensureOpen();
|
this.ensureOpen();
|
||||||
@@ -1966,10 +2020,7 @@ class KVStoreEngine {
|
|||||||
if (action === 'DROP') {
|
if (action === 'DROP') {
|
||||||
// 重写存储行(移除该列)
|
// 重写存储行(移除该列)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ---- CRUD ----
|
// ---- CRUD ----
|
||||||
@@ -2051,10 +2102,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async delete(tableName, query) {
|
async delete(tableName, query) {
|
||||||
@@ -2076,10 +2124,7 @@ class KVStoreEngine {
|
|||||||
Object.assign(puts, diff.puts);
|
Object.assign(puts, diff.puts);
|
||||||
deletes.push(...diff.deletes);
|
deletes.push(...diff.deletes);
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
@@ -2094,10 +2139,7 @@ class KVStoreEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
// ---- 动态索引 ----
|
// ---- 动态索引 ----
|
||||||
async createIndex(tableName, column, unique) {
|
async createIndex(tableName, column, unique) {
|
||||||
@@ -2152,10 +2194,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
// 事务内 DDL 的 schema 一并持久化
|
// 事务内 DDL 的 schema 一并持久化
|
||||||
if (this.txSchemaChanged) {
|
if (this.txSchemaChanged) {
|
||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
@@ -2236,7 +2275,7 @@ class KVStoreEngine {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
||||||
* 调用方合并到单次原子 putMany/deleteMany(多表操作真原子)。
|
* 调用方合并到单次原子 writeBatch(put+delete 同一条日志记录,多表操作真原子)。
|
||||||
*/
|
*/
|
||||||
async collectTableDiff(tableName) {
|
async collectTableDiff(tableName) {
|
||||||
const prefix = this.rowPrefix(tableName);
|
const prefix = this.rowPrefix(tableName);
|
||||||
@@ -4161,20 +4200,28 @@ class LSM {
|
|||||||
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
||||||
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
||||||
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
||||||
|
//
|
||||||
|
// v0.6.3-fix: ① 入链后立即置空 immutable —— 此前 freezeMemtable 会把同一张
|
||||||
|
// immutable 再次入链 → 重复 SSTable(2 行 flush 出 3 个文件);② frozenMemtables
|
||||||
|
// 保持到链排空后再清理 —— 此前入链后立即清空,链执行期间并发读看不到冻结数据
|
||||||
|
// (短暂数据不可见窗口)。
|
||||||
if (this.immutableMemtable) {
|
if (this.immutableMemtable) {
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
if (this.memtable.getEntryCount() > 0) {
|
if (this.memtable.getEntryCount() > 0) {
|
||||||
this.freezeMemtable();
|
this.freezeMemtable();
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
if (frozen) {
|
if (frozen) {
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.frozenMemtables = [];
|
|
||||||
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
||||||
await this.drainChain();
|
await this.drainChain();
|
||||||
|
// 清理空残留冻结表(flushImmutableAsync 完成时已自行移除已落盘的)
|
||||||
|
this.frozenMemtables = this.frozenMemtables.filter((f) => f.getEntryCount() > 0);
|
||||||
}
|
}
|
||||||
async clear() {
|
async clear() {
|
||||||
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
||||||
@@ -4401,15 +4448,11 @@ class WAL {
|
|||||||
};
|
};
|
||||||
const bytes = this.encodeRecord(fullRecord);
|
const bytes = this.encodeRecord(fullRecord);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 写入失败必须抛给调用方 —— 此前仅 console.warn 吞错:
|
||||||
|
// 内存已提交而 WAL 缺失,崩溃即丢且调用方无感知。
|
||||||
await this.store.append(bytes);
|
await this.store.append(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(bytes);
|
this.buffer.push(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
@@ -4427,15 +4470,10 @@ class WAL {
|
|||||||
}
|
}
|
||||||
const combined = this.mergeChunks(chunks);
|
const combined = this.mergeChunks(chunks);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 同 append —— 批量写入失败抛给调用方(不再吞错)
|
||||||
await this.store.append(combined);
|
await this.store.append(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append batch record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(combined);
|
this.buffer.push(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
@@ -5495,17 +5533,21 @@ class MVCCManager {
|
|||||||
throw new Error(`Transaction ${txnId} not found`);
|
throw new Error(`Transaction ${txnId} not found`);
|
||||||
txn.state = TransactionState.COMMITTED;
|
txn.state = TransactionState.COMMITTED;
|
||||||
this.globalCommitLsn++;
|
this.globalCommitLsn++;
|
||||||
// v0.4.2-fix: 仅标记本事务写入的版本(此前遍历全库 versionStore)
|
// v0.6.3-fix: 已提交版本直接清理 —— 快照读取已移除(v0.5.1),版本链仅作
|
||||||
|
// 事务内 undo 记录(rollback/savepoint 用),提交后 LSM 持有权威数据。
|
||||||
|
// 此前 commit 仅标记 committed → versionStore 随写入量无限增长(行数据双份常驻)。
|
||||||
const writeKeys = this.txnWriteKeys.get(txnId);
|
const writeKeys = this.txnWriteKeys.get(txnId);
|
||||||
if (writeKeys) {
|
if (writeKeys) {
|
||||||
for (const tableKey of writeKeys) {
|
for (const tableKey of writeKeys) {
|
||||||
const versions = this.versionStore.get(tableKey);
|
const versions = this.versionStore.get(tableKey);
|
||||||
if (!versions)
|
if (!versions)
|
||||||
continue;
|
continue;
|
||||||
for (const version of versions) {
|
const filtered = versions.filter((v) => v.txnId !== txnId);
|
||||||
if (version.txnId === txnId) {
|
if (filtered.length === 0) {
|
||||||
version.committed = true;
|
this.versionStore.delete(tableKey);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.versionStore.set(tableKey, filtered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5747,10 +5789,16 @@ class LRUList {
|
|||||||
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
||||||
*/
|
*/
|
||||||
class EvictionManager {
|
class EvictionManager {
|
||||||
constructor(capacity, onEvict) {
|
/**
|
||||||
|
* @param onEvict 驱逐脏页时的写回回调
|
||||||
|
* @param onRemove v0.6.3: 页面被驱逐时的回调(BufferPool 用于同步清理 pages Map;
|
||||||
|
* 此前驱逐只清 LRU 链表,pages Map 无限增长 → 1MB 内存预算形同虚设)
|
||||||
|
*/
|
||||||
|
constructor(capacity, onEvict, onRemove) {
|
||||||
this.lru = new LRUList();
|
this.lru = new LRUList();
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.onEvict = onEvict;
|
this.onEvict = onEvict;
|
||||||
|
this.onRemove = onRemove;
|
||||||
}
|
}
|
||||||
/** 访问页面,更新 LRU */
|
/** 访问页面,更新 LRU */
|
||||||
access(page) {
|
access(page) {
|
||||||
@@ -5783,6 +5831,7 @@ class EvictionManager {
|
|||||||
victim.dirty = false;
|
victim.dirty = false;
|
||||||
}
|
}
|
||||||
this.lru.remove(victim);
|
this.lru.remove(victim);
|
||||||
|
this.onRemove?.(victim);
|
||||||
evicted++;
|
evicted++;
|
||||||
}
|
}
|
||||||
return evicted;
|
return evicted;
|
||||||
@@ -5830,6 +5879,10 @@ class BufferPool {
|
|||||||
await this.pageIO.writePage(page.pageId, page.data);
|
await this.pageIO.writePage(page.pageId, page.data);
|
||||||
page.dirty = false;
|
page.dirty = false;
|
||||||
}
|
}
|
||||||
|
}, (page) => {
|
||||||
|
// v0.6.3-fix: 驱逐时同步从 pages Map 移除 —— 此前仅清 LRU 链表,
|
||||||
|
// pages Map 保留全部历史页面 → 内存无限增长(1MB 预算失效)
|
||||||
|
this.pages.delete(page.pageId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -6822,6 +6875,18 @@ class AriaEngine {
|
|||||||
const walRecords = [];
|
const walRecords = [];
|
||||||
// v0.4.1: 外键级联(环路保护)
|
// v0.4.1: 外键级联(环路保护)
|
||||||
const visited = new Set();
|
const visited = new Set();
|
||||||
|
// v0.6.3-fix: 级联两阶段 —— 先对全部匹配行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 否则第 N 行 RESTRICT 抛错时前 N-1 行的级联已执行 → 无事务部分级联(数据不一致)
|
||||||
|
const matchedPks = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
if (!query.where || Object.keys(query.where).length === 0 || matchWhere(row, query.where)) {
|
||||||
|
matchedPks.push(String(row[this.tablePKs.get(tableName)]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pkValue of matchedPks) {
|
||||||
|
await this.checkCascadeRestrict(tableName, pkValue, restrictVisited);
|
||||||
|
}
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const pkCol = this.tablePKs.get(tableName);
|
const pkCol = this.tablePKs.get(tableName);
|
||||||
const key = `${tableName}:${row[pkCol]}`;
|
const key = `${tableName}:${row[pkCol]}`;
|
||||||
@@ -6853,6 +6918,38 @@ class AriaEngine {
|
|||||||
this.trimAllCaches();
|
this.trimAllCaches();
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一,与 MemoryEngine 对齐)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
async checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refRows = await this.getAllRows(refTableName);
|
||||||
|
const matched = refRows.filter((r) => String(r[colName]) === pkValue);
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && matched.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
const refPkCol = this.tablePKs.get(refTableName);
|
||||||
|
for (const refRow of matched) {
|
||||||
|
await this.checkCascadeRestrict(refTableName, String(refRow[refPkCol]), visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
||||||
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
||||||
@@ -7245,6 +7342,16 @@ class AriaEngine {
|
|||||||
const sp = this.savepoints.get(name);
|
const sp = this.savepoints.get(name);
|
||||||
if (!sp)
|
if (!sp)
|
||||||
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
||||||
|
// v0.6.3-fix: 记录当前快照涉及的表(回滚后重建索引)—— 此前事务内直写
|
||||||
|
// 索引 LSM,savepoint 回滚只还原快照 → savepoint 之后的索引条目残留
|
||||||
|
const affectedTables = new Set();
|
||||||
|
if (this.txnSnapshot) {
|
||||||
|
for (const key of this.txnSnapshot.keys()) {
|
||||||
|
const idx = key.indexOf(':');
|
||||||
|
if (idx > 0)
|
||||||
|
affectedTables.add(key.slice(0, idx));
|
||||||
|
}
|
||||||
|
}
|
||||||
// 恢复到 savepoint 时的快照
|
// 恢复到 savepoint 时的快照
|
||||||
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
||||||
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
||||||
@@ -7260,6 +7367,12 @@ class AriaEngine {
|
|||||||
if (found)
|
if (found)
|
||||||
this.savepoints.delete(k);
|
this.savepoints.delete(k);
|
||||||
}
|
}
|
||||||
|
// v0.6.3-fix: 重建受影响表二级索引(消除 savepoint 之后的过期索引条目)
|
||||||
|
for (const tableName of affectedTables) {
|
||||||
|
if (this.schemas.has(tableName)) {
|
||||||
|
await this.reindexTable(tableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async releaseSavepoint(name) {
|
async releaseSavepoint(name) {
|
||||||
if (!this.savepoints.has(name))
|
if (!this.savepoints.has(name))
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+166
-53
@@ -36,7 +36,7 @@
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// 版本
|
// 版本
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
const VERSION = '0.6.2';
|
const VERSION = '0.6.3';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
* metona-sqlark Shared WHERE Matcher — 统一的条件匹配逻辑
|
||||||
@@ -479,7 +479,16 @@
|
|||||||
toDelete.push(pk);
|
toDelete.push(pk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 级联删除:检查引用此表的其他表
|
// v0.6.3-fix: 级联两阶段 —— 先对全部待删行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 任何一行违规则整体拒绝。此前逐行执行:第 N 行 RESTRICT 抛错时,前 N-1 行的
|
||||||
|
// 级联子行已被删除、父行未删 → 无事务下部分级联(数据不一致)
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pk of toDelete) {
|
||||||
|
const row = table.get(pk);
|
||||||
|
if (row)
|
||||||
|
this.checkCascadeRestrict(tableName, pk, restrictVisited);
|
||||||
|
}
|
||||||
|
// 级联删除:检查引用此表的其他表(RESTRICT 已预检通过,此阶段不再抛错)
|
||||||
let cascadeCount = 0;
|
let cascadeCount = 0;
|
||||||
for (const pk of toDelete) {
|
for (const pk of toDelete) {
|
||||||
const row = table.get(pk);
|
const row = table.get(pk);
|
||||||
@@ -490,6 +499,43 @@
|
|||||||
table.delete(pk);
|
table.delete(pk);
|
||||||
return toDelete.length + cascadeCount;
|
return toDelete.length + cascadeCount;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refTableData = this.tables.get(refTableName);
|
||||||
|
if (!refTableData)
|
||||||
|
continue;
|
||||||
|
const refPks = [];
|
||||||
|
for (const [refPk, refRow] of refTableData) {
|
||||||
|
if (String(refRow[colName]) === pkValue)
|
||||||
|
refPks.push(refPk);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && refPks.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
for (const refPk of refPks) {
|
||||||
|
this.checkCascadeRestrict(refTableName, refPk, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
this.ensureTable(tableName);
|
this.ensureTable(tableName);
|
||||||
const table = this.tables.get(tableName);
|
const table = this.tables.get(tableName);
|
||||||
@@ -784,14 +830,12 @@
|
|||||||
for (const refPk of toDelete) {
|
for (const refPk of toDelete) {
|
||||||
const refRow = refTableData.get(refPk);
|
const refRow = refTableData.get(refPk);
|
||||||
if (refRow) {
|
if (refRow) {
|
||||||
// v0.3.3: 外键列置空后同步更新索引
|
// v0.6.3-fix: 复用 removeIndexEntries 清理旧值索引 —— 此前手动
|
||||||
if (refRow[colName] !== undefined && refRow[colName] !== null) {
|
// `pks.get(v)?.delete(pk)` 后遗留空 Set → checkUniqueness 对旧值
|
||||||
const pks = this.indexes.get(refTableName)?.get(colName);
|
// 永久误报 UNIQUE_VIOLATION(外键列带 unique 约束时)
|
||||||
if (pks) {
|
this.removeIndexEntries(refTableName, refRow, refPk);
|
||||||
pks.get(refRow[colName])?.delete(refPk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
refRow[colName] = null;
|
refRow[colName] = null;
|
||||||
|
this.updateIndexes(refTableName, refRow, refPk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1579,6 +1623,19 @@
|
|||||||
await this.appendRecord({}, keys);
|
await this.appendRecord({}, keys);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: 多 key 混合原子写(put + delete 编码进同一条日志记录)。
|
||||||
|
* 此前 KVStoreEngine 的 delete/update 主键变更等路径 putMany 与 deleteMany
|
||||||
|
* 分两次调用 = 两条记录:崩溃在两条记录之间 → 新旧行并存(重复行/脏数据),
|
||||||
|
* 与"一条日志记录 = 真原子"宣称不符。此方法保证混合操作全有或全无。
|
||||||
|
*/
|
||||||
|
async writeBatch(puts, deletes) {
|
||||||
|
if (Object.keys(puts).length === 0 && deletes.length === 0)
|
||||||
|
return;
|
||||||
|
await this.enqueue(async () => {
|
||||||
|
await this.appendRecord(puts, deletes);
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
||||||
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
||||||
@@ -1778,7 +1835,7 @@
|
|||||||
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
||||||
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
||||||
* 主键变更/级联场景整表 diff;全部原子)
|
* 主键变更/级联场景整表 diff;全部原子)
|
||||||
* - 事务:内存快照 + commit 时受影响表原子 flush(putMany 单记录 = 真原子,
|
* - 事务:内存快照 + commit 时受影响表原子 flush(writeBatch 单记录 = 真原子,
|
||||||
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
||||||
*
|
*
|
||||||
* 数据布局(KVStore keys):
|
* 数据布局(KVStore keys):
|
||||||
@@ -1943,10 +2000,7 @@
|
|||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
// 删除该表全部行(KV 中残留清理)
|
// 删除该表全部行(KV 中残留清理)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
async hasTable(tableName) {
|
async hasTable(tableName) {
|
||||||
this.ensureOpen();
|
this.ensureOpen();
|
||||||
@@ -1972,10 +2026,7 @@
|
|||||||
if (action === 'DROP') {
|
if (action === 'DROP') {
|
||||||
// 重写存储行(移除该列)
|
// 重写存储行(移除该列)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ---- CRUD ----
|
// ---- CRUD ----
|
||||||
@@ -2057,10 +2108,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async delete(tableName, query) {
|
async delete(tableName, query) {
|
||||||
@@ -2082,10 +2130,7 @@
|
|||||||
Object.assign(puts, diff.puts);
|
Object.assign(puts, diff.puts);
|
||||||
deletes.push(...diff.deletes);
|
deletes.push(...diff.deletes);
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
async count(tableName, query) {
|
async count(tableName, query) {
|
||||||
@@ -2100,10 +2145,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0)
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
await this.kv.putMany(diff.puts);
|
|
||||||
if (diff.deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
// ---- 动态索引 ----
|
// ---- 动态索引 ----
|
||||||
async createIndex(tableName, column, unique) {
|
async createIndex(tableName, column, unique) {
|
||||||
@@ -2158,10 +2200,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0)
|
await this.kv.writeBatch(puts, deletes);
|
||||||
await this.kv.putMany(puts);
|
|
||||||
if (deletes.length > 0)
|
|
||||||
await this.kv.deleteMany(deletes);
|
|
||||||
// 事务内 DDL 的 schema 一并持久化
|
// 事务内 DDL 的 schema 一并持久化
|
||||||
if (this.txSchemaChanged) {
|
if (this.txSchemaChanged) {
|
||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
@@ -2242,7 +2281,7 @@
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
||||||
* 调用方合并到单次原子 putMany/deleteMany(多表操作真原子)。
|
* 调用方合并到单次原子 writeBatch(put+delete 同一条日志记录,多表操作真原子)。
|
||||||
*/
|
*/
|
||||||
async collectTableDiff(tableName) {
|
async collectTableDiff(tableName) {
|
||||||
const prefix = this.rowPrefix(tableName);
|
const prefix = this.rowPrefix(tableName);
|
||||||
@@ -4167,20 +4206,28 @@
|
|||||||
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
||||||
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
||||||
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
||||||
|
//
|
||||||
|
// v0.6.3-fix: ① 入链后立即置空 immutable —— 此前 freezeMemtable 会把同一张
|
||||||
|
// immutable 再次入链 → 重复 SSTable(2 行 flush 出 3 个文件);② frozenMemtables
|
||||||
|
// 保持到链排空后再清理 —— 此前入链后立即清空,链执行期间并发读看不到冻结数据
|
||||||
|
// (短暂数据不可见窗口)。
|
||||||
if (this.immutableMemtable) {
|
if (this.immutableMemtable) {
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
if (this.memtable.getEntryCount() > 0) {
|
if (this.memtable.getEntryCount() > 0) {
|
||||||
this.freezeMemtable();
|
this.freezeMemtable();
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
if (frozen) {
|
if (frozen) {
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.frozenMemtables = [];
|
|
||||||
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
||||||
await this.drainChain();
|
await this.drainChain();
|
||||||
|
// 清理空残留冻结表(flushImmutableAsync 完成时已自行移除已落盘的)
|
||||||
|
this.frozenMemtables = this.frozenMemtables.filter((f) => f.getEntryCount() > 0);
|
||||||
}
|
}
|
||||||
async clear() {
|
async clear() {
|
||||||
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
// v0.4.3-fix: 清空前排空后台任务(避免 compaction 在清空后写回残留 meta/数据)
|
||||||
@@ -4407,15 +4454,11 @@
|
|||||||
};
|
};
|
||||||
const bytes = this.encodeRecord(fullRecord);
|
const bytes = this.encodeRecord(fullRecord);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 写入失败必须抛给调用方 —— 此前仅 console.warn 吞错:
|
||||||
|
// 内存已提交而 WAL 缺失,崩溃即丢且调用方无感知。
|
||||||
await this.store.append(bytes);
|
await this.store.append(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(bytes);
|
this.buffer.push(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
@@ -4433,15 +4476,10 @@
|
|||||||
}
|
}
|
||||||
const combined = this.mergeChunks(chunks);
|
const combined = this.mergeChunks(chunks);
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 同 append —— 批量写入失败抛给调用方(不再吞错)
|
||||||
await this.store.append(combined);
|
await this.store.append(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append batch record');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (this.syncMode === 'batch') {
|
else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(combined);
|
this.buffer.push(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
@@ -5501,17 +5539,21 @@
|
|||||||
throw new Error(`Transaction ${txnId} not found`);
|
throw new Error(`Transaction ${txnId} not found`);
|
||||||
txn.state = TransactionState.COMMITTED;
|
txn.state = TransactionState.COMMITTED;
|
||||||
this.globalCommitLsn++;
|
this.globalCommitLsn++;
|
||||||
// v0.4.2-fix: 仅标记本事务写入的版本(此前遍历全库 versionStore)
|
// v0.6.3-fix: 已提交版本直接清理 —— 快照读取已移除(v0.5.1),版本链仅作
|
||||||
|
// 事务内 undo 记录(rollback/savepoint 用),提交后 LSM 持有权威数据。
|
||||||
|
// 此前 commit 仅标记 committed → versionStore 随写入量无限增长(行数据双份常驻)。
|
||||||
const writeKeys = this.txnWriteKeys.get(txnId);
|
const writeKeys = this.txnWriteKeys.get(txnId);
|
||||||
if (writeKeys) {
|
if (writeKeys) {
|
||||||
for (const tableKey of writeKeys) {
|
for (const tableKey of writeKeys) {
|
||||||
const versions = this.versionStore.get(tableKey);
|
const versions = this.versionStore.get(tableKey);
|
||||||
if (!versions)
|
if (!versions)
|
||||||
continue;
|
continue;
|
||||||
for (const version of versions) {
|
const filtered = versions.filter((v) => v.txnId !== txnId);
|
||||||
if (version.txnId === txnId) {
|
if (filtered.length === 0) {
|
||||||
version.committed = true;
|
this.versionStore.delete(tableKey);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.versionStore.set(tableKey, filtered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5753,10 +5795,16 @@
|
|||||||
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
* 驱逐管理器 — 当 Buffer Pool 满时驱逐页面。
|
||||||
*/
|
*/
|
||||||
class EvictionManager {
|
class EvictionManager {
|
||||||
constructor(capacity, onEvict) {
|
/**
|
||||||
|
* @param onEvict 驱逐脏页时的写回回调
|
||||||
|
* @param onRemove v0.6.3: 页面被驱逐时的回调(BufferPool 用于同步清理 pages Map;
|
||||||
|
* 此前驱逐只清 LRU 链表,pages Map 无限增长 → 1MB 内存预算形同虚设)
|
||||||
|
*/
|
||||||
|
constructor(capacity, onEvict, onRemove) {
|
||||||
this.lru = new LRUList();
|
this.lru = new LRUList();
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.onEvict = onEvict;
|
this.onEvict = onEvict;
|
||||||
|
this.onRemove = onRemove;
|
||||||
}
|
}
|
||||||
/** 访问页面,更新 LRU */
|
/** 访问页面,更新 LRU */
|
||||||
access(page) {
|
access(page) {
|
||||||
@@ -5789,6 +5837,7 @@
|
|||||||
victim.dirty = false;
|
victim.dirty = false;
|
||||||
}
|
}
|
||||||
this.lru.remove(victim);
|
this.lru.remove(victim);
|
||||||
|
this.onRemove?.(victim);
|
||||||
evicted++;
|
evicted++;
|
||||||
}
|
}
|
||||||
return evicted;
|
return evicted;
|
||||||
@@ -5836,6 +5885,10 @@
|
|||||||
await this.pageIO.writePage(page.pageId, page.data);
|
await this.pageIO.writePage(page.pageId, page.data);
|
||||||
page.dirty = false;
|
page.dirty = false;
|
||||||
}
|
}
|
||||||
|
}, (page) => {
|
||||||
|
// v0.6.3-fix: 驱逐时同步从 pages Map 移除 —— 此前仅清 LRU 链表,
|
||||||
|
// pages Map 保留全部历史页面 → 内存无限增长(1MB 预算失效)
|
||||||
|
this.pages.delete(page.pageId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -6828,6 +6881,18 @@
|
|||||||
const walRecords = [];
|
const walRecords = [];
|
||||||
// v0.4.1: 外键级联(环路保护)
|
// v0.4.1: 外键级联(环路保护)
|
||||||
const visited = new Set();
|
const visited = new Set();
|
||||||
|
// v0.6.3-fix: 级联两阶段 —— 先对全部匹配行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 否则第 N 行 RESTRICT 抛错时前 N-1 行的级联已执行 → 无事务部分级联(数据不一致)
|
||||||
|
const matchedPks = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
if (!query.where || Object.keys(query.where).length === 0 || matchWhere(row, query.where)) {
|
||||||
|
matchedPks.push(String(row[this.tablePKs.get(tableName)]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const restrictVisited = new Set();
|
||||||
|
for (const pkValue of matchedPks) {
|
||||||
|
await this.checkCascadeRestrict(tableName, pkValue, restrictVisited);
|
||||||
|
}
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const pkCol = this.tablePKs.get(tableName);
|
const pkCol = this.tablePKs.get(tableName);
|
||||||
const key = `${tableName}:${row[pkCol]}`;
|
const key = `${tableName}:${row[pkCol]}`;
|
||||||
@@ -6859,6 +6924,38 @@
|
|||||||
this.trimAllCaches();
|
this.trimAllCaches();
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一,与 MemoryEngine 对齐)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
async checkCascadeRestrict(tableName, pkValue, visited) {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey))
|
||||||
|
return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName)
|
||||||
|
continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete)
|
||||||
|
continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName)
|
||||||
|
continue;
|
||||||
|
const refRows = await this.getAllRows(refTableName);
|
||||||
|
const matched = refRows.filter((r) => String(r[colName]) === pkValue);
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && matched.length > 0) {
|
||||||
|
throw new DatabaseError(`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
const refPkCol = this.tablePKs.get(refTableName);
|
||||||
|
for (const refRow of matched) {
|
||||||
|
await this.checkCascadeRestrict(refTableName, String(refRow[refPkCol]), visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
||||||
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
||||||
@@ -7251,6 +7348,16 @@
|
|||||||
const sp = this.savepoints.get(name);
|
const sp = this.savepoints.get(name);
|
||||||
if (!sp)
|
if (!sp)
|
||||||
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
||||||
|
// v0.6.3-fix: 记录当前快照涉及的表(回滚后重建索引)—— 此前事务内直写
|
||||||
|
// 索引 LSM,savepoint 回滚只还原快照 → savepoint 之后的索引条目残留
|
||||||
|
const affectedTables = new Set();
|
||||||
|
if (this.txnSnapshot) {
|
||||||
|
for (const key of this.txnSnapshot.keys()) {
|
||||||
|
const idx = key.indexOf(':');
|
||||||
|
if (idx > 0)
|
||||||
|
affectedTables.add(key.slice(0, idx));
|
||||||
|
}
|
||||||
|
}
|
||||||
// 恢复到 savepoint 时的快照
|
// 恢复到 savepoint 时的快照
|
||||||
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
||||||
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
||||||
@@ -7266,6 +7373,12 @@
|
|||||||
if (found)
|
if (found)
|
||||||
this.savepoints.delete(k);
|
this.savepoints.delete(k);
|
||||||
}
|
}
|
||||||
|
// v0.6.3-fix: 重建受影响表二级索引(消除 savepoint 之后的过期索引条目)
|
||||||
|
for (const tableName of affectedTables) {
|
||||||
|
if (this.schemas.has(tableName)) {
|
||||||
|
await this.reindexTable(tableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async releaseSavepoint(name) {
|
async releaseSavepoint(name) {
|
||||||
if (!this.savepoints.has(name))
|
if (!this.savepoints.has(name))
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@metona-team/metona-sqlark",
|
"name": "@metona-team/metona-sqlark",
|
||||||
"version": "0.6.2",
|
"version": "0.6.3",
|
||||||
"description": "Frontend SQL database with in-memory and disk dual-mode storage",
|
"description": "Frontend SQL database with in-memory and disk dual-mode storage",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/metona-sqlark.cjs",
|
"main": "dist/metona-sqlark.cjs",
|
||||||
|
|||||||
+1
-1
@@ -214,4 +214,4 @@ export class DatabaseError extends Error {
|
|||||||
// 版本
|
// 版本
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
export const VERSION = '0.6.2';
|
export const VERSION = '0.6.3';
|
||||||
|
|||||||
@@ -101,12 +101,19 @@ export type EvictionCallback = (page: PageHandle) => Promise<void>;
|
|||||||
export class EvictionManager {
|
export class EvictionManager {
|
||||||
private lru: LRUList;
|
private lru: LRUList;
|
||||||
private onEvict: EvictionCallback;
|
private onEvict: EvictionCallback;
|
||||||
|
private onRemove?: (page: PageHandle) => void;
|
||||||
private capacity: number;
|
private capacity: number;
|
||||||
|
|
||||||
constructor(capacity: number, onEvict: EvictionCallback) {
|
/**
|
||||||
|
* @param onEvict 驱逐脏页时的写回回调
|
||||||
|
* @param onRemove v0.6.3: 页面被驱逐时的回调(BufferPool 用于同步清理 pages Map;
|
||||||
|
* 此前驱逐只清 LRU 链表,pages Map 无限增长 → 1MB 内存预算形同虚设)
|
||||||
|
*/
|
||||||
|
constructor(capacity: number, onEvict: EvictionCallback, onRemove?: (page: PageHandle) => void) {
|
||||||
this.lru = new LRUList();
|
this.lru = new LRUList();
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.onEvict = onEvict;
|
this.onEvict = onEvict;
|
||||||
|
this.onRemove = onRemove;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 访问页面,更新 LRU */
|
/** 访问页面,更新 LRU */
|
||||||
@@ -145,6 +152,7 @@ export class EvictionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.lru.remove(victim);
|
this.lru.remove(victim);
|
||||||
|
this.onRemove?.(victim);
|
||||||
evicted++;
|
evicted++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ export class BufferPool {
|
|||||||
await this.pageIO.writePage(page.pageId, page.data);
|
await this.pageIO.writePage(page.pageId, page.data);
|
||||||
page.dirty = false;
|
page.dirty = false;
|
||||||
}
|
}
|
||||||
|
}, (page) => {
|
||||||
|
// v0.6.3-fix: 驱逐时同步从 pages Map 移除 —— 此前仅清 LRU 链表,
|
||||||
|
// pages Map 保留全部历史页面 → 内存无限增长(1MB 预算失效)
|
||||||
|
this.pages.delete(page.pageId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -871,6 +871,19 @@ export class AriaEngine implements IStorageEngine {
|
|||||||
// v0.4.1: 外键级联(环路保护)
|
// v0.4.1: 外键级联(环路保护)
|
||||||
const visited = new Set<string>();
|
const visited = new Set<string>();
|
||||||
|
|
||||||
|
// v0.6.3-fix: 级联两阶段 —— 先对全部匹配行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 否则第 N 行 RESTRICT 抛错时前 N-1 行的级联已执行 → 无事务部分级联(数据不一致)
|
||||||
|
const matchedPks: string[] = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
if (!query.where || Object.keys(query.where).length === 0 || matchWhere(row, query.where)) {
|
||||||
|
matchedPks.push(String(row[this.tablePKs.get(tableName)!]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const restrictVisited = new Set<string>();
|
||||||
|
for (const pkValue of matchedPks) {
|
||||||
|
await this.checkCascadeRestrict(tableName, pkValue, restrictVisited);
|
||||||
|
}
|
||||||
|
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const pkCol = this.tablePKs.get(tableName)!;
|
const pkCol = this.tablePKs.get(tableName)!;
|
||||||
const key = `${tableName}:${row[pkCol]}`;
|
const key = `${tableName}:${row[pkCol]}`;
|
||||||
@@ -907,6 +920,39 @@ export class AriaEngine implements IStorageEngine {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一,与 MemoryEngine 对齐)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
private async checkCascadeRestrict(tableName: string, pkValue: string, visited: Set<string>): Promise<void> {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey)) return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName) continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete) continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName) continue;
|
||||||
|
const refRows = await this.getAllRows(refTableName);
|
||||||
|
const matched = refRows.filter((r) => String(r[colName]) === pkValue);
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && matched.length > 0) {
|
||||||
|
throw new DatabaseError(
|
||||||
|
`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`,
|
||||||
|
'FOREIGN_KEY_VIOLATION',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
const refPkCol = this.tablePKs.get(refTableName)!;
|
||||||
|
for (const refRow of matched) {
|
||||||
|
await this.checkCascadeRestrict(refTableName, String(refRow[refPkCol]), visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
* v0.4.1: 外键级联规则 — 对齐 MemoryEngine.cascadeDelete 行为。
|
||||||
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
* 删除 tableName 主键为 pkValue 的行前,检查引用它的所有表:
|
||||||
@@ -1328,6 +1374,15 @@ export class AriaEngine implements IStorageEngine {
|
|||||||
async rollbackToSavepoint(name: string): Promise<void> {
|
async rollbackToSavepoint(name: string): Promise<void> {
|
||||||
const sp = this.savepoints.get(name);
|
const sp = this.savepoints.get(name);
|
||||||
if (!sp) throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
if (!sp) throw new DatabaseError(`Savepoint "${name}" not found`, 'SAVEPOINT_NOT_FOUND');
|
||||||
|
// v0.6.3-fix: 记录当前快照涉及的表(回滚后重建索引)—— 此前事务内直写
|
||||||
|
// 索引 LSM,savepoint 回滚只还原快照 → savepoint 之后的索引条目残留
|
||||||
|
const affectedTables = new Set<string>();
|
||||||
|
if (this.txnSnapshot) {
|
||||||
|
for (const key of this.txnSnapshot.keys()) {
|
||||||
|
const idx = key.indexOf(':');
|
||||||
|
if (idx > 0) affectedTables.add(key.slice(0, idx));
|
||||||
|
}
|
||||||
|
}
|
||||||
// 恢复到 savepoint 时的快照
|
// 恢复到 savepoint 时的快照
|
||||||
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
this.txnSnapshot = sp.snapshot ? new Map(sp.snapshot) : null;
|
||||||
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
// v0.3.3: 清理该事务在 MVCC 版本链中的全部记录(快照已含正确数据,
|
||||||
@@ -1339,6 +1394,12 @@ export class AriaEngine implements IStorageEngine {
|
|||||||
if (k === name) { found = true; continue; }
|
if (k === name) { found = true; continue; }
|
||||||
if (found) this.savepoints.delete(k);
|
if (found) this.savepoints.delete(k);
|
||||||
}
|
}
|
||||||
|
// v0.6.3-fix: 重建受影响表二级索引(消除 savepoint 之后的过期索引条目)
|
||||||
|
for (const tableName of affectedTables) {
|
||||||
|
if (this.schemas.has(tableName)) {
|
||||||
|
await this.reindexTable(tableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async releaseSavepoint(name: string): Promise<void> {
|
async releaseSavepoint(name: string): Promise<void> {
|
||||||
|
|||||||
@@ -615,20 +615,28 @@ export class LSM {
|
|||||||
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
// memtable flush 的 saveMeta 读到中间态列表(含 compaction 产物)→ 覆盖产物
|
||||||
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
// 引用 → compaction 产物变孤儿 → 索引/主表数据静默丢失(优雅关闭后重开丢 75%)。
|
||||||
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
// 挂链后按序执行(memtable flush 在 compaction 之后),meta 无竞态。
|
||||||
|
//
|
||||||
|
// v0.6.3-fix: ① 入链后立即置空 immutable —— 此前 freezeMemtable 会把同一张
|
||||||
|
// immutable 再次入链 → 重复 SSTable(2 行 flush 出 3 个文件);② frozenMemtables
|
||||||
|
// 保持到链排空后再清理 —— 此前入链后立即清空,链执行期间并发读看不到冻结数据
|
||||||
|
// (短暂数据不可见窗口)。
|
||||||
if (this.immutableMemtable) {
|
if (this.immutableMemtable) {
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
if (this.memtable.getEntryCount() > 0) {
|
if (this.memtable.getEntryCount() > 0) {
|
||||||
this.freezeMemtable();
|
this.freezeMemtable();
|
||||||
const frozen = this.immutableMemtable;
|
const frozen = this.immutableMemtable;
|
||||||
if (frozen) {
|
if (frozen) {
|
||||||
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
this.flushChain = this.enqueueOnChain(() => this.flushImmutableAsync(frozen));
|
||||||
|
this.immutableMemtable = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.frozenMemtables = [];
|
|
||||||
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
// 刷盘完成后级联调度可能触发 compaction → 排空到稳定
|
||||||
await this.drainChain();
|
await this.drainChain();
|
||||||
|
// 清理空残留冻结表(flushImmutableAsync 完成时已自行移除已落盘的)
|
||||||
|
this.frozenMemtables = this.frozenMemtables.filter((f) => f.getEntryCount() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
async clear(): Promise<void> {
|
async clear(): Promise<void> {
|
||||||
|
|||||||
@@ -57,16 +57,19 @@ export class MVCCManager {
|
|||||||
txn.state = TransactionState.COMMITTED;
|
txn.state = TransactionState.COMMITTED;
|
||||||
this.globalCommitLsn++;
|
this.globalCommitLsn++;
|
||||||
|
|
||||||
// v0.4.2-fix: 仅标记本事务写入的版本(此前遍历全库 versionStore)
|
// v0.6.3-fix: 已提交版本直接清理 —— 快照读取已移除(v0.5.1),版本链仅作
|
||||||
|
// 事务内 undo 记录(rollback/savepoint 用),提交后 LSM 持有权威数据。
|
||||||
|
// 此前 commit 仅标记 committed → versionStore 随写入量无限增长(行数据双份常驻)。
|
||||||
const writeKeys = this.txnWriteKeys.get(txnId);
|
const writeKeys = this.txnWriteKeys.get(txnId);
|
||||||
if (writeKeys) {
|
if (writeKeys) {
|
||||||
for (const tableKey of writeKeys) {
|
for (const tableKey of writeKeys) {
|
||||||
const versions = this.versionStore.get(tableKey);
|
const versions = this.versionStore.get(tableKey);
|
||||||
if (!versions) continue;
|
if (!versions) continue;
|
||||||
for (const version of versions) {
|
const filtered = versions.filter((v) => v.txnId !== txnId);
|
||||||
if (version.txnId === txnId) {
|
if (filtered.length === 0) {
|
||||||
version.committed = true;
|
this.versionStore.delete(tableKey);
|
||||||
}
|
} else {
|
||||||
|
this.versionStore.set(tableKey, filtered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,13 +75,10 @@ export class WAL {
|
|||||||
const bytes = this.encodeRecord(fullRecord);
|
const bytes = this.encodeRecord(fullRecord);
|
||||||
|
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 写入失败必须抛给调用方 —— 此前仅 console.warn 吞错:
|
||||||
|
// 内存已提交而 WAL 缺失,崩溃即丢且调用方无感知。
|
||||||
await this.store.append(bytes);
|
await this.store.append(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
} catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append record');
|
|
||||||
}
|
|
||||||
} else if (this.syncMode === 'batch') {
|
} else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(bytes);
|
this.buffer.push(bytes);
|
||||||
this.bufferedBytes += bytes.byteLength;
|
this.bufferedBytes += bytes.byteLength;
|
||||||
@@ -101,13 +98,9 @@ export class WAL {
|
|||||||
const combined = this.mergeChunks(chunks);
|
const combined = this.mergeChunks(chunks);
|
||||||
|
|
||||||
if (this.syncMode === 'full') {
|
if (this.syncMode === 'full') {
|
||||||
try {
|
// v0.6.3-fix: 同 append —— 批量写入失败抛给调用方(不再吞错)
|
||||||
await this.store.append(combined);
|
await this.store.append(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
} catch {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.warn('[AriaEngine WAL] Failed to append batch record');
|
|
||||||
}
|
|
||||||
} else if (this.syncMode === 'batch') {
|
} else if (this.syncMode === 'batch') {
|
||||||
this.buffer.push(combined);
|
this.buffer.push(combined);
|
||||||
this.bufferedBytes += combined.byteLength;
|
this.bufferedBytes += combined.byteLength;
|
||||||
|
|||||||
@@ -223,6 +223,19 @@ export class KVStore {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* v0.6.3: 多 key 混合原子写(put + delete 编码进同一条日志记录)。
|
||||||
|
* 此前 KVStoreEngine 的 delete/update 主键变更等路径 putMany 与 deleteMany
|
||||||
|
* 分两次调用 = 两条记录:崩溃在两条记录之间 → 新旧行并存(重复行/脏数据),
|
||||||
|
* 与"一条日志记录 = 真原子"宣称不符。此方法保证混合操作全有或全无。
|
||||||
|
*/
|
||||||
|
async writeBatch(puts: Record<string, ArrayBuffer>, deletes: string[]): Promise<void> {
|
||||||
|
if (Object.keys(puts).length === 0 && deletes.length === 0) return;
|
||||||
|
await this.enqueue(async () => {
|
||||||
|
await this.appendRecord(puts, deletes);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
* v0.6.1: 追加写入(value 拼接语义)— aria WAL 分片等追加型数据用。
|
||||||
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
* 日志记录 APPEND 类型(O(chunk) 高效),恢复时按 seq 顺序拼接,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
* - 读:始终走内存(写路径同步落盘,重启从 KVStore 恢复)
|
||||||
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
* - 写:内存先行 + KVStore 增量持久化(insert 增量 putMany;update/delete 受影响行重写;
|
||||||
* 主键变更/级联场景整表 diff;全部原子)
|
* 主键变更/级联场景整表 diff;全部原子)
|
||||||
* - 事务:内存快照 + commit 时受影响表原子 flush(putMany 单记录 = 真原子,
|
* - 事务:内存快照 + commit 时受影响表原子 flush(writeBatch 单记录 = 真原子,
|
||||||
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
* 此前 IndexedDBEngine 依赖 IDB 事务,现在完全自研)
|
||||||
*
|
*
|
||||||
* 数据布局(KVStore keys):
|
* 数据布局(KVStore keys):
|
||||||
@@ -194,8 +194,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
// 删除该表全部行(KV 中残留清理)
|
// 删除该表全部行(KV 中残留清理)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0) await this.kv.putMany(diff.puts);
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
if (diff.deletes.length > 0) await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasTable(tableName: string): Promise<boolean> {
|
async hasTable(tableName: string): Promise<boolean> {
|
||||||
@@ -229,8 +228,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
if (action === 'DROP') {
|
if (action === 'DROP') {
|
||||||
// 重写存储行(移除该列)
|
// 重写存储行(移除该列)
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0) await this.kv.putMany(diff.puts);
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
if (diff.deletes.length > 0) await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,8 +317,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
// 单次原子写(一条日志记录 = 真原子,v0.6.1)
|
||||||
if (Object.keys(puts).length > 0) await this.kv.putMany(puts);
|
await this.kv.writeBatch(puts, deletes);
|
||||||
if (deletes.length > 0) await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,8 +339,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
Object.assign(puts, diff.puts);
|
Object.assign(puts, diff.puts);
|
||||||
deletes.push(...diff.deletes);
|
deletes.push(...diff.deletes);
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0) await this.kv.putMany(puts);
|
await this.kv.writeBatch(puts, deletes);
|
||||||
if (deletes.length > 0) await this.kv.deleteMany(deletes);
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,8 +356,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const diff = await this.collectTableDiff(tableName);
|
const diff = await this.collectTableDiff(tableName);
|
||||||
if (Object.keys(diff.puts).length > 0) await this.kv.putMany(diff.puts);
|
await this.kv.writeBatch(diff.puts, diff.deletes);
|
||||||
if (diff.deletes.length > 0) await this.kv.deleteMany(diff.deletes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 动态索引 ----
|
// ---- 动态索引 ----
|
||||||
@@ -419,8 +414,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(puts).length > 0) await this.kv.putMany(puts);
|
await this.kv.writeBatch(puts, deletes);
|
||||||
if (deletes.length > 0) await this.kv.deleteMany(deletes);
|
|
||||||
// 事务内 DDL 的 schema 一并持久化
|
// 事务内 DDL 的 schema 一并持久化
|
||||||
if (this.txSchemaChanged) {
|
if (this.txSchemaChanged) {
|
||||||
await this.persistSchema();
|
await this.persistSchema();
|
||||||
@@ -502,7 +496,7 @@ export class KVStoreEngine implements IStorageEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
* v0.6.1: 整表 diff 收集(不落盘):内存行全部 put + KV 残留行删除。
|
||||||
* 调用方合并到单次原子 putMany/deleteMany(多表操作真原子)。
|
* 调用方合并到单次原子 writeBatch(put+delete 同一条日志记录,多表操作真原子)。
|
||||||
*/
|
*/
|
||||||
private async collectTableDiff(tableName: string): Promise<{ puts: Record<string, ArrayBuffer>; deletes: string[] }> {
|
private async collectTableDiff(tableName: string): Promise<{ puts: Record<string, ArrayBuffer>; deletes: string[] }> {
|
||||||
const prefix = this.rowPrefix(tableName);
|
const prefix = this.rowPrefix(tableName);
|
||||||
|
|||||||
+50
-8
@@ -270,7 +270,15 @@ export class MemoryEngine implements IStorageEngine {
|
|||||||
toDelete.push(pk);
|
toDelete.push(pk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 级联删除:检查引用此表的其他表
|
// v0.6.3-fix: 级联两阶段 —— 先对全部待删行做 RESTRICT 预检(沿 CASCADE 链递归),
|
||||||
|
// 任何一行违规则整体拒绝。此前逐行执行:第 N 行 RESTRICT 抛错时,前 N-1 行的
|
||||||
|
// 级联子行已被删除、父行未删 → 无事务下部分级联(数据不一致)
|
||||||
|
const restrictVisited = new Set<string>();
|
||||||
|
for (const pk of toDelete) {
|
||||||
|
const row = table.get(pk);
|
||||||
|
if (row) this.checkCascadeRestrict(tableName, pk, restrictVisited);
|
||||||
|
}
|
||||||
|
// 级联删除:检查引用此表的其他表(RESTRICT 已预检通过,此阶段不再抛错)
|
||||||
let cascadeCount = 0;
|
let cascadeCount = 0;
|
||||||
for (const pk of toDelete) {
|
for (const pk of toDelete) {
|
||||||
const row = table.get(pk);
|
const row = table.get(pk);
|
||||||
@@ -280,6 +288,42 @@ export class MemoryEngine implements IStorageEngine {
|
|||||||
return toDelete.length + cascadeCount;
|
return toDelete.length + cascadeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* v0.6.3: RESTRICT 预检(delete 级联两阶段之一)。
|
||||||
|
* 递归沿 CASCADE 链检查引用表:RESTRICT 引用存在依赖行则抛 FOREIGN_KEY_VIOLATION。
|
||||||
|
*/
|
||||||
|
private checkCascadeRestrict(tableName: string, pkValue: string, visited: Set<string>): void {
|
||||||
|
const visitKey = `${tableName}:${pkValue}`;
|
||||||
|
if (visited.has(visitKey)) return;
|
||||||
|
visited.add(visitKey);
|
||||||
|
|
||||||
|
for (const [refTableName, refSchema] of this.schemas) {
|
||||||
|
if (refTableName === tableName) continue;
|
||||||
|
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
|
||||||
|
if (!colDef.references || !colDef.onDelete) continue;
|
||||||
|
const [refTable] = colDef.references.split('.');
|
||||||
|
if (refTable !== tableName) continue;
|
||||||
|
const refTableData = this.tables.get(refTableName);
|
||||||
|
if (!refTableData) continue;
|
||||||
|
const refPks: string[] = [];
|
||||||
|
for (const [refPk, refRow] of refTableData) {
|
||||||
|
if (String(refRow[colName]) === pkValue) refPks.push(refPk);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'RESTRICT' && refPks.length > 0) {
|
||||||
|
throw new DatabaseError(
|
||||||
|
`Cannot delete from "${tableName}": foreign key "${colName}" in "${refTableName}" has dependent rows`,
|
||||||
|
'FOREIGN_KEY_VIOLATION',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (colDef.onDelete === 'CASCADE') {
|
||||||
|
for (const refPk of refPks) {
|
||||||
|
this.checkCascadeRestrict(refTableName, refPk, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async count(tableName: string, query?: QueryPlan): Promise<number> {
|
async count(tableName: string, query?: QueryPlan): Promise<number> {
|
||||||
this.ensureTable(tableName);
|
this.ensureTable(tableName);
|
||||||
const table = this.tables.get(tableName)!;
|
const table = this.tables.get(tableName)!;
|
||||||
@@ -554,14 +598,12 @@ export class MemoryEngine implements IStorageEngine {
|
|||||||
for (const refPk of toDelete) {
|
for (const refPk of toDelete) {
|
||||||
const refRow = refTableData.get(refPk);
|
const refRow = refTableData.get(refPk);
|
||||||
if (refRow) {
|
if (refRow) {
|
||||||
// v0.3.3: 外键列置空后同步更新索引
|
// v0.6.3-fix: 复用 removeIndexEntries 清理旧值索引 —— 此前手动
|
||||||
if (refRow[colName] !== undefined && refRow[colName] !== null) {
|
// `pks.get(v)?.delete(pk)` 后遗留空 Set → checkUniqueness 对旧值
|
||||||
const pks = this.indexes.get(refTableName)?.get(colName);
|
// 永久误报 UNIQUE_VIOLATION(外键列带 unique 约束时)
|
||||||
if (pks) {
|
this.removeIndexEntries(refTableName, refRow, refPk);
|
||||||
pks.get(refRow[colName])?.delete(refPk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
refRow[colName] = null;
|
refRow[colName] = null;
|
||||||
|
this.updateIndexes(refTableName, refRow, refPk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,13 @@ describe('AriaEngine — MVCC 事务 + Savepoint', () => {
|
|||||||
expect(id1).not.toBe(id2);
|
expect(id1).not.toBe(id2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('commit 后版本标记已提交', () => {
|
it('commit 后清理本事务版本(undo 记录不保留,v0.6.3)', () => {
|
||||||
const txnId = (engine as any).mvcc.beginTransaction();
|
const txnId = (engine as any).mvcc.beginTransaction();
|
||||||
(engine as any).mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
(engine as any).mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||||||
(engine as any).mvcc.commitTransaction(txnId);
|
(engine as any).mvcc.commitTransaction(txnId);
|
||||||
const store = (engine as any).mvcc.versionStore;
|
const store = (engine as any).mvcc.versionStore;
|
||||||
expect(store.get('users.1')[0].committed).toBe(true);
|
// v0.6.3: 已提交版本随 commit 清理(快照读取已移除,版本链仅作事务内 undo)
|
||||||
|
expect(store.has('users.1')).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rollback 移除本事务版本', () => {
|
it('rollback 移除本事务版本', () => {
|
||||||
@@ -62,16 +63,30 @@ describe('AriaEngine — MVCC 事务 + Savepoint', () => {
|
|||||||
expect(await engine.count('users')).toBe(1);
|
expect(await engine.count('users')).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GC 清理过旧版本(保留最新 N 个)', () => {
|
it('GC 清理过旧版本(未提交版本链保留最新 N 个)', () => {
|
||||||
for (let i = 0; i < 200; i++) {
|
for (let i = 0; i < 200; i++) {
|
||||||
const txnId = (engine as any).mvcc.beginTransaction();
|
const txnId = (engine as any).mvcc.beginTransaction();
|
||||||
(engine as any).mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
(engine as any).mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
||||||
(engine as any).mvcc.commitTransaction(txnId);
|
// 不 commit:未提交版本保留在链上(活跃事务 undo)
|
||||||
}
|
}
|
||||||
(engine as any).mvcc.gc(50);
|
(engine as any).mvcc.gc(50);
|
||||||
expect((engine as any).mvcc.versionStore.get('users.1').length).toBe(50);
|
expect((engine as any).mvcc.versionStore.get('users.1').length).toBe(50);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('多事务并发未提交 + 部分提交:commit 版本清理、未提交保留', () => {
|
||||||
|
const mvcc = (engine as any).mvcc;
|
||||||
|
const txnA = mvcc.beginTransaction();
|
||||||
|
mvcc.writeVersion('users', '1', { name: 'A1' }, txnA);
|
||||||
|
const txnB = mvcc.beginTransaction();
|
||||||
|
mvcc.writeVersion('users', '1', { name: 'B1' }, txnB);
|
||||||
|
mvcc.commitTransaction(txnA);
|
||||||
|
// txnA 版本已清理;txnB 未提交版本保留
|
||||||
|
const versions = mvcc.versionStore.get('users.1') as { txnId: number }[];
|
||||||
|
expect(versions.map((v) => v.txnId)).toEqual([txnB]);
|
||||||
|
mvcc.rollbackTransaction(txnB);
|
||||||
|
expect(mvcc.versionStore.has('users.1')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
// ---- Savepoint ----
|
// ---- Savepoint ----
|
||||||
it('savepoint 创建成功', async () => {
|
it('savepoint 创建成功', async () => {
|
||||||
await engine.beginTransaction();
|
await engine.beginTransaction();
|
||||||
|
|||||||
@@ -157,13 +157,13 @@ describe('AriaEngine — MVCC', () => {
|
|||||||
expect(id1).not.toBe(id2);
|
expect(id1).not.toBe(id2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('commit 标记版本为已提交并清理事务登记', () => {
|
it('commit 清理本事务版本并清理事务登记(v0.6.3)', () => {
|
||||||
const txnId = mvcc.beginTransaction();
|
const txnId = mvcc.beginTransaction();
|
||||||
mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
mvcc.writeVersion('users', '1', { name: 'Alice' }, txnId);
|
||||||
mvcc.commitTransaction(txnId);
|
mvcc.commitTransaction(txnId);
|
||||||
const versions = (mvcc as unknown as { versionStore: Map<string, { committed: boolean }[]> }).versionStore.get('users.1');
|
// v0.6.3: 已提交版本随 commit 清理(快照读取已移除,版本链仅作事务内 undo)
|
||||||
expect(versions).toHaveLength(1);
|
const store = (mvcc as unknown as { versionStore: Map<string, unknown> }).versionStore;
|
||||||
expect(versions![0].committed).toBe(true);
|
expect(store.has('users.1')).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rollback 移除写入的版本', () => {
|
it('rollback 移除写入的版本', () => {
|
||||||
@@ -184,14 +184,14 @@ describe('AriaEngine — MVCC', () => {
|
|||||||
expect(versions[1].prevVersion).not.toBeNull(); // 版本链
|
expect(versions[1].prevVersion).not.toBeNull(); // 版本链
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deleteVersion 写入墓碑版本', () => {
|
it('deleteVersion 写入墓碑版本(未提交可见,commit 清理)', () => {
|
||||||
const txnId = mvcc.beginTransaction();
|
const txnId = mvcc.beginTransaction();
|
||||||
mvcc.deleteVersion('users', '1', txnId);
|
mvcc.deleteVersion('users', '1', txnId);
|
||||||
mvcc.commitTransaction(txnId);
|
const store = (mvcc as unknown as { versionStore: Map<string, { data: Record<string, unknown> }[]> }).versionStore;
|
||||||
const store = (mvcc as unknown as { versionStore: Map<string, { data: Record<string, unknown>; committed: boolean }[]> }).versionStore;
|
|
||||||
const versions = store.get('users.1')!;
|
const versions = store.get('users.1')!;
|
||||||
expect(versions[0].committed).toBe(true);
|
|
||||||
expect((versions[0].data as unknown as { __mvcc_tombstone: boolean }).__mvcc_tombstone).toBe(true);
|
expect((versions[0].data as unknown as { __mvcc_tombstone: boolean }).__mvcc_tombstone).toBe(true);
|
||||||
|
mvcc.commitTransaction(txnId);
|
||||||
|
expect(store.has('users.1')).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('活跃事务可并发登记', () => {
|
it('活跃事务可并发登记', () => {
|
||||||
@@ -205,21 +205,19 @@ describe('AriaEngine — MVCC', () => {
|
|||||||
expect(store.has('users.2')).toBe(true);
|
expect(store.has('users.2')).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('gc 清理过旧版本', () => {
|
it('gc 清理过旧版本(未提交版本链保留最新 N 个)', () => {
|
||||||
// 创建很多版本后 gc
|
// 创建很多未提交版本后 gc(活跃事务 undo 链)
|
||||||
for (let i = 0; i < 200; i++) {
|
for (let i = 0; i < 200; i++) {
|
||||||
const txnId = mvcc.beginTransaction();
|
const txnId = mvcc.beginTransaction();
|
||||||
mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
mvcc.writeVersion('users', '1', { ver: i }, txnId);
|
||||||
mvcc.commitTransaction(txnId);
|
|
||||||
}
|
}
|
||||||
mvcc.gc(100);
|
mvcc.gc(100);
|
||||||
// gc 后版本保留最新 100 个
|
// gc 后版本保留最新 100 个
|
||||||
const store = (mvcc as unknown as { versionStore: Map<string, unknown[]> }).versionStore;
|
const store = (mvcc as unknown as { versionStore: Map<string, unknown[]> }).versionStore;
|
||||||
expect(store.get('users.1')!.length).toBe(100);
|
expect(store.get('users.1')!.length).toBe(100);
|
||||||
// gc 后引擎仍可继续写入
|
// gc 后仍可继续写入
|
||||||
const txnId = mvcc.beginTransaction();
|
const txnId = mvcc.beginTransaction();
|
||||||
mvcc.writeVersion('users', '1', { ver: 999 }, txnId);
|
mvcc.writeVersion('users', '1', { ver: 999 }, txnId);
|
||||||
mvcc.commitTransaction(txnId);
|
|
||||||
expect(store.get('users.1')!.length).toBe(101);
|
expect(store.get('users.1')!.length).toBe(101);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ beforeEach(() => { installOPFSMock(new Map()); });
|
|||||||
|
|
||||||
describe('[v0.2.5] P0-1: 版本号统一', () => {
|
describe('[v0.2.5] P0-1: 版本号统一', () => {
|
||||||
test('VERSION 常量为当前版本(0.6.0)', () => {
|
test('VERSION 常量为当前版本(0.6.0)', () => {
|
||||||
expect(VERSION).toBe('0.6.2');
|
expect(VERSION).toBe('0.6.3');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -403,7 +403,7 @@ describe('[v0.3.3] P1-9: Savepoint + MVCC 一致性', () => {
|
|||||||
|
|
||||||
describe('[v0.3.3] 端到端', () => {
|
describe('[v0.3.3] 端到端', () => {
|
||||||
test('全部修复点可共存于 MetonaSqlark API', async () => {
|
test('全部修复点可共存于 MetonaSqlark API', async () => {
|
||||||
expect(VERSION).toBe('0.6.2');
|
expect(VERSION).toBe('0.6.3');
|
||||||
const db = new MetonaSqlark({ name: `e2e-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, mode: 'memory' });
|
const db = new MetonaSqlark({ name: `e2e-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, mode: 'memory' });
|
||||||
await db.init();
|
await db.init();
|
||||||
await db.defineTable('users', {
|
await db.defineTable('users', {
|
||||||
|
|||||||
@@ -0,0 +1,294 @@
|
|||||||
|
/**
|
||||||
|
* v0.6.3 修复回归测试 — 原子性/一致性/资源治理
|
||||||
|
*
|
||||||
|
* 覆盖:
|
||||||
|
* - B7: KVStore writeBatch 混合 put+delete 单条日志记录(真原子)
|
||||||
|
* - B8: WAL full 模式写入失败抛错(不再吞错)
|
||||||
|
* - B6: MemoryEngine SET NULL 级联索引残留(虚假 UNIQUE_VIOLATION)
|
||||||
|
* - B10: delete 级联两阶段(RESTRICT 预检,无部分级联)
|
||||||
|
* - B13: BufferPool 驱逐同步清理 pages Map(内存预算真实生效)
|
||||||
|
* - B11: LSM.flush 不再重复入链(无重复 SSTable)
|
||||||
|
* - B12: rollbackToSavepoint 重建受影响表二级索引
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { MemoryEngine } from '../src/engine/memory';
|
||||||
|
import { KVStoreEngine } from '../src/engine/kvstore_engine';
|
||||||
|
import { AriaEngine } from '../src/engine/aria/index';
|
||||||
|
import { createSchema } from '../src/table/schema';
|
||||||
|
import { KVStore } from '../src/engine/kvstore/index';
|
||||||
|
import { SharedMemoryBackend } from '../src/engine/kvstore/shared_memory_medium';
|
||||||
|
import { parseLogRecords, KVLogOp } from '../src/engine/kvstore/log';
|
||||||
|
import { WAL } from '../src/engine/aria/wal/log';
|
||||||
|
import { BufferPool } from '../src/engine/aria/buffer/pool';
|
||||||
|
|
||||||
|
function uniqueDB(): string {
|
||||||
|
return `v063-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 断言 Promise 以指定错误码拒绝 */
|
||||||
|
async function expectCode(promise: Promise<unknown>, code: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await promise;
|
||||||
|
} catch (e) {
|
||||||
|
expect((e as { code?: string }).code).toBe(code);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error(`Expected rejection with code "${code}", but promise resolved`);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('v0.6.3 — KVStore writeBatch 混合原子(B7)', () => {
|
||||||
|
it('put+delete 编码进同一条日志记录', async () => {
|
||||||
|
const kv = new KVStore(new SharedMemoryBackend(), 0);
|
||||||
|
await kv.open(uniqueDB());
|
||||||
|
await kv.put('keep', new TextEncoder().encode('k').buffer);
|
||||||
|
await kv.writeBatch(
|
||||||
|
{ a: new TextEncoder().encode('1').buffer, b: new TextEncoder().encode('2').buffer },
|
||||||
|
['old1', 'old2'],
|
||||||
|
);
|
||||||
|
// 直接读取日志文件字节:解析应为 2 条记录(put keep + 混合记录),
|
||||||
|
// 混合记录包含 2 PUT + 2 DELETE 条目
|
||||||
|
const medium = (kv as any).medium as SharedMemoryBackend;
|
||||||
|
const raw = await medium.read('__kv_log');
|
||||||
|
expect(raw).not.toBeNull();
|
||||||
|
const records: { entries: { op: KVLogOp }[] }[] = [];
|
||||||
|
parseLogRecords(new Uint8Array(raw!), (r) => records.push(r as never));
|
||||||
|
expect(records).toHaveLength(2);
|
||||||
|
const mixed = records[1].entries.map((e) => e.op);
|
||||||
|
expect(mixed.filter((op) => op === KVLogOp.PUT)).toHaveLength(2);
|
||||||
|
expect(mixed.filter((op) => op === KVLogOp.DELETE)).toHaveLength(2);
|
||||||
|
await kv.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('KVStoreEngine delete(SET NULL 级联)混合写为单记录', async () => {
|
||||||
|
const name = uniqueDB();
|
||||||
|
const eng = new KVStoreEngine();
|
||||||
|
await eng.open(name, 1);
|
||||||
|
await eng.createTable(createSchema('users', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('orders', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
user_id: { type: 'string', references: 'users.id', onDelete: 'SET NULL' },
|
||||||
|
}));
|
||||||
|
await eng.insert('users', [{ id: 'u1' }]);
|
||||||
|
await eng.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
|
||||||
|
await eng.delete('users', { table: 'users', where: { id: 'u1' } });
|
||||||
|
|
||||||
|
// 日志未 checkpoint:最后一条记录应同时含 orders 行重写(PUT)与 users 行删除(DELETE)
|
||||||
|
const kv = (eng as any).kv as KVStore;
|
||||||
|
const medium = (kv as any).medium as SharedMemoryBackend;
|
||||||
|
const raw = await medium.read('__kv_log');
|
||||||
|
const records: { entries: { op: KVLogOp }[] }[] = [];
|
||||||
|
parseLogRecords(new Uint8Array(raw!), (r) => records.push(r as never));
|
||||||
|
const last = records[records.length - 1].entries.map((e) => e.op);
|
||||||
|
expect(last).toContain(KVLogOp.PUT);
|
||||||
|
expect(last).toContain(KVLogOp.DELETE);
|
||||||
|
await eng.close();
|
||||||
|
|
||||||
|
// 重开后效果完整(SET NULL + 父行删除均生效)
|
||||||
|
const eng2 = new KVStoreEngine();
|
||||||
|
await eng2.open(name, 1);
|
||||||
|
const orders = await eng2.find('orders', { table: 'orders' });
|
||||||
|
expect(orders).toHaveLength(1);
|
||||||
|
expect(orders[0].user_id).toBeNull();
|
||||||
|
expect(await eng2.count('users')).toBe(0);
|
||||||
|
await eng2.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — WAL 写入失败传播(B8)', () => {
|
||||||
|
class FailingStore {
|
||||||
|
async append(_d: Uint8Array): Promise<void> { throw new Error('disk full'); }
|
||||||
|
async readAll(): Promise<Uint8Array> { return new Uint8Array(0); }
|
||||||
|
async truncate(): Promise<void> {}
|
||||||
|
async exists(): Promise<boolean> { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
it("full 模式 append 失败 reject(不再 console.warn 吞错)", async () => {
|
||||||
|
const wal = new WAL(new FailingStore() as never, true, 'full');
|
||||||
|
await expect(wal.append({
|
||||||
|
type: 1, txnId: 0, tableName: 't', key: '1', data: { v: 1 },
|
||||||
|
})).rejects.toThrow('disk full');
|
||||||
|
await expect(wal.appendBatch([{
|
||||||
|
type: 1, txnId: 0, tableName: 't', key: '2', data: { v: 2 },
|
||||||
|
}])).rejects.toThrow('disk full');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — MemoryEngine SET NULL 索引清理(B6)', () => {
|
||||||
|
it('级联 SET NULL 后旧值可被复用(不再虚假 UNIQUE_VIOLATION)', async () => {
|
||||||
|
const eng = new MemoryEngine();
|
||||||
|
await eng.open(uniqueDB(), 1);
|
||||||
|
await eng.createTable(createSchema('users', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('orders', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
user_id: { type: 'string', references: 'users.id', onDelete: 'SET NULL', unique: true },
|
||||||
|
}));
|
||||||
|
await eng.insert('users', [{ id: 'u1' }]);
|
||||||
|
await eng.insert('orders', [{ id: 'o1', user_id: 'u1' }]);
|
||||||
|
await eng.delete('users', { table: 'users', where: { id: 'u1' } });
|
||||||
|
// 旧值 u1 的索引条目应被完整清理(含空 Set 移除)
|
||||||
|
await eng.insert('orders', [{ id: 'o2', user_id: 'u1' }]);
|
||||||
|
const rows = await eng.find('orders', { table: 'orders' });
|
||||||
|
expect(rows).toHaveLength(2);
|
||||||
|
// 索引查询旧值返回正确
|
||||||
|
const byIdx = await eng.find('orders', { table: 'orders', where: { user_id: 'u1' } });
|
||||||
|
expect(byIdx).toHaveLength(1);
|
||||||
|
expect(byIdx[0].id).toBe('o2');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — delete 级联两阶段(B10)', () => {
|
||||||
|
async function buildMemory(): Promise<MemoryEngine> {
|
||||||
|
const eng = new MemoryEngine();
|
||||||
|
await eng.open(uniqueDB(), 1);
|
||||||
|
await eng.createTable(createSchema('parents', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('cascade_child', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
pid: { type: 'string', references: 'parents.id', onDelete: 'CASCADE' },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('restrict_child', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
pid: { type: 'string', references: 'parents.id', onDelete: 'RESTRICT' },
|
||||||
|
}));
|
||||||
|
return eng;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('Memory: 多行删除含 RESTRICT 违规则整体拒绝(无部分级联)', async () => {
|
||||||
|
const eng = await buildMemory();
|
||||||
|
await eng.insert('parents', [{ id: 'p1' }, { id: 'p2' }]);
|
||||||
|
await eng.insert('cascade_child', [{ id: 'c1', pid: 'p1' }]);
|
||||||
|
await eng.insert('restrict_child', [{ id: 'r2', pid: 'p2' }]);
|
||||||
|
await expectCode(eng.delete('parents', { table: 'parents' }), 'FOREIGN_KEY_VIOLATION');
|
||||||
|
// p1 的级联子行必须仍在(修复前:p1 子行已删、父行未删)
|
||||||
|
expect(await eng.count('parents')).toBe(2);
|
||||||
|
expect(await eng.count('cascade_child')).toBe(1);
|
||||||
|
expect(await eng.count('restrict_child')).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Aria: 多行删除含 RESTRICT 违规则整体拒绝(无部分级联)', async () => {
|
||||||
|
const eng = new AriaEngine({ storageBackend: 'memory' });
|
||||||
|
await eng.open(uniqueDB(), 1);
|
||||||
|
await eng.createTable(createSchema('parents', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('cascade_child', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
pid: { type: 'string', references: 'parents.id', onDelete: 'CASCADE' },
|
||||||
|
}));
|
||||||
|
await eng.createTable(createSchema('restrict_child', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
pid: { type: 'string', references: 'parents.id', onDelete: 'RESTRICT' },
|
||||||
|
}));
|
||||||
|
await eng.insert('parents', [{ id: 'p1' }, { id: 'p2' }]);
|
||||||
|
await eng.insert('cascade_child', [{ id: 'c1', pid: 'p1' }]);
|
||||||
|
await eng.insert('restrict_child', [{ id: 'r2', pid: 'p2' }]);
|
||||||
|
await expectCode(eng.delete('parents', { table: 'parents' }), 'FOREIGN_KEY_VIOLATION');
|
||||||
|
expect(await eng.count('parents')).toBe(2);
|
||||||
|
expect(await eng.count('cascade_child')).toBe(1);
|
||||||
|
expect(await eng.count('restrict_child')).toBe(1);
|
||||||
|
await eng.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('深层 CASCADE 链末端的 RESTRICT 也被预检拦截', async () => {
|
||||||
|
const eng = await buildMemory();
|
||||||
|
await eng.createTable(createSchema('deep_child', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
cid: { type: 'string', references: 'cascade_child.id', onDelete: 'RESTRICT' },
|
||||||
|
}));
|
||||||
|
await eng.insert('parents', [{ id: 'p1' }]);
|
||||||
|
await eng.insert('cascade_child', [{ id: 'c1', pid: 'p1' }]);
|
||||||
|
await eng.insert('deep_child', [{ id: 'd1', cid: 'c1' }]);
|
||||||
|
await expectCode(eng.delete('parents', { table: 'parents', where: { id: 'p1' } }), 'FOREIGN_KEY_VIOLATION');
|
||||||
|
expect(await eng.count('parents')).toBe(1);
|
||||||
|
expect(await eng.count('cascade_child')).toBe(1);
|
||||||
|
expect(await eng.count('deep_child')).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('无 RESTRICT 违规时级联删除照常完整执行', async () => {
|
||||||
|
const eng = await buildMemory();
|
||||||
|
await eng.insert('parents', [{ id: 'p1' }, { id: 'p2' }]);
|
||||||
|
await eng.insert('cascade_child', [{ id: 'c1', pid: 'p1' }, { id: 'c2', pid: 'p2' }]);
|
||||||
|
const count = await eng.delete('parents', { table: 'parents' });
|
||||||
|
expect(count).toBe(4); // 2 父 + 2 级联子
|
||||||
|
expect(await eng.count('parents')).toBe(0);
|
||||||
|
expect(await eng.count('cascade_child')).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — BufferPool 内存预算(B13)', () => {
|
||||||
|
it('驱逐同步清理 pages Map(容量约束真实生效)', async () => {
|
||||||
|
const pageIO = {
|
||||||
|
readPage: async (id: number) => {
|
||||||
|
const b = new ArrayBuffer(4096);
|
||||||
|
new DataView(b).setUint32(0, id);
|
||||||
|
new DataView(b).setUint8(4, 1);
|
||||||
|
return b;
|
||||||
|
},
|
||||||
|
writePage: async () => {},
|
||||||
|
allocatePageId: async () => 0,
|
||||||
|
freePageId: async () => {},
|
||||||
|
};
|
||||||
|
const pool = new BufferPool(pageIO, 2);
|
||||||
|
for (let i = 1; i <= 20; i++) {
|
||||||
|
const p = await pool.getPage(i);
|
||||||
|
pool.unpin(p!);
|
||||||
|
}
|
||||||
|
expect((pool as any).pages.size).toBeLessThanOrEqual(2);
|
||||||
|
// 驱逐后再次访问能重新加载(正确性不受影响)
|
||||||
|
const p = await pool.getPage(20);
|
||||||
|
expect(p).not.toBeNull();
|
||||||
|
pool.unpin(p!);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — LSM.flush 不重复入链(B11)', () => {
|
||||||
|
it('2 行 + 中间冻结 + flush → 恰好 2 个 SSTable(无重复产物)', async () => {
|
||||||
|
const eng = new AriaEngine({ storageBackend: 'memory' });
|
||||||
|
await eng.open(uniqueDB(), 1);
|
||||||
|
await eng.createTable(createSchema('t', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
v: { type: 'string' },
|
||||||
|
}));
|
||||||
|
const lsm = (eng as any).lsm;
|
||||||
|
await eng.insert('t', [{ id: '1', v: 'a' }]);
|
||||||
|
lsm.freezeMemtable();
|
||||||
|
await eng.insert('t', [{ id: '2', v: 'b' }]);
|
||||||
|
await lsm.flush();
|
||||||
|
const metas = lsm.levels[0] as { id: number; minKey: string; maxKey: string }[];
|
||||||
|
expect(metas).toHaveLength(2);
|
||||||
|
const keys = metas.flatMap((m) => [m.minKey, m.maxKey]).sort();
|
||||||
|
expect(keys).toEqual(['t:1', 't:1', 't:2', 't:2']);
|
||||||
|
const rows = await eng.find('t', { table: 't' });
|
||||||
|
expect(rows).toHaveLength(2);
|
||||||
|
await eng.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('v0.6.3 — rollbackToSavepoint 索引重建(B12)', () => {
|
||||||
|
it('savepoint 回滚后索引不含过期条目', async () => {
|
||||||
|
const eng = new AriaEngine({ storageBackend: 'memory' });
|
||||||
|
await eng.open(uniqueDB(), 1);
|
||||||
|
await eng.createTable(createSchema('t', {
|
||||||
|
id: { type: 'string', primaryKey: true },
|
||||||
|
tag: { type: 'string', index: true },
|
||||||
|
}));
|
||||||
|
await eng.insert('t', [{ id: '1', tag: 'x' }]);
|
||||||
|
await eng.beginTransaction();
|
||||||
|
await (eng as any).savepoint('sp1');
|
||||||
|
await eng.insert('t', [{ id: '2', tag: 'x' }]);
|
||||||
|
await (eng as any).rollbackToSavepoint('sp1');
|
||||||
|
await eng.commitTransaction();
|
||||||
|
// 数据只有 1 行
|
||||||
|
expect(await eng.count('t')).toBe(1);
|
||||||
|
// 索引查询 x 只返回 id=1(savepoint 后插入的 id=2 索引条目已清理)
|
||||||
|
const rows = await eng.find('t', { table: 't', where: { tag: 'x' } });
|
||||||
|
expect(rows).toHaveLength(1);
|
||||||
|
expect(rows[0].id).toBe('1');
|
||||||
|
await eng.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user