leveldb是google开源的一个高性能kv数据存储库。
# git clone https://github.com/google/leveldb.git
# cd leveldb/
# git checkout -b 1.20 v1.20
// 编译
# make
// 安装
# cp -rf include/leveldb/ /usr/include/
# cp out-shared/libleveldb.so* /usr/lib64/
// test1.cpp
#include <cassert>
#include <iostream>
#include "leveldb/db.h"
int main() {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "./test.db", &db);
assert(status.ok());
std::cout << "leveldb open success!" << std::endl;
std::string key = "testkey1";
std::string value = "testvalue1";
db->Put(leveldb::WriteOptions(), key, value);
std::cout << "put key " << key << " success" << std::endl;
std::string value2;
leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value2);
if (s.ok()) {
std::cout << "found key:" << key << ",value:" << value2 << std::endl;
}
s = db->Delete(leveldb::WriteOptions(), key);
if (s.ok()) {
std::cout << "delete key success which key:" << key << std::endl;
}
s = db->Get(leveldb::ReadOptions(), key, &value2);
if (s.IsNotFound()) {
std::cout << "can not found after delete for key:" << key << std::endl;
}
delete db;
return 0;
}
编译
# g++ -lpthread -lleveldb -o test1 test1.cpp
运行
# ./test1
leveldb open success!
put key testkey1 success
found key:testkey1,value:testvalue1
delete key success which key:testkey1
can not found after delete for key:testkey1
# tree test.db/
test.db/
├── 000003.log
├── CURRENT
├── LOCK
├── LOG
└── MANIFEST-000002
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。