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 . ./dev.sh
go get -u github.com/siddontang/go-log/log go get github.com/siddontang/go-log/log
go get -u github.com/siddontang/go-snappy/snappy 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", "addr": "127.0.0.1:6380",
"data_dir": "/tmp/ledis_server", "data_dir": "/tmp/ledis_server",
"db": { "db": {
"data_db" : {
"compression": false, "compression": false,
"block_size": 32768, "block_size": 32768,
"write_buffer_size": 67108864, "write_buffer_size": 67108864,
"cache_size": 524288000, "cache_size": 524288000,
"max_open_files":1024 "max_open_files":1024
} },
"binlog" : {
"use" : false,
"max_open_files" : 1024
}, },
"access_log" : "access.log" "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 ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/siddontang/copier"
"github.com/siddontang/go-log/log" "github.com/siddontang/go-log/log"
"github.com/siddontang/ledisdb/leveldb" "github.com/siddontang/ledisdb/leveldb"
"path" "path"
@ -10,18 +11,6 @@ import (
"time" "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 { type DB struct {
l *Ledis l *Ledis
@ -60,16 +49,31 @@ func OpenWithJsonConfig(configJson json.RawMessage) (*Ledis, error) {
return Open(&cfg) 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) { func Open(cfg *Config) (*Ledis, error) {
if len(cfg.DataDir) == 0 { if len(cfg.DataDir) == 0 {
return nil, fmt.Errorf("must set correct data_dir") return nil, fmt.Errorf("must set correct data_dir")
} }
if len(cfg.DataDB.Path) == 0 { ldb, err := openDB(cfg)
cfg.DataDB.Path = path.Join(cfg.DataDir, "data")
}
ldb, err := leveldb.Open(&cfg.DataDB)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -81,11 +85,8 @@ func Open(cfg *Config) (*Ledis, error) {
l.ldb = ldb l.ldb = ldb
if cfg.UseBinLog { if cfg.BinLog.Use {
if len(cfg.BinLog.Path) == 0 { l.binlog, err = openBinLog(cfg)
cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log")
}
l.binlog, err = NewBinLog(&cfg.BinLog)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"net" "net"
"path" "path"
@ -30,10 +31,6 @@ func NewApp(cfg *Config) (*App, error) {
return nil, fmt.Errorf("must set data_dir first") return nil, fmt.Errorf("must set data_dir first")
} }
if len(cfg.DB.DataDir) == 0 {
cfg.DB.DataDir = cfg.DataDir
}
app := new(App) app := new(App)
app.quit = make(chan struct{}) 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 return nil, err
} }
@ -75,6 +72,19 @@ func NewApp(cfg *Config) (*App, error) {
return app, nil 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() { func (app *App) Close() {
if app.closed { if app.closed {
return return

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"github.com/siddontang/ledisdb/ledis"
"io/ioutil" "io/ioutil"
) )
@ -11,8 +10,19 @@ type Config struct {
DataDir string `json:"data_dir"` DataDir string `json:"data_dir"`
//if you not set db path, use data_dir DB struct {
DB ledis.Config `json:"db"` 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 //set slaveof to enable replication from master
//empty, no replication //empty, no replication