options.go 28 KB

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