From e073396481e4f4f8b305be08452eb4c9dd7413a1 Mon Sep 17 00:00:00 2001 From: siddontang Date: Sat, 26 Jul 2014 00:46:03 +0800 Subject: [PATCH] update store --- Makefile | 6 ++--- cmd/ledis-server/main.go | 5 +++++ dev.sh | 47 ++++++++++++++++++++++++++++++---------- ledis/config.go | 22 +++++++++++++------ server/config.go | 13 ++++++----- store/store.go | 21 +++++++++++++----- 6 files changed, 80 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index e3f7fc5..cdf55bc 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,10 @@ -GO_BUILD_FLAG += leveldb - all: build build: - go install -tags $(GO_BUILD_FLAG) ./... + go install -tags $(GO_BUILD_TAGS) ./... clean: go clean -i ./... test: - go test -tags $(GO_BUILD_FLAG) ./... + go test -tags $(GO_BUILD_TAGS) ./... diff --git a/cmd/ledis-server/main.go b/cmd/ledis-server/main.go index 84c4442..050daab 100644 --- a/cmd/ledis-server/main.go +++ b/cmd/ledis-server/main.go @@ -10,6 +10,7 @@ import ( ) var configFile = flag.String("config", "/etc/ledis.json", "ledisdb config file") +var storeName = flag.String("store", "", "select a store to use, it will overwrite the config's store") func main() { runtime.GOMAXPROCS(runtime.NumCPU()) @@ -27,6 +28,10 @@ func main() { return } + if len(*storeName) > 0 { + cfg.DB.Name = *storeName + } + var app *server.App app, err = server.NewApp(cfg) if err != nil { diff --git a/dev.sh b/dev.sh index baec6e1..76115ba 100644 --- a/dev.sh +++ b/dev.sh @@ -10,8 +10,9 @@ fi #default snappy and leveldb install path #you may change yourself -export SNAPPY_DIR=/usr/local/snappy -export LEVELDB_DIR=/usr/local/leveldb +SNAPPY_DIR=/usr/local/snappy +LEVELDB_DIR=/usr/local/leveldb +ROCKSDB_DIR= function add_path() { @@ -26,14 +27,38 @@ function add_path() export GOPATH=$(add_path $GOPATH $VTROOT) -export CGO_CFLAGS="-I$LEVELDB_DIR/include -I$SNAPPY_DIR/include" -export CGO_CXXFLAGS="-I$LEVELDB_DIR/include -I$SNAPPY_DIR/include" -export CGO_LDFLAGS="-L$LEVELDB_DIR/lib -L$SNAPPY_DIR/lib -lsnappy" +# check snappy +if [ -f $SNAPPY_DIR/lib/libsnappy.a ]; then + CGO_CFLAGS+="-I$SNAPPY_DIR/include" + CGO_CXXFLAGS+="-I$SNAPPY_DIR/include" + CGO_LDFLAGS+="-L$SNAPPY_DIR/lib -lsnappy" + LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $SNAPPY_DIR/lib) + DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $SNAPPY_DIR/lib) +fi -#for linux, use LD_LIBRARY_PATH -export LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $SNAPPY_DIR/lib) -export LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $LEVELDB_DIR/lib) +# check leveldb +if [ -f $LEVELDB_DIR/lib/libleveldb.a ]; then + CGO_CFLAGS+="-I$LEVELDB_DIR/include" + CGO_CXXFLAGS+="-I$LEVELDB_DIR/include" + CGO_LDFLAGS+="-L$LEVELDB_DIR/lib -lleveldb" + LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $LEVELDB_DIR/lib) + DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $LEVELDB_DIR/lib) + GO_BUILD_TAGS+="leveldb" +fi -#for macos, use DYLD_LIBRARY_PATH -export DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $SNAPPY_DIR/lib) -export DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $LEVELDB_DIR/lib) +# check rocksdb +if [ -f $ROCKSDB_DIR/lib/libleveldb.a ]; then + CGO_CFLAGS+="-I$ROCKSDB_DIR/include" + CGO_CXXFLAGS+="-I$ROCKSDB_DIR/include" + CGO_LDFLAGS+="-L$ROCKSDB_DIR/lib -lleveldb" + LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $ROCKSDB_DIR/lib) + DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $ROCKSDB_DIR/lib) + GO_BUILD_TAGS+="rocksdb" +fi + +export CGO_CFLAGS +export CGO_CXXFLAGS +export CGO_LDFLAGS +export LD_LIBRARY_PATH +export DYLD_LIBRARY_PATH +export GO_BUILD_TAGS diff --git a/ledis/config.go b/ledis/config.go index ffe64d5..f616172 100644 --- a/ledis/config.go +++ b/ledis/config.go @@ -1,6 +1,7 @@ package ledis import ( + "fmt" "github.com/siddontang/copier" "github.com/siddontang/ledisdb/store" "path" @@ -10,12 +11,13 @@ type Config struct { DataDir string `json:"data_dir"` DB struct { - Compression bool `json:"compression"` - BlockSize int `json:"block_size"` - WriteBufferSize int `json:"write_buffer_size"` - CacheSize int `json:"cache_size"` - MaxOpenFiles int `json:"max_open_files"` - MapSize int `json:"map_size"` + Name string `json:"name"` + Compression bool `json:"compression"` + BlockSize int `json:"block_size"` + WriteBufferSize int `json:"write_buffer_size"` + CacheSize int `json:"cache_size"` + MaxOpenFiles int `json:"max_open_files"` + MapSize int `json:"map_size"` } `json:"db"` BinLog struct { @@ -26,10 +28,16 @@ type Config struct { } func (cfg *Config) NewDBConfig() *store.Config { - dbPath := path.Join(cfg.DataDir, "data") + if len(cfg.DB.Name) == 0 { + fmt.Printf("no store set, use default %s\n", store.DefaultStoreName) + cfg.DB.Name = store.DefaultStoreName + } dbCfg := new(store.Config) copier.Copy(dbCfg, &cfg.DB) + + dbPath := path.Join(cfg.DataDir, fmt.Sprintf("%s_data", cfg.DB.Name)) + dbCfg.Path = dbPath return dbCfg } diff --git a/server/config.go b/server/config.go index 96fbd1a..024eeb9 100644 --- a/server/config.go +++ b/server/config.go @@ -13,12 +13,13 @@ type Config struct { DataDir string `json:"data_dir"` DB struct { - Compression bool `json:"compression"` - BlockSize int `json:"block_size"` - WriteBufferSize int `json:"write_buffer_size"` - CacheSize int `json:"cache_size"` - MaxOpenFiles int `json:"max_open_files"` - MapSize int `json:"map_size"` + Name string `json:"name"` + Compression bool `json:"compression"` + BlockSize int `json:"block_size"` + WriteBufferSize int `json:"write_buffer_size"` + CacheSize int `json:"cache_size"` + MaxOpenFiles int `json:"max_open_files"` + MapSize int `json:"map_size"` } `json:"db"` BinLog struct { diff --git a/store/store.go b/store/store.go index fb64bd2..f415b7f 100644 --- a/store/store.go +++ b/store/store.go @@ -6,7 +6,7 @@ import ( "os" ) -const DefaultStoreName = "lmdb" +const DefaultStoreName = "goleveldb" type Store interface { Open(cfg *Config) (driver.IDB, error) @@ -17,24 +17,33 @@ var dbs = map[string]Store{} func Register(name string, store Store) { if _, ok := dbs[name]; ok { - panic(fmt.Errorf("db %s is registered", name)) + panic(fmt.Errorf("store %s is registered", name)) } dbs[name] = store } -func Open(cfg *Config) (*DB, error) { - if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil { - return nil, err +func ListStores() []string { + s := []string{} + for k, _ := range dbs { + s = append(s, k) } + return s +} + +func Open(cfg *Config) (*DB, error) { if len(cfg.Name) == 0 { cfg.Name = DefaultStoreName } s, ok := dbs[cfg.Name] if !ok { - return nil, fmt.Errorf("db %s is not registered", cfg.Name) + return nil, fmt.Errorf("store %s is not registered", cfg.Name) + } + + if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil { + return nil, err } idb, err := s.Open(cfg)