Repairing the Tree
FlexTree is based on the Left-Right Value algorithm. The integrity of the tree structure strictly depends on the correctness of every node's leftValue and rightValue.
Directly manipulating the data table, abnormal interruption of write operations, or other external causes may corrupt the left/right values or levels of nodes. In such cases, normal query, traversal, and other operations will return incorrect results.
FlexTreeManager provides a repair method to repair a corrupted tree structure; in addition, flextree also exports a pure function repairTree that can directly repair an array of nodes without going through the database.
Note
repair is a write operation. It is automatically executed inside write and a database transaction, so there is no need to wrap it in write manually.
Repairing the Entire Tree
Call manager.repair() to repair the tree managed by the current manager.
import { FlexTreeManager } from 'flextree'
import SqliteAdapter from 'flextree-sqlite-adapter'
const tree = new FlexTreeManager('tree', {
adapter: new SqliteAdapter(),
})
await tree.repair()Notes
repairdirectly reads theid,level,leftValue, andrightValueof all nodes in the table (since the tree may already be corrupted, normal query methods cannot be relied upon). After rebuilding the structure, it writes only the changed nodes back to the database.- In multiple-trees-in-a-single-table scenarios, both reading and updating are automatically scoped to the current
treeIdrange, and will not affect other trees.
Repair Algorithm
repair rebuilds the tree structure based on the node's level information. The core steps are as follows:
- Sort by
leftValueto preserve the original order of nodes; - Use a stack and judge parent-child relationships based on
levelto reassign contiguousleftValue/rightValue, ensuring all values fall within the1..2Nrange with no gaps; - Normalize
level: the root node is0, incrementing by1per layer, automatically fixing level jumps (e.g.0 → 3 → 7will be normalized to0 → 1 → 2); - After repair, an integrity verification is performed automatically. A failed verification will throw an exception instead of returning incorrect data.
For example, a tree whose left/right values have been corrupted:
- Root
- A
- A1
- A2
- B
- A
After executing repair, the leftValue/rightValue of all nodes will be reassigned to contiguous and correct values, and the levels will also be normalized.
The repairTree Pure Function
If you already hold an array of nodes (for example, from another data source or a backup), you can use the exported repairTree pure function to repair them directly, without going through the database.
import { repairTree } from 'flextree'
const repaired = repairTree(nodes, {
keyFields: { /* Custom field names, optional */ },
treeId: 1, // Inject treeId for multi-tree tables, optional
})- Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
nodes | Record<string, any>[] | None | The array of nodes to repair (will not be modified) |
options.keyFields | CustomTreeKeyFields | default fields | Custom key field names |
options.treeId | any | None | The treeId injected into the resulting nodes for multi-tree tables |
Notes
repairTreeis a pure function. It does not modify the inputnodesand returns a new array of nodes that have been repaired and sorted byleftValue.- Nodes whose values have changed will carry
_level/_leftValue/_rightValuemetadata, recording the original values before repair for easy comparison.
Note
Before repairing, you can use the verify method to detect whether the tree structure is corrupted; after repairing, you can verify again to confirm integrity.