From 5d466fb82fb8abcfa7951656ef8a9ed7b42bf4f6 Mon Sep 17 00:00:00 2001 From: siddontang Date: Fri, 6 Jun 2014 10:34:57 +0800 Subject: [PATCH] add data_dir, like mysql to store all data --- ledis/dump_test.go | 9 ++++----- ledis/ledis.go | 14 +++++++++++++- ledis/ledis_test.go | 6 ++---- ledis/replication_test.go | 24 +++++++----------------- replication/binlog.go | 7 ------- replication/binlog_test.go | 1 + replication/log.go | 11 +++++++---- replication/relaylog.go | 7 ------- replication/relaylog_test.go | 1 + server/app_test.go | 2 +- 10 files changed, 36 insertions(+), 46 deletions(-) diff --git a/ledis/dump_test.go b/ledis/dump_test.go index b27d8f0..f15f8f4 100644 --- a/ledis/dump_test.go +++ b/ledis/dump_test.go @@ -8,14 +8,13 @@ import ( ) func TestDump(t *testing.T) { - os.RemoveAll("/tmp/testdb_master") - os.RemoveAll("/tmp/testdb_slave") - os.Remove("/tmp/testdb.dump") + os.RemoveAll("/tmp/test_ledis_master") + os.RemoveAll("/tmp/test_ledis_slave") var masterConfig = []byte(` { + "data_dir" : "/tmp/test_ledis_master", "data_db" : { - "path" : "/tmp/testdb_master", "compression":true, "block_size" : 32768, "write_buffer_size" : 2097152, @@ -31,8 +30,8 @@ func TestDump(t *testing.T) { var slaveConfig = []byte(` { + "data_dir" : "/tmp/test_ledis_slave", "data_db" : { - "path" : "/tmp/testdb_slave", "compression":true, "block_size" : 32768, "write_buffer_size" : 2097152, diff --git a/ledis/ledis.go b/ledis/ledis.go index 50214ad..8ba83b2 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -5,12 +5,18 @@ import ( "fmt" "github.com/siddontang/go-leveldb/leveldb" "github.com/siddontang/ledisdb/replication" + "path" "sync" ) type Config struct { + DataDir string `json:"data_dir"` + + //data_db path is data_dir/data 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"` } @@ -51,6 +57,11 @@ func Open(configJson json.RawMessage) (*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) if err != nil { return nil, err @@ -62,7 +73,8 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) { 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) if err != nil { return nil, err diff --git a/ledis/ledis_test.go b/ledis/ledis_test.go index 902d545..dc28b5c 100644 --- a/ledis/ledis_test.go +++ b/ledis/ledis_test.go @@ -13,8 +13,8 @@ func getTestDB() *DB { f := func() { var d = []byte(` { + "data_dir" : "/tmp/test_ledis", "data_db" : { - "path" : "/tmp/testdb", "compression":true, "block_size" : 32768, "write_buffer_size" : 2097152, @@ -22,15 +22,13 @@ func getTestDB() *DB { }, "binlog" : { - "path" : "/tmp/testdb_binlog", "max_file_size" : 1073741824, "max_file_num" : 3 } } `) - os.RemoveAll("/tmp/testdb") - os.RemoveAll("/tmp/testdb_binlog") + os.RemoveAll("/tmp/test_ledis") var err error testLedis, err = Open(d) diff --git a/ledis/replication_test.go b/ledis/replication_test.go index 5f2243a..f22617e 100644 --- a/ledis/replication_test.go +++ b/ledis/replication_test.go @@ -12,18 +12,14 @@ func TestReplication(t *testing.T) { var slave *Ledis var err error - os.RemoveAll("/tmp/repl") - os.MkdirAll("/tmp/repl", os.ModePerm) + os.RemoveAll("/tmp/repl_repl") master, err = Open([]byte(` { - "data_db" : { - "path" : "/tmp/repl/master_db" - }, - - "binlog" : { - "path" : "/tmp/repl/master_binlog" - } + "data_dir" : "/tmp/test_repl/master", + "binlog": { + "name" : "ledis" + } } `)) if err != nil { @@ -32,13 +28,7 @@ func TestReplication(t *testing.T) { slave, err = Open([]byte(` { - "data_db" : { - "path" : "/tmp/repl/slave_db" - }, - - "binlog" : { - "path" : "/tmp/repl/slave_binlog" - } + "data_dir" : "/tmp/test_repl/slave" } `)) if err != nil { @@ -50,7 +40,7 @@ func TestReplication(t *testing.T) { db.Set([]byte("b"), []byte("2")) 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 offset, err = slave.RepliateRelayLog(relayLog, 0) diff --git a/replication/binlog.go b/replication/binlog.go index 349880c..ad9163c 100644 --- a/replication/binlog.go +++ b/replication/binlog.go @@ -44,13 +44,6 @@ func (cfg *BinLogConfig) adjust() { cfg.MaxFileNum = MaxBinLogFileNum } - if len(cfg.BaseName) == 0 { - cfg.BaseName = "ledis" - } - if len(cfg.IndexName) == 0 { - cfg.IndexName = "ledis" - } - //binlog not care space limit cfg.SpaceLimit = -1 diff --git a/replication/binlog_test.go b/replication/binlog_test.go index a66ea11..48263ae 100644 --- a/replication/binlog_test.go +++ b/replication/binlog_test.go @@ -12,6 +12,7 @@ func TestBinLog(t *testing.T) { cfg.MaxFileNum = 1 cfg.MaxFileSize = 1024 cfg.Path = "/tmp/ledis_binlog" + cfg.Name = "ledis" os.RemoveAll(cfg.Path) diff --git a/replication/log.go b/replication/log.go index 6648b40..ad5c166 100644 --- a/replication/log.go +++ b/replication/log.go @@ -21,8 +21,7 @@ type logHandler interface { } type LogConfig struct { - BaseName string `json:"base_name"` - IndexName string `json:"index_name"` + Name string `json:"name"` LogType string `json:"log_type"` Path string `json:"path"` MaxFileSize int `json:"max_file_size"` @@ -52,6 +51,10 @@ func newLog(handler logHandler, cfg *LogConfig) (*Log, error) { l.cfg = cfg 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 { return nil, err } @@ -93,7 +96,7 @@ func (l *Log) flushIndex() 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) { //no index file, nothing to do } else { @@ -145,7 +148,7 @@ func (l *Log) loadIndex() error { } 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 { diff --git a/replication/relaylog.go b/replication/relaylog.go index 6b4894d..4ee2a4a 100644 --- a/replication/relaylog.go +++ b/replication/relaylog.go @@ -21,13 +21,6 @@ func (cfg *RelayLogConfig) adjust() { cfg.MaxFileSize = MaxRelayLogFileSize } - if len(cfg.BaseName) == 0 { - cfg.BaseName = "ledis" - } - if len(cfg.IndexName) == 0 { - cfg.IndexName = "ledis" - } - //relaylog not care file num cfg.MaxFileNum = -1 cfg.LogType = "relay" diff --git a/replication/relaylog_test.go b/replication/relaylog_test.go index 2e37af8..0cde67b 100644 --- a/replication/relaylog_test.go +++ b/replication/relaylog_test.go @@ -11,6 +11,7 @@ func TestRelayLog(t *testing.T) { cfg.MaxFileSize = 1024 cfg.SpaceLimit = 1024 cfg.Path = "/tmp/ledis_relaylog" + cfg.Name = "ledis" os.RemoveAll(cfg.Path) diff --git a/server/app_test.go b/server/app_test.go index 2293ce2..efaf08e 100644 --- a/server/app_test.go +++ b/server/app_test.go @@ -40,8 +40,8 @@ func startTestApp() { { "addr" : "127.0.0.1:16380", "db" : { + "data_dir" : "/tmp/testdb", "data_db" : { - "path" : "/tmp/testdb", "compression":true, "block_size" : 32768, "write_buffer_size" : 2097152,