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”.
創建數據庫
- [root @localhost ~]# tcfmgr create user.f
[root@localhost ~]# tcfmgr create user.f
插入數據
- [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'
查詢
- [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”.
創建數據庫
- [root @localhost ~]# tcbmgr create user
[root@localhost ~]# tcbmgr create user
插入記錄,重複key
- [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
查詢所有記錄
- [root @localhost ~]# tcbmgr list -pv user
- u1 123
- u1 456
- u1 789
- u2 abc
- u2 efg
[root@localhost ~]# tcbmgr list -pv user
u1 123
u1 456
u1 789
u2 abc
u2 efg
前綴查找
- [root @localhost ~]# tcbmgr list -pv -fm u1 user
- u1 123
- u1 456
- u1 789
[root@localhost ~]# tcbmgr list -pv -fm u1 user
u1 123
u1 456
u1 789
範圍查找
- [root @localhost ~]# tcbmgr list -pv -rb u1 u2 user
- u1 123
- u1 456
- u1 789
- u2 abc
- u2 efg
[root@localhost ~]# tcbmgr list -pv -rb u1 u2 user
u1 123
u1 456
u1 789
u2 abc
u2 efg
四、Table Database
Table Database的特點是支持檢索,支持多列字段,支持列索引,性能不如其它結構。
Table
Database提供了類似RMDB的存儲功能,一個主鍵可以有多個字段,例如,在RMDB中user表可能會有user_id、name和
password等字段,而在Table Database也提供這種支持。
Table database支持的參數有:”mode”, “bnum”, “apow”, “fpow”, “opts”, “rcnum”, “lcnum”, “ncnum”, “xmsiz”, “dfunit”, and “idx”.
1.類RMDB的表結構
Table Database最大的特點是支持類RMDB的表結構功能。
創建user表
- [root @localhost ttserver]# tctmgr create user
[root@localhost ttserver]# tctmgr create user
向表裏插入記錄
- [root @localhost ttserver]# tctmgr put user 1 “name” “u1″ “password” “123″
- [root @localhost ttserver]# tctmgr put user 3 “name” “u3″ “password” “123456″
- [root @localhost ttserver]# tctmgr put user 9 “name” “u9″ “password” “123456789″
[root@localhost ttserver]# tctmgr put user 1 "name" "u1" "password" "123"
[root@localhost ttserver]# tctmgr put user 3 "name" "u3" "password" "123456"
[root@localhost ttserver]# tctmgr put user 9 "name" "u9" "password" "123456789"
刪除記錄
- [root @localhost ttserver]# tctmgr out user 3
[root@localhost ttserver]# tctmgr out user 3
2.查詢
查詢所有記錄
- [root @localhost ttserver]# tctmgr list -pv user
- 1 name u1 password 123
- 3 name u3 password 123456
- 9 name u9 password 123456789
[root@localhost ttserver]# tctmgr list -pv user
1 name u1 password 123
3 name u3 password 123456
9 name u9 password 123456789
通過主鍵查詢
- [root @localhost ttserver]# tctmgr get user 1
- name u1
- password 123
[root@localhost ttserver]# tctmgr get user 1
name u1
password 123
通過其它字段查詢
- [root @localhost ttserver]# tctmgr search -pv user name STREQ “u1″
- 1 name u1 password 123
- [root @localhost ttserver]# tctmgr search -pv user name STREQ “u1″ password STREQ “123″
- 1 name u1 password 123
[root@localhost ttserver]# tctmgr search -pv user name STREQ "u1"
1 name u1 password 123
[root@localhost ttserver]# tctmgr search -pv user name STREQ "u1" password STREQ "123"
1 name u1 password 123
附查詢條件表達式(前面加~,相當於標準sql中的非)[資料來源採用tokyo cabinet搭建表格型DBM ]
- STREQ :完全包含字符串,相當於標準sql中的where 字段=‘字符串’
- STRINC :包含此字符串,相當於標準sql中like ‘*字符串*’
- STRBW :以此字符串開頭的內容,相當於標準sql中like ‘字符串*’
- STREW :以此字符串結尾的內容,相當於標準sql中like ‘*字符串’
- STRAND :包含在某區間內的內容,如標準sql中like ‘a-z’,那麼他只能是a到z的字母
- STROR :不包含在某區間內的內容,如標準sql中like ‘!a-z’,那麼他只能是數字或其他的符號
- STRRX :組合式結構,如標準sql中like “a“!b-m”#”
- NUMEQ :數值大小一樣 相當於標準sql中 where a=‘1’
- NUMGT :數值大於某一值 相當於標準sql中 where a>‘1’
- NUMGE :數值大於等於某一值 相當於標準sql中 where a>=‘1’
- NUMLT :數值小於某一值 相當於標準sql中 where a<‘1’
- NUMLE :數值小於某一值 相當於標準sql中 where a<=‘1’
- NUMBT :數值處於某一數值區間,相當於標準sql中 where a> 1 and a < 10
- NUMOREQ :數值不處於某一數值的區間,如果是a大於1,小於10的話,那這個表達式右邊的內容應該相當於標準sql中 where a< 1 or a > 10
3.排序
- [root @localhost ttserver]# tctmgr search -pv -ord name STRDESC user
- 9 name u9 password 123456789
- 3 name u3 password 123456
- 1 name u1 password 123
[root@localhost ttserver]# tctmgr search -pv -ord name STRDESC user
9 name u9 password 123456789
3 name u3 password 123456
1 name u1 password 123
排序參數含義如下:
- STRASC:按字符升序
- STRDESC:按字符降序
- NUMASC:按數字升序
- NUMDESC:按字符降序
STRASC:按字符升序
STRDESC:按字符降序
NUMASC:按數字升序
NUMDESC:按字符降序
4.設置索引
- [root @localhost ttserver]# tctmgr setindex -it “lexical” user name
[root@localhost ttserver]# tctmgr setindex -it "lexical" user name
索引類型
- lexical:詞彙
- decimal:數字
- token:不明白
- qgram:不明白
- void : 不明白
lexical:詞彙
decimal:數字
token:不明白
qgram:不明白
void:不明白
5.tctmgr
tctmgr支持的命令和參數
- tctmgr create [-tl] [-td|-tb|-tt|-tx] path [bnum [apow [fpow]]]
- 創建數據庫
- tctmgr inform [-nl|-nb] path
- 輸出數據庫的狀況
- tctmgr put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path pkey [cols ...]
- 創建記錄
- tctmgr out [-nl|-nb] [-sx] path pkey
- 刪除記錄
- tctmgr get [-nl|-nb] [-sx] [-px] [-pz] path pkey
- 通過主鍵查詢記錄
- tctmgr list [-nl|-nb] [-m num] [-pv] [-px] [-fm str] path
- 輸出所有記錄
- tctmgr search [-nl|-nb] [-ord name type] [-m num] [-sk num] [-kw] [-pv] [-px] [-ph] [-bt num] [-rm] [-ms type] path [name op expr ...]
- 通過自定義條件查詢記錄
- tctmgr optimize [-tl] [-td|-tb|-tt|-tx] [-tz] [-nl|-nb] [-df] path [bnum [apow [fpow]]]
- 優化數據庫
- tctmgr setindex [-nl|-nb] [-it type] path name
- 設置索引
- tctmgr importtsv [-nl|-nb] [-sc] path [file]
- Store records of TSV in each line of a file.
- tctmgr version
- Print the version information of Tokyo Cabinet.
tctmgr create [-tl] [-td|-tb|-tt|-tx] path [bnum [apow [fpow]]]
創建數據庫
tctmgr inform [-nl|-nb] path
輸出數據庫的狀況
tctmgr put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path pkey [cols ...]
創建記錄
tctmgr out [-nl|-nb] [-sx] path pkey
刪除記錄
tctmgr get [-nl|-nb] [-sx] [-px] [-pz] path pkey
通過主鍵查詢記錄
tctmgr list [-nl|-nb] [-m num] [-pv] [-px] [-fm str] path
輸出所有記錄
tctmgr search [-nl|-nb] [-ord name type] [-m num] [-sk num] [-kw] [-pv] [-px] [-ph] [-bt num] [-rm] [-ms type] path [name op expr ...]
通過自定義條件查詢記錄
tctmgr optimize [-tl] [-td|-tb|-tt|-tx] [-tz] [-nl|-nb] [-df] path [bnum [apow [fpow]]]
優化數據庫
tctmgr setindex [-nl|-nb] [-it type] path name
設置索引
tctmgr importtsv [-nl|-nb] [-sc] path [file]
Store records of TSV in each line of a file.
tctmgr version
Print the version information of Tokyo Cabinet.
金山公司在Table Database的基礎上,提供了SQL適配器,還整合了些方便使用的功能,TCSQL
TC博大精深,還有太多沒有理解的地方,錯誤在所謂難免,不妥之處請予賜教。。