-
Website
http://seancribbs.com/ -
Original page
http://seancribbs.com/tech/2009/09/28/modeling-a-tree-in-a-document-database/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
Ryan Bates
1 comment · 2 points
-
jnunemaker
1 comment · 5 points
-
Hogan Long
1 comment · 1 points
-
jgarber
1 comment · 2 points
-
Professor Sideburns
1 comment · 1 points
-
-
Popular Threads
{
"node" : "some_id",
"parent": "parent_id",
"mpath": [parent_id, that_nodes_parent,....,root]
}
The material path is the killer, because then a mapreduce view in couch will allow you to emit for each node in the material path to get aggregate counts for any sub-tree in the graph (assumes acyclic).
for (node in doc.mpath) {
emit( node, <something_to_aggregate>);
}
and then you've got a fully traversable index to query with start/endkey. Augmented with a reduce function you can also then get summary statistics (e.g. total descendant count, tree level, etc). We actually do a bunch of stuff in a single view by using complex keys:
emit( ["descendant_count", node], 1);
emit( ["daughters_at_depth", node_level], 1);
...
This is such a natural pattern that we should likely build it into couch as a default tree view
{ "id:a","id:b", "id:c", "id:d", "null", "null", "null" }
Working with heaps is easy.
-- For those that don't know, child-right is index*2, child-left is index*2+1, parent is floor(index/2)
(if it gets to big, make heap of heaps... say 255 nodes or something)