leveldb 代码阅读三

options 解读

Options 数据库设置

压缩类型

//数据库内容存储在一组块中,每个块包含一个键、值对序列。
//在存储到文件中之前,可以压缩每个块。
//下面的枚举描述用于压缩块的压缩方法(如果有)。
enum CompressionType {
  	kNoCompression     = 0x0,
  	kSnappyCompression = 0x1
};

Options 结构

struct LEVELDB_EXPORT Options {
  	//比较器用于定义表中键的顺序。
	//默认值:使用字典式字节顺序的比较器
	//要求:客户机必须确保这里提供的比较器具有相同的名称,并且ORDERS键*与以前在同一数据库上打开调用时提供的比较器完全相同。
  	const Comparator* comparator;

  	// If true, the database will be created if it is missing.
  	//为真时,当数据库丢失时创建数据库对象
  	// 默认为false
  	bool create_if_missing;

  	// 为真时, 如果数据库存在则会引发错误
  	// 默认: false
  	bool error_if_exists;

  	// 为真时,则实现对正在处理的数据进行积极的检查,如果检测到任何错误,则将提前停止
  	// 这可能导致一些不可预见的结果:例如,一个数据库条目的损坏可能导致大量条目无法读取或整个数据库无法打开
  	// 默认值:false
  	bool paranoid_checks;

  	//用一个特殊对象和环境变量进行交互.
  	// 默认值: Env::Default()
  	Env* env;

  	// 如果数据库生成的任何内部进度/错误信息非空,则将其写入信息日志;
  	// 如果信息日志为空,则将写入与数据库内容存储在同一目录中的文件。
  	// 默认值:空
  	Logger* info_log;

  	// 该参数会影响性能
  	// 写缓冲大小
  	// 默认值: 4MB
  	size_t write_buffer_size;

  	// 数据库可以使用的打开的文件数;
  	// 如果数据库有较大的工作集,则需要增加此值
  	// 默认值: 1000
  	int max_open_files;

  	// 控制块 (用户数据存储在一组块中,块是从磁盘读取的单位).
  	// 不为空, 则对块使用指定的缓存.
  	// 为空, leveldb 将自动创建并使用8MB的内部缓存.
  	// 默认值: nullptr
  	Cache* block_cache;

  	// 每个块压缩的用户数据的近似大小。特别注意的是,次数指定的块大小对应于未压缩的数据。
  	// 如果启用压缩,从磁盘读取的单元的实际大小可能会更小。此参数可以动态更改。
  	// 默认值: 4K
  	size_t block_size;

  	// 键的增量编码的重新启动点之间的键数。此参数可以动态更改。大多数客户机应该只保留此参数。
  	// 默认值: 16
  	int block_restart_interval;

  	// 存储的文件大小
  	// 默认值: 2MB
  	size_t max_file_size;

  	// 使用指定的压缩算法压缩块。此参数可以动态更改。
	//默认值:ksnappycompression,它提供轻量但快速的压缩。
	//在Intel(R)Core(TM)2 2.4GHz上ksnapycompression的典型速度:
	//~200-500MB/s压缩
	//~400-800MB/s解压
	//请注意,这些速度明显快于大多数持久存储速度,因此通常不值得切换到knocompression。
    //即使输入数据不可压缩,ksnappycompression实现也会有效地检测到这一点并切换到未压缩模式。
  	CompressionType compression;

  	// 为真时,则打开数据库时附加到现有清单和日志文件.  这可以显著加快打开速度.
  	// 默认值:目前为false,以后可能为真.
  	bool reuse_logs;

  	// 当此值部位空时,用指定的筛选策略来减少磁盘读取。
  	// 许多程序此处都会传递布隆过滤器
  	const FilterPolicy* filter_policy;

  	Options();
};

ReadOptions 读设置

// Options that control read operations
struct LEVELDB_EXPORT ReadOptions {
  ReadOptions() = default;

  // If true, all data read from underlying storage will be
  // verified against corresponding checksums.
  <!如果为ture,所有读取数据都会校验>
  bool verify_checksums = false;

  // Should the data read for this iteration be cached in memory?
  // Callers may wish to set this field to false for bulk scans.
  <!从迭代器读取的数据是否要缓存在内存中,
    数据批量扫描可能希望为false>
  bool fill_cache = true;

  // If "snapshot" is non-null, read as of the supplied snapshot
  // (which must belong to the DB that is being read and which must
  // not have been released).  If "snapshot" is null, use an implicit
  // snapshot of the state at the beginning of this read operation.
  <!快照,有快照就读取快照数据,没快照就正常读取>
  const Snapshot* snapshot = nullptr;
};

