LevelDB在windows平台下的编译很麻烦,但是在linux中,安装和编译过程非常简单,主要步骤如下:
下载源码:
https://code.google.com/p/leveldb/
LevelDB编译
下载完成后,进入leveldb目录,使用make指令进行源码编译。
- cd leveldb
- make all
此时,leveldb目录内会生成libleveldb.a文件,然后将/include/leveldb目录内头文件全部复制到系统头文件目录/usr/include下
cp -r ./include/leveldb /usr/include/
安装完毕。
LevelDB测试程序
#include <assert.h> #include <string> #include <leveldb/db.h> #include <iostream> #include "leveldb/write_batch.h" 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()); status = db->Put(leveldb::WriteOptions(), "testkey", "testValue"); // Read value1 by key1 status = db->Get(leveldb::ReadOptions(), key, &value); assert(status.ok()); std::cout << value << std::endl; status = db->Get(leveldb::ReadOptions(), "testkey", &value); assert(status.ok()); std::cout << value << std::endl; leveldb::Slice msg1=leveldb::Slice("testkey"); status = db->Delete(leveldb::WriteOptions(), msg1); if(status.ok()) std::cout<<"success delete the key of testkey"<<std::endl; status = db->Get(leveldb::ReadOptions(), "testkey", &value); if(status.ok()) { std::cout << value << std::endl; } else { std::cout<<"cannot get the key of testkey"<<std::endl; } std::cout<<"Add some records into this leveldb."<<std::endl; leveldb::WriteBatch batch; batch.Put("2013-0", "20132014-0"); batch.Put("2013-1", "20132014-1"); batch.Put("2013-2", "20132014-2"); batch.Put("2013-3", "20132014-3"); batch.Put("2013-4", "20132014-4"); batch.Put("2013-5", "20132014-5"); batch.Put("2013-6", "20132014-6"); batch.Put("2013-7", "20132014-7"); status = db->Write(leveldb::WriteOptions(), &batch); if(status.ok()) { std::cout << "batch write many records success." << std::endl; } else { std::cout<<"batch write many records failed."<<std::endl; } std::cout<<"It's time to print all information about this leveldb."<<std::endl; leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); for (it->SeekToFirst(); it->Valid(); it->Next()) { std::cout << it->key().ToString() << ": " << it->value().ToString() << std::endl; } // Delete databse delete db; return 0; }
运行结果:

参考文献:
http://blog.csdn.net/cszhangchao/article/details/25245759
http://blog.csdn.net/cszhangchao/article/details/25340827
http://blog.csdn.net/dyllove98/article/details/9412763
http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html
http://dirlt.com/leveldb.html#sec-1-1
浙公网安备 33010602011771号