日期:2014-05-16  浏览次数:20393 次

【收藏】Tokyo Cabinet 的四种数据结构
Tokyo Cabinet提供了Hash、Fixed-length、Table和B+ Tree四种数据结构,不同的结构特性和应用场景都不一样。TC本身提供了专门测试和调试工具tc (h/f/t/b) mgr。

Tokyo Tyrant在启动的时候,通过数据库文件名后缀来表示使用哪种数据结构。
以下是结构和后缀对应表:

Hash Database :.tch
B+ tree database :.tcb
fixed-length database :.tcf
table database :.tct
内存Hash Database :*
内存B+ tree database :+
启动时,还可以根据不同数据结构设置不同参数,用#开始,参数和值用=分开,例如/ttserver/database.tch#capnum=1#capsiz=1

参数含义如下,适用于TT:

capnum :设置记录的最大容量
capsiz :设置内存型database的内存容量,内存不足记录将按照顺序移除
mode : 可选的选项:w (写)、r (读)、c (创建)、t (截断)、t (无锁)、f (非阻塞锁)。默认值为 :wc
idx :设置索引的列名,用:分割
opts :可选的选项:l (64位bucket数组,database容量可以超过2G)、d (Deflate压缩)、b(BZIP2压缩)、t(TCBS压缩)
bnum :bucket的数量
apow :specifies the size of record alignment by power of 2. 如果负数,设置无效
fpow :specifies the maximum number of elements of the free block pool by power of 2. 如果负数,设置无效
rcnum :设置缓存记录的最大数,如果数值不是大于0则会禁用缓存,默认禁用
lcnum :设置缓存叶节点(leaf nodes)的最大数,如果数值不是大于0则会禁用缓存,默认值4096
ncnum :设置缓存非叶节点(non-leaf nodes)的最大数,如果数值不是大于0则会禁用缓存,默认值512
xmsiz :设置额外内存映射容量,如果数值不是大于0则会禁用内存映射,默认值67108864
dfunit :specifie the unit step number. If it is not more than 0, the auto defragmentation is disabled. It is disabled by default.
width :设置记录的固定大小,如果数值不是大于0,则默认是255
limsiz :设置数据库文件的大小,如果数值不是大于0,则默认是268435456
lmemb :设置每个叶节点页(leaf page)的成员数,如果数值不是大于0,则默认是128
nmemb :设置每个非叶节点页(non-leaf page)的成员数,如果数值不是大于0,则默认是256
TC有自己的一套读写缓冲机制,通过xmsiz设置内存缓冲大小,这个参数对整体性能影响比较大。

一、Hash Database
Hash Database是最基本的结构了,只提供key-value存储方式,类似于memcached,Hash Database的特点是查找速度很快,bucket越多,数据越分散,查找越快。

Hash database支持的参数有:"mode", "bnum", "apow", "fpow", "opts", "rcnum", "xmsiz", 和 "dfunit".
内存Hash Database支持的参数有:"bnum", "capnum", 和 "capsiz"

二、Fixed-length Database
Fixed-length Database的读写速度是最快的,并且存储所需的空间是最小的(因为不需要存储数据以外的结构关系,但是因为是定长的,所以会有空间浪费),key只能是数字,而value的长度是有限的,所以必须设置一个合适的value长度,太长会浪费空间,间接影响性能(TPS)。

Fixed-length database支持的参数有:"mode", "width", 和 "limsiz".

创建数据库
Java代码 
[root@localhost ~]# tcfmgr create user.f 

[root@localhost ~]# tcfmgr create user.f
插入数据
Java代码 
[root@localhost ~]# tcfmgr put user.f 123 00 
[root@localhost ~]# tcfmgr put user.f 124 'aa' 

[root@localhost ~]# tcfmgr put user.f 123 00
[root@localhost ~]# tcfmgr put user.f 124 'aa'
查询
Java代码 
[root@localhost ~]# tcfmgr get user.f 123 
00 

[root@localhost ~]# tcfmgr get user.f 123
00

三、B+ Tree Database
B+ Tree Database的特点是一个key可以有重复value,而且允许在value之间上下移动,value按插入顺序排列,可以范围查找key,也可以前缀查找key,查找的复杂度是O(log n),所以n越大性能越低。

B+ tree database支持的参数有:"mode", "lmemb", "nmemb", "bnum", "apow", "fpow", "opts", "lcnum", "ncnum", "xmsiz", and "dfunit"
内存B+ Tree Database支持的参数有:"capnum" and "capsiz".

创建数据库
Java代码 
[root@localhost ~]# tcbmgr create user 

[root@localhost ~]# tcbmgr create user

插入记录,重复key
Java代码 
[root@localhost ~]# tcbmgr put -dd user u1 123 
[root@localhost ~]# tcbmgr put -dd user u1 456 
[root@localhost ~]# tcbmgr put -dd user u1 789 
[root@localhost ~]# tcbmgr put -dd user u2 abc  
[root@localhost ~]# tcbmgr put -dd user u2 efg 

[root@localhost ~]# tcbmgr put -dd user u1 123
[root@localhost ~]# tcbmgr put -dd user u1 456
[root@localhost ~]# tcbmgr put -dd user u1 789
[root@localhost ~]# tcbmgr put -dd user u2 abc
[root@localhost ~]# tcbmgr put -dd user u2 efg

查询所有记录
Java代码 
[root@localhost ~]# tcbmgr list -pv user  
u1      123 
u1      456 
u1      789