六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 25|回复: 0

第二章 C语言实例 — Tokyo Cabinet 的数据库操作

[复制链接]

升级  9%

63

主题

63

主题

63

主题

举人

Rank: 3Rank: 3

积分
227
 楼主| 发表于 2013-1-26 12:37:08 | 显示全部楼层 |阅读模式
最近在研究C语言对一些库的编程
 
首先自然要安装Tokyo Cabinet
 
tar zxvf tokyocabinet-1.4.28.tar.gzcd tokyocabinet-1.4.28/./configuremakemake installcd ../  
 
以下是对Tokyo Cabinet 的操作,具体解释可以看代码—代码是最好的解释.
文件名称是:tcbdb.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;} 
 
编译
 
gcc tcbdb.c  -I/usr/local/include/ -L/usr/local/lib/ -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc -O2 -g --static 
 
执行结果
 
[root@zhoubc queue]# ./a.out ============== Store value. ================ key:t_key;  value:zhoubaochuan  ============== Retrieve a record. ================Retriver's Value:zhoubaochuan   
参考文献:
http://fallabs.com/tokyocabinet/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表