【leveldb】Options、ReadOptions、WriteOptions(三)

  • Options:主要用於控制DB的一些操作。
  • ReadOptions:用於控制讀操作屬性。
  • WriteOptions:用於控制寫操作屬性。

一、Options

// Options to control the behavior of a database (passed to DB::Open)
 <!用於控制DB的一些特性>
struct LEVELDB_EXPORT Options {
  // Create an Options object with default values for all fields.
  Options();

  // -------------------
  // Parameters that affect behavior

  // Comparator used to define the order of keys in the table.
  // Default: a comparator that uses lexicographic byte-wise ordering
  //
  // REQUIRES: The client must ensure that the comparator supplied
  // here has the same name and orders keys *exactly* the same as the
  // comparator provided to previous open calls on the same DB.
  <!key的排序方式,默認使用字典字節序排序,\
  一個數據庫的排序方式確定之後不要再改變>
  const Comparator* comparator;

  // If true, the database will be created if it is missing.
  <!如果是true,數據庫打開時,若數據庫丟失會重新創建一份>
  bool create_if_missing = false;

  // If true, an error is raised if the database already exists.
  <!如果是true,數據庫打開時如果存在則報錯。>
  bool error_if_exists = false;

  // If true, the implementation will do aggressive checking of the
  // data it is processing and will stop early if it detects any
  // errors.  This may have unforeseen ramifications: for example, a
  // corruption of one DB entry may cause a large number of entries to
  // become unreadable or for the entire DB to become unopenable.
  <!如果爲true,在數據處理過程中會嚴格檢查數據,檢查到任何錯誤都會提前停止,\
    這可能會到來無法預料的後果,例如一個條目的損害可能導致大量的條目無法讀取\
    或者整個數據庫無法打開了,所以這裏應該是建議false>
  bool paranoid_checks = false;

  // Use the specified object to interact with the environment,
  // e.g. to read/write files, schedule background work, etc.
  // Default: Env::Default()
  <!使用指定的對象與環境交互,比如讀寫文件,安排後臺工作等\
    默認使用Env默認值。一些複雜的操作用戶可自己指定而不依賴於默認環境。>
  Env* env;

  // Any internal progress/error information generated by the db will
  // be written to info_log if it is non-null, or to a file stored
  // in the same directory as the DB contents if info_log is null.
  <!如果日誌不爲空,則將db產生的處理和錯誤日誌寫與指定文件,
    如果爲空,則在同一目錄下創建一個文件作爲db內容寫入>
  Logger* info_log = nullptr;

  // -------------------
  // Parameters that affect performance

  // Amount of data to build up in memory (backed by an unsorted log
  // on disk) before converting to a sorted on-disk file.
  //
  // Larger values increase performance, especially during bulk loads.
  // Up to two write buffers may be held in memory at the same time,
  // so you may wish to adjust this parameter to control memory usage.
  // Also, a larger write buffer will result in a longer recovery time
  // the next time the database is opened.
  <!這個大小應該是memtable和immutable大小。
    在大容量負載期間,增大這個值可以提升性能,
    最多可保持兩份寫緩衝區在內存中,通過調節此值
    可控制內存使用大小,唯一不好的是如果寫緩衝區太大,
    下次打開數據庫時會耗時更久。>
  size_t write_buffer_size = 4 * 1024 * 1024;

  // Number of open files that can be used by the DB.  You may need to
  // increase this if your database has a large working set (budget
  // one open file per 2MB of working set).
  <!DB可打開的最大文件數,如果系統工作集很大,可考慮增大此值,\
    每個打開的文件大概會佔用2MB的工作集大小>
  int max_open_files = 1000;

  // Control over blocks (user data is stored in a set of blocks, and
  // a block is the unit of reading from disk).

  // If non-null, use the specified cache for blocks.
  // If null, leveldb will automatically create and use an 8MB internal cache.
  <!block是DB的控制塊,是與磁盤交互的最小單元,用戶數據存儲在這個控制塊中。
    用戶可指定一個cache,如果不指定,系統默認創建一個8MB的內部cache>
  Cache* block_cache = nullptr;

  // Approximate size of user data packed per block.  Note that the
  // block size specified here corresponds to uncompressed data.  The
  // actual size of the unit read from disk may be smaller if
  // compression is enabled.  This parameter can be changed dynamically.
  <!每個block塊的大小,這裏指的是爲壓縮數據大小。如果開啓了數據壓縮,
    則一個從磁盤讀取的塊大小可能是小於未壓縮之前大小的。此參數可動態改變>
  size_t block_size = 4 * 1024;

  // Number of keys between restart points for delta encoding of keys.
  // This parameter can be changed dynamically.  Most clients should
  // leave this parameter alone.
  <!block重啓點之間的key的個數,至於什麼是重啓點可參考(leveldb 二)中的說明>
  int block_restart_interval = 16;

  // Leveldb will write up to this amount of bytes to a file before
  // switching to a new one.
  // Most clients should leave this parameter alone.  However if your
  // filesystem is more efficient with larger files, you could
  // consider increasing the value.  The downside will be longer
  // compactions and hence longer latency/performance hiccups.
  // Another reason to increase this parameter might be when you are
  // initially populating a large database.
  <!落地磁盤文件最大大小,超過此大小重新一個新文件寫。
    若文件系統在大文件方面表現好,可增大此值。值的增大會帶來以下問題:
    1、更長的壓縮耗時,2、更長的性能中斷。增大此值的一個原因可能是
    一開始就要填充一個大的數據庫>
  size_t max_file_size = 2 * 1024 * 1024;

  // Compress blocks using the specified compression algorithm.  This
  // parameter can be changed dynamically.
  //
  // Default: kSnappyCompression, which gives lightweight but fast
  // compression.
  //
  // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
  //    ~200-500MB/s compression
  //    ~400-800MB/s decompression
  // Note that these speeds are significantly faster than most
  // persistent storage speeds, and therefore it is typically never
  // worth switching to kNoCompression.  Even if the input data is
  // incompressible, the kSnappyCompression implementation will
  // efficiently detect that and will switch to uncompressed mode.
  <!壓縮block的方式。壓縮速率指標如上,作者是是建議壓縮的,即使輸入數據不可壓縮
    壓縮算法也可以自己檢測出來而切換到不壓縮模式>
  CompressionType compression = kSnappyCompression;

  // EXPERIMENTAL: If true, append to existing MANIFEST and log files
  // when a database is opened.  This can significantly speed up open.
  //
  // Default: currently false, but may become true later.
  <!如果爲true,直接使用文件追加的方式,可使打開DB更快,
    作者默認默認false>
  bool reuse_logs = false;

  // If non-null, use the specified filter policy to reduce disk reads.
  // Many applications will benefit from passing the result of
  // NewBloomFilterPolicy() here.
  <!指定過濾策略來減少磁盤的讀取,作者推薦使用NewBloomFilterPolicy()>
  const FilterPolicy* filter_policy = nullptr;
};

二、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;
};
發佈了50 篇原創文章 · 獲贊 31 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章