C++操作leveldb示例笔记

leveldb是一个google实现的超高性能的k/v数据库,官方介绍说读写性能都很强大。下载下来make生成动态链接库libleveldb.so以及静态链接库libleveldb.a文件,下面就写一个测试文件使用leveldb吧。

#include<iostream>
#include<string>
#include"leveldb/db.h"

using namespace std;
using namespace leveldb;

int main()
{
        DB *db;
        Options options;
        options.create_if_missing = true;
        //options.error_if_exists = true;
        Status s = DB::Open(options,"/data/leveldb/lvd.db",&db);
        if(!s.ok()){
                cerr << s.ToString() << endl;exit(-1);
        }
        string key = "name",val = "ciaos";
        s = db->Put(WriteOptions(),key,val);
        if(!s.ok()){
                cerr << s.ToString() << endl;exit(-1);
        }
        s = db->Get(ReadOptions(),key,&val);
        if(s.ok()){
                cout << key << "=" << val << endl;
                s = db->Put(leveldb::WriteOptions(),"key2",val);
                if(s.ok()){
                        s = db->Delete(leveldb::WriteOptions(),key);
                        if(!s.ok()){
                                cerr << s.ToString() << endl;exit(-1);
                        }
                }
        }
}
上面实现一个简单的数据库插入查找以及删除操作,编译方法如下,先导出引用库的环境变量,然后加载依赖的库以及头文件

export LD_LIBRARY_PATH=/root/penjin/leveldb/

g++ test.cpp -lleveldb -I/root/penjin/leveldb/include -L/root/penjin/leveldb/

与redis,memcached等典型的k/v数据库不一样,我感觉它更像sqlite一样的嵌入式数据库,或者仅仅是一个存储引擎。

下面写个程序测试一下性能,读写性能是很好的

vm6245:~/penjin/leveldb/include # ./a.out
write db cost time 0 101.000000(ms) 0.000000(ops)
write db cost time 10000 24646.000000(ms) 405745.354216(ops)
write db cost time 20000 50850.000000(ms) 393313.667650(ops)
write db cost time 30000 77596.000000(ms) 386617.866900(ops)
write db cost time 40000 100854.000000(ms) 396612.925615(ops)
write db cost time 50000 144434.000000(ms) 346178.877550(ops)
write db cost time 60000 294458.000000(ms) 203764.204063(ops)
write db cost time 70000 317026.000000(ms) 220802.079325(ops)
write db cost time 80000 340610.000000(ms) 234872.728340(ops)
write db cost time 90000 520569.000000(ms) 172887.743988(ops)
read db cost time 0 36.000000(ms) 0.000000(ops)
read db cost time 10000 53165.000000(ms) 188093.670648(ops)
read db cost time 20000 88073.000000(ms) 227084.350482(ops)
read db cost time 30000 123043.000000(ms) 243817.202116(ops)
read db cost time 40000 157281.000000(ms) 254321.882491(ops)
read db cost time 50000 191701.000000(ms) 260822.843908(ops)
read db cost time 60000 225490.000000(ms) 266087.187902(ops)
read db cost time 70000 242869.000000(ms) 288221.222140(ops)
read db cost time 80000 261222.000000(ms) 306252.918973(ops)
read db cost time 90000 278175.000000(ms) 323537.341602(ops)
测试代码如下:
#include<iostream>
#include<string>
#include<sys/time.h>
#include"leveldb/db.h"

using namespace std;
using namespace leveldb;

#define TEST_DATA_NUM 100000
#define TEST_DATA_LEN 100

#define MAX_LEN 10000

struct timeval start,current;

