José Diaz Seng > DBIx-Table-TestDataGenerator-0.005 > DBIx::Table::TestDataGenerator::Tree

Download:
DBIx-Table-TestDataGenerator-0.005.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.005   Source  

NAME ^

DBIx::Table::TestDataGenerator::Tree - tree builder, used internally to handle self-references in the target table

DESCRIPTION ^

This module has nothing to do with databases and could be used on its own. It handles ordered directed graphs which we will call trees here. The trees are represented as hashes where the keys are seen as parent identifiers and the values are references to arrays containing the child identifiers as elements. The purpose of the current class is to allow to determine insertion points for new nodes based on criteria defined by the corresponding parameters. I did not want to write yet another tree handling module, but I could not find anything on CPAN fitting my special needs.

The method add_child adds a node in a place automatically determined and satisfying constraints defined by the parameters passed to it. (Implementation detail: For a branch where no more descendants need to be added, the base node is removed from the tree to avoid reconsidering the branch, therefore the tree will sometimes grow and sometimes be pruned.)

The algorithm used here is not based on recursion, I could not find a way to use this approach without sacrificing performance (to be honest, I could not find a complete solution based on recursion). Using the handled accessor and the @stack variable we maintain a state allowing us to continue to fill the tree relative to the place where we left it.

A note on terminology: When using the current class for TestDataGenerator, the root node of the represented tree does not correspond to a record in the target database, what we have called root records there corresponds to the (direct) children of the root node here. So root here does not mean the same thing as in the TestDataGenerator code.

SUBROUTINES/METHODS ^

root

Accessor for the (artificial) root of the tree, its id being determined as a GUID.

nodes

Accessor for the data structure representing the handled tree.

handled

Contains the handled nodes at level 1, internal use only.

add_auto_child

Arguments:

Returns a pair [ $id, $parent_id ] where the first element is the automatically determined id of the added child node and the second that of its parent as determined by the current method. As a side-effect, the tree is modified accordingly.

The purpose of the current method is the following: There are cases where we do not yet know the exact id a child node will get, e.g. in the case of an auto-incremented primary key column. For these cases, the current method allows to insert a child using an automatically determined (temporary) id. add_auto_child of course lets add_child do the hard work.

remove_leaf_node

Arguments:

Removes the node with id $child_id from the tree, should be used for leaf nodes only (this is not enforced but taken into account here) and will not work correctly for non-leaf nodes. Is used when handling auto-incremented primary key columns to remove temporary nodes.

add_leaf_node

Arguments:

Adds the leaf node with id $child_id to the tree. Also updates the @stack.

add_child

Arguments:

The main method of the current class, handles adding a node to a tree under the described constraints and in a depth-first, right-to-left manner.

depth

Returns the maximum depth of a tree, children of the root node having level 1.

AUTHOR ^

Jose Diaz Seng, <josediazseng at gmx.de>

COPYRIGHT AND LICENSE ^

Copyright (C) 2012-2013, Jose Diaz Seng.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

syntax highlighting: