Heykuki News

TopNewBestAskShowJobs
TopNewBestAskShowJobs
Ask HN: How would you implement a threaded forum?
7 points
mixmax
17 years ago
How would you go about making a threaded forum, like on HN assuming that you want it to be efficient and deal well with edge cases? And also assuming that you want to load entries from a database, as opposed to having the information in a flat file (As I believe HN does)

The obvious and easy way would be to write a small recursive function that loads posts from the database where the parent is the current post. There are two disadvantages to this approach as I see it:

1) It doesn't deal well with posts that have children being deleted.

2) You would need to make a DB call every time you call the recursive function, resulting in a large number of DB calls to show a thread.

The first problem could be solved by not actually deleting the DB entry when an entry is deleted, but merely deleting the text. When you display the post you can check whether the textfield is empty and display [deleted] or something similar.

The second problem is a bit more hairy, I think. You could load all the entries of a thread into an array in one DB call and have your recursive function look through the array every time it runs. The problem is that this would be using a Shlemiel the painter's algorithm[1]. So maybe the solution is to delete entries from the array as the recursive function uses them.

How would you do it?

[1] http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm and http://www.joelonsoftware.com/printerFriendly/articles/fog0000000319.html

7 comments