Leveldb代码阅读_leveldb源码分析

时间:2021年05月10日 01:23:38
代码结构db include helpers port table utilincludeexport.h#if !defined(LEVELDB_EXPORT)#if defined(LEVELDB_SHARED_LIBRARY)#if defined(_WIN32)#if defined(LEVELDB_COMPILE_LIBRARY)#define LEVELDB_EXPORT __declspec(dllexport)#else#define LEVELDB_EXPORT __declspec(dllimport)#endif// defined(LEVELDB_COMPILE_LIBRARY)#else// defined(_WIN32)#if defined(LEVELDB_COMPILE_LIBRARY)// linux下导出函数符号#define LEVELDB_EXPORT __attribute__((visibility("default")))#else#define LEVELDB_EXPORT#endif#endif// defined(_WIN32)#else// defined(LEVELDB_SHARED_LIBRARY)#define LEVELDB_EXPORT#endif#endif// !defined(LEVELDB_EXPORT)c.h: c语言接口#ifdef __cplusplusextern "C" {#endifLEVELDB_EXPORT ## 导出符号#ifdef __cplusplus}#endifdb.hdb抽象类 db db接口和流程 db类 class LEVELDB_EXPORT DB { public:// @Desc: 打开特定名称的dbstatic Status Open(const Options& options, const std::string& name, DB** dbptr);DB() = default;DB(const DB&) = delete;DB& operator=(const DB&) = delete;virtual ~DB();// @Desc: 写kv// Note: consider setting options.sync = true.virtual Status Put(const WriteOptions& options, const Slice& key, const Slice& value) = 0;// @Desc: 删除kv// Note: consider setting options.sync = true.virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;// @Desc: 批量写// Note: consider setting options.sync = true.virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;virtual Status Get(const ReadOptions& options, const Slice& key, std::string* value) = 0;// @Desc: 迭代器,需要先seek一下,参看iteratorvirtual Iterator* NewIterator(const ReadOptions& options) = 0;// 获取db状态virtual const Snapshot* GetSnapshot() = 0;// @Desc: 释放virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0;virtual bool GetProperty(const Slice& property, std::string* value) = 0;// @Desc: 预估大小virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes) = 0;// @Desc: 合并范围virtual void CompactRange(const Slice* begin, const Slice* end) = 0;}; PUT流程

DBImpl 继承自抽象类 DB

原型如下:

virtual Status DBImpl::Put(const WriteOptions&, const Slice& key, const Slice& value);

这个函数调用了抽象类DB的Put方法,而db则内部则是构建了一个batch,调用了db的write方法。

Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {

###

GET流程