一.leveldb安裝與使用
1. 下載下傳leveldb-1.15.0.tar.gz
2. 解壓tar -zxvf leveldb-1.15.0.tar.gz
3. cd leveldb-1.15.0
4. Make生成libleveldb.a
5. Cp –r include/leveldb /usr/local/include
6. Cp libleveldb.a /usr/local/leveldbtest
7. 代碼
#include<iostream>
#include<string>
#include<assert.h>
#include"leveldb/db.h"
using namespace std;
int main(){
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
string dbpath = "/usr/local/leveldbtest/db2";
leveldb::Status status = leveldb::DB::Open(options,dbpath , &db);
string key1 = "k1",value1 ="v1",key2 = "k2",value2="v2",res1,res2;
string key3="k3",value3="v3",key4="k4",value4="v4";
if(status.ok()){
cout<<"open db ok,start to put data......"<<endl;
db->Put(leveldb::WriteOptions(),key1,value1);
db->Put(leveldb::WriteOptions(),key2,value2);
db->Put(leveldb::WriteOptions(),key3,value3);
db->Put(leveldb::WriteOptions(),key4,value4);
}
if(status.ok()){
db->Get(leveldb::ReadOptions(),key2,&res1);
cout<<"the reslut of find1 is: "<<key2<<"---->"<<res1<<endl;
}
if(status.ok()){
db->Delete(leveldb::WriteOptions(),key2);
db->Get(leveldb::ReadOptions(),key2,&res2);
}
if(!status.ok())
cout<<"the reslut of find is: "<<key2<<"---->"<<status.ToString()<<endl;
else
cout<<"the reslut of find2 is: "<<key2<<"---->"<<res2<<endl;
delete db;
return 0;
}
8. g++ -otest test.cc libleveldb.a -lpthread
二.leveldb簡介
1. LevelDb是能夠處理十億級别規模Key-Value型資料持久性存儲的C++程式庫。它将大部分資料存儲到磁盤上
2. LevleDb在存儲資料時,是根據記錄的key值有序存儲的,可以自定義key大小比較函數,LevleDb會按照使用者定義的比較函數依序存儲這些記錄。
3. 基本操作包括寫、讀、删除記錄。也支援針對多條操作的原子批量操作。
4. LevelDb支援資料快照(snapshot)功能,使得讀取操作不受寫操作影響,可以在讀操作過程中始終看到一緻的資料。
5. LevelDb還支援資料壓縮等操作,這對于減小存儲空間以及增快IO效率都有直接的幫助。
6. 随機寫性能達到40萬條記錄每秒,而随機讀性能達到6萬條記錄每秒。總體來說,LevelDb的寫操作要大大快于讀操作,而順序讀寫操作則大大快于随機讀寫操作。
本文參考 朗格科技
http://www.samecity.com/blog/Index.asp?SortID=12
http://leveldb.googlecode.com/svn/trunk/doc/index.html
http://duanple.blog.163.com/blog/static/70971767201171705113636/