1. 程式人生 > >leveldb研究系列一 .leveldb的簡單介紹和簡單使用

leveldb研究系列一 .leveldb的簡單介紹和簡單使用

       前些日子閒來無聊,花了一段時間研究了一下leveldb,在網上下了原始碼結合網上的技術文件和部落格,做了細緻的學習,這幾天打算想整理成系列部落格,以便大家參考和指正,這樣是我本人第一次寫技術部落格,不足之處還望指正。

        本系列博文,將對 leveldb做全域性介紹,從安裝使用,適用場景, 關鍵演算法, 程式碼實現,關鍵資料結構,等等做一個全面深入的探討和學習。 我一直相信學習是個迭代的過程,第一次看不懂不要緊,等有一段時間的積累,回頭再看一次,如夢初醒。

       概要介紹:leveldb是一個google實現的非常高效的kv資料庫, 具有很高的隨機寫順序讀和寫

效能(據說可以達到billion級別的資料量了),但是隨機讀的效能很一般,也就是說,leveldb很適合應用在查詢較少,而寫很多的場景。leveldb應用了lsm策略,lsm_tree對索引變更進行延遲及批量處理,並通過一種類似於歸併排序的方式高效地將更新遷移到磁碟。降低索引插入開銷關鍵策略lsm演算法,我會在下一篇詳細講解。

      本節主要講leveldb的簡單安轉使用,http://code.google.com/p/leveldb/downloads/list 這裡可以下載最新的原始碼壓縮包,今天我下了1.15的最新安轉包(Dec,10 ,2013更新)

    下載以後解壓到我的/home/pengshan/下  重新命名 mv  /home/pengshan/leveldbXXX   /home/pengshan/leveldb

      make 即可檔案包即可  make  /home/pengshan/leveldb/

     leveldb/下便有了libleveldb.a 執行庫 

    介面api在leveldb/include/db.h中  主要介面如下

 virtual Status Put(const WriteOptions& options,
                     const Slice& key,
                     const Slice& value) = 0;

  // Remove the database entry (if any) for "key".  Returns OK on
  // success, and a non-OK status on error.  It is not an error if "key"
  // did not exist in the database.
  // Note: consider setting options.sync = true.
  virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;

  // Apply the specified updates to the database.
  // Returns OK on success, non-OK on failure.
  // Note: consider setting options.sync = true.
  virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;

  // If the database contains an entry for "key" store the
  // corresponding value in *value and return OK.
  //
  // If there is no entry for "key" leave *value unchanged and return
  // a status for which Status::IsNotFound() returns true.
  //
  // May return some other Status on an error.
  virtual Status Get(const ReadOptions& options,
                     const Slice& key, std::string* value) = 0;

  // Return a heap-allocated iterator over the contents of the database.
  // The result of NewIterator() is initially invalid (caller must
  // call one of the Seek methods on the iterator before using it).
  //
  // Caller should delete the iterator when it is no longer needed.
  // The returned iterator should be deleted before this db is deleted.

我在本機做了簡單程式設計  注意標頭檔案的包含

//============================================================================
// Name        : leveldb_test.cpp
// Author      : pengshanzhou
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "/usr/local/include/leveldb/db.h"
using namespace std;

int main(int argc,char * argv[])

{

leveldb::DB* db;

leveldb::Options options;

options.create_if_missing = true;// If true, the database will be created if it is missing.
                                                         // Default: false

string dbpath = "levledb_path";

leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);

if(status.ok()==false)
{
	cout<<"leveldb open falied "<<endl;
	return 0;
}
string key = "pengshanzhou";

string value = "good to see you ";

cout<<"Open db OK"<<std::endl;
string result;

