{"version":3,"file":"node-iterator.cjs","names":[],"sources":["../../src/storage/node-iterator.ts"],"sourcesContent":["/**\n * NodeIterator - DFS traversal iterator for tree nodes\n * Used for orphan detection and tree comparison operations\n *\n * Based on Go IAVL v1.3.5 iterator.go NodeIterator\n */\n\nimport type {\n  Node,\n} from \"../node.js\";\nimport type {\n  NodeDB,\n} from \"./node-db.js\";\n\n/**\n * NodeIterator provides depth-first traversal of a tree starting from a root node.\n * Unlike TreeIterator which iterates key-value pairs, NodeIterator traverses nodes themselves.\n *\n * This is primarily used for:\n * - Orphan detection (comparing trees at different versions)\n * - Tree structure traversal\n * - Node-level operations\n */\nexport class NodeIterator {\n  private nodesToVisit: Node[];\n  private readonly ndb: NodeDB;\n  private err: Error | null = null;\n\n  /**\n   * Creates a new NodeIterator starting from the given root node key.\n   *\n   * @param rootKey - The node key to start iteration from (null for empty tree)\n   * @param ndb - NodeDB instance for loading child nodes\n   */\n  private constructor(rootKey: Uint8Array | null, ndb: NodeDB) {\n    this.ndb = ndb;\n    this.nodesToVisit = [];\n\n    if (rootKey && rootKey.length > 0) {\n      // Load the root node synchronously (will be loaded in static async factory)\n      this.nodesToVisit = []; // Will be set by factory\n    }\n  }\n\n  /**\n   * Factory method to create a new NodeIterator.\n   * Must be async to load the initial root node from storage.\n   *\n   * @param rootKey - The node key to start iteration from (null for empty tree)\n   * @param ndb - NodeDB instance for loading child nodes\n   * @returns A new NodeIterator instance\n   */\n  static async create(\n    rootKey: Uint8Array | null,\n    ndb: NodeDB,\n  ): Promise<NodeIterator> {\n    const iter = new NodeIterator(rootKey, ndb);\n\n    if (rootKey && rootKey.length > 0) {\n      const node = await ndb.getNode(rootKey);\n      if (!node) {\n        throw new Error(\"root node not found\");\n      }\n      iter.nodesToVisit = [node];\n    }\n\n    return iter;\n  }\n\n  /**\n   * Returns the current node being visited.\n   *\n   * @returns The current node (last in the nodesToVisit stack)\n   */\n  getNode(): Node {\n    return this.nodesToVisit[this.nodesToVisit.length - 1];\n  }\n\n  /**\n   * Checks if the iterator is still valid (has nodes to visit and no errors).\n   *\n   * @returns true if iterator can continue, false otherwise\n   */\n  valid(): boolean {\n    return this.err === null && this.nodesToVisit.length > 0;\n  }\n\n  /**\n   * Returns any error that occurred during iteration.\n   *\n   * @returns The error if any, null otherwise\n   */\n  error(): Error | null {\n    return this.err;\n  }\n\n  /**\n   * Moves to the next node in depth-first order.\n   *\n   * If isSkipped is true, the subtree under the current node is skipped entirely.\n   * Otherwise, the current node's children are added to the traversal stack.\n   *\n   * Traversal order: current node -> left subtree -> right subtree (DFS pre-order)\n   *\n   * @param isSkipped - If true, skip the current node's subtree\n   */\n  async next(isSkipped: boolean): Promise<void> {\n    if (!this.valid()) {\n      return;\n    }\n\n    const node = this.getNode();\n    // Pop current node from stack\n    this.nodesToVisit = this.nodesToVisit.slice(0, -1);\n\n    // If skipping, don't add children\n    if (isSkipped) {\n      return;\n    }\n\n    // If leaf node, no children to add\n    if (node.isLeaf()) {\n      return;\n    }\n\n    // Add right child first (so left child is processed first - stack LIFO)\n    if (node.rightNodeKey) {\n      try {\n        const rightNode = await this.ndb.getNode(node.rightNodeKey);\n        if (!rightNode) {\n          this.err = new Error(\"right child node not found\");\n          return;\n        }\n        this.nodesToVisit.push(rightNode);\n      }\n      catch (err) {\n        this.err = err instanceof Error ? err : new Error(String(err));\n        return;\n      }\n    }\n\n    // Add left child (will be processed first due to stack)\n    if (node.leftNodeKey) {\n      try {\n        const leftNode = await this.ndb.getNode(node.leftNodeKey);\n        if (!leftNode) {\n          this.err = new Error(\"left child node not found\");\n          return;\n        }\n        this.nodesToVisit.push(leftNode);\n      }\n      catch (err) {\n        this.err = err instanceof Error ? err : new Error(String(err));\n        return;\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;AAuBA,IAAa,eAAb,MAAa,aAAa;CACxB,AAAQ;CACR,AAAiB;CACjB,AAAQ,MAAoB;;;;;;;CAQ5B,AAAQ,YAAY,SAA4B,KAAa;AAC3D,OAAK,MAAM;AAC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