leveldb 04 SkipList学习

学习了levelDB源码分析-Skiplist
1. 在levelDB里Skiplist是个模板类。 它的声明和定义全部都放在了.h文件中。 普通class一般是把声明和定义分别放到.h和.cpp文件里的,而模板类必须把声明和定义都放到.h文件中。具体原因,参考学习博客 c++ 模板类 声明和定义都放在.h文件的原因.
2. 首先声明了 struct Node,这是前置声明,因为Node类是在后面的位置声明和定义的。

3. 接着public声明了explict的构造函数和其他两个普通接口:Insert 和 Contains。 注意它们的传参都是const 引用类型,这样可以避免copy构造和析构的成本。对于自定义class和template类,在effective C++书中作者都是推荐的这种 “pass by const reference”。

  // 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;


4. 以内部类的方式,实现了一个Iterator。
5. enum { kMaxHeight = 12 }; 这是用enum hack来替代传统C里面的宏了。 这个enum hack技巧在effective c++ item2有提到。
6. 跟着声明了private 的成员变量,可以看到作者使用了很多const来修饰。 这个是和effective C++里item 3提倡的是一致的。

  // Immutable after construction
  Comparator const compare_;
  Arena* const arena_;    // Arena used for allocations of nodes

  Node* const head_;


7. GetMaxHeight函数在class的声明里写出了定义,普通类应该不用inline修饰本身也是隐式inline的。 这里作者显式写出inline。应该是template类中确实需要inline的就明确写出inline关键字。 这里可以参考博客C++ inline函数和template函数

  inline int GetMaxHeight() const {
    return static_cast<int>(
        reinterpret_cast<intptr_t>(max_height_.NoBarrier_Load()));
  }


8. 成员函数,很多都加了const修饰。表示该成员函数不会修改成员变量的值。

  // Return the last node in the list.
  // Return head_ if list is empty.
  Node* FindLast() const;


9. 禁止复制的方式和effective C++提倡的方式是一致。

   // No copying allowed
  SkipList(const SkipList&);
  void operator=(const SkipList&);


10. new (mem) Node(key);使用了placement new, 是在mem中创建了Node对象。 placement new效率更高。 参考博客 【原创】LevelDB中的placement new

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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python,"import leveldb"是用于导入LevelDB的Python开发包,使得我们可以在代码使用LevelDB的功能和方法。 当我们在代码使用"import leveldb"时,我们可以通过创建一个LevelDB对象来打开一个数据库连接,从而可以对数据库进行读写操作。然而,如果我们尝试在已经打开的连接上再次打开连接,就会引发错误。这是因为LevelDB只允许一个进程同时持有一个数据库的锁定。如果数据库已经被其他进程锁定,就会出现"leveldb.LevelDBError: IO error: lock /var/tmp/ldb1.ldb/LOCK: already held by process"的错误提示。 此外,LevelDB还提供了一些API用法。在使用LevelDB之前,我们可以包装相关的import语句和Options对象来打开和关闭数据库连接,以及其他操作。具体的API用法可以参考LevelDB的官方文档或相关教程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LevelDB的Python开发包 py-leveldb基本使用方法的代码](https://blog.csdn.net/weixin_43896490/article/details/121946555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [leveldb 的部署和使用](https://blog.csdn.net/Moolight_shadow/article/details/119276763)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [leveldb:LevelDB到Java的端口](https://download.csdn.net/download/weixin_42098892/18545599)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值