adjust config

This commit is contained in:
siddontang 2014-06-06 11:25:13 +08:00
parent 5d466fb82f
commit 68c63c416a
6 changed files with 29 additions and 6 deletions

View File

@ -1,8 +1,8 @@
{ {
"addr": "127.0.0.1:6380", "addr": "127.0.0.1:6380",
"data_dir": "/tmp/ledis_server",
"db": { "db": {
"data_db" : { "data_db" : {
"path": "/tmp/ledisdb",
"compression": false, "compression": false,
"block_size": 32768, "block_size": 32768,
"write_buffer_size": 67108864, "write_buffer_size": 67108864,

View File

@ -13,11 +13,13 @@ type Config struct {
DataDir string `json:"data_dir"` DataDir string `json:"data_dir"`
//data_db path is data_dir/data //data_db path is data_dir/data
//if you not set leveldb path, use data_dir/data
DataDB leveldb.Config `json:"data_db"` DataDB leveldb.Config `json:"data_db"`
//binlog path is data_dir/binlog //binlog path is data_dir/binlog
//you muse set binlog name to enable binlog //you muse set binlog name to enable binlog
BinLog replication.BinLogConfig `json:"binlog"` //if you not set bin log path, use data_dir/bin_log
BinLog replication.BinLogConfig `json:"bin_log"`
} }
type DB struct { type DB struct {
@ -61,7 +63,10 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) {
return nil, fmt.Errorf("must set correct data_dir") return nil, fmt.Errorf("must set correct data_dir")
} }
cfg.DataDB.Path = path.Join(cfg.DataDir, "data") if len(cfg.DataDB.Path) == 0 {
cfg.DataDB.Path = path.Join(cfg.DataDir, "data")
}
ldb, err := leveldb.OpenWithConfig(&cfg.DataDB) ldb, err := leveldb.OpenWithConfig(&cfg.DataDB)
if err != nil { if err != nil {
return nil, err return nil, err
@ -74,7 +79,9 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) {
l.ldb = ldb l.ldb = ldb
if len(cfg.BinLog.Name) > 0 { if len(cfg.BinLog.Name) > 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "binlog") if len(cfg.BinLog.Path) == 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log")
}
l.binlog, err = replication.NewBinLogWithConfig(&cfg.BinLog) l.binlog, err = replication.NewBinLogWithConfig(&cfg.BinLog)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -17,7 +17,7 @@ func TestReplication(t *testing.T) {
master, err = Open([]byte(` master, err = Open([]byte(`
{ {
"data_dir" : "/tmp/test_repl/master", "data_dir" : "/tmp/test_repl/master",
"binlog": { "bin_log": {
"name" : "ledis" "name" : "ledis"
} }
} }

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"fmt"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"net" "net"
"strings" "strings"
@ -17,6 +18,14 @@ type App struct {
} }
func NewApp(cfg *Config) (*App, error) { func NewApp(cfg *Config) (*App, error) {
if len(cfg.DataDir) == 0 {
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.closed = false app.closed = false

View File

@ -38,9 +38,9 @@ func startTestApp() {
var d = []byte(` var d = []byte(`
{ {
"data_dir" : "/tmp/testdb",
"addr" : "127.0.0.1:16380", "addr" : "127.0.0.1:16380",
"db" : { "db" : {
"data_dir" : "/tmp/testdb",
"data_db" : { "data_db" : {
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,

View File

@ -3,13 +3,20 @@ package server
import ( import (
"encoding/json" "encoding/json"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"github.com/siddontang/ledisdb/replication"
"io/ioutil" "io/ioutil"
) )
type Config struct { type Config struct {
Addr string `json:"addr"` Addr string `json:"addr"`
DataDir string `json:"data_dir"`
//if you not set db path, use data_dir
DB ledis.Config `json:"db"` DB ledis.Config `json:"db"`
//if you not set relay log path, use data_dir/realy_log
RelayLog replication.RelayLogConfig `json:"relay_log"`
} }
func NewConfig(data json.RawMessage) (*Config, error) { func NewConfig(data json.RawMessage) (*Config, error) {