首先天然要安裝Tokyo Cabinetspa
Sh代碼 .net
tar zxvf tokyocabinet-1.4.28.tar.gz get
cd tokyocabinet-1.4.28/ it
./configure io
make 編譯
make install thread
cd ../ test
如下是對Tokyo Cabinet 的操做,具體解釋能夠看代碼—代碼是最好的解釋.email
文件名稱是:tcbdb.cstream
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/