leveldb中的memtable仅仅是一个封装类,它的底层实现是一个跳表。

跳表是一种基于随机数的平衡数据结构。其它的平衡数据结构还有红黑树、AVL树。但跳表的原理比它们简单非常多。跳表有点像链表,仅仅只是每一个节点是多层结构,通过在每一个节点中添加向前的指针提高查找效率。例如以下图:



在/leveldb/db目录下有跳表的实现skiplist.h和跳表的測试程序skiplist_test.cc。

template<typename Key, class Comparator>
class SkipList {

能够看出leveldb的skiplist是一个模板类,key是跳表每一个节点存储的信息类,跳表是一个顺序结构,comparator是跳表key的比較器。

成员变量:

private:
//设定的跳表最大层数,新增节点的随机层数不能大于此值
enum { kMaxHeight = 12 };
// Immutable after construction
//key的比較器
Comparator const compare_;
//内存池
Arena* const arena_; // Arena used for allocations of nodes
//跳表的头结点
Node* const head_;
// Modified only by Insert(). Read racily by readers, but stale
// values are ok.
// 跳表的最大层数,不包含head节点,head节点的key为0,小于不论什么key,层数为kmaxheight=12
// AtomicPointer是leveldb定义的一个原子指针,它的读取和写入都设立了内存屏障,保证读取的值是即时的、最新的
// 这里直接将int型转化为指针保存,由于不会对其取地址。所以可行。值得借鉴
port::AtomicPointer max_height_; // Height of the entire list

成员函数有:

// Insert key into 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;
Node* NewNode(const Key& key, int height);
int RandomHeight();
bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
// Return true if key is greater than the data stored in "n"
bool KeyIsAfterNode(const Key& key, Node* n) const;
// Return the earliest node that comes at or after key.
// Return NULL if there is no such node.
//
// If prev is non-NULL, fills prev[level] with pointer to previous
// node at "level" for every level in [0..max_height_-1].
Node* FindGreaterOrEqual(const Key& key, Node** prev) const;
// Return the latest node with a key < key.
// Return head_ if there is no such node.
Node* FindLessThan(const Key& key) const;
// Return the last node in the list.
// Return head_ if list is empty.
Node* FindLast() const;

相信你们能够看懂这些函数功能的凝视。

节点和迭代器:

skiplist的节点Node和迭代器Iterator都是以嵌套类定义在类skiplist中的

class Iterator {
public:
// Initialize an iterator over the specified list.
// The returned iterator is not valid.
explicit Iterator(const SkipList* list);
// Returns true iff the iterator is positioned at a valid node.
bool Valid() const;
// Returns the key at the current position.
// REQUIRES: Valid()
const Key& key() const;
// Advances to the next position.
// REQUIRES: Valid()
void Next();
// Advances to the previous position.
// REQUIRES: Valid()
void Prev();
// Advance to the first entry with a key >= target
void Seek(const Key& target);
// Position at the first entry in list.
// Final state of iterator is Valid() iff list is not empty.
void SeekToFirst();
// Position at the last entry in list.
// Final state of iterator is Valid() iff list is not empty.
void SeekToLast();
private:
const SkipList* list_;
Node* node_;
// Intentionally copyable
};

迭代器仅仅有一个list指针(保存所指skiplist的指针)和node指针(所指的节点)。

迭代器主要操作有前进,后退,定位头结点。尾节点,封装了list和node的操作。比方:

template<typename Key, class Comparator>
inline void SkipList<Key,Comparator>::Iterator::Next() {
assert(Valid());
node_ = node_->Next(0);
}
template<typename Key, class Comparator>
inline void SkipList<Key,Comparator>::Iterator::Prev() {
// Instead of using explicit "prev" links, we just search for the
// last node that falls before key.
assert(Valid());
node_ = list_->FindLessThan(node_->key);
if (node_ == list_->head_) {
node_ = NULL;
}
}

注意next操作时沿节点最低层的指针前进的,实际上prev也是,这样保证能够遍历skiplist每一个节点。实际上跳表的多层指针结构为了提高查询的效率。

以下来看看节点node的定义:

template<typename Key, class Comparator>
struct SkipList<Key,Comparator>::Node {
explicit Node(const Key& k) : key(k) { }
//所携带的数据,memtable中为我们指明了实例化版本号是char* key
Key const key;
// Accessors/mutators for links. Wrapped in methods so we can
// add the appropriate barriers as necessary.
//返回本节点第n层的下一个节点指针
Node* Next(int n) {
assert(n >= 0);
// Use an 'acquire load' so that we observe a fully initialized
// version of the returned Node.
return reinterpret_cast<Node*>(next_[n].Acquire_Load());
}
//重设n层的next指针
void SetNext(int n, Node* x) {
assert(n >= 0);
// Use a 'release store' so that anybody who reads through this
// pointer observes a fully initialized version of the inserted node.
next_[n].Release_Store(x);
}
// No-barrier variants that can be safely used in a few locations.
Node* NoBarrier_Next(int n) {
assert(n >= 0);
return reinterpret_cast<Node*>(next_[n].NoBarrier_Load());
}
void NoBarrier_SetNext(int n, Node* x) {
assert(n >= 0);
next_[n].NoBarrier_Store(x);
}
private:
// Array of length equal to the node height. next_[0] is lowest level link.
// 本节点的n层后向的指针
port::AtomicPointer next_[1];
};

在memtable的实现中,我们看到skiplist的实例化版本号是SkipList

template<typename Key, class Comparator>
typename SkipList<Key,Comparator>::Node*
SkipList<Key,Comparator>::NewNode(const Key& key, int height) {
char* mem = arena_->AllocateAligned(
sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1));
return new (mem) Node(key);
}

