天天看點

leveldb源碼分析 之 入門使用

LevelDB是google開源的一個key-value存儲引擎庫,類似于開源的Lucene索引庫一樣。其他的軟體開發者可以利用該庫做二次開發,來滿足定制需求。LevelDB采用日志式的寫方式來提高寫性能,但是犧牲了部分讀性能。為了彌補犧牲了的讀性能,一些人提議使用SSD作為存儲媒體。

對于本地化的Key-value存儲引擎來說,簡單的使用一般都分成三個基本的步驟:(1)打開一個資料庫執行個體;(2)對這個資料庫執行個體進行插入,修改和查詢操作;(3)最後在使用完成之後,關閉該資料庫。下面将詳細讨論該三個步驟:

一、打開一個資料庫執行個體

一個leveldb資料庫有一個對應一個檔案系統目錄的名字。該資料庫的所有内容都存儲在這個目錄下。下面的代碼描述了怎樣打開一個資料庫或者建立一個新的資料庫。

Cpp代碼

leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
  1. #include <assert.h>  
  2. #include "leveldb/db.h"  
  3. leveldb::DB* db;   
  4. leveldb::Options options;   
  5. options.create_if_missing = true;   
  6. leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);   
  7. assert(status.ok());   
  8. 如果打開已存在資料庫的時候,需要抛出錯誤。将以下代碼插在leveldb::DB::Open方法前面:   
  9. options.error_if_exists = true;  
#include <assert.h>
#include "leveldb/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
assert(status.ok());

如果打開已存在資料庫的時候,需要抛出錯誤。将以下代碼插在leveldb::DB::Open方法前面:
options.error_if_exists = true;


           

二、對資料庫的簡單讀、寫操作

LevelDB提供了Put,Delete和Get三個方法對資料庫進行修改和查詢。例如,下面的代碼片段描述了怎樣将key1對應的value值,移到key2對應的值。

C代碼

leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
  1. std::string value;   
  2. leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);   
  3. if(s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);   
  4. if(s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);  
std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
if(s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);
if(s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);      

三、關閉資料庫

在對資料庫進行了一系列的操作之後,需要對資料庫進行關閉。該操作比較簡單:

C代碼

leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
  1. ... open the db as described above...   
  2. ... do something with db ...   
  3. delete db;  
... open the db as described above...
... do something with db ...
delete db;      

上面對levelDB的簡單使用做了基本的介紹,接下來就是如何自己寫一個完成并且能運作的例子。

1、下載下傳源碼 git clone https://code.google.com/p/leveldb/

2、編譯源碼 cd leveldb && make all

3、編寫test.cpp

C代碼

leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
  1. #include <assert.h>  
  2. #include <string.h>  
  3. #include <leveldb/db.h>  
  4. #include <iostream>  
  5. int main(){   
  6.     leveldb::DB* db;   
  7.     leveldb::Options options;   
  8.     options.create_if_missing = true;   
  9.     leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);   
  10.     assert(status.ok());   
  11.     //write key1,value1  
  12.     std::string key="key";   
  13.     std::string value = "value";   
  14.     status = db->Put(leveldb::WriteOptions(), key,value);   
  15.     assert(status.ok());   
  16.     status = db->Get(leveldb::ReadOptions(), key, &value);   
  17.     assert(status.ok());   
  18.     std::cout<<value<<std::endl;   
  19.     std::string key2 = "key2";   
  20.     //move the value under key to key2  
  21.     status = db->Put(leveldb::WriteOptions(),key2,value);   
  22.     assert(status.ok());   
  23.     status = db->Delete(leveldb::WriteOptions(), key);   
  24.     assert(status.ok());   
  25.     status = db->Get(leveldb::ReadOptions(),key2, &value);   
  26.     assert(status.ok());   
  27.     std::cout<<key2<<"==="<<value<<std::endl;   
  28.     status = db->Get(leveldb::ReadOptions(),key, &value);   
  29.     if(!status.ok()) std::cerr<<key<<"  "<<status.ToString()<<std::endl;   
  30.     else std::cout<<key<<"==="<<value<<std::endl;   
  31.     delete db;   
  32.     return 0;   
  33. }  
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>

int main(){
	leveldb::DB* db;
	leveldb::Options options;
	options.create_if_missing = true;
	leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
	assert(status.ok());

	//write key1,value1
	std::string key="key";
	std::string value = "value";

	status = db->Put(leveldb::WriteOptions(), key,value);
	assert(status.ok());

	status = db->Get(leveldb::ReadOptions(), key, &value);
	assert(status.ok());
	std::cout<<value<<std::endl;
	std::string key2 = "key2";
    
	//move the value under key to key2
	
	status = db->Put(leveldb::WriteOptions(),key2,value);
	assert(status.ok());
	status = db->Delete(leveldb::WriteOptions(), key);

	assert(status.ok());
	
	status = db->Get(leveldb::ReadOptions(),key2, &value);
	
	assert(status.ok());
	std::cout<<key2<<"==="<<value<<std::endl;
	
	status = db->Get(leveldb::ReadOptions(),key, &value);
	
	if(!status.ok()) std::cerr<<key<<"  "<<status.ToString()<<std::endl;
	else std::cout<<key<<"==="<<value<<std::endl;
	
	delete db;
	return 0;
}      

4、編譯連結 g++ -o test test.cpp ../leveldb/libleveldb.a -lpthread -I../leveldb/include

     注意libleveldb.a 和leveldb include的路徑。

 編譯指令:

g++ -o leveldb leveldb.cpp -L /root/research/leveldb-1.7.0 -lleveldb -I /root/research/leveldb-1.7.0/include

運作時會出現 :./leveldb: error while loading shared libraries: libleveldb.so.1: cannot open shared object file: No such file or directory

這個問題。

解決方法:

把自己需要的so會存放在/usr/local/lib這個目錄底下,然後。

是以,在/etc/ld.so.conf.d的檔案夾中 建立一個檔案 加入/usr/local/lib這一行,儲存之後,再運作:/sbin/ldconfig –v更新一下配置即可。

ldconfig 指令的用途,主要是在預設搜尋目錄(/lib和/usr/lib)以及動态庫配置檔案/etc/ld.so.conf内所列的目錄下,搜尋出可共享的動态 連結庫(格式如前介紹,lib*.so*),進而建立出動态裝入程式(ld.so)所需的連接配接和緩存檔案.緩存檔案預設為 /etc/ld.so.cache,此檔案儲存已排好序的動态連結庫名字清單.

ldconfig通常在系統啟動時運作,而當使用者安裝了一個新的動态連結庫時,就需要手工運作這個指令.

ldconfig指令行用法如下:

ldconfig [-v|--verbose] [-n] [-N] [-X] [-f CONF] [-C CACHE] [-r ROOT] [-l] [-p|--print-cache] [-c FORMAT] [--format=FORMAT] [-V] [- |--help|--usage] path...

ldconfig可用的選項說明如下:

(1) -v或--verbose : 用此選項時,ldconfig将顯示正在掃描的目錄及搜尋到的動态連結庫,還有它所建立的連接配接的名字.

5、運作結果./test:

C代碼

leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
leveldb源碼分析 之 入門使用
  1. value   
  2. key2===value   
  3. key  NotFound: