leveldb源码学习之跳表skiplist

Skip list(跳跃表)是一种可以代替平衡树的数据结构。Skip lists应用概率保证平衡,平衡树采用严格的旋转(比如平衡二叉树有左旋右旋)来保证平衡,因此Skip list比较容易实现,而且相比平衡树有着较高的运行效率

由于skip list比较简单,实现起来会比较容易,虽然和平衡树有着相同的时间复杂度(O(logn)),但是skip list的常数项相对小很多。skip list在空间上也比较节省。一个节点平均只需要1.333个指针(甚至更少),并且不需要存储保持平衡的变量。

在Leveldb中,skip list是实现memtable的核心数据结构,memtable的KV数据都存储在skip list中。

 

跳表类定义

template <typename Key, class Comparator>
class SkipList {
 private:
  struct Node;

 public:
  // 创建SkipList,以"cmp"作为key的比较器
  // 使用"*arena"分配内存,分配内存的对象必须在跳表生命周期中保持有效.
  explicit SkipList(Comparator cmp, Arena* arena);

  SkipList(const SkipList&) = delete; // 禁用拷贝构造
  SkipList& operator=(const SkipList&) = delete; //禁用赋值

  // 往跳表插入key,需要key不存.
  void Insert(const Key& key);

  // 若跳表中key存在,返回true.
  bool Contains(const Key& key) const;

  // 迭代器
  class Iterator {
   public:
    // 初始化跳表迭代器, 但返回值不指向有效节点.
    explicit Iterator(const SkipList* list);

    // 若迭代器指向有效节点,返回true.
    bool Valid() const;

    // 返回当前位置key,Valid()必须是true
    const Key& key() const;

    // 访问下一个位置,Valid()必须是true
    void Next();

    // 访问上一个位置,Valid()必须是true
    void Prev();

    // 搜索第一个key>=target的节点
    void Seek(const Key& target);

    // 定位到第一个节点,若跳表非空,则此时迭代器Valid为true.
    void SeekToFirst();

    // 定位到最后一个节点,若跳表非空,则此时迭代器Valid为true.
    void SeekToLast();

   private:
    const SkipList* list_; // 指向跳表
    Node* node_;
    // Intentionally copyable
  };

 private:
  enum { kMaxHeight = 12 };

  inline int GetMaxHeight() const {
    return max_height_.load(std::memory_order_relaxed);
  }

  Node* NewNode(const Key& key, int height);
  int RandomHeight();
  bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }

  // 若key大于n 节点的key值,返回true
  bool KeyIsAfterNode(const Key& key, Node* n) const;

  // 搜索首个key大于等于给定key的节点 key,若不存在返回nullptr.
  //若前驱prev非空,则[0..max_height_-1] 每一层的前驱都赋值为该层prev
  Node* FindGreaterOrEqual(const Key& key, Node** prev) const;

  // 返回key < 给定key的最后一个节点,若不存在返回头指针head_ 
  Node* FindLessThan(const Key& key) const;

  // 返回末尾节点,若跳表为空返回头指针head_ 
  Node* FindLast() const;

  // 构造后不可修改
  Comparator const compare_;
  Arena* const arena_;  // 用于节点的空间分配

  Node* const head_;  //头指针

  // 只有 Insert() 操作会修改.  极少读取,旧的值也可接受.
  std::atomic<int> max_height_;  // 跳表高度

  // 只有 Insert() 会读写.
  Random rnd_;
};

 

内部节点定义

template <typename Key, class Comparator>
struct SkipList<Key, Comparator>::Node {
  explicit Node(const Key& k) : key(k) {}

  Key const key;

  // next指针的访问/修改器,封装在函数里,可以添加必要的同步限制如memory_order.
  Node* Next(int n) {
    assert(n >= 0);
    // memory_order_acquire 表示当前线程读写指令不能被重排到此操作之前,其他线程对此变量的写入对当前线程可见.
    return next_[n].load(std::memory_order_acquire);
  }
  void SetNext(int n, Node* x) {
    assert(n >= 0);
    // memory_order_release表示当前线程的读写指令不能被重排到该操作之后,且此操作对其他读写此变量的线程可见。.
    next_[n].store(x, std::memory_order_release);
  }

  // 部分场景下可以无障碍使用的变量,memory_order_relaxed 表示无同步或顺序约束.
  Node* NoBarrier_Next(int n) {
    assert(n >= 0);
    return next_[n].load(std::memory_order_relaxed);
  }
  void NoBarrier_SetNext(int n, Node* x) {
    assert(n >= 0);
    next_[n].store(x, std::memory_order_relaxed);
  }

