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代碼 
  1. [root@localhost ~]# tcfmgr create user.f  

插入數據 
Java代碼 
  1. [root@localhost ~]# tcfmgr put user.f 123 00  
  2. [root@localhost ~]# tcfmgr put user.f 124 'aa'  

查詢 
Java代碼 
  1. [root@localhost ~]# tcfmgr get user.f 123  
  2. 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代碼 
  1. [root@localhost ~]# tcbmgr create user  


插入記錄,重複key 
Java代碼 
  1. [root@localhost ~]# tcbmgr put -dd user u1 123  
  2. [root@localhost ~]# tcbmgr put -dd user u1 456  
  3. [root@localhost ~]# tcbmgr put -dd user u1 789  
  4. [root@localhost ~]# tcbmgr put -dd user u2 abc  
  5. [root@localhost ~]# tcbmgr put -dd user u2 efg  


查詢所有記錄 
Java代碼 
  1. [root@localhost ~]# tcbmgr list -pv user  
  2. u1      123  
  3. u1      456  
  4. u1      789  
  5. u2      abc  
  6. u2      efg  


前綴查找 
Java代碼 
  1. [root@localhost ~]# tcbmgr list -pv -fm u1 user  
  2. u1      123  
  3. u1      456  
  4. u1      789  


範圍查找 
Java代碼 
  1. [root@localhost ~]# tcbmgr list -pv -rb u1 u2 user  
  2. u1      123  
  3. u1      456  
  4. u1      789  
  5. u2      abc  
  6. 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表 
Java代碼 
  1. [root@localhost ttserver]# tctmgr create user  

向表裏插入記錄 
Java代碼 
  1. [root@localhost ttserver]# tctmgr put user 1 "name" "u1" "password" "123"  
  2. [root@localhost ttserver]# tctmgr put user 3 "name" "u3" "password" "123456"  
  3. [root@localhost ttserver]# tctmgr put user 9 "name" "u9" "password" "123456789"  

刪除記錄 
Java代碼 
  1. [root@localhost ttserver]# tctmgr out user 3  


2.查詢 
查詢所有記錄 
Java代碼 
  1. [root@localhost ttserver]# tctmgr list -pv user  
  2. 1       name    u1      password        123  
  3. 3       name    u3      password        123456  
  4. 9       name    u9      password        123456789  


通過主鍵查詢 
Java代碼 
  1. [root@localhost ttserver]# tctmgr get user 1  
  2. name    u1  
  3. password        123  


通過其它字段查詢 
Java代碼 
  1. [root@localhost ttserver]# tctmgr search -pv user name STREQ "u1"  
  2. 1       name    u1      password        123  
  3. [root@localhost ttserver]# tctmgr search -pv user name STREQ "u1" password STREQ "123"  
  4. 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.排序 
Java代碼 
  1. [root@localhost ttserver]# tctmgr search -pv -ord name STRDESC user  
  2. 9       name    u9      password        123456789  
  3. 3       name    u3      password        123456  
  4. 1       name    u1      password        123  
排序參數含義如下: 
Java代碼 
  1. STRASC:按字符升序  
  2. STRDESC:按字符降序  
  3. NUMASC:按數字升序  
  4. NUMDESC:按字符降序  


4.設置索引 
Java代碼 
  1. [root@localhost ttserver]# tctmgr setindex -it "lexical" user name  

索引類型 
Java代碼 
  1. lexical:詞彙  
  2. decimal:數字  
  3. token:不明白  
  4. qgram:不明白  
  5. void:不明白  


5.tctmgr 
tctmgr支持的命令和參數 
Java代碼 
  1. tctmgr create [-tl] [-td|-tb|-tt|-tx] path [bnum [apow [fpow]]]  
  2. 創建數據庫  
  3. tctmgr inform [-nl|-nb] path  
  4. 輸出數據庫的狀況  
  5. tctmgr put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path pkey [cols ...]  
  6. 創建記錄  
  7. tctmgr out [-nl|-nb] [-sx] path pkey  
  8. 刪除記錄  
  9. tctmgr get [-nl|-nb] [-sx] [-px] [-pz] path pkey  
  10. 通過主鍵查詢記錄  
  11. tctmgr list [-nl|-nb] [-m num] [-pv] [-px] [-fm str] path  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章