首先自然要安装Tokyo Cabinet
Sh代码
tar zxvf tokyocabinet-1.4.28.tar.gz
cd tokyocabinet-1.4.28/
./configure
make
make install
cd ../
以下是对Tokyo Cabinet 的操作,具体解释可以看代码—代码是最好的解释.
文件名称是:tcbdb.c
C代码
/**
* Operate the Tokyo Cabinet
* @author zhoubaochuan
* @email :rtxbc@163.com
* @date:2011-07-13
* @gcc : gcc tcbdb.c -I/usr/local/include/ -L/usr/local/lib/ -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc -O2 -g --static
*/
#include <stdio.h>
/* Import the Tokyo Cabinet C's library*/
#include <tcbdb.h>
int main(int argc, char *argv[]){
char *datapath = "/opt/data/test/C/queue/tcbdb.db";
/* Allocate a region on memory */
char *dataname = (char *)tccalloc(1,1024);
TCBDB *db = tcbdbnew(); /* Create a B+ tree database object */
tcbdbsetmutex(db);
tcbdbtune(db,128,256, 32749,8,10,100);
tcbdbsetcache(db,1024,512);
tcbdbsetxmsiz(db,1024100);
if(!tcbdbopen(db,datapath,BDBOWRITER|BDBOREADER)){ /* Open a database file and connect a B+ tree database object. */
fprintf(stderr,"It is failure to open a database !\n");
exit(1);
}
//free((void *)datapath);
//fprintf(stderr,"==============It is successful to open a database : ===============\n");
char *key = "t_key";
char *value = "zhoubaochuan";
fprintf(stderr, "============== Store value. ================\n key:%s; \n value:%s \n ", key, value);
/* Store a new record into a B+ tree database object. */
tcbdbput2(db, key, value);
fprintf(stderr, "============== Retrieve a record. ================\n");
/* Retrieve a record in a B+ tree database object as a volatile buffer. */
char *retrieve = tcbdbget2(db,key);
fprintf(stderr, "Retriver's Value:%s \n", retrieve);
/* Synchronize updated contents of a B+ tree database object with the file and the device. */
tcbdbsync(db); /* 实时刷新到磁盘 */
/* Close a B+ tree database object. */
tcbdbclose(db);
return 0;
}
编译
Sh代码
gcc tcbdb.c -I/usr/local/include/ -L/usr/local/lib/ -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc -O2 -g --static
执行结果
Sh代码
[root@zhoubc queue]# ./a.out
============== Store value. ================
key:t_key;
value:zhoubaochuan
============== Retrieve a record. ================
Retriver's Value:zhoubaochuan
参考文献:
http://fallabs.com/tokyocabinet/