Scoop -- the swiss army chainsaw of content management
Front Page · Everything · News · Code · Help! · Wishlist · Project · Scoop Sites · Dev Notes · Latest CVS changes · Development Activities
How to get a thread in one query Feature Requests
By KWillets , Section Code []
Posted on Tue Jun 18, 2002 at 12:00:00 PM PST
In a recent story, it was mentioned that the Comments.pm code has a very clumsy way of retrieving and constructing the comments tree.

There are a number of semi-established methods for getting all the nodes in a tree or subtree in one query. I'll try to enumerate all of them, in the hope that one method will be adopted.

I'm willing to work on whatever solution seems workable.

It is an established fact that SQL-92 cannot query a tree or graph structure (represented as parent-child pairs) without procedural extensions. However, with minor preprocessing, a tree can be augmented with keys which make retrieval simple and fast. There are several approaches which have different insert and update performance, and space requirements.

Traversal Order Indexing (also known as the "nested sets" approach)

Imagine traversing a tree (in pre-order, say) and tagging each node with a sequence number in the order encountered. The useful property of these sequence numbers is that they fall into a continous range for each subtree, i.e. if a given node X is assigned sequence number k, its descendants will have sequence numbers k+1...k+C, where C is the number of descendants of X. Finding the descendants is as simple as retrieving all nodes within that range. If, during the traversal, we also tag X with the last number (k+C) in its subtree, X will have all the information needed to get X's descendants.

This method requires that the sequence numbers be updated when a new node is inserted; every sequence number after the new node must be incremented. In a database which uses table locks, this requirement may not be so onerous, but in general, it is. Various workarounds avoid having to renumber on every insert by leaving empty slots in the numbering.

Materialized Paths

The familiar dotted notation (chapter 2, section 2.1, 2.2, 2.2.1, etc.) for chapters and sections of documents uses this construct, as does just about every filesystem. Each node X is given a path X1.X2...Xk with the id's of its ancestors starting from root, and ending at X. X itself can be omitted from the path, saving some space. Finding the descendants of X requires a simple range query to find all nodes with a path between X1...Xk and X1...XkZ, where Z is a maximum value like MAXINT. Ordering by the paths puts the nodes in the correct display order.

Inserting nodes is simpler because no global updates are needed; new paths simply fit in between the older ones.

Maintaining the whole transitive closure

The transitive closure of a tree is a table of all ancestor-descendant pairs, no matter how many hops between them. For a node X at depth D, the TC will contain D ancestor-X pairs. Finding all descendants of a node is a very straightforward query, but the results may be hard to put in the correct display order.

Updating the TC on inserts and deletes is also a fairly complex operation, but not impossible.

Recursive queries

While I said I was talking about SQL-92, the more recent standard includes recursive queries, where a query can refer to its own result set in the FROM clause. So expressing defining a set S as "the root node, plus any node connected to S" is possible.

And, of course, Oracle has its venerable "connect by" clause, which is more limited.

< Private Messaging revisited | Delete Account Feature >

Menu
· create account
· faq
· search
· report bugs
· Scoop Administrators Guide
· Scoop Box Exchange

Login
Make a new account
Username:
Password:

Poll
comments should be retrieved by
· The current approach 0%
· Traversal order indexing (Nested Sets) 0%
· Materialized Paths 0%
· Upgrading to DB2 and using recursive queries 0%
· Maintain transitive closure 0%

Votes: 0
Results | Other Polls

Related Links
· More on Feature Requests
· Also by KWillets

Story Views
  38 Scoop users have viewed this story.

Display: Sort:
How to get a thread in one query | 5 comments (5 topical, 0 hidden)
More notes/questions (none / 0) (#1)
by KWillets on Thu Jun 20, 2002 at 11:53:55 AM PST

I've been looking at Comments.pm, Format.pm, and Post.pm, and I'm still very confused. But I think it's possible to make this work. My method would be to pull the comments into an ordered list and then format and indent/listify them for display. The current method seems to be to pull all the comment data and then pass it to a recursive threading process. The data and display layers are all mixed together.

If we wanted to do thread retrievals in one step, we would have to modify post_comment to add the path to each comment (using the materialized path method), and modify display_comments and/or format_comment to use them in the WHERE clause. It's possible that format_comment and get_list would go away or be simplified.

I haven't figured out all the security modes, but it would be fairly simple to get all the nested comments for a pid, in threaded order, as follows:

select path into $pidpath from comments where cid = $pid

select c.path, cid, ... from comments c where c.path between $pidpath and concat($pidpath, 'FF')
order by c.path

This is assuming the path is encoded as a string of fixed-length hex cid's. Depth or indentation level can be found by looking at the length of the path.



Avengers (none / 0) (#3)
by maicer on Sun Sep 18, 2016 at 10:22:13 PM PST

Marvel Comics has been trying to register the trademark for "Avengers" for jewelry for some time now. However, the US trademark office believes there would be a clash between them and high-end watch manufacturer breitling replica who already registered that trademark for watches. Marvel tried to argue that theirs was the better known trademark but that was used as evidence against them - that it might harm Breitling's high-end reputation if Marvel got the trademark. Marvel are appealing the decision. But they are also battling with gucci replica Watches over the far less But on the other hand... you have an Omega watch. And Marvel are still battling with Omega over the far less well known trademark for ray ban replica The Unknown. They tried to settle their difference over the summer, but to naught. And it looks like they will be going to trial next year.



Payday Loans Costa-mesa (none / 0) (#4)
by Pervez on Sun Apr 08, 2018 at 09:39:44 PM PST

I just earnestly almost like your website I just benefit from a time and energy dissertation planet. A piece of writing is really advantageous regarding Western say not to mention quite a lot of some people to edit. Equipped toil will be able altogether bring back within your websites for the purpose of spare items. Nowadays then click Payday Loans Costa-mesa Thanks a lot for the purpose of decent piece of writing.



kitchen renovation near me (none / 0) (#5)
by Pervez on Sun Mar 24, 2019 at 02:33:17 PM PST

Amazing which was unusual. I simply authored a very lengthy remark however when I clicked on publish my personal remark did not seem. Right now, click the link as well as adhere to this particular key phrase kitchen renovation near me Anyhow simply desired to state fantastic weblog!



How to get a thread in one query | 5 comments (5 topical, 0 hidden)
Display: Sort:

Hosted by ScoopHost.com Powered by Scoop
All trademarks and copyrights on this page are owned by their respective companies. Comments are owned by the Poster. The Rest © 1999 The Management

create account | faq | search