WriteOptions 写设置

// Options that control write operations
struct LEVELDB_EXPORT WriteOptions {
  WriteOptions() = default;

  // If true, the write will be flushed from the operating system
  // buffer cache (by calling WritableFile::Sync()) before the write
  // is considered complete.  If this flag is true, writes will be
  // slower.
  //
  // If this flag is false, and the machine crashes, some recent
  // writes may be lost.  Note that if it is just the process that
  // crashes (i.e., the machine does not reboot), no writes will be
  // lost even if sync==false.
  //
  // In other words, a DB write with sync==false has similar
  // crash semantics as the "write()" system call.  A DB write
  // with sync==true has similar crash semantics to a "write()"
  // system call followed by "fsync()".
  <!是否写同步,同步写是慢于异步写的,但不会造成数据丢失,
    如果是异步写,只有在机器重启的情况下才会造成数据丢失,
    其它情况这不会丢失>
  bool sync = false;
};

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
leveldb实现解析 淘宝-核心系统研发-存储 那岩 neveray@gmail.com 2011-12-13 目录 一、 代码目录结构 ....................................................................... 1 1. doc/ ............................................................................ 1 2. include/leveldb/ ................................................................. 1 3. db/ ............................................................................. 1 4. table/........................................................................... 1 5. port/ ........................................................................... 1 6. util/ ........................................................................... 1 7. helper/memenv/ ................................................................... 1 二、 基本概念 .......................................................................... 1 1. Slice (include/leveldb/slice.h) .................................................. 1 2. Option (include/leveldb/option.h) .............................................. 2 3. Env (include/leveldb/env.h util/env_posix.h) .................................... 3 4. varint (util/coding.h) ........................................................... 3 5. ValueType (db/dbformat.h) ...................................................... 3 6. SequnceNnumber (db/dbformat.h) ................................................. 4 7. user key......................................................................... 4 8. ParsedInternalKey (db/dbformat.h) .............................................. 4 9. InternalKey (db/dbformat.h) ...................................................... 4 10. LookupKey (db/dbformat.h) ........................................................ 4 11. Comparator (include/leveldb/comparator.h util/comparator.cc) .................... 4 12. InternalKeyComparator (db/dbformat.h) ............................................ 5 13. WriteBatch (db/write_batch.cc) ................................................. 5 14. Memtable (db/memtable.cc db/skiplist.h) .......................................... 5 15. Sstable (table/table.cc) ......................................................... 5 16. FileMetaData (db/version_edit.h) ............................................... 5 17. block (table/block.cc) ........................................................... 6 18. BlockHandle(table/format.h) ...................................................... 6 19. FileNumber(db/dbformat.h) ...................................................... 6 20. filename (db/filename.cc) ........................................................ 6 21. level-n (db/version_set.h) ....................................................... 7 22. Compact (db/db_impl.cc db/version_set.cc) ........................................ 7 23. Compaction(db/version_set.cc) .................................................... 7 24. Version (db/version_set.cc) ...................................................... 8 25. VersionSet (db/version_set.cc) ................................................. 9 26. VersionEdit(db/version_edit.cc) ................................................. 10 27. VersionSet::Builder (db/version_set.cc) ......................................... 11 28. Manifest(descriptor)(db/version_set.cc)...................................... 12 29. TableBuilder/BlockBuilder(table/table_builder.cc table/block_builder.cc) ......... 12 30. Iterator (include/leveldb/iterator.h) ........................................... 12 、 存储结构的格式定义与操作 .......................................................... 12 1. memtable (db/skiplist.h db/memtable) ............................................ 12 2. block of sstable (table/block_builder.cc table/block.cc) ......................... 14 3. sstable (table/table_bulder.cc/table.cc) ........................................ 16 4. block of log (db/log_format.h db/log_writer.cc db/log_reader.cc) .............. 18 5. log (db/log_format.h db/log_writer.cc db/log_reader.cc) ........................ 18 6. cache(util/cache.cc) .......................................................... 19 7. Snapshot (include/leveldb/snapshot.h) ......................................... 19 8. Iterator (include/leveldb/iterator.h) ........................................... 19 四、 主要流程 ......................................................................... 24 1. open ........................................................................... 24 2. put ............................................................................ 25 3. get ............................................................................ 25 4. delete.......................................................................... 26 5. snapshot........................................................................ 26 6. NewIterator ..................................................................... 26 7. compact......................................................................... 26 五、 总结 ............................................................................. 30 1. 设计/实现中的优化 ............................................................... 30 2. 可以做的优化 .................................................................... 31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值