int main()
{
	DB *db;
	Options options;
	options.create_if_missing = true;
	//options.error_if_exists = true;
	Status s = DB::Open(options,"/data/leveldb/lvd.db",&db);
	if(!s.ok()){
		cerr << s.ToString() << endl;exit(-1);
	}

	int i;
	char k[100],v[MAX_LEN+1];
	string key,val;
	for(i = 0;i < MAX_LEN && i < TEST_DATA_LEN ;i ++)
	{
		v[i] = 'x';
	}
	v[i] = '\0';
	val = v;

	//write db test
	gettimeofday(&start,NULL);
	for(i = 0;i < TEST_DATA_NUM; i ++){
		snprintf(k,sizeof(k),"key%d",i);
		key = k;

		s = db->Put(WriteOptions(),key,val);
		if(!s.ok()){
			cerr << s.ToString() << endl;exit(-1);
		}
		if(i % 10000 == 0){
			gettimeofday(¤t,NULL);
			double microsecs = 1000000*(current.tv_sec-start.tv_sec)+(current.tv_usec-start.tv_usec);
			printf("write db cost time %d %lf(ms) %lf(ops)\n",i,microsecs,i/(microsecs/1000000));

		}	
	}

	//read db test
	gettimeofday(&start,NULL);
	for(i = 0;i < TEST_DATA_NUM; i ++){
		snprintf(k,sizeof(k),"key%d",i);
		key = k;

		s = db->Get(ReadOptions(),key,&val);
		if(!s.ok()){
			cerr << s.ToString() << endl;exit(-1);
		}
		if(i % 10000 == 0){
			gettimeofday(¤t,NULL);
			double microsecs = 1000000*(current.tv_sec-start.tv_sec)+(current.tv_usec-start.tv_usec);
			printf("read db cost time %d %lf(ms) %lf(ops)\n",i,microsecs,i/(microsecs/1000000));

		}	
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
leveldb实现解析 淘宝-核心系统研发-存储 那岩 neveray@gmail.com 2011-12-13 目录 一、 代码目录结构 ....................................................................... 1 1. doc/ ............................................................................ 1 2. include/leveldb/ ................................................................. 1 3. db/ ............................................................................. 1 4. table/........................................................................... 1 5. port/ ........................................................................... 1 6. util/ ........................................................................... 1 7. helper/memenv/ ................................................................... 1 二、 基本概念 .......................................................................... 1 1. Slice (include/leveldb/slice.h) .................................................. 1 2. Option (include/leveldb/option.h) .............................................. 2 3. Env (include/leveldb/env.h util/env_posix.h) .................................... 3 4. varint (util/coding.h) ........................................................... 3 5. ValueType (db/dbformat.h) ...................................................... 3 6. SequnceNnumber (db/dbformat.h) ................................................. 4 7. user key......................................................................... 4 8. ParsedInternalKey (db/dbformat.h) .............................................. 4 9. InternalKey (db/dbformat.h) ...................................................... 4 10. LookupKey (db/dbformat.h) ........................................................ 4 11. Comparator (include/leveldb/comparator.h util/comparator.cc) .................... 4 12. InternalKeyComparator (db/dbformat.h) ............................................ 5 13. WriteBatch (db/write_batch.cc) ................................................. 5 14. Memtable (db/memtable.cc db/skiplist.h) .......................................... 5 15. Sstable (table/table.cc) ......................................................... 5 16. FileMetaData (db/version_edit.h) ............................................... 5 17. block (table/block.cc) ........................................................... 6 18. BlockHandle(table/format.h) ...................................................... 6 19. FileNumber(db/dbformat.h) ...................................................... 6 20. filename (db/filename.cc) ........................................................ 6 21. level-n (db/version_set.h) ....................................................... 7 22. Compact (db/db_impl.cc db/version_set.cc) ........................................ 7 23. Compaction(db/version_set.cc) .................................................... 7 24. Version (db/version_set.cc) ...................................................... 8 25. VersionSet (db/version_set.cc) ................................................. 9 26. VersionEdit(db/version_edit.cc) ................................................. 10 27. VersionSet::Builder (db/version_set.cc) ......................................... 11 28. Manifest(descriptor)(db/version_set.cc)...................................... 12 29. TableBuilder/BlockBuilder(table/table_builder.cc table/block_builder.cc) ......... 12 30. Iterator (include/leveldb/iterator.h) ........................................... 12 三、 存储结构的格式定义与操作 .......................................................... 12 1. memtable (db/skiplist.h db/memtable) ............................................ 12 2. block of sstable (table/block_builder.cc table/block.cc) ......................... 14 3. sstable (table/table_bulder.cc/table.cc) ........................................ 16 4. block of log (db/log_format.h db/log_writer.cc db/log_reader.cc) .............. 18 5. log (db/log_format.h db/log_writer.cc db/log_reader.cc) ........................ 18 6. cache(util/cache.cc) .......................................................... 19 7. Snapshot (include/leveldb/snapshot.h) ......................................... 19 8. Iterator (include/leveldb/iterator.h) ........................................... 19 四、 主要流程 ......................................................................... 24 1. open ........................................................................... 24 2. put ............................................................................ 25 3. get ............................................................................ 25 4. delete.......................................................................... 26 5. snapshot........................................................................ 26 6. NewIterator ..................................................................... 26 7. compact......................................................................... 26 五、 总结 ............................................................................. 30 1. 设计/实现中的优化 ............................................................... 30 2. 可以做的优化 .................................................................... 31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值