fix: dts TS类型修复
This commit is contained in:
+46
-13
@@ -20,9 +20,12 @@ import { WALRecordType, type WALRecord } from './types';
|
||||
import { CheckpointManager } from './wal/checkpoint';
|
||||
import { IndexedDBBackend, MemoryBackend, type IStorageBackend } from './store/backend';
|
||||
import { OPFSBackend } from './store/opfs_backend';
|
||||
import { FileManager } from './store/file_manager';
|
||||
import { MVCCManager } from './transaction/mvcc';
|
||||
import { BloomFilter } from './index/bloom';
|
||||
import { BufferPool, type PageIO } from './buffer/pool';
|
||||
import { BufferPool } from './buffer/pool';
|
||||
import { compressLZ4, decompressLZ4 } from './compression/lz4';
|
||||
import { isCryptoEnabled, encryptPage, decryptPage } from './crypto';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// AriaEngine
|
||||
@@ -78,14 +81,10 @@ export class AriaEngine implements IStorageEngine {
|
||||
}
|
||||
await this.backend.open(dbName);
|
||||
|
||||
// 2a. 初始化 Buffer Pool(页面缓存)
|
||||
const pageIO: PageIO = {
|
||||
readPage: async (pageId) => this.backend.read(`pg_${pageId}`),
|
||||
writePage: async (pageId, data) => this.backend.write(`pg_${pageId}`, data),
|
||||
allocatePageId: async () => Date.now(),
|
||||
freePageId: async () => {},
|
||||
};
|
||||
this.bufferPool = new BufferPool(pageIO, this.config.bufferPoolPages);
|
||||
// 2a. FileManager (PageIO 实现) + Buffer Pool
|
||||
const fileManager = new FileManager(this.backend);
|
||||
await fileManager.init(dbName);
|
||||
this.bufferPool = new BufferPool(fileManager, this.config.bufferPoolPages);
|
||||
|
||||
// 2. 构建 SSTableStore
|
||||
const sstableStore = this.createSSTableStore();
|
||||
@@ -482,7 +481,7 @@ export class AriaEngine implements IStorageEngine {
|
||||
|
||||
async beginTransaction(): Promise<void> {
|
||||
if (this.currentTxnId) throw new DatabaseError('Transaction already in progress', 'TX_ACTIVE');
|
||||
this.currentTxnId = Date.now();
|
||||
this.currentTxnId = this.mvcc.beginTransaction();
|
||||
this.txnSnapshot = new Map();
|
||||
|
||||
this.wal.append({
|
||||
@@ -506,6 +505,8 @@ export class AriaEngine implements IStorageEngine {
|
||||
}
|
||||
}
|
||||
|
||||
this.mvcc.commitTransaction(this.currentTxnId);
|
||||
|
||||
this.wal.append({
|
||||
type: WALRecordType.COMMIT,
|
||||
txnId: this.currentTxnId,
|
||||
@@ -521,6 +522,7 @@ export class AriaEngine implements IStorageEngine {
|
||||
async rollbackTransaction(): Promise<void> {
|
||||
if (!this.currentTxnId) throw new DatabaseError('No active transaction', 'TX_NONE');
|
||||
|
||||
this.mvcc.rollbackTransaction(this.currentTxnId);
|
||||
this.txnSnapshot = null;
|
||||
|
||||
this.wal.append({
|
||||
@@ -666,12 +668,43 @@ export class AriaEngine implements IStorageEngine {
|
||||
|
||||
return {
|
||||
save: async (id, data) => {
|
||||
const buf = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer;
|
||||
let buf: ArrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer;
|
||||
// 压缩(若启用)
|
||||
if (this.config.compression) {
|
||||
const compressed = compressLZ4(new Uint8Array(buf));
|
||||
buf = compressed.buffer.slice(compressed.byteOffset, compressed.byteOffset + compressed.byteLength) as ArrayBuffer;
|
||||
}
|
||||
// 加密(若启用)
|
||||
if (isCryptoEnabled()) {
|
||||
const enc = await encryptPage(buf);
|
||||
const header = new Uint8Array(12 + 4); // IV(12) + originalLen(4)
|
||||
header.set(enc.iv, 0);
|
||||
new DataView(header.buffer).setUint32(12, data.byteLength, false);
|
||||
const combined = new Uint8Array(header.length + enc.data.byteLength);
|
||||
combined.set(header, 0);
|
||||
combined.set(new Uint8Array(enc.data), header.length);
|
||||
buf = combined.buffer;
|
||||
}
|
||||
await this.backend.write(`sst_${id}`, buf);
|
||||
},
|
||||
load: async (id) => {
|
||||
const buf = await this.backend.read(`sst_${id}`);
|
||||
return buf ? new Uint8Array(buf) : null;
|
||||
const raw = await this.backend.read(`sst_${id}`);
|
||||
if (!raw) return null;
|
||||
let buf = new Uint8Array(raw);
|
||||
// 解密(若数据带加密头)
|
||||
if (isCryptoEnabled() && buf.length > 16) {
|
||||
const iv = buf.slice(0, 12);
|
||||
const origLen = new DataView(buf.buffer, buf.byteOffset + 12, 4).getUint32(0, false);
|
||||
const ciphertext = buf.slice(16).buffer;
|
||||
const decrypted = await decryptPage(iv, ciphertext);
|
||||
buf = new Uint8Array(decrypted, 0, origLen);
|
||||
}
|
||||
// 解压(若启用)
|
||||
if (this.config.compression) {
|
||||
const decompressed = decompressLZ4(buf, buf.length * 2); // 估计原始大小
|
||||
buf = decompressed as any;
|
||||
}
|
||||
return buf;
|
||||
},
|
||||
delete: async (id) => {
|
||||
await this.backend.delete(`sst_${id}`);
|
||||
|
||||
@@ -27,9 +27,8 @@ export class FileManager implements PageIO {
|
||||
/** 初始化:从存储中读取元数据 */
|
||||
async init(dbName: string): Promise<void> {
|
||||
this.dbName = dbName;
|
||||
// 读取 nextPageId
|
||||
const meta = await this.backend.read('__aria_meta');
|
||||
if (meta) {
|
||||
if (meta && meta instanceof ArrayBuffer && meta.byteLength >= 4) {
|
||||
const view = new DataView(meta);
|
||||
this.nextPageId = view.getUint32(0, false);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user