change configuration

Config struct now not includes other configs, but shows all

People can see configuration clearly
This commit is contained in:
siddontang 2014-07-23 07:55:32 +08:00
parent edeee8f645
commit 7a763da082
10 changed files with 88 additions and 45 deletions

View File

@ -2,5 +2,6 @@
. ./dev.sh
go get -u github.com/siddontang/go-log/log
go get -u github.com/siddontang/go-snappy/snappy
go get github.com/siddontang/go-log/log
go get github.com/siddontang/go-snappy/snappy
go get github.com/siddontang/copier

View File

@ -2,13 +2,16 @@
"addr": "127.0.0.1:6380",
"data_dir": "/tmp/ledis_server",
"db": {
"data_db" : {
"compression": false,
"block_size": 32768,
"write_buffer_size": 67108864,
"cache_size": 524288000,
"max_open_files":1024
}
},
"binlog" : {
"use" : false,
"max_open_files" : 1024
},
"access_log" : "access.log"

19
ledis/config.go Normal file
View File

@ -0,0 +1,19 @@
package ledis
type Config struct {
DataDir string `json:"data_dir"`
DB struct {
Compression bool `json:"compression"`
BlockSize int `json:"block_size"`
WriteBufferSize int `json:"write_buffer_size"`
CacheSize int `json:"cache_size"`
MaxOpenFiles int `json:"max_open_files"`
} `json:"db"`
BinLog struct {
Use bool `json:"use"`
MaxFileSize int `json:"max_file_size"`
MaxFileNum int `json:"max_file_num"`
} `json:"binlog"`
}

View File

@ -3,6 +3,7 @@ package ledis
import (
"encoding/json"
"fmt"
"github.com/siddontang/copier"
"github.com/siddontang/go-log/log"
"github.com/siddontang/ledisdb/leveldb"
"path"
@ -10,18 +11,6 @@ import (
"time"
)
type Config struct {
DataDir string `json:"data_dir"`
//if you not set leveldb path, use data_dir/data
DataDB leveldb.Config `json:"data_db"`
UseBinLog bool `json:"use_bin_log"`
//if you not set bin log path, use data_dir/bin_log
BinLog BinLogConfig `json:"bin_log"`
}
type DB struct {
l *Ledis
@ -60,16 +49,31 @@ func OpenWithJsonConfig(configJson json.RawMessage) (*Ledis, error) {
return Open(&cfg)
}
func openDB(cfg *Config) (*leveldb.DB, error) {
dbPath := path.Join(cfg.DataDir, "data")
dbCfg := new(leveldb.Config)
copier.Copy(dbCfg, &cfg.DB)
dbCfg.Path = dbPath
return leveldb.Open(dbCfg)
}
func openBinLog(cfg *Config) (*BinLog, error) {
binLogPath := path.Join(cfg.DataDir, "bin_log")
c := new(BinLogConfig)
copier.Copy(c, &cfg.BinLog)
c.Path = binLogPath
return NewBinLog(c)
}
func Open(cfg *Config) (*Ledis, error) {
if len(cfg.DataDir) == 0 {
return nil, fmt.Errorf("must set correct data_dir")
}
if len(cfg.DataDB.Path) == 0 {
cfg.DataDB.Path = path.Join(cfg.DataDir, "data")
}
ldb, err := leveldb.Open(&cfg.DataDB)
ldb, err := openDB(cfg)
if err != nil {
return nil, err
}
@ -81,11 +85,8 @@ func Open(cfg *Config) (*Ledis, error) {
l.ldb = ldb
if cfg.UseBinLog {
if len(cfg.BinLog.Path) == 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log")
}
l.binlog, err = NewBinLog(&cfg.BinLog)
if cfg.BinLog.Use {
l.binlog, err = openBinLog(cfg)
if err != nil {
return nil, err
}

View File

@ -14,7 +14,7 @@ func getTestDB() *DB {
var d = []byte(`
{
"data_dir" : "/tmp/test_ledis",
"data_db" : {
"db" : {
"compression":true,
"block_size" : 32768,
"write_buffer_size" : 2097152,

View File

@ -35,8 +35,8 @@ func TestReplication(t *testing.T) {
master, err = OpenWithJsonConfig([]byte(`
{
"data_dir" : "/tmp/test_repl/master",
"use_bin_log" : true,
"bin_log" : {
"binlog" : {
"use" : true,
"max_file_size" : 50
}
}

View File

@ -2,6 +2,7 @@ package server
import (
"fmt"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/ledis"
"net"
"path"
@ -30,10 +31,6 @@ func NewApp(cfg *Config) (*App, error) {
return nil, fmt.Errorf("must set data_dir first")
}
if len(cfg.DB.DataDir) == 0 {
cfg.DB.DataDir = cfg.DataDir
}
app := new(App)
app.quit = make(chan struct{})
@ -66,7 +63,7 @@ func NewApp(cfg *Config) (*App, error) {
}
}
if app.ldb, err = ledis.Open(&cfg.DB); err != nil {
if app.ldb, err = openLedis(cfg); err != nil {
return nil, err
}
@ -75,6 +72,19 @@ func NewApp(cfg *Config) (*App, error) {
return app, nil
}
func openLedis(cfg *Config) (*ledis.Ledis, error) {
c := new(ledis.Config)
c.DataDir = cfg.DataDir
copier.Copy(&c.DB, &cfg.DB)
copier.Copy(&c.BinLog, &cfg.BinLog)
println("max open files", c.DB.MaxOpenFiles)
return ledis.Open(c)
}
func (app *App) Close() {
if app.closed {
return

View File

@ -35,12 +35,11 @@ func startTestApp() {
"data_dir" : "/tmp/testdb",
"addr" : "127.0.0.1:16380",
"db" : {
"data_db" : {
"compression":true,
"block_size" : 32768,
"write_buffer_size" : 2097152,
"cache_size" : 20971520
}
"compression":true,
"block_size" : 32768,
"write_buffer_size" : 2097152,
"cache_size" : 20971520,
"max_open_files" : 1024
}
}
`)

View File

@ -32,7 +32,7 @@ func TestReplication(t *testing.T) {
masterCfg := new(Config)
masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir)
masterCfg.Addr = "127.0.0.1:11182"
masterCfg.DB.UseBinLog = true
masterCfg.BinLog.Use = true
var master *App
var slave *App

View File

@ -2,7 +2,6 @@ package server
import (
"encoding/json"
"github.com/siddontang/ledisdb/ledis"
"io/ioutil"
)
@ -11,8 +10,19 @@ type Config struct {
DataDir string `json:"data_dir"`
//if you not set db path, use data_dir
DB ledis.Config `json:"db"`
DB struct {
Compression bool `json:"compression"`
BlockSize int `json:"block_size"`
WriteBufferSize int `json:"write_buffer_size"`
CacheSize int `json:"cache_size"`
MaxOpenFiles int `json:"max_open_files"`
} `json:"db"`
BinLog struct {
Use bool `json:"use"`
MaxFileSize int `json:"max_file_size"`
MaxFileNum int `json:"max_file_num"`
} `json:"binlog"`
//set slaveof to enable replication from master
//empty, no replication