2014-08-07 12:49:48 +04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-10-08 12:02:00 +04:00
|
|
|
"bytes"
|
2014-10-08 12:20:09 +04:00
|
|
|
"errors"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/BurntSushi/toml"
|
2014-10-08 12:02:00 +04:00
|
|
|
"github.com/siddontang/go/ioutil2"
|
|
|
|
"io"
|
2014-08-07 12:49:48 +04:00
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2014-10-08 12:20:09 +04:00
|
|
|
var (
|
|
|
|
ErrNoConfigFile = errors.New("Running without a config file")
|
|
|
|
)
|
2014-08-07 12:49:48 +04:00
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultAddr string = "127.0.0.1:6380"
|
|
|
|
DefaultHttpAddr string = "127.0.0.1:11181"
|
|
|
|
|
|
|
|
DefaultDBName string = "goleveldb"
|
|
|
|
|
|
|
|
DefaultDataDir string = "./var"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LevelDBConfig struct {
|
2014-08-27 11:37:42 +04:00
|
|
|
Compression bool `toml:"compression"`
|
|
|
|
BlockSize int `toml:"block_size"`
|
|
|
|
WriteBufferSize int `toml:"write_buffer_size"`
|
|
|
|
CacheSize int `toml:"cache_size"`
|
|
|
|
MaxOpenFiles int `toml:"max_open_files"`
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type LMDBConfig struct {
|
2014-08-27 11:37:42 +04:00
|
|
|
MapSize int `toml:"map_size"`
|
|
|
|
NoSync bool `toml:"nosync"`
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
type ReplicationConfig struct {
|
2014-10-05 13:24:44 +04:00
|
|
|
Path string `toml:"path"`
|
|
|
|
Sync bool `toml:"sync"`
|
|
|
|
WaitSyncTime int `toml:"wait_sync_time"`
|
|
|
|
WaitMaxSlaveAcks int `toml:"wait_max_slave_acks"`
|
2014-10-09 11:05:15 +04:00
|
|
|
ExpiredLogDays int `toml:"expired_log_days"`
|
|
|
|
SyncLog int `toml:"sync_log"`
|
2014-10-05 13:24:44 +04:00
|
|
|
Compression bool `toml:"compression"`
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 13:57:18 +04:00
|
|
|
type SnapshotConfig struct {
|
|
|
|
Path string `toml:"path"`
|
|
|
|
MaxNum int `toml:"max_num"`
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
type Config struct {
|
2014-10-08 12:20:09 +04:00
|
|
|
FileName string `toml:"-"`
|
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
Addr string `toml:"addr"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
HttpAddr string `toml:"http_addr"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
SlaveOf string `toml:"slaveof"`
|
|
|
|
|
2014-10-10 05:49:16 +04:00
|
|
|
Readonly bool `toml:readonly`
|
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
DataDir string `toml:"data_dir"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
DBName string `toml:"db_name"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-09-18 18:30:33 +04:00
|
|
|
DBPath string `toml:"db_path"`
|
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
LevelDB LevelDBConfig `toml:"leveldb"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
LMDB LMDBConfig `toml:"lmdb"`
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-08-27 11:37:42 +04:00
|
|
|
AccessLog string `toml:"access_log"`
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
UseReplication bool `toml:"use_replication"`
|
|
|
|
Replication ReplicationConfig `toml:"replication"`
|
2014-10-10 13:57:18 +04:00
|
|
|
|
|
|
|
Snapshot SnapshotConfig `toml:"snapshot"`
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigWithFile(fileName string) (*Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:20:09 +04:00
|
|
|
if cfg, err := NewConfigWithData(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
cfg.FileName = fileName
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigWithData(data []byte) (*Config, error) {
|
|
|
|
cfg := NewConfigDefault()
|
|
|
|
|
|
|
|
_, err := toml.Decode(string(data), cfg)
|
|
|
|
if err != nil {
|
2014-08-27 11:37:42 +04:00
|
|
|
return nil, err
|
2014-08-07 12:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigDefault() *Config {
|
|
|
|
cfg := new(Config)
|
|
|
|
|
|
|
|
cfg.Addr = DefaultAddr
|
|
|
|
cfg.HttpAddr = DefaultHttpAddr
|
|
|
|
|
|
|
|
cfg.DataDir = DefaultDataDir
|
|
|
|
|
|
|
|
cfg.DBName = DefaultDBName
|
|
|
|
|
|
|
|
cfg.SlaveOf = ""
|
2014-10-10 05:49:16 +04:00
|
|
|
cfg.Readonly = false
|
2014-08-07 12:49:48 +04:00
|
|
|
|
|
|
|
// disable access log
|
|
|
|
cfg.AccessLog = ""
|
|
|
|
|
2014-09-04 10:02:47 +04:00
|
|
|
cfg.LMDB.MapSize = 20 * 1024 * 1024
|
2014-08-25 10:18:23 +04:00
|
|
|
cfg.LMDB.NoSync = true
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
cfg.Replication.WaitSyncTime = 1
|
2014-09-27 05:10:08 +04:00
|
|
|
cfg.Replication.Compression = true
|
2014-10-05 13:24:44 +04:00
|
|
|
cfg.Replication.WaitMaxSlaveAcks = 2
|
2014-09-23 13:28:09 +04:00
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *LevelDBConfig) Adjust() {
|
|
|
|
if cfg.CacheSize <= 0 {
|
|
|
|
cfg.CacheSize = 4 * 1024 * 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.BlockSize <= 0 {
|
|
|
|
cfg.BlockSize = 4 * 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.WriteBufferSize <= 0 {
|
|
|
|
cfg.WriteBufferSize = 4 * 1024 * 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MaxOpenFiles < 1024 {
|
|
|
|
cfg.MaxOpenFiles = 1024
|
|
|
|
}
|
|
|
|
}
|
2014-10-08 12:02:00 +04:00
|
|
|
|
|
|
|
func (cfg *Config) Dump(w io.Writer) error {
|
|
|
|
e := toml.NewEncoder(w)
|
|
|
|
e.Indent = ""
|
|
|
|
return e.Encode(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) DumpFile(fileName string) error {
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
if err := cfg.Dump(&b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil2.WriteFileAtomic(fileName, b.Bytes(), 0644)
|
|
|
|
}
|
2014-10-08 12:20:09 +04:00
|
|
|
|
|
|
|
func (cfg *Config) Rewrite() error {
|
|
|
|
if len(cfg.FileName) == 0 {
|
|
|
|
return ErrNoConfigFile
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.DumpFile(cfg.FileName)
|
|
|
|
}
|