非常多人可能之前就奇怪为什么每一个节点的n层后向指针却是next_[1]。仅仅有一个成员?由于节点的高度须要由一个随机算子产生,也就是说height对于每一个节点是无法提前预知的,自然也就不能在node定义中确定next数组的大小。那么怎样保证next数组足够用呢?newnode为我们展现了这种一种奇技淫巧。实际上新节点在确定height后,向内存申请空间时,申请了一块sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1)大小的空间,确保next指针的空间足够。并用placement new为新node指定空间。

结构已经铺垫好。接下来我们就看看skiplist成员函数的实现吧。

//寻找关键字大于等于key值的近期节点。指针数组prev保存此节点每一层上訪问的前一个节点。

template<typename Key, class Comparator>
typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindGreaterOrEqual(const Key& key, Node** prev)
const {
Node* x = head_;
//首先获得跳表的最高层数,减一是数组next最大下标
int level = GetMaxHeight() - 1;
//查找操作開始
while (true) {
//跳表能够看成多层的链表。层数越高。链表的节点数越少。查找也就从高层数的链表開始
//假设key在本节点node之后,继续前进
//若果小于本节点node,把本节点的level层上的前节点指针记录进数组prev中,并跳向第一层的链表
//反复上述过程。直至来到最底层
Node* next = x->Next(level);
if (KeyIsAfterNode(key, next)) {
// Keep searching in this list
x = next;
} else {
if (prev != NULL) prev[level] = x;
if (level == 0) {
return next;
} else {
// Switch to next list
level--;
}
}
}
}

跳表实际上是相似一种多层的有序链表,高层的链表比底层的链表节点更少。在更高层的链表上能更快的遍历完整个链表,跳到更底层的链表更利于精确的定位。以上便是skiplist利用空间换取时间的方法精髓。想首先从跳表头结点的最高层開始遍历。key值大于节点key值。则前往同一层的下一个节点,否则跳到节点的低一层并记录上一层的最后訪问的节点。直到来到第一层(最底层)。以下其它操作的分析均源于此。贴一张跳表的示意图,帮助理解

相似的函数还有FindLessThan。FindLast,大家自己理解理解。

事实上FindGreaterOrEqual函数返回的前向节点指针数组是为了向跳表插入节点时用的。想想链表的插入操作。insert一个key时。首先新建一个node(key)。把node->next指向prev-next,再把prev->next指向node。跳表也是,仅仅只是须要操作多个链表。

skiplist::insert函数例如以下:

template<typename Key, class Comparator>
void SkipList<Key,Comparator>::Insert(const Key& key) {
// TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
// here since Insert() is externally synchronized.
Node* prev[kMaxHeight];
Node* x = FindGreaterOrEqual(key, prev);
// Our data structure does not allow duplicate insertion
assert(x == NULL || !Equal(key, x->key));
int height = RandomHeight();
if (height > GetMaxHeight()) {
for (int i = GetMaxHeight(); i < height; i++) {
prev[i] = head_;
}
max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
}
x = NewNode(key, height);
for (int i = 0; i < height; i++) {
x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
prev[i]->SetNext(i, x);
}
}

总结

好了。看了leveldb的skiplist和memtable,确实受益匪浅,不仅学习了skiplist的实现和memtable的封装,还认识了内存屏障以及怎样在C++中插入汇编语句。

但我认为看leveldb最重要的还是学习人家的设计思路。说究竟事实上跳表的实现并不难,抛开性能上的差距,或许我也能实现,但怎样真正做到面向对象,怎样理解需求和设计出最简明的结构。怎样让你的代码层次清楚,一目了然,这真的是一门大智慧。

