首页 > 其他分享 >leveldb 代码阅读三

leveldb 代码阅读三

时间:2024-06-05 17:56:01浏览次数:15  
标签:leveldb false 压缩 代码 write bool 阅读 默认值 数据库

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

标签:leveldb,false,压缩,代码,write,bool,阅读,默认值,数据库
From: https://www.cnblogs.com/lihaihui1991/p/18233404

相关文章