Operating on trees

22 views
Skip to first unread message

Mike Orr

unread,
Apr 26, 2013, 11:56:51 PM4/26/13
to Kotti mailing list
Are there any efficient techniques for selecting or deleting a tree of nodes? Or does it have to be one level's children at a time in a loop? I have two use cases:

- Importing pages in bulk from an existing site. If it doesn't work right I want to be able to delete a subtree and try again.

- Comments. It seems like there should be a way to get a tree of comments related to a document in one query. But this doesn't seem to be possible if the comments are regular nodes. I'm wondering if it's worth making a separate comments table that's not a node, so that it can have a node_id field tying the tree of comments to the node. Is there a way to do this in the regular node/document tables?

--
Mike Orr <slugg...@gmail.com>

Andreas Kaiser

unread,
Apr 27, 2013, 1:25:50 AM4/27/13
to ko...@googlegroups.com
Hi Mike!


On 27.04.2013, at 05:56, Mike Orr <slugg...@gmail.com> wrote:

> Are there any efficient techniques for selecting or deleting a tree of nodes? Or does it have to be one level's children at a time in a loop? I have two use cases:
>
> - Importing pages in bulk from an existing site. If it doesn't work right I want to be able to delete a subtree and try again.

If you delete the topmost node in the tree all children will also be
deleted. This should work OOTB, because the _children relation is
defined with cascade='all' (see
https://github.com/Kotti/Kotti/blob/master/kotti/resources.py#L221),
which implies 'delete'. This behavior is tested in
https://github.com/Kotti/Kotti/blob/master/kotti/tests/test_node.py#L117
and should thus work on all supported databases.

> - Comments. It seems like there should be a way to get a tree of comments related to a document in one query. But this doesn't seem to be possible if the comments are regular nodes. I'm wondering if it's worth making a separate comments table that's not a node, so that it can have a node_id field tying the tree of comments to the node. Is there a way to do this in the regular node/document tables?

DBSession().query(Comment).filter(Comment.parent_id == document_instance.id).delete()


HTH,

Andreas

Mike Orr

unread,
Apr 27, 2013, 12:20:53 PM4/27/13
to Kotti mailing list
In this case I'm selecting, not deleting. It looks like this will go only one level deep, but it won't get comments whose parent is a comment. That's what I'm getting at, how to select all records that have the page in their ancestry. I can convert it from a flat list to a tree myself, but I need the records. I could of course select all comments and ignore the ones that aren't descended from the page. Is there another way than either doing that or querying each comment to see if it has children?

Daniel Nouri

unread,
Apr 27, 2013, 12:46:17 PM4/27/13
to Kotti mailing list, Mike Orr
There isn't anything clever for this really. I've thought about
putting the object path (e.g. /folder/this/) into the Nodes table.
This could be quite useful for a number of things.

In your specific case, adding an additional 'commented_content'
pointer from the Comment to the document it belongs to could be
sufficient. That is if what you're trying to do is select all
comments of a given document.

Daniel

Mike Orr

unread,
Apr 27, 2013, 8:10:53 PM4/27/13
to Kotti mailing list
I knew somebody would come up with something better than I could. I'm trying to avoid adding columns to the built-in tables because that messes up upgrades and things. But a column in the comment table would work.
 


--
Mike Orr <slugg...@gmail.com>
Reply all
Reply to author
Forward
0 new messages