leveldb学习:skiplist的更多相关文章

  1. leveldb 学习记录(四) skiplist补与变长数字

    在leveldb 学习记录(一) skiplist 已经将skiplist的插入 查找等操作流程用图示说明 这里在介绍 下skiplist的代码 里面有几个模块 template<typenam ...

  2. leveldb 学习记录(一) skiplist

    leveldb LevelDb是一个持久化存储的KV系统,并非完全将数据放置于内存中,部分数据也会存储到磁盘上. 想了解这个由谷歌大神编写的经典项目. 可以从数据结构以及数据结构的处理下手,也可以从示 ...

  3. leveldb 学习记录(三) MemTable 与 Immutable Memtable

    前文: leveldb 学习记录(一) skiplist leveldb 学习记录(二) Slice 存储格式: leveldb数据在内存中以 Memtable存储(核心结构是skiplist 已介绍 ...

  4. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  5. LevelDB学习笔记 (3): 长文解析memtable、跳表和内存池Arena

    LevelDB学习笔记 (3): 长文解析memtable.跳表和内存池Arena 1. MemTable的基本信息 我们前面说过leveldb的所有数据都会先写入memtable中,在leveldb ...

  6. leveldb学习:Versionedit和Versionset

    VersionEdit: compact过程中会有一系列改变当前Version的操作(FileNumber添加.删除input的sstable,添加输出的sstable).为了缩小version切换的 ...

  7. LevelDB学习笔记 (1):初识LevelDB

    LevelDB学习笔记 (1):初识LevelDB 1. 写在前面 1.1 什么是levelDB LevelDB就是一个由Google开源的高效的单机Key/Value存储系统,该存储系统提供了Key ...

  8. LevelDB 学习笔记1:布隆过滤器

    LevelDB 学习笔记1:布隆过滤器 底层是位数组,初始都是 0 插入时,用 k 个哈希函数对插入的数字做哈希,并用位数组长度取余,将对应位置 1 查找时,做同样的哈希操作,查看这些位的值 如果所有 ...

  9. LevelDB 学习笔记2:合并

    LevelDB 学习笔记2:合并 部分图片来自 RocksDB 文档 Minor Compaction 将内存数据库刷到硬盘的过程称为 minor compaction 产出的 L0 层的 sstab ...

随机推荐

  1. Logstash Json 过滤器插件

    1. Json Filter 功能概述 这是一个JSON解析过滤器.它接受一个包含JSON的现有字段,并将其扩展为Logstash事件中的实际数据结构. 默认情况下,它将把解析过的JSON放在Logs ...

  2. css 0.5px

    1. 利用css3新属性scale(好用推荐) 其实现的关键就是利用before或after伪元素,宽高同时设为200%,然后再缩放0.5,自然就是0.5px了 代码如下: div { positon ...

  3. VUE笔记 - 列表过渡动画 v-enter, v-leave-to | v-enter-active, v-leave-active | v-move

    本例要结合过渡动画四个过程的示意图一起理解. https://cn.vuejs.org/v2/guide/transitions.html 疑问: v-for="(item,i) in li ...

  4. 转载的:Python os 和 os.path模块详解

    os.getcwd()获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curdi ...

  5. 洛谷 P2299 Mzc和体委的争夺战

    洛谷 P2299 Mzc和体委的争夺战 题目背景 mzc与djn第四弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙), ...

  6. hdu5389

    题意:给你n个人每一个人手里有一个id,然后给你两个数a和b.让你把n个人分为两组.条件是 一组人手里的id和等于a 另一组人的id和等于b,这里的和是指加起来之后对9取余,假设sum等于0 则sum ...

  7. jquery的滚动事件

    $(selector).scroll(function);当滚动到合适的条件下,就触发某个函数. 现在基本就是前端利用AJAX对数据进行拼接操作,渲染进html的DOM结构中.

  8. P2P网贷-借款与发标

     P2P网贷-借款与发标 关于借款,我想说,需要资金的人真的很多.贷款利率不太高的情况下,借款客户相对而言还是比较好开发的, 比较难的是,确保客户能按时还款.目前,信用还是比较混乱的. 借款来源,客户 ...

  9. 【例题 6-11 UVA-297】Quadtrees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 发现根本不用存节点信息. 遇到了叶子节点且为黑色,就直接覆盖矩阵就好(因为是并集); [代码] #include <bits/ ...

  10. 折腾了我两天的springboot数据源datasource循环依赖问题,都被搞疯掉了

    在做项目重构的时候增加了两个功能 1.多数据源. 2.token的验证从以前的数据库验证,移到了redis端. 1.多数据源使用 druid-spring-boot-starter 套件 其核心代码如 ...