options.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. import (
  18. "github.com/dgraph-io/badger/options"
  19. )
  20. // NOTE: Keep the comments in the following to 75 chars width, so they
  21. // format nicely in godoc.
  22. // Options are params for creating DB object.
  23. //
  24. // This package provides DefaultOptions which contains options that should
  25. // work for most applications. Consider using that as a starting point before
  26. // customizing it for your own needs.
  27. type Options struct {
  28. // 1. Mandatory flags
  29. // -------------------
  30. // Directory to store the data in. Should exist and be writable.
  31. Dir string
  32. // Directory to store the value log in. Can be the same as Dir. Should
  33. // exist and be writable.
  34. ValueDir string
  35. // 2. Frequently modified flags
  36. // -----------------------------
  37. // Sync all writes to disk. Setting this to true would slow down data
  38. // loading significantly.
  39. SyncWrites bool
  40. // How should LSM tree be accessed.
  41. TableLoadingMode options.FileLoadingMode
  42. // How should value log be accessed.
  43. ValueLogLoadingMode options.FileLoadingMode
  44. // How many versions to keep per key.
  45. NumVersionsToKeep int
  46. // 3. Flags that user might want to review
  47. // ----------------------------------------
  48. // The following affect all levels of LSM tree.
  49. MaxTableSize int64 // Each table (or file) is at most this size.
  50. LevelSizeMultiplier int // Equals SizeOf(Li+1)/SizeOf(Li).
  51. MaxLevels int // Maximum number of levels of compaction.
  52. // If value size >= this threshold, only store value offsets in tree.
  53. ValueThreshold int
  54. // Maximum number of tables to keep in memory, before stalling.
  55. NumMemtables int
  56. // The following affect how we handle LSM tree L0.
  57. // Maximum number of Level 0 tables before we start compacting.
  58. NumLevelZeroTables int
  59. // If we hit this number of Level 0 tables, we will stall until L0 is
  60. // compacted away.
  61. NumLevelZeroTablesStall int
  62. // Maximum total size for L1.
  63. LevelOneSize int64
  64. // Size of single value log file.
  65. ValueLogFileSize int64
  66. // Max number of entries a value log file can hold (approximately). A value log file would be
  67. // determined by the smaller of its file size and max entries.
  68. ValueLogMaxEntries uint32
  69. // Number of compaction workers to run concurrently.
  70. NumCompactors int
  71. // Transaction start and commit timestamps are manaVgedTxns by end-user. This
  72. // is a private option used by ManagedDB.
  73. managedTxns bool
  74. // 4. Flags for testing purposes
  75. // ------------------------------
  76. DoNotCompact bool // Stops LSM tree from compactions.
  77. maxBatchCount int64 // max entries in batch
  78. maxBatchSize int64 // max batch size in bytes
  79. // Open the DB as read-only. With this set, multiple processes can
  80. // open the same Badger DB. Note: if the DB being opened had crashed
  81. // before and has vlog data to be replayed, ReadOnly will cause Open
  82. // to fail with an appropriate message.
  83. ReadOnly bool
  84. // Truncate value log to delete corrupt data, if any. Would not truncate if ReadOnly is set.
  85. Truncate bool
  86. }
  87. // DefaultOptions sets a list of recommended options for good performance.
  88. // Feel free to modify these to suit your needs.
  89. var DefaultOptions = Options{
  90. DoNotCompact: false,
  91. LevelOneSize: 256 << 20,
  92. LevelSizeMultiplier: 10,
  93. TableLoadingMode: options.LoadToRAM,
  94. ValueLogLoadingMode: options.MemoryMap,
  95. // table.MemoryMap to mmap() the tables.
  96. // table.Nothing to not preload the tables.
  97. MaxLevels: 7,
  98. MaxTableSize: 64 << 20,
  99. NumCompactors: 3,
  100. NumLevelZeroTables: 5,
  101. NumLevelZeroTablesStall: 10,
  102. NumMemtables: 5,
  103. SyncWrites: true,
  104. NumVersionsToKeep: 1,
  105. // Nothing to read/write value log using standard File I/O
  106. // MemoryMap to mmap() the value log files
  107. ValueLogFileSize: 1 << 30,
  108. ValueLogMaxEntries: 1000000,
  109. ValueThreshold: 32,
  110. Truncate: false,
  111. }
  112. // LSMOnlyOptions follows from DefaultOptions, but sets a higher ValueThreshold so values would
  113. // be colocated with the LSM tree, with value log largely acting as a write-ahead log only. These
  114. // options would reduce the disk usage of value log, and make Badger act like a typical LSM tree.
  115. var LSMOnlyOptions = Options{}
  116. func init() {
  117. LSMOnlyOptions = DefaultOptions
  118. LSMOnlyOptions.ValueThreshold = 65500 // Max value length which fits in uint16.
  119. LSMOnlyOptions.ValueLogFileSize = 64 << 20 // Allow easy space reclamation.
  120. LSMOnlyOptions.ValueLogLoadingMode = options.FileIO
  121. }