leveldb研究系列一 .leveldb的简单介绍和简单使用

本文介绍了LevelDB这一高效键值数据库的基本概念与应用场景,并通过示例演示如何在本地环境中安装和使用LevelDB。
 

leveldb研究系列一 .leveldb的简单介绍和简单使用

标签: leveldbkv数据库linuxC++
  2128人阅读  评论(1)  收藏  举报
  分类:

       前些日子闲来无聊,花了一段时间研究了一下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中  主要接口如下

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. virtual Status Put(const WriteOptions& options,  
  2.                     const Slice& key,  
  3.                     const Slice& value) = 0;  
  4.   
  5.  // Remove the database entry (if any) for "key".  Returns OK on  
  6.  // success, and a non-OK status on error.  It is not an error if "key"  
  7.  // did not exist in the database.  
  8.  // Note: consider setting options.sync = true.  
  9.  virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;  
  10.   
  11.  // Apply the specified updates to the database.  
  12.  // Returns OK on success, non-OK on failure.  
  13.  // Note: consider setting options.sync = true.  
  14.  virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;  
  15.   
  16.  // If the database contains an entry for "key" store the  
  17.  // corresponding value in *value and return OK.  
  18.  //  
  19.  // If there is no entry for "key" leave *value unchanged and return  
  20.  // a status for which Status::IsNotFound() returns true.  
  21.  //  
  22.  // May return some other Status on an error.  
  23.  virtual Status Get(const ReadOptions& options,  
  24.                     const Slice& key, std::string* value) = 0;  
  25.   
  26.  // Return a heap-allocated iterator over the contents of the database.  
  27.  // The result of NewIterator() is initially invalid (caller must  
  28.  // call one of the Seek methods on the iterator before using it).  
  29.  //  
  30.  // Caller should delete the iterator when it is no longer needed.  
  31.  // The returned iterator should be deleted before this db is deleted.  

我在本机做了简单编程  注意头文件的包含

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //============================================================================  
  2. // Name        : leveldb_test.cpp  
  3. // Author      : pengshanzhou  
  4. // Version     :  
  5. // Copyright   : Your copyright notice  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8.   
  9. #include <iostream>  
  10. #include "/usr/local/include/leveldb/db.h"  
  11. using namespace std;  
  12.   
  13. int main(int argc,char * argv[])  
  14.   
  15. {  
  16.   
  17. leveldb::DB* db;  
  18.   
  19. leveldb::Options options;  
  20.   
  21. options.create_if_missing = true;// If true, the database will be created if it is missing.  
  22.                                                          // Default: false  
  23.   
  24. string dbpath = "levledb_path";  
  25.   
  26. leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);  
  27.   
  28. if(status.ok()==false)  
  29. {  
  30.     cout<<"leveldb open falied "<<endl;  
  31.     return 0;  
  32. }  
  33. string key = "pengshanzhou";  
  34.   
  35. string value = "good to see you ";  
  36.   
  37. cout<<"Open db OK"<<std::endl;  
  38. string result;  
  39.   
  40. status = db->Put(leveldb::WriteOptions(), key, value);/*key和value作为一对key-value对插入*/  
  41. if(status.ok()==false)  
  42. {  
  43.     cout<<"leveldb write falied "<<endl;  
  44.     return 0;  
  45. }  
  46. cout<<"intser key(pengshanzhou) value(good to see you)"<<endl;  
  47. status = db->Get(leveldb::ReadOptions(), key, &result);/*根据key返回对应的value值*/  
  48. if(status.ok()==false)  
  49. {  
  50.     cout<<"leveldb read falied "<<endl;  
  51.     return 0;  
  52. }  
  53. cout<<"find the value of key(pengshan)"<<result<<endl;  
  54.   
  55. delete db;/*删除数据库*/  
  56.   
  57. return 0;  
  58.   
  59. }  
编译代码 因为leveldb用到了 多线程所以要 -lpthread  我的机子上完整命令  g++ -o leveldb_test leveldb_test.cpp  /home/pengshan/leveldb/libleveldb.a -lpthread

执行root@ubuntu:/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



注意权限问题   附图片一张


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值