update config

This commit is contained in:
siddontang 2014-07-23 08:30:00 +08:00
parent 80689cb3fd
commit f95494386a
5 changed files with 40 additions and 47 deletions

View File

@ -3,11 +3,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"github.com/siddontang/ledisdb/leveldb" "github.com/siddontang/ledisdb/leveldb"
"io/ioutil" "io/ioutil"
"path"
) )
var fileName = flag.String("config", "/etc/ledis.json", "ledisdb config file") var fileName = flag.String("config", "/etc/ledis.json", "ledisdb config file")
@ -37,13 +35,7 @@ func main() {
return return
} }
dbPath := path.Join(cfg.DataDir, "data") if err = leveldb.Repair(cfg.NewDBConfig()); err != nil {
dbCfg := new(leveldb.Config)
copier.Copy(dbCfg, &cfg.DB)
dbCfg.Path = dbPath
if err = leveldb.Repair(dbCfg); err != nil {
println("repair error: ", err.Error()) println("repair error: ", err.Error())
} }
} }

View File

@ -1,5 +1,11 @@
package ledis package ledis
import (
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/leveldb"
"path"
)
type Config struct { type Config struct {
DataDir string `json:"data_dir"` DataDir string `json:"data_dir"`
@ -17,3 +23,20 @@ type Config struct {
MaxFileNum int `json:"max_file_num"` MaxFileNum int `json:"max_file_num"`
} `json:"binlog"` } `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
}

View File

@ -3,10 +3,8 @@ 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"
"sync" "sync"
"time" "time"
) )
@ -49,31 +47,12 @@ 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")
} }
ldb, err := openDB(cfg) ldb, err := leveldb.Open(cfg.NewDBConfig())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -86,7 +65,7 @@ func Open(cfg *Config) (*Ledis, error) {
l.ldb = ldb l.ldb = ldb
if cfg.BinLog.Use { if cfg.BinLog.Use {
l.binlog, err = openBinLog(cfg) l.binlog, err = NewBinLog(cfg.NewBinLogConfig())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,7 +2,6 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"net" "net"
"path" "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 return nil, err
} }
@ -72,19 +71,6 @@ 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

@ -2,6 +2,8 @@ package server
import ( import (
"encoding/json" "encoding/json"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/ledis"
"io/ioutil" "io/ioutil"
) )
@ -50,3 +52,14 @@ func NewConfigWithFile(fileName string) (*Config, error) {
return NewConfig(data) 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
}