leveldb

http://zhoutall.com/archives/579
leveldb是一个Google开发的高性能的字符串类型的K-V存储C/C++类库,其详细介绍可参考主页https://code.google.com/p/leveldb/,下面介绍一下如何在我们的项目中使用leveldb。

首先纠正一个常见的理解错误,leveldb是一个C/C++类库,并不是一个数据库,所以leveldb并没有client-server的架构,也没有命令行交互接口,但是我们可以轻松的利用他的API,把leveldb集成到我们自己的项目里面。

关于leveldb全面的功能引用如下:

Features
Keys and values are arbitrary byte arrays.
Data is stored sorted by key.
Callers can provide a custom comparison function to override the sort order.
The basic operations are Put(key,value), Get(key), Delete(key).
Multiple changes can be made in one atomic batch.
Users can create a transient snapshot to get a consistent view of data.
Forward and backward iteration is supported over the data.
Data is automatically compressed using the Snappy compression library.
External activity (file system operations etc.) is relayed through a virtual interface so users can customize the operating system interactions.
Detailed documentation about how to use the library is included with the source code.
Limitations
This is not a SQL database. It does not have a relational data model, it does not support SQL queries, and it has no support for indexes.
Only a single process (possibly multi-threaded) can access a particular database at a time.
There is no client-server support builtin to the library. An application that needs such support will have to wrap their own server around the library.
其主要可以归纳为一下几点:

字符串K-V数据的存储
字符串K-V数据的查找
按照key的顺序对一段数据进行迭代遍历
当我们自己的项目需要一个SQL数据存储的话,一般我们会考虑sqlite,现在需要一个k-v存储的话,不妨试试leveldb。

如何在我们的项目中集成leveldb:

首先我们把leveldb最新的代码拉下来,由于google code的git仓库好像翻墙才能访问,所以需要先打开goagent,然后在终端设置https的代理。

export https_proxy=127.0.0.1:8087
git clone https://code.google.com/p/leveldb/

之后我们查看leveldb的git branch信息可以看到有包括android和windows的分支,当然主分支就是linux上面的。我们可以checkout到其中一个release。(对于源码获取,也可以直接下载各个release版本的代码压缩包)

然后进入包含makefile的主目录,执行make即进入编译状态。

很快编译结束会发现多了以下文件:libleveldb.so和libleveldb.a,.so是动态链接库,.a是静态链接库。

然后把leveldb的头文件拷贝到公共目录。

sudo cp ./include/leveldb /usr/local/include/

把libleveldb.a拷贝到你自己项目的目录下,假设你的代码是a.cpp,执行以下编译命令就万事大吉了。

g++ -o a a.cpp libleveldb.a -lpthread

如果你没有sudo权限,前面leveldb头文件无法拷贝到公共目录,编译命令可以类似这样:

export LEVELDB=~/leveldb
g++ -o a a.cpp $LEVELDB/libleveldb.a -lpthread -I $LEVELDB/include

a.cpp代码如下:

#include <iostream>
#include "leveldb/db.h"

using namespace std;

int main()
{
    leveldb::DB* db;
    leveldb::Options options;
    leveldb::Status s;

    options.create_if_missing = true;
    //options.error_if_exists = true;
    std::string dbpath = "testdb";
    s = leveldb::DB::Open(options, dbpath, &db);
    if (!s.ok()) {
        cerr << s.ToString() << endl;
        return -1;
    }

    std::string value;
    s = db->Put(leveldb::WriteOptions(), "k1", "v1");
    cout << s.ok() << endl;
    s = db->Get(leveldb::ReadOptions(), "k1", &value);
    cout << s.ok() << " " << value << std::endl;
    s = db->Delete(leveldb::WriteOptions(), "k1");
    cout << s.ok() << endl;
    value.clear();
    s = db->Get(leveldb::ReadOptions(), "k1", &value);
    cout << s.ok() << "" << value << std::endl;

    delete db;
    return 0;
}

相关:
关于leveldb的功能和API的详细文档
gitub主页
http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值