add data_dir, like mysql to store all data

This commit is contained in:
siddontang 2014-06-06 10:34:57 +08:00
parent d9f81233d6
commit 5d466fb82f
10 changed files with 36 additions and 46 deletions

View File

@ -8,14 +8,13 @@ import (
) )
func TestDump(t *testing.T) { func TestDump(t *testing.T) {
os.RemoveAll("/tmp/testdb_master") os.RemoveAll("/tmp/test_ledis_master")
os.RemoveAll("/tmp/testdb_slave") os.RemoveAll("/tmp/test_ledis_slave")
os.Remove("/tmp/testdb.dump")
var masterConfig = []byte(` var masterConfig = []byte(`
{ {
"data_dir" : "/tmp/test_ledis_master",
"data_db" : { "data_db" : {
"path" : "/tmp/testdb_master",
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,
"write_buffer_size" : 2097152, "write_buffer_size" : 2097152,
@ -31,8 +30,8 @@ func TestDump(t *testing.T) {
var slaveConfig = []byte(` var slaveConfig = []byte(`
{ {
"data_dir" : "/tmp/test_ledis_slave",
"data_db" : { "data_db" : {
"path" : "/tmp/testdb_slave",
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,
"write_buffer_size" : 2097152, "write_buffer_size" : 2097152,

View File

@ -5,12 +5,18 @@ import (
"fmt" "fmt"
"github.com/siddontang/go-leveldb/leveldb" "github.com/siddontang/go-leveldb/leveldb"
"github.com/siddontang/ledisdb/replication" "github.com/siddontang/ledisdb/replication"
"path"
"sync" "sync"
) )
type Config struct { type Config struct {
DataDir string `json:"data_dir"`
//data_db path is data_dir/data
DataDB leveldb.Config `json:"data_db"` DataDB leveldb.Config `json:"data_db"`
//binlog path is data_dir/binlog
//you muse set binlog name to enable binlog
BinLog replication.BinLogConfig `json:"binlog"` BinLog replication.BinLogConfig `json:"binlog"`
} }
@ -51,6 +57,11 @@ func Open(configJson json.RawMessage) (*Ledis, error) {
} }
func OpenWithConfig(cfg *Config) (*Ledis, error) { func OpenWithConfig(cfg *Config) (*Ledis, error) {
if len(cfg.DataDir) == 0 {
return nil, fmt.Errorf("must set correct data_dir")
}
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
@ -62,7 +73,8 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) {
l.ldb = ldb l.ldb = ldb
if len(cfg.BinLog.Path) > 0 { if len(cfg.BinLog.Name) > 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "binlog")
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

@ -13,8 +13,8 @@ func getTestDB() *DB {
f := func() { f := func() {
var d = []byte(` var d = []byte(`
{ {
"data_dir" : "/tmp/test_ledis",
"data_db" : { "data_db" : {
"path" : "/tmp/testdb",
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,
"write_buffer_size" : 2097152, "write_buffer_size" : 2097152,
@ -22,15 +22,13 @@ func getTestDB() *DB {
}, },
"binlog" : { "binlog" : {
"path" : "/tmp/testdb_binlog",
"max_file_size" : 1073741824, "max_file_size" : 1073741824,
"max_file_num" : 3 "max_file_num" : 3
} }
} }
`) `)
os.RemoveAll("/tmp/testdb") os.RemoveAll("/tmp/test_ledis")
os.RemoveAll("/tmp/testdb_binlog")
var err error var err error
testLedis, err = Open(d) testLedis, err = Open(d)

View File

@ -12,18 +12,14 @@ func TestReplication(t *testing.T) {
var slave *Ledis var slave *Ledis
var err error var err error
os.RemoveAll("/tmp/repl") os.RemoveAll("/tmp/repl_repl")
os.MkdirAll("/tmp/repl", os.ModePerm)
master, err = Open([]byte(` master, err = Open([]byte(`
{ {
"data_db" : { "data_dir" : "/tmp/test_repl/master",
"path" : "/tmp/repl/master_db" "binlog": {
}, "name" : "ledis"
}
"binlog" : {
"path" : "/tmp/repl/master_binlog"
}
} }
`)) `))
if err != nil { if err != nil {
@ -32,13 +28,7 @@ func TestReplication(t *testing.T) {
slave, err = Open([]byte(` slave, err = Open([]byte(`
{ {
"data_db" : { "data_dir" : "/tmp/test_repl/slave"
"path" : "/tmp/repl/slave_db"
},
"binlog" : {
"path" : "/tmp/repl/slave_binlog"
}
} }
`)) `))
if err != nil { if err != nil {
@ -50,7 +40,7 @@ func TestReplication(t *testing.T) {
db.Set([]byte("b"), []byte("2")) db.Set([]byte("b"), []byte("2"))
db.Set([]byte("c"), []byte("3")) db.Set([]byte("c"), []byte("3"))
relayLog := "/tmp/repl/master_binlog/ledis-bin.0000001" relayLog := "/tmp/test_repl/master/binlog/ledis-bin.0000001"
var offset int64 var offset int64
offset, err = slave.RepliateRelayLog(relayLog, 0) offset, err = slave.RepliateRelayLog(relayLog, 0)

View File

@ -44,13 +44,6 @@ func (cfg *BinLogConfig) adjust() {
cfg.MaxFileNum = MaxBinLogFileNum cfg.MaxFileNum = MaxBinLogFileNum
} }
if len(cfg.BaseName) == 0 {
cfg.BaseName = "ledis"
}
if len(cfg.IndexName) == 0 {
cfg.IndexName = "ledis"
}
//binlog not care space limit //binlog not care space limit
cfg.SpaceLimit = -1 cfg.SpaceLimit = -1

View File

@ -12,6 +12,7 @@ func TestBinLog(t *testing.T) {
cfg.MaxFileNum = 1 cfg.MaxFileNum = 1
cfg.MaxFileSize = 1024 cfg.MaxFileSize = 1024
cfg.Path = "/tmp/ledis_binlog" cfg.Path = "/tmp/ledis_binlog"
cfg.Name = "ledis"
os.RemoveAll(cfg.Path) os.RemoveAll(cfg.Path)

View File

@ -21,8 +21,7 @@ type logHandler interface {
} }
type LogConfig struct { type LogConfig struct {
BaseName string `json:"base_name"` Name string `json:"name"`
IndexName string `json:"index_name"`
LogType string `json:"log_type"` LogType string `json:"log_type"`
Path string `json:"path"` Path string `json:"path"`
MaxFileSize int `json:"max_file_size"` MaxFileSize int `json:"max_file_size"`
@ -52,6 +51,10 @@ func newLog(handler logHandler, cfg *LogConfig) (*Log, error) {
l.cfg = cfg l.cfg = cfg
l.handler = handler l.handler = handler
if len(l.cfg.Name) == 0 {
return nil, fmt.Errorf("you must set log name first")
}
if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil { if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil {
return nil, err return nil, err
} }
@ -93,7 +96,7 @@ func (l *Log) flushIndex() error {
} }
func (l *Log) loadIndex() error { func (l *Log) loadIndex() error {
l.indexName = path.Join(l.cfg.Path, fmt.Sprintf("%s-%s.index", l.cfg.IndexName, l.cfg.LogType)) l.indexName = path.Join(l.cfg.Path, fmt.Sprintf("%s-%s.index", l.cfg.Name, l.cfg.LogType))
if _, err := os.Stat(l.indexName); os.IsNotExist(err) { if _, err := os.Stat(l.indexName); os.IsNotExist(err) {
//no index file, nothing to do //no index file, nothing to do
} else { } else {
@ -145,7 +148,7 @@ func (l *Log) loadIndex() error {
} }
func (l *Log) getLogFile() string { func (l *Log) getLogFile() string {
return fmt.Sprintf("%s-%s.%07d", l.cfg.BaseName, l.cfg.LogType, l.lastLogIndex) return fmt.Sprintf("%s-%s.%07d", l.cfg.Name, l.cfg.LogType, l.lastLogIndex)
} }
func (l *Log) openNewLogFile() error { func (l *Log) openNewLogFile() error {

View File

@ -21,13 +21,6 @@ func (cfg *RelayLogConfig) adjust() {
cfg.MaxFileSize = MaxRelayLogFileSize cfg.MaxFileSize = MaxRelayLogFileSize
} }
if len(cfg.BaseName) == 0 {
cfg.BaseName = "ledis"
}
if len(cfg.IndexName) == 0 {
cfg.IndexName = "ledis"
}
//relaylog not care file num //relaylog not care file num
cfg.MaxFileNum = -1 cfg.MaxFileNum = -1
cfg.LogType = "relay" cfg.LogType = "relay"

View File

@ -11,6 +11,7 @@ func TestRelayLog(t *testing.T) {
cfg.MaxFileSize = 1024 cfg.MaxFileSize = 1024
cfg.SpaceLimit = 1024 cfg.SpaceLimit = 1024
cfg.Path = "/tmp/ledis_relaylog" cfg.Path = "/tmp/ledis_relaylog"
cfg.Name = "ledis"
os.RemoveAll(cfg.Path) os.RemoveAll(cfg.Path)

View File

@ -40,8 +40,8 @@ func startTestApp() {
{ {
"addr" : "127.0.0.1:16380", "addr" : "127.0.0.1:16380",
"db" : { "db" : {
"data_dir" : "/tmp/testdb",
"data_db" : { "data_db" : {
"path" : "/tmp/testdb",
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,
"write_buffer_size" : 2097152, "write_buffer_size" : 2097152,