/** * Configuration options for IAVL tree. * Based on the Go iavl options.go implementation. */ import type { Logger, } from "../utils/logger.js"; /** * Statistics tracking for IAVL operations. * Based on Go IAVL v1.3.5 options.go Statistics struct. */ export interface Statistics { /** Each time GetNode operation hit cache */ cacheHitCnt: number /** Each time GetNode operation miss cache */ cacheMissCnt: number /** Each time GetFastNode operation hit cache */ fastCacheHitCnt: number /** Each time GetFastNode operation miss cache */ fastCacheMissCnt: number } /** * Creates a new statistics object with all counters at zero. */ export function createStatistics(): Statistics { return { cacheHitCnt: 0, cacheMissCnt: 0, fastCacheHitCnt: 0, fastCacheMissCnt: 0, }; } /** * Helper functions for updating statistics. * These are null-safe - they do nothing if statistics is undefined. */ export function incCacheHitCnt(stats: Statistics | undefined): void { if (stats) { stats.cacheHitCnt++; } } export function incCacheMissCnt(stats: Statistics | undefined): void { if (stats) { stats.cacheMissCnt++; } } export function incFastCacheHitCnt(stats: Statistics | undefined): void { if (stats) { stats.fastCacheHitCnt++; } } export function incFastCacheMissCnt(stats: Statistics | undefined): void { if (stats) { stats.fastCacheMissCnt++; } } export function resetStatistics(stats: Statistics | undefined): void { if (stats) { stats.cacheHitCnt = 0; stats.cacheMissCnt = 0; stats.fastCacheHitCnt = 0; stats.fastCacheMissCnt = 0; } } /** * Options for configuring an IAVL tree. */ export interface Options { /** * Whether to fsync writes to disk. * Default: false */ sync?: boolean /** * Initial version number for new trees. * Default: 0 */ initialVersion?: number /** * Threshold in bytes for flushing batch writes. * When accumulated writes exceed this size, they are committed. * Default: 100000 (100KB) */ flushThreshold?: number /** * Whether to enable asynchronous pruning of old versions. * Default: false */ asyncPruning?: boolean /** * Maximum number of nodes to cache in memory (LRU). * Default: 10000 */ cacheSize?: number /** * Maximum number of fast nodes to cache in memory (LRU). * Fast nodes store the latest key-value state. * Default: 100000 */ fastCacheSize?: number /** * Whether to skip fast storage upgrade when loading existing trees. * Default: false */ skipFastStorageUpgrade?: boolean /** * Logger instance for IAVL operations. * If not provided, uses the default logger. */ logger?: Logger /** * Statistics tracker for monitoring IAVL operations. * If provided, statistics will be collected. */ statistics?: Statistics } /** * Default options for IAVL tree. * Matches Go's DefaultOptions() - only flushThreshold is set by default. * initialVersion is intentionally undefined (not set) to match Go behavior. */ export const DEFAULT_OPTIONS = { sync: false, // initialVersion is intentionally omitted - undefined by default like Go flushThreshold: 100_000, // 100KB asyncPruning: false, cacheSize: 10_000, fastCacheSize: 100_000, skipFastStorageUpgrade: false, } as const; /** * Merges user-provided options with defaults. * * @param options - User-provided options * @param defaultLogger - Default logger to use if none provided * @returns Complete options object with defaults filled in (except initialVersion which is optional) */ export function mergeOptions(options?: Options, defaultLogger?: Logger): Required> & Pick { const merged = { ...DEFAULT_OPTIONS, statistics: createStatistics(), ...options, }; return { ...merged, logger: merged.logger ?? defaultLogger ?? (() => { throw new Error("Logger must be provided either in options or as defaultLogger parameter"); })(), }; } /** * Validates options and throws if invalid. * * @param options - Options to validate * @throws Error if options are invalid */ export function validateOptions(options: Options): void { if (options.initialVersion !== undefined && options.initialVersion < 0) { throw new Error("initialVersion must be non-negative"); } if (options.flushThreshold !== undefined && options.flushThreshold <= 0) { throw new Error("flushThreshold must be positive"); } if (options.cacheSize !== undefined && options.cacheSize < 0) { throw new Error("cacheSize must be non-negative"); } if (options.fastCacheSize !== undefined && options.fastCacheSize < 0) { throw new Error("fastCacheSize must be non-negative"); } }