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. 谈谈Command对象与数据检索

    摘要 到目前为止,我相信大家对于ADO.NET如何与外部数据源建立连接以及如何提高连接性能等相关知识已经牢固于心了.连接对象作为ADO.NET的主力先锋,为用户与数据库交互搭建了扎实的桥梁.它的一生是 ...

  2. 16、cgminer学习之:pthread_mutex_init和pthread_cond_init

    1.原理 假设有两个线程同时访问一个全局变量 n,这个全局变量的初始值等于0. Int  n = 0 ; 消费者线程 A 进入临界区,访问 n,A 必须等到 n 大于 0 才能接着往下执行,如果 n= ...

  3. 常用协议(SPI, UART, I2C)

    SPI: SPI是全双工的同步串行接口,数据速率可达几Mbps,在一般应用中有4根信号线:MOSI, MISO, SCK, SS. 根据时钟极性(CPOL)及相位(CPHA)不同可以组合成4种工作模式 ...

  4. 使用Profiles分析SQL语句运行时间和消耗资源

    打开profiling,默认是没开启的. mysql> set profiling=1; 运行要分析的SQL语句 mysql> select count(1) from wechat_em ...

  5. maven 怎么在MyEclipse中打开Navigator视图

    方法一:1.点击菜单window2.选择show view菜单项3.选择other菜单项4.点击general,在其中可以找到 方法二:1.点击菜单window2.选择show view菜单项3.选择 ...

  6. 《机器学习实战》---第二章 k近邻算法 kNN

    下面的代码是在python3中运行, # -*- coding: utf-8 -*- """ Created on Tue Jul 3 17:29:27 2018 @au ...

  7. vue指令应用--实现输入框常见过滤功能

    前端开发最常碰到的就是输入框,经常要做各种验证,本公司惯用的需求是直接屏蔽特定字符的输入,如禁止非数字输入,特殊符号输入,空格输入等,这些功能反复使用,做成指令的形式,直接调用,非常方便,上代码: 目 ...

  8. QT5.5

    QT创建空项目时,会有报错“无法解析的外部符号”,方法是在 .pro文件中添加 以下,即可QT+=core gui widgets

  9. 公钥,私钥和数字签名这样最好理解 分类: B3_LINUX 2015-05-06 16:25 59人阅读 评论(0) 收藏

    一.公钥加密 假设一下,我找了两个数字,一个是1,一个是2.我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥. 我有一个文件,不能让别人看,我就用1加密了.别人找到了这个 ...

  10. ZOJ 2514 Generate Passwords 水

    啦啦啦,水一发准备去复习功课~ ------------------------------------------水一发的分割线----------------------------------- ...