{"version":3,"file":"options.cjs","names":[],"sources":["../../src/types/options.ts"],"sourcesContent":["/**\n * Configuration options for IAVL tree.\n * Based on the Go iavl options.go implementation.\n */\n\nimport type {\n  Logger,\n} from \"../utils/logger.js\";\n\n/**\n * Statistics tracking for IAVL operations.\n * Based on Go IAVL v1.3.5 options.go Statistics struct.\n */\nexport interface Statistics {\n  /** Each time GetNode operation hit cache */\n  cacheHitCnt: number\n  /** Each time GetNode operation miss cache */\n  cacheMissCnt: number\n  /** Each time GetFastNode operation hit cache */\n  fastCacheHitCnt: number\n  /** Each time GetFastNode operation miss cache */\n  fastCacheMissCnt: number\n}\n\n/**\n * Creates a new statistics object with all counters at zero.\n */\nexport function createStatistics(): Statistics {\n  return {\n    cacheHitCnt: 0,\n    cacheMissCnt: 0,\n    fastCacheHitCnt: 0,\n    fastCacheMissCnt: 0,\n  };\n}\n\n/**\n * Helper functions for updating statistics.\n * These are null-safe - they do nothing if statistics is undefined.\n */\n\nexport function incCacheHitCnt(stats: Statistics | undefined): void {\n  if (stats) {\n    stats.cacheHitCnt++;\n  }\n}\n\nexport function incCacheMissCnt(stats: Statistics | undefined): void {\n  if (stats) {\n    stats.cacheMissCnt++;\n  }\n}\n\nexport function incFastCacheHitCnt(stats: Statistics | undefined): void {\n  if (stats) {\n    stats.fastCacheHitCnt++;\n  }\n}\n\nexport function incFastCacheMissCnt(stats: Statistics | undefined): void {\n  if (stats) {\n    stats.fastCacheMissCnt++;\n  }\n}\n\nexport function resetStatistics(stats: Statistics | undefined): void {\n  if (stats) {\n    stats.cacheHitCnt = 0;\n    stats.cacheMissCnt = 0;\n    stats.fastCacheHitCnt = 0;\n    stats.fastCacheMissCnt = 0;\n  }\n}\n\n/**\n * Options for configuring an IAVL tree.\n */\nexport interface Options {\n  /**\n   * Whether to fsync writes to disk.\n   * Default: false\n   */\n  sync?: boolean\n\n  /**\n   * Initial version number for new trees.\n   * Default: 0\n   */\n  initialVersion?: number\n\n  /**\n   * Threshold in bytes for flushing batch writes.\n   * When accumulated writes exceed this size, they are committed.\n   * Default: 100000 (100KB)\n   */\n  flushThreshold?: number\n\n  /**\n   * Whether to enable asynchronous pruning of old versions.\n   * Default: false\n   */\n  asyncPruning?: boolean\n\n  /**\n   * Maximum number of nodes to cache in memory (LRU).\n   * Default: 10000\n   */\n  cacheSize?: number\n\n  /**\n   * Maximum number of fast nodes to cache in memory (LRU).\n   * Fast nodes store the latest key-value state.\n   * Default: 100000\n   */\n  fastCacheSize?: number\n\n  /**\n   * Whether to skip fast storage upgrade when loading existing trees.\n   * Default: false\n   */\n  skipFastStorageUpgrade?: boolean\n\n  /**\n   * Logger instance for IAVL operations.\n   * If not provided, uses the default logger.\n   */\n  logger?: Logger\n\n  /**\n   * Statistics tracker for monitoring IAVL operations.\n   * If provided, statistics will be collected.\n   */\n  statistics?: Statistics\n}\n\n/**\n * Default options for IAVL tree.\n * Matches Go's DefaultOptions() - only flushThreshold is set by default.\n * initialVersion is intentionally undefined (not set) to match Go behavior.\n */\nexport const DEFAULT_OPTIONS = {\n  sync: false,\n  // initialVersion is intentionally omitted - undefined by default like Go\n  flushThreshold: 100_000, // 100KB\n  asyncPruning: false,\n  cacheSize: 10_000,\n  fastCacheSize: 100_000,\n  skipFastStorageUpgrade: false,\n} as const;\n\n/**\n * Merges user-provided options with defaults.\n *\n * @param options - User-provided options\n * @param defaultLogger - Default logger to use if none provided\n * @returns Complete options object with defaults filled in (except initialVersion which is optional)\n */\nexport function mergeOptions(options?: Options, defaultLogger?: Logger): Required<Omit<Options, \"initialVersion\">> & Pick<Options, \"initialVersion\"> {\n  const merged = {\n    ...DEFAULT_OPTIONS,\n    statistics: createStatistics(),\n    ...options,\n  };\n\n  return {\n    ...merged,\n    logger: merged.logger ?? defaultLogger ?? (() => {\n      throw new Error(\"Logger must be provided either in options or as defaultLogger parameter\");\n    })(),\n  };\n}\n\n/**\n * Validates options and throws if invalid.\n *\n * @param options - Options to validate\n * @throws Error if options are invalid\n */\nexport function validateOptions(options: Options): void {\n  if (options.initialVersion !== undefined && options.initialVersion < 0) {\n    throw new Error(\"initialVersion must be non-negative\");\n  }\n\n  if (options.flushThreshold !== undefined && options.flushThreshold <= 0) {\n    throw new Error(\"flushThreshold must be positive\");\n  }\n\n  if (options.cacheSize !== undefined && options.cacheSize < 0) {\n    throw new Error(\"cacheSize must be non-negative\");\n  }\n\n  if (options.fastCacheSize !== undefined && options.fastCacheSize < 0) {\n    throw new Error(\"fastCacheSize must be non-negative\");\n  }\n}\n"],"mappings":";;;;;AA2BA,SAAgB,mBAA+B;AAC7C,QAAO;EACL,aAAa;EACb,cAAc;EACd,iBAAiB;EACjB,kBAAkB;EACnB;;;;;;AAQH,SAAgB,eAAe,OAAqC;AAClE,KAAI,MACF,OAAM;;AAIV,SAAgB,gBAAgB,OAAqC;AACnE,KAAI,MACF,OAAM;;AAIV,SAAgB,mBAAmB,OAAqC;AACtE,KAAI,MACF,OAAM;;AAIV,SAAgB,oBAAoB,OAAqC;AACvE,KAAI,MACF,OAAM;;;;;;;AA+EV,MAAa,kBAAkB;CAC7B,MAAM;CAEN,gBAAgB;CAChB,cAAc;CACd,WAAW;CACX,eAAe;CACf,wBAAwB;CACzB;;;;;;;;AASD,SAAgB,aAAa,SAAmB,eAAqG;CACnJ,MAAM,SAAS;EACb,GAAG;EACH,YAAY,kBAAkB;EAC9B,GAAG;EACJ;AAED,QAAO;EACL,GAAG;EACH,QAAQ,OAAO,UAAU,wBAAwB;AAC/C,SAAM,IAAI,MAAM,0EAA0E;MACxF;EACL;;;;;;;;AASH,SAAgB,gBAAgB,SAAwB;AACtD,KAAI,QAAQ,mBAAmB,UAAa,QAAQ,iBAAiB,EACnE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,KAAI,QAAQ,mBAAmB,UAAa,QAAQ,kBAAkB,EACpE,OAAM,IAAI,MAAM,kCAAkC;AAGpD,KAAI,QAAQ,cAAc,UAAa,QAAQ,YAAY,EACzD,OAAM,IAAI,MAAM,iCAAiC;AAGnD,KAAI,QAAQ,kBAAkB,UAAa,QAAQ,gBAAgB,EACjE,OAAM,IAAI,MAAM,qCAAqC"}