SSTable之overview-leveldb源码剖析(6)

SSTable全称sorted string table,是不可修改且持久化到磁盘的文件,将Immutable Memtable的内存数据flush到磁盘或相邻层的数据compaction机制等生成,可以多线程环境下被安全的访问而无需任何额外的同步措施。

SSTable的整体设计是一个相对比较大的篇幅,包括索引元数据及block格式、TableCache、BlockCache、读取查找、FilterPolicy、build生成、compaction等,本篇主要讲整体设计,后续几篇会详细讲细节。

相较于linux文件系统的设计,为解决低速磁盘与应用程序读写之间的速度不匹配引入了page cache,应用程序读写page cache(内存),读未命中页面缓存再从磁盘加载块到cache,写先写cache再由操作系统根据一定策略将脏页写入磁盘,page cache块大小默认为4K(即内存页大小,可通过getconf PAGESIZE获取)。leveldb在应用层上设计与此相似,因LSM树结构的存储设计机制,有两级cache,在查找时如果未命中MemTable(内存),先访问缓存SSTable文件指针及偏移(相当于SSTable索引信息)的TableCache,定位到SSTable再访问BlockCache,BlockCache是存放SSTable实际数据的基本单位,最后通过块缓存来获取key对应的实际数据,未命中时则反向更新相应缓存。

来看下SSTable的使用接口及设计,如下:

  1. class Table {
  2. public:
  3. static Status Open(const Options& options, RandomAccessFile* file,
  4. uint64_t file_size, Table** table);
  5.  
  6. ~Table();
  7.  
  8. // Returns a new iterator over the table contents.
  9. // The result of NewIterator() is initially invalid (caller must
  10. // call one of the Seek methods on the iterator before using it).
  11. Iterator* NewIterator(const ReadOptions&) const;
  12.  
  13. // Given a key, return an approximate byte offset in the file where
  14. // the data for that key begins (or would begin if the key were
  15. // present in the file). The returned value is in terms of file
  16. // bytes, and so includes effects like compression of the underlying data.
  17. // E.g., the approximate offset of the last key in the table will
  18. // be close to the file length.
  19. uint64_t ApproximateOffsetOf(const Slice& key) const;
  20.  
  21. private:
  22. struct Rep;
  23. Rep* rep_;
  24.  
  25. explicit Table(Rep* rep) { rep_ = rep; }
  26. static Iterator* BlockReader(void*, const ReadOptions&, const Slice&);
  27.  
  28. // Calls (*handle_result)(arg, ...) with the entry found after a call
  29. // to Seek(key). May not make such a call if filter policy says
  30. // that key is not present.
  31. friend class TableCache;
  32. Status InternalGet(
  33. const ReadOptions&, const Slice& key,
  34. void* arg,
  35. void (*handle_result)(void* arg, const Slice& k, const Slice& v));
  36.  
  37.  
  38. void ReadMeta(const Footer& footer);
  39. void ReadFilter(const Slice& filter_handle_value);
  40.  
  41. // No copying allowed
  42. Table(const Table&);
  43. void operator=(const Table&);
  44. };
  45.  

Open方法以随机读的方式打开sst文件并读取索引元数据。
ApproximateOffsetOf通过key查找在数据块中的大致位置(通过索引块),以便后续发起读操作。
NewIterator返回访问SSTable的迭代器。

迭代器设计是整个leveldb设计上的一个亮点,定义一组接口,简化代码,隐藏实现细节,从MemTable、SkipList,再到SSTale、Block、Cache、DB,多层次迭代器组织实现对数据的访问。

再看内部有个private的Rep结构体指针成员,其定义如下:

  1. struct Table::Rep {
  2. ~Rep() {
  3. delete filter;
  4. delete [] filter_data;
  5. delete index_block;
  6. }
  7.  
  8. Options options;
  9. Status status;
  10. RandomAccessFile* file;
  11. uint64_t cache_id;
  12. FilterBlockReader* filter;
  13. const char* filter_data;
  14.  
  15. BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
  16. Block* index_block;
  17. };

Rep的成员明明可以直接放在Table类的成员中,这里却组织成一个结构体。在TableBuilder中也有类似设计,用意何在?
设计者并不想将内部私有数据成员的细节暴露给外部调用者,这里通过独立的结构体指针不放在头文件而放在具体实现文件来隐藏实现细节,类似Pimpl Idiom (private implementation),头文件中仅做前向声明,可以在设计上做到更好的封装,隐藏实现细节,用户无需重新编译也能方便升级,尤其对库开发者,这种设计模式是极其重要的,也能减小不同模块间的耦合。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注