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) {
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,

View File

@ -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

View File

@ -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)

View File

@ -12,17 +12,13 @@ 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"
},
"data_dir" : "/tmp/test_repl/master",
"binlog": {
"path" : "/tmp/repl/master_binlog"
"name" : "ledis"
}
}
`))
@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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 {

View File

@ -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"

View File

@ -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)

View File

@ -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,