{"id":269,"date":"2010-05-08T14:55:35","date_gmt":"2010-05-08T14:55:35","guid":{"rendered":"http:\/\/www.zhuangbiaowei.com\/blog\/?p=269"},"modified":"2010-05-08T14:55:35","modified_gmt":"2010-05-08T14:55:35","slug":"tcdatabase%e4%bb%8b%e7%bb%8d%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.zhuangbiaowei.com\/?p=269","title":{"rendered":"TCDatabase\u4ecb\u7ecd\uff081\uff09"},"content":{"rendered":"<p>TCDatabase\uff0c\u662f\u6211\u5728\u521b\u65b0\u9662\u7684\u540c\u4e8b\uff0c<a href=\"http:\/\/www.cnblogs.com\/riceball\/\" target=\"_blank\" rel=\"noopener\">riceball<\/a>\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\u3002<a href=\"http:\/\/code.google.com\/p\/tcdatabase\">http:\/\/code.google.com\/p\/tcdatabase<\/a><\/p>\n<p>\u4ed6\u81ea\u5df1\u4e5f\u5199\u4e86\u4e24\u7bc7blog\u4f5c\u4ecb\u7ecd\u3002<a href=\"http:\/\/www.cnblogs.com\/riceball\/archive\/2010\/05\/07\/tcdatabase-1.html\">tcdatabase(\u4e00\uff09<\/a>&#160;<a href=\"http:\/\/www.cnblogs.com\/riceball\/archive\/2010\/05\/07\/tcdatabase-2.html\">tcdatabase(\u4e8c)<\/a><\/p>\n<p>\u4e0d\u8fc7\u6211\u603b\u611f\u89c9\u5199\u5f97\u592a\u50cf\u5e72\u5df4\u5df4\u7684\u6280\u672f\u6587\u6863\u4e86\uff0c\u6240\u4ee5\u6211\u81ea\u544a\u594b\u52c7\u7684\u6765\u5e2e\u4ed6\u53e6\u5199\u4e00\u4e2a\u4ecb\u7ecd\uff0c\u4ee5\u4e0b\u662f\u7b2c\u4e00\u90e8\u5206\uff1a<\/p>\n<p>&#160;<\/p>\n<h2>\u4e00\u3001TokyoCabinet\u3001TokyoTyrant\u7b80\u4ecb<\/h2>\n<p>\u6211\u4eec\u5e38\u8bf4\u7684TC\/TT\uff0c\u662fTokyoCabinet\/TokyoTyrant\u7684\u7b80\u79f0\u3002\u8fd9\u4e24\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u90fd\u662f\u7531\u65e5\u672c\u4eba\u5e73\u6797\u5e79\u96c4\u5f00\u53d1\u7684\u3002\uff08<a href=\"http:\/\/1978th.net\/\">Mikio Hirabayashi&#8217;s Homepage<\/a> twitter: @hirabayashiM\uff09<\/p>\n<h3>1\u3001TokyoCabinet<\/h3>\n<p>TC\uff0c\u662f\u4e00\u4e2a<font color=\"#0000ff\">Key-Value\u7684\u6570\u636e\u5e93library<\/font>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7C\u8bed\u8a00\u7a0b\u5e8f\u6765\u8bbf\u95eeTC\u63d0\u4f9b\u7684\u5404\u79cd\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5404\u79cd\u8bed\u8a00\u7ed1\u5b9a\uff0c\u4f8b\u5982perl\u3001ruby\u3001java\u3001lua\u3002<\/p>\n<p>TC\u5bf9\u5916\u7684\u8868\u73b0\u5f62\u5f0f\uff0c\u65e0\u975e\u662f\u4e00\u7ec4put\/get\u65b9\u6cd5\uff0c\u4ece\u5185\u90e8\u5b9e\u73b0\u6765\u8bf4\uff0cTC\u4e00\u5171\u652f\u63016\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62echash\u6570\u636e\u5e93\uff0cB+\u6811\u6570\u636e\u5e93\uff0c\u5b9a\u957f\u6570\u636e\u5e93\u3001\u8868\u683c\u6570\u636e\u5e93\u3001\u5185\u5b58hash\u6570\u636e\u5e93\u4ee5\u53ca\u5185\u5b58B+\u6811\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4ee5ruby\u8bed\u8a00\u4e3e\u4f8b\uff1a<\/p>\n<blockquote>\n<p>hdb = HDB::new<\/p>\n<p>hdb.open(&quot;casket.tch&quot;, HDB::OWRITER | HDB::OCREAT)<\/p>\n<p>hdb.put(&quot;foo&quot;, &quot;hop&quot;)<\/p>\n<p>value = hdb.get(&quot;foo&quot;)<\/p>\n<p>hdb.close<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acasket.tch\u7684Hash\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u5e76\u8fdb\u884cput\/get\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7ADB\uff08Abstract database\uff09\uff0c\u4ee5\u5b8c\u5168\u76f8\u540c\u7684API\uff0c\u521b\u5efa\u5e76\u8bbf\u95ee\u4e0d\u540c\u7684\u6570\u636e\u5e93\u3002<\/p>\n<blockquote>\n<p>adb = ADB::new<\/p>\n<p>adb.open(name)&#160; <\/p>\n<p>\u2026<\/p>\n<p>adb.close<\/p>\n<\/blockquote>\n<p>\u5176\u4e2d\uff0c\u5982\u679cname\u4e3a*\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u5185\u5b58hash\u6570\u636e\u5e93\uff1bname\u4e3a+\uff0c\u5219\u662f\u5185\u5b58B+\u6811\u6570\u636e\u5e93\uff1b\u6587\u4ef6\u540d\u4e3a*.tch\u3001*.tcb\u3001*.tcf\u3001*.tct\u5219\u5206\u522b\u5bf9\u5e94\u4e8ehash\u3001B+Tree\u3001fixed-length\u548ctable\u7c7b\u578b\u3002<\/p>\n<h3>2\u3001TokyoTyrant<\/h3>\n<p>\u81f3\u4e8eTT\uff0c\u5219\u662f<font color=\"#0000ff\">\u5728TC\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684\u4e00\u4e2aserver<\/font>\u3002TT\u63a5\u53d7\u6765\u81easocket\u8fde\u63a5\u7684\u5404\u79cd\u8bf7\u6c42\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u7f51\u7edc\u670d\u52a1\u800c\u5b58\u5728\u7740\u3002\u901a\u5e38\u6211\u4eec\u4f1a\u8fd9\u6837\u6765\u542f\u52a8TT\u3002<\/p>\n<blockquote>\n<p>ttserver \u2013port 3900 \/ttdata\/casket.tch<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\uff0c\u57283900\u7aef\u53e3\uff0c\u5c31\u542f\u52a8\u4e86\u4e00\u4e2a\u6570\u636e\u5e93\u670d\u52a1\uff0c\u8fd9\u4e2a\u6570\u636e\u5e93\u7684\u6570\u636e\uff0c\u5c31\u4fdd\u5b58\u5728\/ttdata\/casket.tch\u4e2d\u3002<\/p>\n<p>\u800c\u5728client\u7aef\uff0c\u4e5f\u591a\u79cd\u4e0d\u540c\u7684\u8bed\u8a00\u5b9e\u73b0\uff0c\u4f8b\u5982ruby\u7684\u4ee3\u7801\u4f1a\u5199\u6210\u8fd9\u6837\uff1a<\/p>\n<blockquote>\n<p>rdb = RDB::new<\/p>\n<p>rdb.open(&quot;localhost&quot;, 3900)<\/p>\n<p>rdb.put(&quot;foo&quot;, &quot;hop&quot;)<\/p>\n<p>value = rdb.get(&quot;foo&quot;)<\/p>\n<p>rdb.close<\/p>\n<\/blockquote>\n<p>\u5bf9\u4e8etable\u7c7b\u578b\u7684\u6570\u636e\u5e93\uff0c\u5219\u9700\u8981\u521b\u5efa\u4e00\u4e2aRDBTBL\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u4e00\u4e9b\u8bbf\u95eeAPI\uff0c\u4f8b\u5982\uff1a<\/p>\n<blockquote>\n<p>rdb = RDBTBL::new<\/p>\n<p>rdb.open(&quot;localhost&quot;, 1978)<\/p>\n<p>rdb.put(&quot;1&quot;, { &quot;name&quot; =&gt; &quot;mikio&quot;, &quot;age&quot; =&gt; &quot;30&quot;, &quot;lang&quot; =&gt; &quot;ja,en,c&quot; })<\/p>\n<p>qry = RDBQRY::new(rdb)<\/p>\n<p>qry = RDBQRY::new(rdb) <\/p>\n<p>qry.addcond(&quot;age&quot;, RDBQRY::QCNUMGE, &quot;20&quot;) <\/p>\n<p>qry.addcond(&quot;lang&quot;, RDBQRY::QCSTROR, &quot;ja,en&quot;) <\/p>\n<p>qry.setorder(&quot;name&quot;, RDBQRY::QOSTRASC)<\/p>\n<p>qry.setlimit(10) <\/p>\n<p>res = qry.search <\/p>\n<p>res.each do |rkey| <\/p>\n<p>&#160; rcols = rdb.get(rkey) <\/p>\n<p>&#160; printf(&quot;name:%s\\n&quot;, rcols[&quot;name&quot;]) <\/p>\n<p>end<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\u7684\u64cd\u4f5c\uff0c\u5c31\u5df2\u7ecf\u76f8\u5f53\u63a5\u8fd1\u4e8e\u5bf9\u4e00\u4e2a\u4f20\u7edf\u8868\u7684\u64cd\u4f5c\u4e86\u3002<\/p>\n<p>&#160;<\/p>\n<h2>\u4e8c\u3001TCDatabase\u5bf9TokyoTyrant\u7684\u6269\u5c55<\/h2>\n<h3>1\u3001skeleton\u673a\u5236<\/h3>\n<p>TT\u5bf9\u4e8e\u6269\u5c55\u7684\u652f\u6301\u76f8\u5f53\u53cb\u597d\uff0c\u5728ttserver\u4e2d\uff0c\u6709\u4e00\u4e2a-skel\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u542f\u52a8ttserver\u7684\u65f6\u5019\uff0c\u6302\u4e00\u4e2a\u81ea\u5df1\u5199\u7684\u9aa8\u67b6\u7cfb\u7edf\uff0c\u4f8b\u5982\uff1a<\/p>\n<blockquote>\n<p>ttserver -skel mydb.so -port 3900 myfile.tct<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\uff0cttserver\u7684\u529f\u80fd\uff0c\u5c31\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7edc\u63a5\u53e3\uff0c\u800c\u63a5\u6536\u5230\u7684\u5404\u79cd\u8bf7\u6c42\uff0c\u90fd\u4e3a\u8f6c\u4ea4\u7ed9mydb.so\u6765\u5904\u7406\u3002\u63a5\u4e0b\u6765\u7684\u4e8b\u60c5\uff0c\u5c31\u6d77\u9614\u5929\u7a7a\u4e86\u3002\u5bf9\u4e8eclient\u7aef\u6765\u8bf4\uff0c\u4ed6\u8bbf\u95ee\u7684\u662f\u6807\u51c6\u7684TT\u63a5\u53e3\uff0c\u800c\u5728server\u7aef\uff0c\u5374\u5b8c\u5168\u53ef\u4ee5\u901a\u8fc7\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u5c06\u6570\u636e\u5b58\u5230mysql\u91cc\u9762\u53bb\u3002<\/p>\n<p>\u800ctcdatabase\uff0c\u5c31\u662fTT\u7684\u4e00\u4e2askeleton\u5b9e\u73b0\u3002\u6240\u4ee5\uff0c\u4ed6\u7684\u542f\u52a8\u53c2\u6570\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<blockquote>\n<p>ttserver -skel tcdatabase.so -port 3900 db_filename.tcb<\/p>\n<\/blockquote>\n<h3>2\u3001TCTDB\u7684\u4e0d\u8db3\u4e4b\u5904<\/h3>\n<p>\u4f5c\u4e3a\u6700\u50cf\u4f20\u7edf\u8868\u7684Key-Value\u6570\u636e\u5e93\uff0cTCTDB\u6709\u5f88\u591a\u4f18\u70b9\uff0c\u8fd9\u6210\u4e3a\u6211\u4eec\u9879\u76ee\u9009\u62e9\u7684\u4e3b\u8981\u8003\u8651\u5bf9\u8c61\uff0c\u4f46\u662f\u5b83\u4e5f\u5b58\u5728\u7740\u8bf8\u591a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2aTable Database\u4ec5\u652f\u6301\u4e00\u4e2a\u8868\uff0c\u4e5f\u5c31\u662f\u8bf4value\u4e2d\u7684\u5b57\u6bb5\u5fc5\u987b\u56fa\u5b9a\u4e00\u81f4\u3002\u5047\u8bbe\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528\u4e8680\u591a\u4e2a\u8868\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u5f00\u542f80\u591a\u4e2a ttserver\u8fdb\u7a0b\uff0c\u5e76\u4e3a\u6bcf\u4e00\u4e2a\u201c\u8868\u201d\u63d0\u4f9b\u652f\u6301\u3002 <\/li>\n<li>\u529f\u80fd\u7684\u589e\u5f3a,\u4e5f\u5c31\u610f\u5473\u7740\u8981\u727a\u7272\u6027\u80fd\u3002TCTDB \u8868\u683c\u578b\u6570\u636e\u5e93\u7684\u5e73\u5747\u8bfb\u53d6\u901f\u5ea6\u5927\u7ea6\u572840\u4e07\u6761\/\u79d2,\u76f8\u6bd4 TCHDB\u54c8\u5e0c\u6570\u636e\u5e93\u7684180\u4e07\u6761\/\u79d2\u548cTCBDB B+Tree\u6570\u636e\u5e93 \u7684100\u4e07\u6761\/\u79d2\u8981\u6162\u3002 <\/li>\n<li>TCTDB\u867d\u7136\u53ef\u4ee5\u5efa\u7acb\u6570\u503c\u578b\u7d22\u5f15,\u4f46\u662f\u5b83\u662f\u5c06\u6240\u6709value\u6570\u636e\u90fd\u5f53\u6210\u5b57\u7b26\u578b\u6765\u5904\u7406\u7684,\u65e0\u6cd5\u533a\u5206value\u7c7b\u578b\u3002 <\/li>\n<li>TCTDB\u5355\u6570\u636e\u5e93\u6587\u4ef6\u5b58\u50a8\u7684\u8bb0\u5f55\u6570\u4e0a\u4ebf\u6761\u540e,\u6027\u80fd\u4f1a\u6709\u6bd4\u8f83\u660e\u663e\u7684\u4e0b\u964d\u3002 <\/li>\n<li>\u4e0d\u80fd\u5355\u72ec\u83b7\u53d6value\u4e2d\u7684\u67d0\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\uff1b <\/li>\n<li>\u4e0d\u80fd\u652f\u6301\u4ec5\u66f4\u65b0UPDATE key\u4e2d\u67d0\u4e00\u4e2a\u5b57\u6bb5\uff1a\u5fc5\u987b\u5148\u53d6\u51favalue\u7684\u5168\u90e8\u5b57\u6bb5\uff0c\u518d\u5b58\u5165\uff1b <\/li>\n<\/ul>\n<h3>3\u3001TCDatabase\u7684\u6539\u8fdb<\/h3>\n<ul>\n<li>\u652f\u6301\u591a\u4e2atable\u4ece\u4e00\u4e2a\u7aef\u53e3\u8bbf\u95ee\uff0c\u4ecetable\u53d8\u6210\u771f\u6b63\u7684database\uff0c <\/li>\n<li>\u6570\u636e\u6587\u4ef6\u6539\u7528\u91c7\u7528TCBDB(B+Tree Database)\u8fdb\u884c\u5b58\u50a8\uff0c\u4e3a\u4e86\u89e3\u51b3\u6570\u636e\u91cf\u4e0a\u4ebf\u540e\u7684HashDB\u6027\u80fd\u7684\u95ee\u9898 <\/li>\n<li>\u652f\u6301\u5217\u8bfb\u5199 <\/li>\n<li>\u5e94\u8be5\u8fd8\u6709\u5176\u5b83\u66f4\u591a\u6539\u8fdb\uff0c\u4e0d\u8fc7\u90fd\u8fd8\u5728\u89c4\u5212\u4e4b\u4e2d <\/li>\n<\/ul>\n<h3>&#160;<\/h3>\n<p>(\u5f85\u7eed)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TCDatabase\uff0c\u662f\u6211\u5728\u521b\u65b0\u9662\u7684\u540c\u4e8b\uff0criceball\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\u3002http:\/\/code.google [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-49"],"_links":{"self":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":0,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}