在系列二我们给出leveldb读写时序,在写<key,value>时候,write先写磁盘log,然后在内存插入到memtable,然后就返回。 作为leveldb的核心组件memtable是我们本篇所要给大家讲述的。
首先 class memtable是对skiplist进行了封装,核心数据结构就是skiplist.我们先讲skiplist
skiplist是一种随机化的数据结构,目前开源软件 Redis 和 LevelDB 都有用到它,它的效率和红黑树以及 AVL 树不相上下但是skiplist更适合快速插入调整的节点指针小于maxheight。
Skiplist本身是一种十分简单的数据结构
http://kenby.iteye.com/blog/1187303 这篇博文讲跳表讲的非常好,可以先看看。
leveldb对skiplist又做了封装(封装了Node) class skiplist定义如下
namespace leveldb {
class Arena;
template<typename Key, class Comparator>
class SkipList {
private:
struct Node;
public:
// Create a new SkipList object that will use "cmp" for comparing keys,
// and will allocate memory using "*arena". Objects allocated in the arena
// must remain allocated for the lifetime of the skiplist object.
explicit SkipList(Comparator cmp, Arena* arena);
// Insert key into the list.
// REQUIRES: nothing that compares equal to key is currently in the list.
void Insert(const Key& key);
// Returns true iff an entry that compares equal to key is in the list.
bool Contains(const Key& key) const;
// Iteration over the contents of a skip list