/** * In-memory database implementation for testing. * Based on the Go iavl db/memdb implementation. */ import { bytesToHex, compareBytes, copyBytes, } from "../utils/bytes.js"; import type { Batch, Database, Iterator, } from "./types.js"; /** * In-memory iterator implementation. */ class MemIterator implements Iterator { private index = 0; constructor( private readonly keys: Uint8Array[], private readonly db: Map, ) {} key(): Uint8Array { if (!this.valid()) { throw new Error("Iterator is not valid"); } return copyBytes(this.keys[this.index]); } value(): Uint8Array { if (!this.valid()) { throw new Error("Iterator is not valid"); } const keyHex = bytesToHex(this.keys[this.index]); const value = this.db.get(keyHex); if (!value) { throw new Error("Value not found for key"); } return copyBytes(value); } valid(): boolean { return this.index >= 0 && this.index < this.keys.length; } next(): void { this.index++; } close(): void { // No resources to clean up for in-memory iterator } } /** * In-memory batch implementation. */ class MemBatch implements Batch { private operations: Array<{ type: "set" | "delete" key: Uint8Array value?: Uint8Array }> = []; private closed = false; constructor(private readonly db: MemDB) {} set(key: Uint8Array, value: Uint8Array): void { if (this.closed) { throw new Error("Batch is closed"); } this.operations.push({ type: "set", key: copyBytes(key), value: copyBytes(value), }); } delete(key: Uint8Array): void { if (this.closed) { throw new Error("Batch is closed"); } this.operations.push({ type: "delete", key: copyBytes(key), }); } async write(): Promise { if (this.closed) { throw new Error("Batch is closed"); } for (const op of this.operations) { if (op.type === "set" && op.value) { await this.db.set(op.key, op.value); } else if (op.type === "delete") { await this.db.delete(op.key); } } this.close(); } close(): void { this.closed = true; this.operations = []; } } /** * In-memory database implementation using Map. * Useful for testing and development. */ export class MemDB implements Database { private data: Map = new Map(); private closed = false; async get(key: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } const keyHex = bytesToHex(key); const value = this.data.get(keyHex); return value ? copyBytes(value) : null; } async has(key: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } const keyHex = bytesToHex(key); return this.data.has(keyHex); } async set(key: Uint8Array, value: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } const keyHex = bytesToHex(key); this.data.set(keyHex, copyBytes(value)); } async delete(key: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } const keyHex = bytesToHex(key); this.data.delete(keyHex); } async iterator(start?: Uint8Array, end?: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } return this.createIterator(start, end, false); } async reverseIterator(start?: Uint8Array, end?: Uint8Array): Promise { if (this.closed) { throw new Error("Database is closed"); } return this.createIterator(start, end, true); } batch(): Batch { if (this.closed) { throw new Error("Database is closed"); } return new MemBatch(this); } async close(): Promise { this.closed = true; this.data.clear(); } /** * Gets the number of keys in the database. * Useful for testing. */ size(): number { return this.data.size; } private createIterator(start: Uint8Array | undefined, end: Uint8Array | undefined, reverse: boolean): Iterator { // Get all keys and convert from hex const allKeys: Uint8Array[] = []; for (const keyHex of this.data.keys()) { // Convert hex back to Uint8Array const bytes = new Uint8Array(keyHex.length / 2); for (let i = 0; i < bytes.length; i++) { bytes[i] = Number.parseInt(keyHex.slice(i * 2, i * 2 + 2), 16); } allKeys.push(bytes); } // Sort keys lexicographically allKeys.sort(compareBytes); // Filter by range let filteredKeys = allKeys; if (start) { filteredKeys = filteredKeys.filter(key => compareBytes(key, start) >= 0); } if (end) { filteredKeys = filteredKeys.filter(key => compareBytes(key, end) < 0); } // Reverse if needed if (reverse) { filteredKeys.reverse(); } return new MemIterator(filteredKeys, this.data); } }