bbs的树形结构一直是大家讨论的话题,以前我做都是利用命名规则来实现,这样的好处是表的冗余字段少,结构清楚,容易理解,但其局限性也很明显。感谢廖家远提供算法(实话说,当年算法就没有学好),我决定采用一下这种算法来实现bbs的树形结构。基本思路如下:
bbs文章表中有这样几个字段:
rootid : 根id , 新发贴子及其所有子贴都相同。
fatherid: 父id , 父贴子id
layer: 层数 , 贴子在树中的深度。
ordernum: 排序基数,关键所在,根据它来排序。
基本算法举例如下:
根16(拿个小的举例)
id ordernum layer
1 16 0
2 16+16/2 1 回复第1贴
3 16+16/(2^2) 1 回复第1贴
4 16+16/2+16/(2^3) 2 回复第2贴
5 16+16/(2^2)+16/(2^4) 2 回复第3贴
然后,根据排序的结果是(加上回复的深度,就成了树状结构)
id ordernum 深度
1 16 0
3 16+16/(2^2) 1
5 16+16/(2^2)+16/(2^4) 2
2 16+16/2 1
4 16+16/2+16/(2^3) 2
成了这样的树:
1
3
5
2
4
根据以上思路,我们设计表如下:
/*bbs文章表*/
if exists (select * from sysobjects where id = object_id("bbs"))
drop table bbs
go
create table bbs
(
id int primary key identity not null ,
rootid int default 0 not null ,
fatherid int default 0 not null ,
layer tinyint default 0 not null ,
forumid int default 0 not null ,
userid int default 0 not null ,
title varchar(255) default "" not null ,
content text default "" ,
posttime datetime default getdate() not null ,
faceid tinyint default 0 not null ,
totalchilds int default 0 not null ,
ordernum float default power(2,30) not null ,
hits int default 0 not null ,
selected bit default 0 not null ,
closed bit default 0 not null ,
ifemail bit default 0 not null ,
ifsignature bit default 0 not null
)
go
/*bbs注册用户表*/
if exists(select * from sysobjects where id = object_id("bbsuser"))
drop table bbsuser
go
create table bbsuser
(
id int primary key identity not null ,
username varchar(20) default "" not null ,
password varchar(10) default "" not null ,
usertype tinyint default 0 not null , –用户类型,1为斑竹
email varchar(100) default "" not null ,
homepage varchar(100) default "" not null ,
icq varchar(20) default "" not null ,
signature varchar(255) default "" not null , –签名
point int default 0 not null , –用户积分
)
go
表结构定了,剩下的就是怎样实现。我把所有相关的功能集成在一个存储过程中,包括贴子本身的存储,其关键是排序基数的生成;父贴子相关字段的更新 ; 根贴相关字段的更新,这些都放到一个事务中,以保持数据的一致性,另外如果父贴要求有回复用email通知,在存储过程中实现发回复email的功能,而不必借助任何asp或其他的组件。这样就使所有任务在一个存储过程中实现。