status = db->Put(leveldb::WriteOptions(), key, value);/*key和value作為一對key-value對插入*/
if(status.ok()==false)
{
	cout<<"leveldb write falied "<<endl;
	return 0;
}
cout<<"intser key(pengshanzhou) value(good to see you)"<<endl;
status = db->Get(leveldb::ReadOptions(), key, &result);/*根據key返回對應的value值*/
if(status.ok()==false)
{
	cout<<"leveldb read falied "<<endl;
	return 0;
}
cout<<"find the value of key(pengshan)"<<result<<endl;

delete db;/*刪除資料庫*/

return 0;

}
編譯程式碼因為leveldb用到了多執行緒所以要 -lpthread  我的機子上完整命令 g++ -o leveldb_test leveldb_test.cpp  /home/pengshan/leveldb/libleveldb.a -lpthread

執行[email protected]:/usr/local/source# ./level_test 

Open db OK
intser key(pengshanzhou) value(good to see you)
find the value of key(pengshan)good to see you

注意許可權問題   附圖片一張


相關推薦

leveldb研究系列 .leveldb簡單介紹簡單使用

       前些日子閒來無聊,花了一段時間研究了一下leveldb,在網上下了原始碼結合網上的技術文件和部落格,做了細緻的學習,這幾天打算想整理成系列部落格,以便大家參考和指正,這樣是我本人第一次寫技術部落格,不足之處還望指正。         本系列博文,將對 leve

Kafka系列之架構介紹安裝

環境 指南 ont fcm port 架構 star cfg 自己的 Kafka架構介紹和安裝 寫在前面 還是那句話,當你學習一個新的東西之前,你總得知道這個東西是什麽?這個東西可以用來做什麽?然後你才會去學習它,使用它。簡單來說,kafka既是一個消息隊列,如今,它也演變

EF Code first DDD (領域驅動設計研究)系列

發的 tex bsp cti 設計 ron 映射 developer devel 在上個公司工作時,開發公司產品的過程中,接觸到了EF Code first. 當時,整個產品的架構都是Lead developer設計建立的,自己也不是特別理解,就趕鴨子上架跟著一起開發了。

Redis學習筆記()---Redis的五種資料型別的簡單介紹使用

1.準備工作:     1.1在Linux下安裝Redis    https://www.cnblogs.com/dddyyy/p/9763098.html    1.2啟動Redis     先把root/redis的redis.conf放到 /usr/local/redis/

DJANGO入門系列之(模板層的簡單介紹視圖層的掃尾)

