Like World

Like's blog

LevelDB安装及试玩

LevelDB的核心代码很短,1W行不到的样子。注释详细,可以用来学习C++在实际工程项目上的用法。

安装LevelDB

安装很简单,环境为Ubuntu 12.04

1
2
3
4
5
6
git clone https://code.google.com/p/leveldb/
cd leveldb
make
sudo cp -r include/leveldb /usr/local/include/
sudo cp -r libleveldb.so* /usr/local/lib/
sudo ldconfig

简单试用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "leveldb/db.h"
#include <iostream>

using namespace std;

int main()
{
  leveldb::DB *db;
  leveldb::Options option;

  option.create_if_missing = true;
  leveldb::DB::Open(option, "/tmp/leveldb_t", &db);

  string key = "Name";
  string value = "Like";
  db->Put(leveldb::WriteOptions(), key, value);

  key = "Major";
  value = "Computer Science and Technology";
  db->Put(leveldb::WriteOptions(), key, value);


  string ret_s;
  db->Get(leveldb::ReadOptions(), "Name", &ret_s);
  cout << "key = Name" << endl
       << "value = " << ret_s << endl;

  db->Get(leveldb::ReadOptions(), "Major", &ret_s);
  cout << "key = Major" << endl
       << "value = " << ret_s << endl;
  
  delete db;
  return 0;
}

编译运行也很简单:

1
2
g++ test.cpp -lleveldb
./a.out

References:

[1] Google LevelDB 試玩心得