 private:
  // 节点每一层的后继指针.  next_[0]是最底层的后继指针.
  std::atomic<Node*> next_[1];
};

 

创建节点

template <typename Key, class Comparator>
typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::NewNode(
    const Key& key, int height) {
  char* const node_memory = arena_->AllocateAligned(
      sizeof(Node) + sizeof(std::atomic<Node*>) * (height - 1)); // 分配空间,包括节点自身和每一层的后继指针
  return new (node_memory) Node(key); // placement new 在指定地址调构造函数
}

 

迭代器函数实现

// 根据list初始化,但是此时node不指向任何节点
template<typename Key, class Comparator>
inline SkipList<Key,Comparator>::Iterator::Iterator(const SkipList* list) {
  list_ = list;
  node_ = nullptr;
}

// 是否合法:node是否为空
template<typename Key, class Comparator>
inline bool SkipList<Key,Comparator>::Iterator::Valid() const {
  return node_ != nullptr;
}

template<typename Key, class Comparator>
inline const Key& SkipList<Key,Comparator>::Iterator::key() const {
  assert(Valid());
  return node_->key;
}

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() {
  // 不使用 "prev" 指针, 用搜索key之前的节点(为什么?).
  assert(Valid());
  node_ = list_->FindLessThan(node_->key);
  if (node_ == list_->head_) {
    node_ = nullptr;
  }
}

template<typename Key, class Comparator>
inline void SkipList<Key,Comparator>::Iterator::Seek(const Key& target) {
  node_ = list_->FindGreaterOrEqual(target, nullptr);
}

template<typename Key, class Comparator>
inline void SkipList<Key,Comparator>::Iterator::SeekToFirst() {
  node_ = list_->head_->Next(0);
}

template<typename Key, class Comparator>
inline void SkipList<Key,Comparator>::Iterator::SeekToLast() {
  node_ = list_->FindLast();
  if (node_ == list_->head_) {
    node_ = nullptr;
  }
}

这里迭代器访问前驱直接用find 代替了,find的复杂度是logn,二分查找,并不慢。如果是增加prev指针,可能还涉及指针的存储空间还有元素增删时的维护,并不简单。

 

跳表函数实现

// 以1/kBranching 概率增加高度
template<typename Key, class Comparator>
int SkipList<Key,Comparator>::RandomHeight() {
  static const unsigned int kBranching = 4;
  int height = 1;
  while (height < kMaxHeight && ((rnd_.Next() % kBranching) == 0)) {
    height++;
  }
  assert(height > 0);
  assert(height <= kMaxHeight);
  return height;
}

// 判断 key是否应该排在n节点后
template<typename Key, class Comparator>
bool SkipList<Key,Comparator>::KeyIsAfterNode(const Key& key, Node* n) const {
  //n 为空表示无穷大
  return (n != nullptr) && (compare_(n->key, key) < 0);
}

// 找出key 大于等于所给key的节点,若所给的prev参数不为空,则返回的时候prev每个元素是key在每一层的前驱节点(666)
template<typename Key, class Comparator>
typename SkipList<Key,Comparator>::Node* 
SkipList<Key,Comparator>::FindGreaterOrEqual(const Key& key, Node** prev)
    const {
  Node* x = head_; // 头结点
  int level = GetMaxHeight() - 1; // 跳表高度,参考本文开头的跳表结构
  while (true) {
    Node* next = x->Next(level);
    if (KeyIsAfterNode(key, next)) {
      // Keep searching in this list
      x = next;
    } else {
      if (prev != nullptr) prev[level] = x;
      if (level == 0) { // 第0层,元素都在这层,所以key的后继应该就是next所指节点。
        return next;
      } else {
        // 跳到下一层(更多元素)寻找
        level--;
      }
    }
  }
}

// 寻找key的前驱,和寻找大于等于key的函数类似,从高层到底层,逐渐缩小范围
template<typename Key, class Comparator>
typename SkipList<Key,Comparator>::Node*
SkipList<Key,Comparator>::FindLessThan(const Key& key) const {
  Node* x = head_;
  int level = GetMaxHeight() - 1;
  while (true) {
    assert(x == head_ || compare_(x->key, key) < 0);
    Node* next = x->Next(level);
    if (next == nullptr || compare_(next->key, key) >= 0) {
      if (level == 0) {
        return x;
      } else {
        // Switch to next list
        level--;
      }
    } else {
      x = next;
    }
  }
}