dir 解析 http eth endif () 查詢 文件上傳 lte 昨日回顧:1 虛擬環境 -1 pycharm裏創建 -2 用命令串講2 視圖層: 1 Request對象---GET,POST,method,body,FILES,META,path(只是

CSS簡單學習()-CSS介紹CSS引入方式

CSS作用和發展 CSS(Cascading Style Sheet),稱之為層疊樣式表 一種專門描述結構文件的變現方式文件,主要用於網頁風格設計,包括字型大小,顏色,以及元素的精確定位等。 在傳統的網頁設計裡,使用CSS能讓單調的HTML網頁更富表現力 作用:

Sqoop資料遷移工具之簡單介紹安裝(

1、概述  sqoop 是 apache 旗下一款“Hadoop 和關係資料庫伺服器之間傳送資料”的工具。   匯入資料:MySQL,Oracle 匯入資料到 Hadoop 的 HDFS、HIVE、HBASE 等資料儲存系統 匯出資料:從 Hadoop 的檔案系統中匯出資料到

Netty專欄 ()——— Netty初步介紹簡單Demo

Netty初步介紹和簡單Demo @author 魯偉林 記錄《Netty 實戰》中各章節學習過程,寫下一些自己的思考和總結,幫助使用Netty框架的開發技術人員們,能夠有所得,避免踩坑。 本部落格目錄結構將嚴格按照書本《Netty 實戰》,省略與Netty無關的內容,可能

Furure的簡單介紹使用

bpa cfb idm fat pwm actions ddl effect knn 引子: 上圖是兩個系統交互的情況,現在我想將對外系統的調用做成異步實現,那麽就需要考慮兩個問題: 主線程可以得到異步線程的結果,在得到結果之後再進行operation-4 ?主線程如何得

一致性Hash簡單介紹使用

mes red second count main ace 背景 -- file 背景: 一致性Hash用於分布式緩存系統,將Key值映射到詳細機器Ip上,而且添加和刪除1臺機器的數據移動量較小,對現網影響較小 實現: 1 Hash環:將節點

MongoDB 系列() C# 類似EF語法簡單封裝

reference cor family sha 測試 性別 權限 lec 情況 之前寫過一篇關於MongoDB的封裝 發現太過繁瑣 於是打算從新寫一篇簡易版 1:關於MongoDB的安裝請自行百度,進行權限認證的時候有一個小坑,3.0之後授權認證方式默認的SCRAM-SH

初識LVS()——LVS介紹LVS的NAT工作模式

lvs;natLVS介紹集群按照類型劃分可以分為:LB負載均衡型;HA高可用型;HP高性能型。LVS(linux virtual machine)是一款國人編寫用於LB負載均衡型被收錄進Linux內核的工具。LVS由ipvs和ipvsadmin組成,ipvs工作在netfilter框架上,而ipvsadm則是

幾個常用規則引擎的簡單介紹演示

規則引擎 drools ilog odm Ilog JRules 是最有名的商用BRMS;Drools 是最活躍的開源規則引擎;Jess 是Clips的java實現,就如JRuby之於Ruby,是AI系的代表;Visual Rules(旗正規則引擎)國內商業規則引擎品牌。今天對比了一下這四個頗

TypeScript的簡單介紹win環境安裝

type -- 編程 語言 targe body 本質 基於 版本 TypeScript是一種由微軟開發的自由和開源的編程語言。它是JavaScript的一個超集,而且本質上向這個語言添加了可選的靜態類型和基於類的面向對象編程。特點是一門強類型語言. 安裝: 1 首先我

springmvc系列 之配置介紹(包含官網doc)

不同 oca handle bsp cut targe sevlet 繼承 流程 1.springmvc 官網參考地址:   https://docs.spring.io/spring/docs/current/spring-framework-reference/web.

markdown的簡單介紹語法

markdownmarkdown的使用場景 markdown可以簡單高效的讓我們專註寫作。用一些特定的語法標記可以做到快速排版的效果。 我所使用的工具 markdown有很多編輯器,像word一樣,有很多種選擇。這邊我用的是<font color=red>typora</font>

C# try catch finally簡單介紹應用

val hat CA one ... 出錯 結構 介紹 有關 今天看代碼書的時候,有用到try--catch--finally,然後就查了下具體的註意事項和應用。 簡單來說就是:   try {     //有可能出錯誤的代碼或者代碼片段   }   catch{

Web2.0簡單介紹軟件開發結構淺談

Web2.0簡單介紹和軟件開發結構淺談 1、Web2.0指的是利用Web的平臺,由用戶主導而生成內容的互聯網產品模式,為了區別由網站雇員主導生成內容的傳統網站而定義為Web2.0基於Web2.0這些特點所產生的具有代表性的服務如下:博客、內容源、WiKi、參與評論與評分的Digg機制、美味書簽、社會化網絡、

Polly的多種彈性策略介紹簡單使用

故障 廣泛 forever 影響 結合 ssim 封裝 n) csharp 什麽是Polly? Polly是一個.NET彈性和瞬態故障處理庫.允許我們以非常順暢和線程安全的方式來執行諸如行重試,斷路,超時,故障恢復等策略。 Polly項目地址:https://github.

8.7 每日課後作業系列之常用模板(介紹運用)

ali baidu not in {} int time 緩存 date 方式 # 今日作業:# p1.簡述# 什麽是模塊#一系列功能的集合體# 模塊有哪些來源# p1.內置# 2.第三方# 3.自定義# 模塊的格式要求有哪些# p1 .py文件# 2 已被編譯為共享庫或D