options.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
  5. #define STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
  6. #include <stddef.h>
  7. #include "leveldb/export.h"
  8. namespace leveldb {
  9. class Cache;
  10. class Comparator;
  11. class Env;
  12. class FilterPolicy;
  13. class Logger;
  14. class Snapshot;
  15. // DB contents are stored in a set of blocks, each of which holds a
  16. // sequence of key,value pairs. Each block may be compressed before
  17. // being stored in a file. The following enum describes which
  18. // compression method (if any) is used to compress a block.
  19. enum CompressionType {
  20. // NOTE: do not change the values of existing entries, as these are
  21. // part of the persistent format on disk.
  22. kNoCompression = 0x0,
  23. kSnappyCompression = 0x1
  24. };
  25. // Options to control the behavior of a database (passed to DB::Open)
  26. struct LEVELDB_EXPORT Options {
  27. // -------------------
  28. // Parameters that affect behavior
  29. // Comparator used to define the order of keys in the table.
  30. // Default: a comparator that uses lexicographic byte-wise ordering
  31. //
  32. // REQUIRES: The client must ensure that the comparator supplied
  33. // here has the same name and orders keys *exactly* the same as the
  34. // comparator provided to previous open calls on the same DB.
  35. const Comparator* comparator;
  36. // If true, the database will be created if it is missing.
  37. bool create_if_missing = false;
  38. // If true, an error is raised if the database already exists.
  39. bool error_if_exists = false;
  40. // If true, the implementation will do aggressive checking of the
  41. // data it is processing and will stop early if it detects any
  42. // errors. This may have unforeseen ramifications: for example, a
  43. // corruption of one DB entry may cause a large number of entries to
  44. // become unreadable or for the entire DB to become unopenable.
  45. bool paranoid_checks = false;
  46. // Use the specified object to interact with the environment,
  47. // e.g. to read/write files, schedule background work, etc.
  48. // Default: Env::Default()
  49. Env* env;
  50. // Any internal progress/error information generated by the db will
  51. // be written to info_log if it is non-null, or to a file stored
  52. // in the same directory as the DB contents if info_log is null.
  53. Logger* info_log = nullptr;
  54. // -------------------
  55. // Parameters that affect performance
  56. // Amount of data to build up in memory (backed by an unsorted log
  57. // on disk) before converting to a sorted on-disk file.
  58. //
  59. // Larger values increase performance, especially during bulk loads.
  60. // Up to two write buffers may be held in memory at the same time,
  61. // so you may wish to adjust this parameter to control memory usage.
  62. // Also, a larger write buffer will result in a longer recovery time
  63. // the next time the database is opened.
  64. size_t write_buffer_size = 4 * 1024 * 1024;
  65. // Number of open files that can be used by the DB. You may need to
  66. // increase this if your database has a large working set (budget
  67. // one open file per 2MB of working set).
  68. int max_open_files = 1000;
  69. // Control over blocks (user data is stored in a set of blocks, and
  70. // a block is the unit of reading from disk).
  71. // If non-null, use the specified cache for blocks.
  72. // If null, leveldb will automatically create and use an 8MB internal cache.
  73. Cache* block_cache = nullptr;
  74. // Approximate size of user data packed per block. Note that the
  75. // block size specified here corresponds to uncompressed data. The
  76. // actual size of the unit read from disk may be smaller if
  77. // compression is enabled. This parameter can be changed dynamically.
  78. size_t block_size = 4 * 1024;
  79. // Number of keys between restart points for delta encoding of keys.
  80. // This parameter can be changed dynamically. Most clients should
  81. // leave this parameter alone.
  82. int block_restart_interval = 16;
  83. // Leveldb will write up to this amount of bytes to a file before
  84. // switching to a new one.
  85. // Most clients should leave this parameter alone. However if your
  86. // filesystem is more efficient with larger files, you could
  87. // consider increasing the value. The downside will be longer
  88. // compactions and hence longer latency/performance hiccups.
  89. // Another reason to increase this parameter might be when you are
  90. // initially populating a large database.
  91. size_t max_file_size = 2 * 1024 * 1024;
  92. // Compress blocks using the specified compression algorithm. This
  93. // parameter can be changed dynamically.
  94. //
  95. // Default: kSnappyCompression, which gives lightweight but fast
  96. // compression.
  97. //
  98. // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
  99. // ~200-500MB/s compression
  100. // ~400-800MB/s decompression
  101. // Note that these speeds are significantly faster than most
  102. // persistent storage speeds, and therefore it is typically never
  103. // worth switching to kNoCompression. Even if the input data is
  104. // incompressible, the kSnappyCompression implementation will
  105. // efficiently detect that and will switch to uncompressed mode.
  106. CompressionType compression = kSnappyCompression;
  107. // EXPERIMENTAL: If true, append to existing MANIFEST and log files
  108. // when a database is opened. This can significantly speed up open.
  109. //
  110. // Default: currently false, but may become true later.
  111. bool reuse_logs = false;
  112. // If non-null, use the specified filter policy to reduce disk reads.
  113. // Many applications will benefit from passing the result of
  114. // NewBloomFilterPolicy() here.
  115. const FilterPolicy* filter_policy = nullptr;
  116. // Create an Options object with default values for all fields.
  117. Options();
  118. };
  119. // Options that control read operations
  120. struct LEVELDB_EXPORT ReadOptions {
  121. // If true, all data read from underlying storage will be
  122. // verified against corresponding checksums.
  123. bool verify_checksums = false;
  124. // Should the data read for this iteration be cached in memory?
  125. // Callers may wish to set this field to false for bulk scans.
  126. bool fill_cache = true;
  127. // If "snapshot" is non-null, read as of the supplied snapshot
  128. // (which must belong to the DB that is being read and which must
  129. // not have been released). If "snapshot" is null, use an implicit
  130. // snapshot of the state at the beginning of this read operation.
  131. const Snapshot* snapshot = nullptr;
  132. ReadOptions() = default;
  133. };
  134. // Options that control write operations
  135. struct LEVELDB_EXPORT WriteOptions {
  136. // If true, the write will be flushed from the operating system
  137. // buffer cache (by calling WritableFile::Sync()) before the write
  138. // is considered complete. If this flag is true, writes will be
  139. // slower.
  140. //
  141. // If this flag is false, and the machine crashes, some recent
  142. // writes may be lost. Note that if it is just the process that
  143. // crashes (i.e., the machine does not reboot), no writes will be
  144. // lost even if sync==false.
  145. //
  146. // In other words, a DB write with sync==false has similar
  147. // crash semantics as the "write()" system call. A DB write
  148. // with sync==true has similar crash semantics to a "write()"
  149. // system call followed by "fsync()".
  150. bool sync = false;
  151. WriteOptions() = default;
  152. };
  153. } // namespace leveldb
  154. #endif // STORAGE_LEVELDB_INCLUDE_OPTIONS_H_