1. 程式人生 > >C++操作leveldb示例筆記

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));

		}	
	}
}