0%

rocksdb笔记

下载源码

1
git clone https://github.com/facebook/rocksdb.git

编译 & 安装

1
2
make
make install

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <assert.h>
#include "rocksdb/db.h"

#include <string>
#include <iostream>

int main() {
rocksdb::DB* db;
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::Status status =
rocksdb::DB::Open(options, "./db_dir", &db);

status = db->Put(rocksdb::WriteOptions(), "hello", "world"); //写
if (status.ok()) {
std::string k("hello");
std::string v;
db->Get(rocksdb::ReadOptions(), k, &v); //读
std::cout << v << std::endl;
}
assert(status.ok());
}

编译 & 链接

尝试:

1
g++ test.cc

报错:

1
2
3
4
/usr/local/include/rocksdb/wide_columns.h:51:21: error: ‘make_from_tuple’ is not a member of ‘std’; did you mean ‘make_tuple’?
51 | value_(std::make_from_tuple<Slice>(std::forward<VTuple>(value_tuple))) {
| ^~~~~~~~~~~~~~~
| make_tuple

解决方法:

1
g++ test.cc -std=c++17

报错:

1
undefined reference to `rocksdb::DB::Open(rocksdb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, rocksdb::DB**)'

解决方法:

1
g++ test.cc -std=c++17 -lrocksdb

报错:

1
undefined reference to `pthread_mutex_trylock'

解决方法:

1
g++ test.cc -std=c++17 -lrocksdb -lpthread

报错:

1
undefined reference to `dlopen'

解决方法:

1
g++ test.cc -std=c++17 -lrocksdb -lpthread -ldl

ok, 成功啦。啰啰嗦嗦地写了这么多,是为了加强自己对编译链接的理解。

遍历rocksdb

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
#include <cassert>
#include <rocksdb/db.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
rocksdb::DB* db; rocksdb::Options options;
options.create_if_missing = true; //打开数据库
rocksdb::Status status = rocksdb::DB::Open(options, "./omap", &db);
assert(status.ok()); //插入数据
// db->Put(rocksdb::WriteOptions(), "key1", "value1");
// db->Put(rocksdb::WriteOptions(), "key2", "value2");
// db->Put(rocksdb::WriteOptions(), "key3", "value3");
// db->Put(rocksdb::WriteOptions(), "key4", "value4");
// db->Put(rocksdb::WriteOptions(), "key5", "value5");
rocksdb::Iterator* it = db->NewIterator(rocksdb::ReadOptions()); //使用迭代器获取所有key和value
for (it->SeekToFirst(); it->Valid(); it->Next()) {
cout << "key is: " << it->key().ToString() << endl;
cout << "value is: " << hex << it->value().ToString() << endl;
cout << "++++++++++++++++++++" << endl;
}
assert(it->status().ok());
delete it; //关闭数据库
status= db->Close();
delete db;
}