1. leveldb简介
leveldb是一个key/value型的存储引擎,由google开发,并宣布在BSD许可下开放源代码。
2. leveldb下载和安装
leveldb托管在google code上,可以使用git下载源代码:
- git clone https://code.google.com/p/leveldb/
- cd leveldb
- make all
- cp -r ./include/leveldb /usr/include/
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;
- }
- g++ -o test test.cpp libleveldb.a -lpthread
- [root@mdss33 test]# ./test
- value