leveldb源代码分析1

来源:互联网 发布:网络怎样阅卷 编辑:程序博客网 时间:2024/05/24 06:27

1. leveldb简介

leveldb是一个key/value型的存储引擎,由google开发,并宣布在BSD许可下开放源代码。

2. leveldb下载和安装

leveldb托管在google code上,可以使用git下载源代码:

git clone https://code.google.com/p/leveldb/
下载完成之后,开始编译leveldb

cd leveldbmake all
此时生成libleveldb.a库文件。拷贝leveldb的头文件到/usr/include下

cp -r ./include/leveldb /usr/include/
即完成leveldb的安装工作

3. leveldb客户端程序示例

测试程序如下:

#include <assert.h>#include <string>#include <leveldb/db.h>#include <iostream>int main(){    // Open a database    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>    const int WRITE_TIMES = 10000;    int i = 0;    std::string key = "key";    std::string value = "value";    status = db->Put(leveldb::WriteOptions(), key, value);    assert(status.ok());    // Read value1 by key1    status = db->Get(leveldb::ReadOptions(), key, &value);    assert(status.ok());    std::cout << value << std::endl;    // Delete databse    delete db;    return 0;}
将生成libleveldb.a拷贝到源文件相同的目录下,编译该程序:

 g++ -o test test.cpp libleveldb.a -lpthread
此时即生成test可执行文件。执行test结果如下:

[root@mdss33 test]# ./test value

原创粉丝点击