options.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package badger
  6. import (
  7. "fmt"
  8. "os"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/dgraph-io/badger/v4/options"
  14. "github.com/dgraph-io/badger/v4/table"
  15. "github.com/dgraph-io/badger/v4/y"
  16. "github.com/dgraph-io/ristretto/v2/z"
  17. )
  18. // Note: If you add a new option X make sure you also add a WithX method on Options.
  19. // Options are params for creating DB object.
  20. //
  21. // This package provides DefaultOptions which contains options that should
  22. // work for most applications. Consider using that as a starting point before
  23. // customizing it for your own needs.
  24. //
  25. // Each option X is documented on the WithX method.
  26. type Options struct {
  27. testOnlyOptions
  28. // Required options.
  29. Dir string
  30. ValueDir string
  31. // Usually modified options.
  32. SyncWrites bool
  33. NumVersionsToKeep int
  34. ReadOnly bool
  35. Logger Logger
  36. Compression options.CompressionType
  37. InMemory bool
  38. MetricsEnabled bool
  39. // Sets the Stream.numGo field
  40. NumGoroutines int
  41. // Fine tuning options.
  42. MemTableSize int64
  43. BaseTableSize int64
  44. BaseLevelSize int64
  45. LevelSizeMultiplier int
  46. TableSizeMultiplier int
  47. MaxLevels int
  48. VLogPercentile float64
  49. ValueThreshold int64
  50. NumMemtables int
  51. // Changing BlockSize across DB runs will not break badger. The block size is
  52. // read from the block index stored at the end of the table.
  53. BlockSize int
  54. BloomFalsePositive float64
  55. BlockCacheSize int64
  56. IndexCacheSize int64
  57. NumLevelZeroTables int
  58. NumLevelZeroTablesStall int
  59. ValueLogFileSize int64
  60. ValueLogMaxEntries uint32
  61. NumCompactors int
  62. CompactL0OnClose bool
  63. LmaxCompaction bool
  64. ZSTDCompressionLevel int
  65. // When set, checksum will be validated for each entry read from the value log file.
  66. VerifyValueChecksum bool
  67. // Encryption related options.
  68. EncryptionKey []byte // encryption key
  69. EncryptionKeyRotationDuration time.Duration // key rotation duration
  70. // BypassLockGuard will bypass the lock guard on badger. Bypassing lock
  71. // guard can cause data corruption if multiple badger instances are using
  72. // the same directory. Use this options with caution.
  73. BypassLockGuard bool
  74. // ChecksumVerificationMode decides when db should verify checksums for SSTable blocks.
  75. ChecksumVerificationMode options.ChecksumVerificationMode
  76. // DetectConflicts determines whether the transactions would be checked for
  77. // conflicts. The transactions can be processed at a higher rate when
  78. // conflict detection is disabled.
  79. DetectConflicts bool
  80. // NamespaceOffset specifies the offset from where the next 8 bytes contains the namespace.
  81. NamespaceOffset int
  82. // Magic version used by the application using badger to ensure that it doesn't open the DB
  83. // with incompatible data format.
  84. ExternalMagicVersion uint16
  85. // Transaction start and commit timestamps are managed by end-user.
  86. // This is only useful for databases built on top of Badger (like Dgraph).
  87. // Not recommended for most users.
  88. managedTxns bool
  89. // 4. Flags for testing purposes
  90. // ------------------------------
  91. maxBatchCount int64 // max entries in batch
  92. maxBatchSize int64 // max batch size in bytes
  93. maxValueThreshold float64
  94. }
  95. // DefaultOptions sets a list of recommended options for good performance.
  96. // Feel free to modify these to suit your needs with the WithX methods.
  97. func DefaultOptions(path string) Options {
  98. return Options{
  99. Dir: path,
  100. ValueDir: path,
  101. MemTableSize: 64 << 20,
  102. BaseTableSize: 2 << 20,
  103. BaseLevelSize: 10 << 20,
  104. TableSizeMultiplier: 2,
  105. LevelSizeMultiplier: 10,
  106. MaxLevels: 7,
  107. NumGoroutines: 8,
  108. MetricsEnabled: true,
  109. NumCompactors: 4, // Run at least 2 compactors. Zero-th compactor prioritizes L0.
  110. NumLevelZeroTables: 5,
  111. NumLevelZeroTablesStall: 15,
  112. NumMemtables: 5,
  113. BloomFalsePositive: 0.01,
  114. BlockSize: 4 * 1024,
  115. SyncWrites: false,
  116. NumVersionsToKeep: 1,
  117. CompactL0OnClose: false,
  118. VerifyValueChecksum: false,
  119. Compression: options.Snappy,
  120. BlockCacheSize: 256 << 20,
  121. IndexCacheSize: 0,
  122. // The following benchmarks were done on a 4 KB block size (default block size). The
  123. // compression is ratio supposed to increase with increasing compression level but since the
  124. // input for compression algorithm is small (4 KB), we don't get significant benefit at
  125. // level 3.
  126. // NOTE: The benchmarks are with DataDog ZSTD that requires CGO. Hence, no longer valid.
  127. // no_compression-16 10 502848865 ns/op 165.46 MB/s -
  128. // zstd_compression/level_1-16 7 739037966 ns/op 112.58 MB/s 2.93
  129. // zstd_compression/level_3-16 7 756950250 ns/op 109.91 MB/s 2.72
  130. // zstd_compression/level_15-16 1 11135686219 ns/op 7.47 MB/s 4.38
  131. // Benchmark code can be found in table/builder_test.go file
  132. ZSTDCompressionLevel: 1,
  133. // (2^30 - 1)*2 when mmapping < 2^31 - 1, max int32.
  134. // -1 so 2*ValueLogFileSize won't overflow on 32-bit systems.
  135. ValueLogFileSize: 1<<30 - 1,
  136. ValueLogMaxEntries: 1000000,
  137. VLogPercentile: 0.0,
  138. ValueThreshold: maxValueThreshold,
  139. Logger: defaultLogger(INFO),
  140. EncryptionKey: []byte{},
  141. EncryptionKeyRotationDuration: 10 * 24 * time.Hour, // Default 10 days.
  142. DetectConflicts: true,
  143. NamespaceOffset: -1,
  144. }
  145. }
  146. func buildTableOptions(db *DB) table.Options {
  147. opt := db.opt
  148. dk, err := db.registry.LatestDataKey()
  149. y.Check(err)
  150. return table.Options{
  151. ReadOnly: opt.ReadOnly,
  152. MetricsEnabled: db.opt.MetricsEnabled,
  153. TableSize: uint64(opt.BaseTableSize),
  154. BlockSize: opt.BlockSize,
  155. BloomFalsePositive: opt.BloomFalsePositive,
  156. ChkMode: opt.ChecksumVerificationMode,
  157. Compression: opt.Compression,
  158. ZSTDCompressionLevel: opt.ZSTDCompressionLevel,
  159. BlockCache: db.blockCache,
  160. IndexCache: db.indexCache,
  161. AllocPool: db.allocPool,
  162. DataKey: dk,
  163. }
  164. }
  165. const (
  166. maxValueThreshold = (1 << 20) // 1 MB
  167. )
  168. // LSMOnlyOptions follows from DefaultOptions, but sets a higher ValueThreshold
  169. // so values would be collocated with the LSM tree, with value log largely acting
  170. // as a write-ahead log only. These options would reduce the disk usage of value
  171. // log, and make Badger act more like a typical LSM tree.
  172. func LSMOnlyOptions(path string) Options {
  173. // Let's not set any other options, because they can cause issues with the
  174. // size of key-value a user can pass to Badger. For e.g., if we set
  175. // ValueLogFileSize to 64MB, a user can't pass a value more than that.
  176. // Setting it to ValueLogMaxEntries to 1000, can generate too many files.
  177. // These options are better configured on a usage basis, than broadly here.
  178. // The ValueThreshold is the most important setting a user needs to do to
  179. // achieve a heavier usage of LSM tree.
  180. // NOTE: If a user does not want to set 64KB as the ValueThreshold because
  181. // of performance reasons, 1KB would be a good option too, allowing
  182. // values smaller than 1KB to be collocated with the keys in the LSM tree.
  183. return DefaultOptions(path).WithValueThreshold(maxValueThreshold /* 1 MB */)
  184. }
  185. // parseCompression returns badger.compressionType and compression level given compression string
  186. // of format compression-type:compression-level
  187. func parseCompression(cStr string) (options.CompressionType, int, error) {
  188. cStrSplit := strings.Split(cStr, ":")
  189. cType := cStrSplit[0]
  190. level := 3
  191. var err error
  192. if len(cStrSplit) == 2 {
  193. level, err = strconv.Atoi(cStrSplit[1])
  194. y.Check(err)
  195. if level <= 0 {
  196. return 0, 0,
  197. fmt.Errorf("ERROR: compression level(%v) must be greater than zero", level)
  198. }
  199. } else if len(cStrSplit) > 2 {
  200. return 0, 0, fmt.Errorf("ERROR: Invalid badger.compression argument")
  201. }
  202. switch cType {
  203. case "zstd":
  204. return options.ZSTD, level, nil
  205. case "snappy":
  206. return options.Snappy, 0, nil
  207. case "none":
  208. return options.None, 0, nil
  209. }
  210. return 0, 0, fmt.Errorf("ERROR: compression type (%s) invalid", cType)
  211. }
  212. // generateSuperFlag generates an identical SuperFlag string from the provided Options.
  213. func generateSuperFlag(options Options) string {
  214. superflag := ""
  215. v := reflect.ValueOf(&options).Elem()
  216. optionsStruct := v.Type()
  217. for i := 0; i < v.NumField(); i++ {
  218. if field := v.Field(i); field.CanInterface() {
  219. name := strings.ToLower(optionsStruct.Field(i).Name)
  220. kind := v.Field(i).Kind()
  221. switch kind {
  222. case reflect.Bool:
  223. superflag += name + "="
  224. superflag += fmt.Sprintf("%v; ", field.Bool())
  225. case reflect.Int, reflect.Int64:
  226. superflag += name + "="
  227. superflag += fmt.Sprintf("%v; ", field.Int())
  228. case reflect.Uint32, reflect.Uint64:
  229. superflag += name + "="
  230. superflag += fmt.Sprintf("%v; ", field.Uint())
  231. case reflect.Float64:
  232. superflag += name + "="
  233. superflag += fmt.Sprintf("%v; ", field.Float())
  234. case reflect.String:
  235. superflag += name + "="
  236. superflag += fmt.Sprintf("%v; ", field.String())
  237. default:
  238. continue
  239. }
  240. }
  241. }
  242. return superflag
  243. }
  244. // FromSuperFlag fills Options fields for each flag within the superflag. For
  245. // example, replacing the default Options.NumGoroutines:
  246. //
  247. // options := FromSuperFlag("numgoroutines=4", DefaultOptions(""))
  248. //
  249. // It's important to note that if you pass an empty Options struct, FromSuperFlag
  250. // will not fill it with default values. FromSuperFlag only writes to the fields
  251. // present within the superflag string (case insensitive).
  252. //
  253. // It specially handles compression subflag.
  254. // Valid options are {none,snappy,zstd:<level>}
  255. // Example: compression=zstd:3;
  256. // Unsupported: Options.Logger, Options.EncryptionKey
  257. func (opt Options) FromSuperFlag(superflag string) Options {
  258. // currentOptions act as a default value for the options superflag.
  259. currentOptions := generateSuperFlag(opt)
  260. currentOptions += "compression=;"
  261. flags := z.NewSuperFlag(superflag).MergeAndCheckDefault(currentOptions)
  262. v := reflect.ValueOf(&opt).Elem()
  263. optionsStruct := v.Type()
  264. for i := 0; i < v.NumField(); i++ {
  265. // only iterate over exported fields
  266. if field := v.Field(i); field.CanInterface() {
  267. // z.SuperFlag stores keys as lowercase, keep everything case
  268. // insensitive
  269. name := strings.ToLower(optionsStruct.Field(i).Name)
  270. if name == "compression" {
  271. // We will specially handle this later. Skip it here.
  272. continue
  273. }
  274. kind := v.Field(i).Kind()
  275. switch kind {
  276. case reflect.Bool:
  277. field.SetBool(flags.GetBool(name))
  278. case reflect.Int, reflect.Int64:
  279. field.SetInt(flags.GetInt64(name))
  280. case reflect.Uint32, reflect.Uint64:
  281. field.SetUint(flags.GetUint64(name))
  282. case reflect.Float64:
  283. field.SetFloat(flags.GetFloat64(name))
  284. case reflect.String:
  285. field.SetString(flags.GetString(name))
  286. }
  287. }
  288. }
  289. // Only update the options for special flags that were present in the input superflag.
  290. inputFlag := z.NewSuperFlag(superflag)
  291. if inputFlag.Has("compression") {
  292. ctype, clevel, err := parseCompression(flags.GetString("compression"))
  293. switch err {
  294. case nil:
  295. opt.Compression = ctype
  296. opt.ZSTDCompressionLevel = clevel
  297. default:
  298. ctype = options.CompressionType(flags.GetUint32("compression"))
  299. y.AssertTruef(ctype <= 2, "ERROR: Invalid format or compression type. Got: %s",
  300. flags.GetString("compression"))
  301. opt.Compression = ctype
  302. }
  303. }
  304. return opt
  305. }
  306. // WithDir returns a new Options value with Dir set to the given value.
  307. //
  308. // Dir is the path of the directory where key data will be stored in.
  309. // If it doesn't exist, Badger will try to create it for you.
  310. // This is set automatically to be the path given to `DefaultOptions`.
  311. func (opt Options) WithDir(val string) Options {
  312. opt.Dir = val
  313. return opt
  314. }
  315. // WithValueDir returns a new Options value with ValueDir set to the given value.
  316. //
  317. // ValueDir is the path of the directory where value data will be stored in.
  318. // If it doesn't exist, Badger will try to create it for you.
  319. // This is set automatically to be the path given to `DefaultOptions`.
  320. func (opt Options) WithValueDir(val string) Options {
  321. opt.ValueDir = val
  322. return opt
  323. }
  324. // WithSyncWrites returns a new Options value with SyncWrites set to the given value.
  325. //
  326. // Badger does all writes via mmap. So, all writes can survive process crashes or k8s environments
  327. // with SyncWrites set to false.
  328. //
  329. // When set to true, Badger would call an additional msync after writes to flush mmap buffer over to
  330. // disk to survive hard reboots. Most users of Badger should not need to do this.
  331. //
  332. // The default value of SyncWrites is false.
  333. func (opt Options) WithSyncWrites(val bool) Options {
  334. opt.SyncWrites = val
  335. return opt
  336. }
  337. // WithNumVersionsToKeep returns a new Options value with NumVersionsToKeep set to the given value.
  338. //
  339. // NumVersionsToKeep sets how many versions to keep per key at most.
  340. //
  341. // The default value of NumVersionsToKeep is 1.
  342. func (opt Options) WithNumVersionsToKeep(val int) Options {
  343. opt.NumVersionsToKeep = val
  344. return opt
  345. }
  346. // WithNumGoroutines sets the number of goroutines to be used in Stream.
  347. //
  348. // The default value of NumGoroutines is 8.
  349. func (opt Options) WithNumGoroutines(val int) Options {
  350. opt.NumGoroutines = val
  351. return opt
  352. }
  353. // WithReadOnly returns a new Options value with ReadOnly set to the given value.
  354. //
  355. // When ReadOnly is true the DB will be opened on read-only mode.
  356. // Multiple processes can open the same Badger DB.
  357. // Note: if the DB being opened had crashed before and has vlog data to be replayed,
  358. // ReadOnly will cause Open to fail with an appropriate message.
  359. //
  360. // The default value of ReadOnly is false.
  361. func (opt Options) WithReadOnly(val bool) Options {
  362. opt.ReadOnly = val
  363. return opt
  364. }
  365. // WithMetricsEnabled returns a new Options value with MetricsEnabled set to the given value.
  366. //
  367. // When MetricsEnabled is set to false, then the DB will be opened and no badger metrics
  368. // will be logged. Metrics are defined in metric.go file.
  369. //
  370. // This flag is useful for use cases like in Dgraph where we open temporary badger instances to
  371. // index data. In those cases we don't want badger metrics to be polluted with the noise from
  372. // those temporary instances.
  373. //
  374. // Default value is set to true
  375. func (opt Options) WithMetricsEnabled(val bool) Options {
  376. opt.MetricsEnabled = val
  377. return opt
  378. }
  379. // WithLogger returns a new Options value with Logger set to the given value.
  380. //
  381. // Logger provides a way to configure what logger each value of badger.DB uses.
  382. //
  383. // The default value of Logger writes to stderr using the log package from the Go standard library.
  384. func (opt Options) WithLogger(val Logger) Options {
  385. opt.Logger = val
  386. return opt
  387. }
  388. // WithLoggingLevel returns a new Options value with logging level of the
  389. // default logger set to the given value.
  390. // LoggingLevel sets the level of logging. It should be one of DEBUG, INFO,
  391. // WARNING or ERROR levels.
  392. //
  393. // The default value of LoggingLevel is INFO.
  394. func (opt Options) WithLoggingLevel(val loggingLevel) Options {
  395. opt.Logger = defaultLogger(val)
  396. return opt
  397. }
  398. // WithBaseTableSize returns a new Options value with BaseTableSize set to the given value.
  399. //
  400. // BaseTableSize sets the maximum size in bytes for LSM table or file in the base level.
  401. //
  402. // The default value of BaseTableSize is 2MB.
  403. func (opt Options) WithBaseTableSize(val int64) Options {
  404. opt.BaseTableSize = val
  405. return opt
  406. }
  407. // WithLevelSizeMultiplier returns a new Options value with LevelSizeMultiplier set to the given
  408. // value.
  409. //
  410. // LevelSizeMultiplier sets the ratio between the maximum sizes of contiguous levels in the LSM.
  411. // Once a level grows to be larger than this ratio allowed, the compaction process will be
  412. // triggered.
  413. //
  414. // The default value of LevelSizeMultiplier is 10.
  415. func (opt Options) WithLevelSizeMultiplier(val int) Options {
  416. opt.LevelSizeMultiplier = val
  417. return opt
  418. }
  419. // WithMaxLevels returns a new Options value with MaxLevels set to the given value.
  420. //
  421. // Maximum number of levels of compaction allowed in the LSM.
  422. //
  423. // The default value of MaxLevels is 7.
  424. func (opt Options) WithMaxLevels(val int) Options {
  425. opt.MaxLevels = val
  426. return opt
  427. }
  428. // WithValueThreshold returns a new Options value with ValueThreshold set to the given value.
  429. //
  430. // ValueThreshold sets the threshold used to decide whether a value is stored directly in the LSM
  431. // tree or separately in the log value files.
  432. //
  433. // The default value of ValueThreshold is 1 MB, and LSMOnlyOptions sets it to maxValueThreshold
  434. // which is set to 1 MB too.
  435. func (opt Options) WithValueThreshold(val int64) Options {
  436. opt.ValueThreshold = val
  437. return opt
  438. }
  439. // WithVLogPercentile returns a new Options value with ValLogPercentile set to given value.
  440. //
  441. // VLogPercentile with 0.0 means no dynamic thresholding is enabled.
  442. // MinThreshold value will always act as the value threshold.
  443. //
  444. // VLogPercentile with value 0.99 means 99 percentile of value will be put in LSM tree
  445. // and only 1 percent in vlog. The value threshold will be dynamically updated within the range of
  446. // [ValueThreshold, Options.maxValueThreshold]
  447. //
  448. // # Say VLogPercentile with 1.0 means threshold will eventually set to Options.maxValueThreshold
  449. //
  450. // The default value of VLogPercentile is 0.0.
  451. func (opt Options) WithVLogPercentile(t float64) Options {
  452. opt.VLogPercentile = t
  453. return opt
  454. }
  455. // WithNumMemtables returns a new Options value with NumMemtables set to the given value.
  456. //
  457. // NumMemtables sets the maximum number of tables to keep in memory before stalling.
  458. //
  459. // The default value of NumMemtables is 5.
  460. func (opt Options) WithNumMemtables(val int) Options {
  461. opt.NumMemtables = val
  462. return opt
  463. }
  464. // WithMemTableSize returns a new Options value with MemTableSize set to the given value.
  465. //
  466. // MemTableSize sets the maximum size in bytes for memtable table.
  467. //
  468. // The default value of MemTableSize is 64MB.
  469. func (opt Options) WithMemTableSize(val int64) Options {
  470. opt.MemTableSize = val
  471. return opt
  472. }
  473. // WithBloomFalsePositive returns a new Options value with BloomFalsePositive set
  474. // to the given value.
  475. //
  476. // BloomFalsePositive sets the false positive probability of the bloom filter in any SSTable.
  477. // Before reading a key from table, the bloom filter is checked for key existence.
  478. // BloomFalsePositive might impact read performance of DB. Lower BloomFalsePositive value might
  479. // consume more memory.
  480. //
  481. // The default value of BloomFalsePositive is 0.01.
  482. //
  483. // Setting this to 0 disables the bloom filter completely.
  484. func (opt Options) WithBloomFalsePositive(val float64) Options {
  485. opt.BloomFalsePositive = val
  486. return opt
  487. }
  488. // WithBlockSize returns a new Options value with BlockSize set to the given value.
  489. //
  490. // BlockSize sets the size of any block in SSTable. SSTable is divided into multiple blocks
  491. // internally. Each block is compressed using prefix diff encoding.
  492. //
  493. // The default value of BlockSize is 4KB.
  494. func (opt Options) WithBlockSize(val int) Options {
  495. opt.BlockSize = val
  496. return opt
  497. }
  498. // WithNumLevelZeroTables sets the maximum number of Level 0 tables before compaction starts.
  499. //
  500. // The default value of NumLevelZeroTables is 5.
  501. func (opt Options) WithNumLevelZeroTables(val int) Options {
  502. opt.NumLevelZeroTables = val
  503. return opt
  504. }
  505. // WithNumLevelZeroTablesStall sets the number of Level 0 tables that once reached causes the DB to
  506. // stall until compaction succeeds.
  507. //
  508. // The default value of NumLevelZeroTablesStall is 15.
  509. func (opt Options) WithNumLevelZeroTablesStall(val int) Options {
  510. opt.NumLevelZeroTablesStall = val
  511. return opt
  512. }
  513. // WithBaseLevelSize sets the maximum size target for the base level.
  514. //
  515. // The default value is 10MB.
  516. func (opt Options) WithBaseLevelSize(val int64) Options {
  517. opt.BaseLevelSize = val
  518. return opt
  519. }
  520. // WithValueLogFileSize sets the maximum size of a single value log file.
  521. //
  522. // The default value of ValueLogFileSize is 1GB.
  523. func (opt Options) WithValueLogFileSize(val int64) Options {
  524. opt.ValueLogFileSize = val
  525. return opt
  526. }
  527. // WithValueLogMaxEntries sets the maximum number of entries a value log file
  528. // can hold approximately. A actual size limit of a value log file is the
  529. // minimum of ValueLogFileSize and ValueLogMaxEntries.
  530. //
  531. // The default value of ValueLogMaxEntries is one million (1000000).
  532. func (opt Options) WithValueLogMaxEntries(val uint32) Options {
  533. opt.ValueLogMaxEntries = val
  534. return opt
  535. }
  536. // WithNumCompactors sets the number of compaction workers to run concurrently. Setting this to
  537. // zero stops compactions, which could eventually cause writes to block forever.
  538. //
  539. // The default value of NumCompactors is 4. One is dedicated just for L0 and L1.
  540. func (opt Options) WithNumCompactors(val int) Options {
  541. opt.NumCompactors = val
  542. return opt
  543. }
  544. // WithCompactL0OnClose determines whether Level 0 should be compacted before closing the DB. This
  545. // ensures that both reads and writes are efficient when the DB is opened later.
  546. //
  547. // The default value of CompactL0OnClose is false.
  548. func (opt Options) WithCompactL0OnClose(val bool) Options {
  549. opt.CompactL0OnClose = val
  550. return opt
  551. }
  552. // WithEncryptionKey is used to encrypt the data with AES. Type of AES is used based on the key
  553. // size. For example 16 bytes will use AES-128. 24 bytes will use AES-192. 32 bytes will
  554. // use AES-256.
  555. func (opt Options) WithEncryptionKey(key []byte) Options {
  556. opt.EncryptionKey = key
  557. return opt
  558. }
  559. // WithEncryptionKeyRotationDuration returns new Options value with the duration set to
  560. // the given value.
  561. //
  562. // Key Registry will use this duration to create new keys. If the previous generated
  563. // key exceed the given duration. Then the key registry will create new key.
  564. // The default value is set to 10 days.
  565. func (opt Options) WithEncryptionKeyRotationDuration(d time.Duration) Options {
  566. opt.EncryptionKeyRotationDuration = d
  567. return opt
  568. }
  569. // WithCompression is used to enable or disable compression. When compression is enabled, every
  570. // block will be compressed using the specified algorithm. This option doesn't affect existing
  571. // tables. Only the newly created tables will be compressed.
  572. //
  573. // The default compression algorithm used is snappy. Compression is enabled by default.
  574. func (opt Options) WithCompression(cType options.CompressionType) Options {
  575. opt.Compression = cType
  576. return opt
  577. }
  578. // WithVerifyValueChecksum is used to set VerifyValueChecksum. When VerifyValueChecksum is set to
  579. // true, checksum will be verified for every entry read from the value log. If the value is stored
  580. // in SST (value size less than value threshold) then the checksum validation will not be done.
  581. //
  582. // The default value of VerifyValueChecksum is False.
  583. func (opt Options) WithVerifyValueChecksum(val bool) Options {
  584. opt.VerifyValueChecksum = val
  585. return opt
  586. }
  587. // WithChecksumVerificationMode returns a new Options value with ChecksumVerificationMode set to
  588. // the given value.
  589. //
  590. // ChecksumVerificationMode indicates when the db should verify checksums for SSTable blocks.
  591. //
  592. // The default value of VerifyValueChecksum is options.NoVerification.
  593. func (opt Options) WithChecksumVerificationMode(cvMode options.ChecksumVerificationMode) Options {
  594. opt.ChecksumVerificationMode = cvMode
  595. return opt
  596. }
  597. // WithBlockCacheSize returns a new Options value with BlockCacheSize set to the given value.
  598. //
  599. // This value specifies how much data cache should hold in memory. A small size
  600. // of cache means lower memory consumption and lookups/iterations would take
  601. // longer. It is recommended to use a cache if you're using compression or encryption.
  602. // If compression and encryption both are disabled, adding a cache will lead to
  603. // unnecessary overhead which will affect the read performance. Setting size to
  604. // zero disables the cache altogether.
  605. //
  606. // Default value of BlockCacheSize is 256 MB.
  607. func (opt Options) WithBlockCacheSize(size int64) Options {
  608. opt.BlockCacheSize = size
  609. return opt
  610. }
  611. // WithInMemory returns a new Options value with Inmemory mode set to the given value.
  612. //
  613. // When badger is running in InMemory mode, everything is stored in memory. No value/sst files are
  614. // created. In case of a crash all data will be lost.
  615. func (opt Options) WithInMemory(b bool) Options {
  616. opt.InMemory = b
  617. return opt
  618. }
  619. // WithZSTDCompressionLevel returns a new Options value with ZSTDCompressionLevel set
  620. // to the given value.
  621. //
  622. // The ZSTD compression algorithm supports 20 compression levels. The higher the compression
  623. // level, the better is the compression ratio but lower is the performance. Lower levels
  624. // have better performance and higher levels have better compression ratios.
  625. // We recommend using level 1 ZSTD Compression Level. Any level higher than 1 seems to
  626. // deteriorate badger's performance.
  627. // The following benchmarks were done on a 4 KB block size (default block size). The compression is
  628. // ratio supposed to increase with increasing compression level but since the input for compression
  629. // algorithm is small (4 KB), we don't get significant benefit at level 3. It is advised to write
  630. // your own benchmarks before choosing a compression algorithm or level.
  631. //
  632. // NOTE: The benchmarks are with DataDog ZSTD that requires CGO. Hence, no longer valid.
  633. // no_compression-16 10 502848865 ns/op 165.46 MB/s -
  634. // zstd_compression/level_1-16 7 739037966 ns/op 112.58 MB/s 2.93
  635. // zstd_compression/level_3-16 7 756950250 ns/op 109.91 MB/s 2.72
  636. // zstd_compression/level_15-16 1 11135686219 ns/op 7.47 MB/s 4.38
  637. // Benchmark code can be found in table/builder_test.go file
  638. func (opt Options) WithZSTDCompressionLevel(cLevel int) Options {
  639. opt.ZSTDCompressionLevel = cLevel
  640. return opt
  641. }
  642. // WithBypassLockGuard returns a new Options value with BypassLockGuard
  643. // set to the given value.
  644. //
  645. // When BypassLockGuard option is set, badger will not acquire a lock on the
  646. // directory. This could lead to data corruption if multiple badger instances
  647. // write to the same data directory. Use this option with caution.
  648. //
  649. // The default value of BypassLockGuard is false.
  650. func (opt Options) WithBypassLockGuard(b bool) Options {
  651. opt.BypassLockGuard = b
  652. return opt
  653. }
  654. // WithIndexCacheSize returns a new Options value with IndexCacheSize set to
  655. // the given value.
  656. //
  657. // This value specifies how much memory should be used by table indices. These
  658. // indices include the block offsets and the bloomfilters. Badger uses bloom
  659. // filters to speed up lookups. Each table has its own bloom
  660. // filter and each bloom filter is approximately of 5 MB.
  661. //
  662. // Zero value for IndexCacheSize means all the indices will be kept in
  663. // memory and the cache is disabled.
  664. //
  665. // The default value of IndexCacheSize is 0 which means all indices are kept in
  666. // memory.
  667. func (opt Options) WithIndexCacheSize(size int64) Options {
  668. opt.IndexCacheSize = size
  669. return opt
  670. }
  671. // WithDetectConflicts returns a new Options value with DetectConflicts set to the given value.
  672. //
  673. // Detect conflicts options determines if the transactions would be checked for
  674. // conflicts before committing them. When this option is set to false
  675. // (detectConflicts=false) badger can process transactions at a higher rate.
  676. // Setting this options to false might be useful when the user application
  677. // deals with conflict detection and resolution.
  678. //
  679. // The default value of Detect conflicts is True.
  680. func (opt Options) WithDetectConflicts(b bool) Options {
  681. opt.DetectConflicts = b
  682. return opt
  683. }
  684. // WithNamespaceOffset returns a new Options value with NamespaceOffset set to the given value. DB
  685. // will expect the namespace in each key at the 8 bytes starting from NamespaceOffset. A negative
  686. // value means that namespace is not stored in the key.
  687. //
  688. // The default value for NamespaceOffset is -1.
  689. func (opt Options) WithNamespaceOffset(offset int) Options {
  690. opt.NamespaceOffset = offset
  691. return opt
  692. }
  693. // WithExternalMagic returns a new Options value with ExternalMagicVersion set to the given value.
  694. // The DB would fail to start if either the internal or the external magic number fails validated.
  695. func (opt Options) WithExternalMagic(magic uint16) Options {
  696. opt.ExternalMagicVersion = magic
  697. return opt
  698. }
  699. func (opt Options) getFileFlags() int {
  700. var flags int
  701. // opt.SyncWrites would be using msync to sync. All writes go through mmap.
  702. if opt.ReadOnly {
  703. flags |= os.O_RDONLY
  704. } else {
  705. flags |= os.O_RDWR
  706. }
  707. return flags
  708. }