Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure you could. You could also select all comments on the item and rebuild the tree in-memory, as Vlad suggests elsewhere on this thread. The code isn't even as hard as I'd thought it'd be:

  comments = dbh.query('SELECT * FROM comments WHERE news_id = $newsid')
  response_tree = {}
  for comment in comments:
      comment.children = []
      response_tree[comment.id] = comment
  for comment in comments:
      response_tree[comment.parent_id].children.append(comment)
The nifty thing about storing it in the database is that you need no application-level post-processing, you can take advantage of the DB for ad-hoc queries (what if you want to show subtrees of all posts by a certain user, like the "threads" page here?), and it happens to map to a set of simple and fast operations provided by most databases.


After some reflection, I suppose my only hesitation with this method is that it locks one into dealing with the comments in a particular way, whereas the lisp model I outlined is easily adaptable to a variety of schemas.

However, I'm not sure how else one would want to model things. Must think more...


This is closely resembles the way i did it... With recurrsive includes (views). Performs flawlessly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: