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

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

问题描述

我正在开发一个可以支持线程注释的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

应该对这个结构做些什么改变?

What changes should be done to this structure?

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

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)

我需要什么东西在执行此功能时要小心,以便CPU /数据库的负载较少?

What are the stuff I need to take care while implementing this feature so that there is less load on the CPU/Database?

提前感谢。

推荐答案

在数据库中存储树是一个具有许多不同解决方案的主题。这取决于是否要检索子层次(所有项目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.

您的表格具有以下优点:您可以通过在父项目上进行过滤,以1次方式获取所有评论。由于您已经以教科书/幼稚的方式定义了评论的父母,所以您必须在内存中构建树(见下文)。如果要从数据库中获取树,则需要一种不同的方法来存储树:
请参阅我在此处的基于预先计算的方法的描述:
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 where 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天全站免登陆