如何实现线程化注释? [英] How do I implement threaded comments?

查看:184
本文介绍了如何实现线程化注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可以支持线程化注释的Web应用程序。我需要能够根据收到的票数重新排列评论。 (与 reddit 中的线程评论的工作方式相同)

I am developing a web application that can support threaded comments. I need the ability to rearrange the comments based on the number of votes received. (Identical to how threaded comments work in reddit)

我很想听到SO社区对如何做的输入。

I would love to hear the inputs from the SO community on how to do it.

我应该如何设计注释表格?
下面是我现在使用的结构:

How should I design the comments table? Here is the structure I am using now:

Comment
    id
    parent_post
    parent_comment
    author
    points

如何从此表中获取详细信息以正确的方式显示它们?
(欢迎以任何语言实现,我只是想知道如何以最好的方式做)

How should I get the details from this table to display them in the correct manner? (Implementation in any language is welcome. I just want to know how to do it in the best possible manner)

我需要做什么

提前感谢。

推荐答案

在数据库中存储树是一个有很多不同解决方案的主题。它取决于是否要检索子层次结构(所有子项的X),或者如果你只想抓住整个层次结构,并使用字典在内存中以O(n)的方式构建树。

Storing trees in a database is a subject which has many different solutions. It depends on if you want to retrieve a subhierarchy as well (so all children of item X) or if you just want to grab the entire set of hierarchies and build the tree in an O(n) way in memory using a dictionary.

您的表的优点是,您可以通过在父主题上过滤来获取对文章的所有评论。因为你已经在教科书/天真方式中定义了评论的父节点,所以你必须在内存中构建树(见下文)。如果你想从DB获取树,你需要一个不同的方式来存储树:
看到我在这里描述一个基于预计算的方法:
http://www.llblgen.com/tinyforum/GotoMessage.aspx?MessageID=17746&ThreadID=3208
使用CELKO描述的均衡树这里

Your table has the advantage that you can fetch all comments on a post in 1 go, by filtering on the parentpost. As you've defined the comment's parent in the textbook/naive way, you have to build the tree in memory (see below). If you want to obtain the tree from the DB, you need a different way to store a tree: See my description of a pre-calc based approach here: http://www.llblgen.com/tinyforum/GotoMessage.aspx?MessageID=17746&ThreadID=3208 or by using balanced trees described by CELKO here:

或另一种方法:
http://www.sqlteam.com/article/more-trees-hierarchies-in-sql

如果您在内存中的层次结构中获取所有内容并在那里构建树,则可以更高效,因为查询非常简单:select .. from Comment其中ParentPost = @id ORDER BY ParentComment ASC

If you fetch everything in a hierarchy in memory and build the tree there, it can be more efficient due to the fact that the query is pretty simple: select .. from Comment where ParentPost = @id ORDER BY ParentComment ASC

在该查询之后,您只需要在内存中构建一个树,其中包含跟踪元组CommentID - Comment的一个字典。你现在遍历结果集并动态构建树:你遇到的每个注释,你可以在字典中查找其parentcomment,然后将当前处理的注释也存储在该字典中。

After that query, you build the tree in memory with just 1 dictionary which keeps track of the tuple CommentID - Comment. You now walk through the resultset and build the tree on the fly: every comment you run into, you can lookup its parentcomment in the dictionary and then store the comment currently processed also in that dictionary.

这篇关于如何实现线程化注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