Posted on 2012-03-08 11:44
小明 閱讀(4832)
評論(1) 編輯 收藏 所屬分類:
分布式計(jì)算
leveldb是 google對bigtable的一個(gè)簡化版的開源實(shí)現(xiàn),很有研究價(jià)值。
我的編譯環(huán)境:ubuntu 32&g++ 4.6
1.安裝git并下載代碼
sudo apt-get install git-core
git clone https://code.google.com/p/leveldb/
2. 編譯leveldb
cd leveldb
./build_detect_platform
make
為了能夠調(diào)試,修改Makefile為debug mode(B模式)
OPT ?= -g2
編譯后會(huì)生成庫文件:libleveldb.a
3. 編寫測試程序
ldbtest.cpp
#include <iostream>
#include "leveldb/db.h"
using namespace std;
using namespace leveldb;
int main() {
DB *db ;
Options op;
op.create_if_missing = true;
Status s = DB::Open(op,"/tmp/testdb",&db);
if(s.ok()){
cout << "create successfully" << endl;
s = db->Put(WriteOptions(),"abcd","1234");
if(s.ok()){
cout << "put successfully" << endl;
string value;
s = db->Get(ReadOptions(),"abcd",&value);
if(s.ok()){
cout << "get successfully,value:" << value << endl;
}
else{
cout << "get failed" << endl;
}
}
else{
cout << "put failed" << endl;
}
}
else{
cout << "create failed" << endl;
}
delete db;
return 0;
}
注意link的時(shí)候需要加上-lpthread.
運(yùn)行后得到結(jié)果:(Eclipse中運(yùn)行)