本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.4KB

  1. package config
  2. import (
  3. "io/ioutil"
  4. "github.com/BurntSushi/toml"
  5. )
  6. type Size int
  7. const (
  8. DefaultAddr string = "127.0.0.1:6380"
  9. DefaultHttpAddr string = "127.0.0.1:11181"
  10. DefaultDBName string = "goleveldb"
  11. DefaultDataDir string = "./data"
  12. )
  13. const (
  14. MaxBinLogFileSize int = 1024 * 1024 * 1024
  15. MaxBinLogFileNum int = 10000
  16. DefaultBinLogFileSize int = MaxBinLogFileSize
  17. DefaultBinLogFileNum int = 10
  18. )
  19. type LevelDBConfig struct {
  20. Compression bool `toml:"compression"`
  21. BlockSize int `toml:"block_size"`
  22. WriteBufferSize int `toml:"write_buffer_size"`
  23. CacheSize int `toml:"cache_size"`
  24. MaxOpenFiles int `toml:"max_open_files"`
  25. }
  26. type LMDBConfig struct {
  27. MapSize int `toml:"map_size"`
  28. NoSync bool `toml:"nosync"`
  29. }
  30. type BinLogConfig struct {
  31. MaxFileSize int `toml:"max_file_size"`
  32. MaxFileNum int `toml:"max_file_num"`
  33. }
  34. type Config struct {
  35. DataDir string `toml:"data_dir"`
  36. DBName string `toml:"db_name"`
  37. LevelDB LevelDBConfig `toml:"leveldb"`
  38. LMDB LMDBConfig `toml:"lmdb"`
  39. BinLog BinLogConfig `toml:"binlog"`
  40. SlaveOf string `toml:"slaveof"`
  41. AccessLog string `toml:"access_log"`
  42. }
  43. func NewConfigWithFile(fileName string) (*Config, error) {
  44. data, err := ioutil.ReadFile(fileName)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return NewConfigWithData(data)
  49. }
  50. func NewConfigWithData(data []byte) (*Config, error) {
  51. cfg := NewConfigDefault()
  52. _, err := toml.Decode(string(data), cfg)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return cfg, nil
  57. }
  58. func NewConfigDefault() *Config {
  59. cfg := new(Config)
  60. cfg.DataDir = DefaultDataDir
  61. cfg.DBName = DefaultDBName
  62. // disable binlog
  63. cfg.BinLog.MaxFileNum = 0
  64. cfg.BinLog.MaxFileSize = 0
  65. // disable replication
  66. cfg.SlaveOf = ""
  67. // disable access log
  68. cfg.AccessLog = ""
  69. cfg.LMDB.MapSize = 20 * 1024 * 1024
  70. cfg.LMDB.NoSync = true
  71. return cfg
  72. }
  73. func (cfg *LevelDBConfig) Adjust() {
  74. if cfg.CacheSize <= 0 {
  75. cfg.CacheSize = 4 * 1024 * 1024
  76. }
  77. if cfg.BlockSize <= 0 {
  78. cfg.BlockSize = 4 * 1024
  79. }
  80. if cfg.WriteBufferSize <= 0 {
  81. cfg.WriteBufferSize = 4 * 1024 * 1024
  82. }
  83. if cfg.MaxOpenFiles < 1024 {
  84. cfg.MaxOpenFiles = 1024
  85. }
  86. }
  87. func (cfg *BinLogConfig) Adjust() {
  88. if cfg.MaxFileSize <= 0 {
  89. cfg.MaxFileSize = DefaultBinLogFileSize
  90. } else if cfg.MaxFileSize > MaxBinLogFileSize {
  91. cfg.MaxFileSize = MaxBinLogFileSize
  92. }
  93. if cfg.MaxFileNum <= 0 {
  94. cfg.MaxFileNum = DefaultBinLogFileNum
  95. } else if cfg.MaxFileNum > MaxBinLogFileNum {
  96. cfg.MaxFileNum = MaxBinLogFileNum
  97. }
  98. }
上海开阖软件有限公司 沪ICP备12045867号-1