Manage trees of data using the nested set model
Create a table for your tree data.
CREATE TABLE Department ( id INTEGER PRIMARY KEY AUTOINCREMENT, root_id integer, lft integer NOT NULL, rgt integer NOT NULL, level integer NOT NULL, name text NOT NULL, );
In your Schema or \s-1DB\s0 class add Tree::NestedSet to the top of the component list.
_\|_PACKAGE_\|_->load_components(qw( Tree::NestedSet ... ));
Specify the columns required by the module.
package My::Department; _\|_PACKAGE_\|_->tree_columns({ root_column => 'root_id', left_column => 'lft', right_column => 'rgt', level_column => 'level', });
Using it:
my $root = My::Department->create({ ... }); my $child = $root->add_to_children({ ... });
my $rs = $root->children; my @descendants = $root->children;
my $parent = $child->parent; my $rs = $child->ancestors; my @ancestors = $child->ancestors;
This module provides methods for working with nested set trees. The nested tree model is a way of representing hierarchical information in a database. This takes a different approach to the Adjacency List implementation. (see DBIx::Class::Tree::AdjacencyList which uses \*(C`parent\*(C' relationships in a recursive manner.
The NestedSet implementation can be more efficient for most searches than the Adjacency List Implementation, for example, to obtain all descendants requires recursive queries in the Adjacency List implementation but is a single query in the NestedSet implementation.
The trade-off is that NestedSet inserts are more expensive so it is most useful if you have an application that does many reads but few inserts.
More about NestedSets can be found at <http://en.wikipedia.org/wiki/Nested_set_model>
Oh, and although I give some code examples of familial relationships (where there are usually two parents), both Adjacency List and NestedSet implementations can only have one parent.
This module automatically creates several relationships.
$root_node = $node->root;
A belongs_to relation to the root of $nodes tree.
$all_nodes = $node->nodes; $new_node = $node->add_to_nodes({name => 'Mens Wear'});
A has_many relationship to all the nodes of $nodes tree.
Adding to this relationship creates a rightmost child to $node.
$parent = $node->parent;
A belongs_to relationship to the parent node of $nodes tree.
Note that only the root node does not have a parent.
$rs = $node->children; @children = $node->children; $child = $node->add_to_children({name => 'Toys'});
A has_many relation to the children of $node.
Adding to this relationship creates a rightmost child to $node.
$rs = $node->descendants; @descendants = $node->descendants; $child = $node->add_to_descendants({name => 'Mens Wear'});
A has_many relation to the descendants of $node.
Adding to this relationship creates a rightmost child to $node.
$rs = $node->ancestors; @ancestors = $node->ancestors; $parent = $node->add_to_ancestors({name => 'Head office'});
A has_many relation to the ancestors of $node.
Adding to this relationship creates a new node in place of $node and makes it the parent of $node. All descendants of $node will likewise be pushed town the hierarchy.
Many methods have alternative names, e.g. \*(C`left_siblings\*(C' and \*(C`previous_siblings\*(C'
This is in deference to the DBIx::Class::Ordered module which uses terms \*(C`previous\*(C' \*(C`next\*(C' \*(C`first\*(C' and \*(C`last\*(C'.
Similarly DBIx::Class::Tree::AdjacencyList::Ordered uses terms \*(C`append\*(C', \*(C`prepend\*(C', \*(C`before\*(C' and \*(C`after\*(C'
However, my preference to use terms \*(C`left\*(C' and \*(C`right\*(C' consistently when using this module. However, the other names are available if you are more familiar with those modules.
_\|_PACKAGE_\|_->tree_columns({ left_column => 'lft', right_column => 'rgt', root_column => 'root_id', level_column => 'level', });
Declare the name of the columns defined in the database schema.
None of these columns should be modified outside if this module. left_column and right_column are unlikely to be of any use to your application. They should be integer fields.
Multiple trees are allowed in the same table, each tree will have a unique value in the root_column. In the current implementation this should be an integer field
The level_column may be of use in your application, it defines the depth of each node in the tree (with the root at level zero).
my $tree = $schema->resultset('My::Department')->create({ name = 'Head Office', });
my $tree = $schema->resultset('My::Department')->create({ name = 'UK Office', root_id = $uk_office_ident, });
Creates a new root node.
If the root_column (root_id) is not provided then it defaults to producing a node where the root_column has the same value as the primary key. This will croak if the table is defined with multiple key primary index.
Note that no checks (yet) are made to stop you creating another key with the same root_id as an existing tree. If you do so you will get into a terrible mess!
$department->delete;
This will delete the node and all descendants. Cascade Delete is turned off in the has_many relationships \*(C`nodes\*(C' \*(C`children\*(C' \*(C`descendants\*(C' so that delete \s-1DTRT\s0.
if ($node->is_root) { print "Node is a root\n"; }
Returns true if the $node is a root node
$has_children = $node->is_branch;
Returns true if the node is a branche (i.e. has children)
$is_terminal_node = $node->is_leaf;
Returns true if the node is a leaf (i.e. it has no children)
@siblings = $node->siblings; $siblings_rs = $node->siblings;
Returns all siblings of this $node excluding $node itself.
Since a root node has no siblings it returns undef.
@younger_siblings = $node->left_siblings; $younger_siblings_rs = $node->left_siblings;
Returns all siblings of this $node to the left this $node.
Since a root node has no siblings it returns undef.
@older_siblings = $node->right_siblings; $older_siblings_rs = $node->right_siblings;
Returns all siblings of this $node to the right of this $node.
Since a root node has no siblings it returns undef.
$younger_sibling = $node->left_sibling;
Returns the sibling immediately to the left of this $node (if any).
$older_sibling = $node->right_sibling;
Returns the sibling immediately to the right of this $node (if any).
$youngest_sibling = $node->leftmost_sibling;
Returns the left most sibling relative to this $node (if any).
Does not return this $node if this node is the leftmost sibling.
$oldest_sibling = $node->rightmost_sibling;
Returns the right most sibling relative to this $node (if any).
Does not return this $node if this node is the rightmost sibling.
The following create methods create a new node in relation to an existing node.
$bart->create_right_sibling({ name => 'Lisa' });
Create a new node as a right sibling to $bart.
$bart->create_left_sibling({ name => 'Maggie' });
Create a new node as a left sibling to $bart.
$homer->create_rightmost_child({ name => 'Lisa' });
Create a new node as a rightmost child to $homer
$homer->create_leftmost_child({ name => 'Maggie' });
Create a new node as a leftmost child to $homer
The following attach methods take an existing node (and all of it's descendants) and attaches them to the tree in relation to an existing node.
The node being inserted can either be from the same tree (as identified by the root_column) or from another tree. If the root of another tree is attached then the whole of that tree becomes a sub-tree of this node's tree.
The only restriction is that the node being attached cannot be an ancestor of this node.
When attaching multiple nodes we try to \s-1DWIM\s0 so that the order they are specified in the call represents the order they appear in the siblings list.
e.g. if we had a parent with children A,B,C,D,E
and we attached nodes 1,2,3 in the following calls, we expect the following results.
$parent->attach_rightmost_child 1,2,3 gives us children A,B,C,D,E,1,2,3
$parent->attach_leftmost_child 1,2,3 gives us children 1,2,3,A,B,C,D,E
$child_C->attach_right_sibling 1,2,3 gives us children A,B,C,1,2,3,D,E
$child_C->attach_left_sibling 1,2,3 gives us children A,B,1,2,3,C,D,E
$child_C->attach_rightmost_sibling 1,2,3 gives us children A,B,C,D,E,1,2,3
$child_C->attach_leftmost_sibling 1,2,3 gives us children 1,2,3,A,B,C,D,E
$parent->attach_rightmost_child($other_node); $parent->attach_rightmost_child($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $parent as the rightmost children.
$parent->attach_leftmost_child($other_node); $parent->attach_leftmost_child($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $parent as the leftmost children.
$node->attach_right_sibling($other_node); $node->attach_right_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's siblings.
$node->attach_left_sibling($other_node); $node->attach_left_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's left siblings.
$node->attach_rightmost_sibling($other_node); $node->attach_rightmost_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's rightmost siblings.
$node->attach_leftmost_sibling($other_node); $node->attach_leftmost_sibling($other_node_1, $other_node_2, ...);
Attaches the other_nodes to $node as it's leftmost siblings.
$node->move_left;
Exchange the $node with the sibling immediately to the left and return the node it exchanged with.
If the $node is already the leftmost node then no exchange takes place and the method returns undef.
$node->move_right;
Exchange the $node with the sibling immediately to the right and return the node it exchanged with.
If the $node is already the rightmost node then no exchange takes place and the method returns undef.
$node->move_leftmost;
Exchange the $node with the leftmost sibling and return the node it exchanged with.
If the $node is already the leftmost node then no exchange takes place and the method returns undef.
$node->move_rightmost;
Exchange the $node with the rightmost sibling and return the node it exchanged with.
If the $node is already the rightmost node then no exchange takes place and the method returns undef.
Cuts the invocant and its descendants out of the tree they are in, making the invocant the root of a new tree. Returns the modified invocant.
Dissolves the entire thread, that is turn each node of the thread into a single-item tree of its own.
Support for Multiple Column Primary Keys is limited (mainly because I rarely use them) but I have tried to make it possible to use them. Please let me know if this does not work as well as you expect.
By the nature of Nested Set implementations, moving, inserting or deleting nodes in the tree will potentially update many (sometimes most) other nodes.
Even if you have preloaded some of the objects, if you make a change to one object the other objects will not reflect their new value until you have reloaded them from the database. (see \*(L"discard_changes\*(R" in DBIx::Class::Row)
A simple demonstration of this
$grampa = $schema->schema->resultset('Simpsons')->create({ name => 'Abraham' }); $homer = $grampa->add_children({name => 'Homer'}); $bart = $homer->add_children({name => 'Bart'});
The methods in this module will do their best to keep instances that they know about updated. For example the first call to \*(C`add_children\*(C' in the above example will update $grampa and $homer with the latest changes to the database.
However, the second call to \*(C`add_children\*(C' only knows about $homer and $bart and in adding a new node to the tree it will update the $grampa node in the database. To ensure you have the latest changes do the following.
$grampa->discard_changes.
Not doing so will have unpredictable results.
Code by Ian Docherty <[email protected]>
Based on original code by Florian Ragwitz <[email protected]>
Incorporating ideas and code from Pedro Melo <[email protected]>
Special thanks to Moritz Lenz who sent in lots of patches and changes for version 0.08
Copyright (c) 2009-2011 The above authors
This is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.