// 寻找末尾元素
template<typename Key, class Comparator>
typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindLast()
    const {
  Node* x = head_;
  int level = GetMaxHeight() - 1;
  while (true) {
    Node* next = x->Next(level);
    if (next == nullptr) {
      if (level == 0) {
        return x;
      } else {
        // Switch to next list
        level--;
      }
    } else {
      x = next;
    }
  }
}

// 跳表构造函数
template<typename Key, class Comparator>
SkipList<Key,Comparator>::SkipList(Comparator cmp, Arena* arena)
    : compare_(cmp),
      arena_(arena),
      head_(NewNode(0 /* any key will do */, kMaxHeight)),
      max_height_(1),
      rnd_(0xdeadbeef) {
  for (int i = 0; i < kMaxHeight; i++) {
    head_->SetNext(i, nullptr);
  }
}

// 跳表插入元素
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); // 找到待插入key的所有前驱

  // 不允许重复key
  assert(x == nullptr || !Equal(key, x->key));

  // 随机增加高度
  int height = RandomHeight();
  if (height > GetMaxHeight()) {
    for (int i = GetMaxHeight(); i < height; i++) {
      prev[i] = head_;
    }
    // It is ok to mutate max_height_ without any synchronization
    // with concurrent readers.  A concurrent reader that observes
    // the new value of max_height_ will see either the old value of
    // new level pointers from head_ (nullptr), or a new value set in
    // the loop below.  In the former case the reader will
    // immediately drop to the next level since nullptr sorts after all
    // keys.  In the latter case the reader will use the new node.
    max_height_.store(height, std::memory_order_relaxed);
  }

 // 遍历每一层,设置好前驱后继
  x = NewNode(key, height);
  for (int i = 0; i < height; i++) {
    // NoBarrier_SetNext() suffices since we will add a barrier when
    // we publish a pointer to "x" in prev[i].
    x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));
    prev[i]->SetNext(i, x);
  }
}

// 是否包含key
template<typename Key, class Comparator>
bool SkipList<Key,Comparator>::Contains(const Key& key) const {
  Node* x = FindGreaterOrEqual(key, nullptr);
  if (x != nullptr && Equal(key, x->key)) {
    return true;
  } else {
    return false;
  }
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要阅读Leveldb码,你可以按照以下步骤进行: 1. 确保你对C++语言有基本的了解。Leveldb是用C++编写的,因此你需要熟悉C++的语法和面向对象编程的概念。 2. 阅读Leveldb的README文件。这个文件提供了关于Leveldb的基本信息,如其用途、功能和性能特征。同时,它还列出了Leveldb的依赖关系,这对于理解码以及构建和运行Leveldb非常重要。 3. 了解Leveldb的核心概念和数据结构。Leveldb是一个高效的键值存储库,它使用了一些关键的数据结构,如有序字符串(Skip List)和持久化存储。 4. 查看Leveldb的目录结构。Leveldb码包含了一些核心文件和目录,如“db”目录下的文件是Leveldb的核心实现。理解码的组织结构可以帮助你快速找到感兴趣的部分。 5. 阅读核心文件的码。从“db/db_impl.cc”文件开始,这个文件是Leveldb的主要实现。阅读这个文件可以帮助你了解Leveldb如何管理内存、实施并发控制和实现持久化存储。 6. 跟踪函数调用和数据流。了解Leveldb的主要功能是如何通过函数调用进行实现的很重要。你可以使用调试器或添加日志输出来跟踪函数调用和数据流,这有助于你了解代码的执行流程和逻辑。 7. 阅读Leveldb的测试用例。Leveldb码中包含了大量的测试用例,这些用例对于理解Leveldb的不同功能和特性非常有帮助。通过阅读和运行这些测试用例,你可以对Leveldb的行为有更深入的了解。 8. 参考文档和论文。如果你想更深入地了解Leveldb的实现原理和技术细节,可以查阅Leveldb的官方文档或相关的论文。这些文档可以为你提供更详细的信息和背景知识。 最后,要理解Leveldb码并不是一件简单的任务,需要投入大量的时间和精力。所以,建议你在阅读码之前,对C++和数据库原理有一定的了解和经验,同时也要具备耐心和持续学习的精神。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值