- LevelDB的Put操作,是一个顺序写log,然后插入memtable(数据结构是skiplist)的过程,调用关系如下图所示:

- LevelDB的Get操作,会优先查找memtable,如果memtable查找不到,逐层在sstable中查找。下图是在memtable不存在,但在sstable中可以找到key的调用关系图:

1 #include "third_party/leveldb/db.h"
2
3 int main(void) {
4 leveldb::DB* db;
5 leveldb::Options options;
6
7 options.create_if_missing = true;
8 leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
9 if (!status.ok()) {
10 printf("%s\n", status.ToString().c_str());
11 }
12 std::string key = "hello";
13 std::string value = "world";
14 std::string exist_key = "nwlrbbmqbhcdarzowkkyhiddqscdxrjmowfr";
15 std::string empty_value;
16 status = db->Put(leveldb::WriteOptions(), key, value);
17 if (!status.ok()) {
18 printf("%s\n", status.ToString().c_str());
19 }
20 status = db->Get(leveldb::ReadOptions(), exist_key, &empty_value);
21 if (!status.ok()) {
22 printf("%s\n", status.ToString().c_str());
23 }
24 }