目录
- 关于leveldb
- 下载leveldb源码
- 增加测试文件
- 更新cmake文件
- 运行
- 关于RocksDB
- 下载RocksDB代码
- 修改CMakelist.txt
- 运行
- 参考资料:
关于leveldb
下载leveldb源码
链接: leveldbGit地址
增加测试文件
使用clion打开项目,在根目录下新建一个app目录,app目录下新建一个main.cpp。代码如下:
//
//
#include <cassert>
#include <iostream>
#include "include/leveldb/db.h"
int main() {
//std::cout << "leveldb open success!" << std::endl;
//return 0;
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "c://myLevelDB", &db);
assert(status.ok());
std::cout << "leveldb open success!" << std::endl;
std::string value;
std::string key1 = "testkey1";
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.IsNotFound()) {
std::cout << "can not found for key:" << key1 << std::endl;
db->Put(leveldb::WriteOptions(), key1, "testvalue1");
}
s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.ok()) {
std::cout << "found key:" << key1 << ",value:" << value << std::endl;
}
s = db->Delete(leveldb::WriteOptions(), key1);
if (s.ok()) {
std::cout << "delete key success which key:" << key1 << std::endl;
}
s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.IsNotFound()) {
std::cout << "can not found after delete for key:" << key1 << std::endl;
}
delete db;
return 0;
}
更新cmake文件
在CMakeList.txt里面寻找leveldbutil字符串,然后在下面增加
add_executable(myTest "app/main.cpp")
target_link_libraries(myTest leveldb)
最终效果如下:
运行
关于RocksDB
下载RocksDB代码
同上,在根目录的app目录下增加测试文件main.cpp,代码如下:
//
// Created by on 2024/1/11.
//
#include <iostream>
#include <string>
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
using ROCKSDB_NAMESPACE::DB;
using ROCKSDB_NAMESPACE::Options;
using ROCKSDB_NAMESPACE::PinnableSlice;
using ROCKSDB_NAMESPACE::ReadOptions;
using ROCKSDB_NAMESPACE::Status;
using ROCKSDB_NAMESPACE::WriteBatch;
using ROCKSDB_NAMESPACE::WriteOptions;
// rocksdb存储路径
std::string kDBPath="c://myrocksdb";
int main()
{
DB* db;
Options options;
options.IncreaseParallelism();
//文件夹没有数据就创建
options.create_if_missing=true;
// 打开数据库,加载数据到内存
Status s=DB::Open(options,kDBPath,&db);
assert(s.ok());
// 写key-value
s=db->Put(WriteOptions(),"key01","value");
assert(s.ok());
std::string value;
s=db->Get(ReadOptions(),"key01",&value);
assert(s.ok());
std::cout<<"get value "<< value<<std::endl;
assert(value=="value");
// 管道,原子方式更新
{
WriteBatch batch;
batch.Delete("key01");
batch.Put("key02",value);
s=db->Write(WriteOptions(),&batch);
}
s=db->Get(ReadOptions(),"key01",&value);
assert(s.IsNotFound());
s=db->Get(ReadOptions(),"key02",&value);
assert(value=="value");
{
PinnableSlice pinnable_val;
// 列族方式读取
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
}
{
std::string string_val;
PinnableSlice pinnable_val(&string_val);
// 列族方式读取
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
assert(pinnable_val.IsPinned() || string_val == "value");
}
PinnableSlice pinnable_val;
s=db->Get(ReadOptions(),db->DefaultColumnFamily(),"key01",&pinnable_val);
assert(s.IsNotFound());
pinnable_val.Reset();
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
pinnable_val.Reset();
delete db;
return 0;
}
修改CMakelist.txt
增加如下内容:
add_executable(myTest "app/main.cpp")
target_link_libraries(myTest ${ROCKSDB_STATIC_LIB})
效果如下:
运行
参考资料:
https://zhuanlan.zhihu.com/p/653465714
https://zhuanlan.zhihu.com/p/611450548