TCTDB存储结构
TCTDB是tokyo cabinet家族中的表格數據庫(如上圖),其實現基于TCHDB(hash database)和TCBDB(B-tree database)。
TCHDB參考:http://blog.chinaunix.net/space.php?uid=20196318&do=blog&id=327754
TCBDB的代碼沒有讀過,有時間也閱讀一下,其結構如下圖所示。
?
TCTDB的主要特性:
1.? 松散表格實現,以primary key標示表格的一行,每行包括多列,列以名字標示。
行的存儲使用TCHDB,把所有的行作為一個value(在內存中是一個map,map中為多個列的列名以及列值)存儲,更新時,也必須整體更新,即使是單個colomn的更新也需要把整體讀出來更新其中的部分信息。
2.?靈活的數據結構,所有的數據結構都作為char[]存儲,no schema,no data type。
3.??查詢機制,實現多種查詢操作(字符串匹配,正則,整數比較等),結果可按列字段排序。
4.??列索引,通過TCBDB存儲基于列的索引,查詢機制基于列索引(toke,q-gram),先后去對應列的索引倒排表,然后在表中進行匹配。
查詢支持的匹配方式
enum {?????????????????????????????????? /* enumeration for query conditions */
? TDBQCSTREQ,??????????????????????????? /* string is equal to */
? TDBQCSTRINC,?????????????????????????? /* string is included in */
? TDBQCSTRBW,??????????????????????????? /* string begins with */
? TDBQCSTREW,??????????????????????????? /* string ends with */
? TDBQCSTRAND,?????????????????????????? /* string includes all tokens in */
? TDBQCSTROR,??????????????????????????? /* string includes at least one token in */
? TDBQCSTROREQ,????????????????????????? /* string is equal to at least one token in */
? TDBQCSTRRX,??????????????????????????? /* string matches regular expressions of */
? TDBQCNUMEQ,??????????????????????????? /* number is equal to */
? TDBQCNUMGT,??????????????????????????? /* number is greater than */
? TDBQCNUMGE,??????????????????????????? /* number is greater than or equal to */
? TDBQCNUMLT,????????? ??????????????????/* number is less than */
? TDBQCNUMLE,??????????????????????????? /* number is less than or equal to */
? TDBQCNUMBT,??????????????????????????? /* number is between two tokens of */
? TDBQCNUMOREQ,????????????????????????? /* number is equal to at least one token in */
? TDBQCFTSPH,??????????????????????????? /* full-text search with the phrase of */
? TDBQCFTSAND,?????????????????????????? /* full-text search with all tokens in */
? TDBQCFTSOR,??????????????????????????? /* full-text search with at least one token in */
? TDBQCFT***,??????????????????????????? /* full-text search with the compound expression of */
? TDBQCNEGATE = 1 << 24,???????????????? /* negation flag */
? TDBQCNOIDX = 1 << 25?????????????????? /* no index flag */
};
?
TC支持多種類型查詢,匹配主要基于字符串和數值,字符串的匹配支持正則表達式,部分匹配,前向/后向匹配;數值的主要基于比較運算符。
?
結果排序方式
enum {?????????????????????????????????? /* enumeration for order types */
? TDBQOSTRASC,?????????????????????????? /* string ascending */
? TDBQOSTRDESC,????????????????????????? /* string descending */
? TDBQONUMASC,?????????????????????????? /* number ascending */
? TDBQONUMDESC?????????????????????????? /* number descending */
};
?
TCTDB可對查詢結果進行排序,支持以上四種方式的排序,按字符串升降序,按數值升降序。
?
索引類型
enum {?????????????????????????????????? /* enumeration for index types */
? TDBITLEXICAL,????????????????????????? /* lexical string */
? TDBITDECIMAL,????????????????????????? /* decimal string */
? TDBITTOKEN,??????????????????????????? /* token inverted index */
? TDBITQGRAM,??????????????????????????? /* q-gram inverted index */
};
?
TCTDB在建立列索引時,可設置索引類型,主要包括字典串序索引,十進制串序索引,TOKEN索引,q-gram索引。如果需要對某個列進行建立索引,則每當插入一個新的行時,會對相應的列添加指定的索引(每個索引對應一個TCBDB來存儲索引數據)。
?
| Name(主key) | Age | Company | Interest |
| Jack | 23 | baidu | basketball,sanguosha |
| Rose | 22 | tencent | pingpong,poker |
| Joe | 25 | taobao | kfc bicycle |
?
如果需要根據Age建立索引,索引類型為TDBITDECIMAL
則當三個行插入后,生成的索引(TCBDB)中包含三行,依次為22(rose),23(jack),25(joe)(索引會根據類型進行排序,括號中代表value)。
?
如果還需要對Company建立索引,類型為TDBITDECIMAL,則當插入三行后,生成的索引中三行依次為,baidu(jack),taobao(joe),tencent(rose)。
?
如果還需要對interest建立索引,類型為TDBITTOKEN,則當插入三行后,生成的索引中依次為,basketball(jack),bicycle(joe),kfc(joe),pingpong(rose),poker(rose),sanguosha(jack)。
?
有了以上索引,則當需要根據某個colomn的值進行查詢時,效率會相當高,其基于btree進行查找。
?
轉載于:https://www.cnblogs.com/yunnotes/archive/2013/04/19/3032349.html
總結