From f95494386ab5ab3c7e39869827e2a5f402911dc9 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 23 Jul 2014 08:30:00 +0800 Subject: [PATCH] update config --- cmd/ledis-repair/main.go | 10 +--------- ledis/config.go | 23 +++++++++++++++++++++++ ledis/ledis.go | 25 ++----------------------- server/app.go | 16 +--------------- server/config.go | 13 +++++++++++++ 5 files changed, 40 insertions(+), 47 deletions(-) diff --git a/cmd/ledis-repair/main.go b/cmd/ledis-repair/main.go index e84ad09..a5b9d73 100644 --- a/cmd/ledis-repair/main.go +++ b/cmd/ledis-repair/main.go @@ -3,11 +3,9 @@ package main import ( "encoding/json" "flag" - "github.com/siddontang/copier" "github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/leveldb" "io/ioutil" - "path" ) var fileName = flag.String("config", "/etc/ledis.json", "ledisdb config file") @@ -37,13 +35,7 @@ func main() { return } - dbPath := path.Join(cfg.DataDir, "data") - - dbCfg := new(leveldb.Config) - copier.Copy(dbCfg, &cfg.DB) - dbCfg.Path = dbPath - - if err = leveldb.Repair(dbCfg); err != nil { + if err = leveldb.Repair(cfg.NewDBConfig()); err != nil { println("repair error: ", err.Error()) } } diff --git a/ledis/config.go b/ledis/config.go index fa4eede..c3a5b84 100644 --- a/ledis/config.go +++ b/ledis/config.go @@ -1,5 +1,11 @@ package ledis +import ( + "github.com/siddontang/copier" + "github.com/siddontang/ledisdb/leveldb" + "path" +) + type Config struct { DataDir string `json:"data_dir"` @@ -17,3 +23,20 @@ type Config struct { MaxFileNum int `json:"max_file_num"` } `json:"binlog"` } + +func (cfg *Config) NewDBConfig() *leveldb.Config { + dbPath := path.Join(cfg.DataDir, "data") + + dbCfg := new(leveldb.Config) + copier.Copy(dbCfg, &cfg.DB) + dbCfg.Path = dbPath + return dbCfg +} + +func (cfg *Config) NewBinLogConfig() *BinLogConfig { + binLogPath := path.Join(cfg.DataDir, "bin_log") + c := new(BinLogConfig) + copier.Copy(c, &cfg.BinLog) + c.Path = binLogPath + return c +} diff --git a/ledis/ledis.go b/ledis/ledis.go index 80dcf29..8d4147d 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -3,10 +3,8 @@ package ledis import ( "encoding/json" "fmt" - "github.com/siddontang/copier" "github.com/siddontang/go-log/log" "github.com/siddontang/ledisdb/leveldb" - "path" "sync" "time" ) @@ -49,31 +47,12 @@ 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") } - ldb, err := openDB(cfg) + ldb, err := leveldb.Open(cfg.NewDBConfig()) if err != nil { return nil, err } @@ -86,7 +65,7 @@ func Open(cfg *Config) (*Ledis, error) { l.ldb = ldb if cfg.BinLog.Use { - l.binlog, err = openBinLog(cfg) + l.binlog, err = NewBinLog(cfg.NewBinLogConfig()) if err != nil { return nil, err } diff --git a/server/app.go b/server/app.go index 756535c..3f0d550 100644 --- a/server/app.go +++ b/server/app.go @@ -2,7 +2,6 @@ package server import ( "fmt" - "github.com/siddontang/copier" "github.com/siddontang/ledisdb/ledis" "net" "path" @@ -63,7 +62,7 @@ func NewApp(cfg *Config) (*App, error) { } } - if app.ldb, err = openLedis(cfg); err != nil { + if app.ldb, err = ledis.Open(cfg.NewLedisConfig()); err != nil { return nil, err } @@ -72,19 +71,6 @@ 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 diff --git a/server/config.go b/server/config.go index 64243e8..13ab286 100644 --- a/server/config.go +++ b/server/config.go @@ -2,6 +2,8 @@ package server import ( "encoding/json" + "github.com/siddontang/copier" + "github.com/siddontang/ledisdb/ledis" "io/ioutil" ) @@ -50,3 +52,14 @@ func NewConfigWithFile(fileName string) (*Config, error) { return NewConfig(data) } + +func (cfg *Config) NewLedisConfig() *ledis.Config { + c := new(ledis.Config) + + c.DataDir = cfg.DataDir + + copier.Copy(&c.DB, &cfg.DB) + copier.Copy(&c.BinLog, &cfg.BinLog) + + return c +}