#levelDB简介
LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
source code: google/leveldb
具体特性参考上面的Github说明页。
levelDB使用说明文档
#示例操作
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 27 28 29 30 31 32 33 34 35 36
| #include "leveldb/db.h" #include "leveldb/write_batch.h" #include <iostream> using namespace std; int main() { leveldb::DB *db; leveldb::Options options; options.create_if_missing = true; leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); assert(status.ok()); leveldb::Status s = db->Put(leveldb::WriteOptions(), "os", "mac os"); if(!s.ok()) { cerr << s.ToString() << endl; delete db; return -1; } string value; s = db->Get(leveldb::ReadOptions(), "os", &value); if(!s.ok()) { delete db; return -1; } cout << "os: " << value << endl; delete db; return 0; }
|
#源码分析
Reads And Writes
read – Get
Get操作支持区间查找’Approximate Sizes’
1 2 3 4 5
| leveldb::Range ranges[2]; ranges[0] = leveldb::Range("a", "c"); ranges[1] = leveldb::Range("x", "z"); uint64_t sizes[2]; leveldb::Status s = db->GetApproximateSizes(ranges, 2, sizes);
|
write – Put
其中Write操作又分为Synchronous Writes和asynchronous writes
Atomic Update
使用leveldb::WriteBatch类实现
Concurrency
leveldb支持单进程中多线程的并发,不支持多进程之间的并发.
Iteration
迭代器遍历levelDB
Snapshots
Slice
leveldb存储类型的底层实现为一个Slice结构
Comparators
支持自定义Comparators来对k/v进行排序
底层存储技术涉及memtable, sstable,Compression, Cache
##Filter
参考资料:
LevelDB Cache实现机制分析