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