update store

This commit is contained in:
siddontang 2014-07-26 00:46:03 +08:00
parent 727286d1ac
commit e073396481
6 changed files with 80 additions and 34 deletions

View File

@ -1,12 +1,10 @@
GO_BUILD_FLAG += leveldb
all: build all: build
build: build:
go install -tags $(GO_BUILD_FLAG) ./... go install -tags $(GO_BUILD_TAGS) ./...
clean: clean:
go clean -i ./... go clean -i ./...
test: test:
go test -tags $(GO_BUILD_FLAG) ./... go test -tags $(GO_BUILD_TAGS) ./...

View File

@ -10,6 +10,7 @@ import (
) )
var configFile = flag.String("config", "/etc/ledis.json", "ledisdb config file") 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() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
@ -27,6 +28,10 @@ func main() {
return return
} }
if len(*storeName) > 0 {
cfg.DB.Name = *storeName
}
var app *server.App var app *server.App
app, err = server.NewApp(cfg) app, err = server.NewApp(cfg)
if err != nil { if err != nil {

47
dev.sh
View File

@ -10,8 +10,9 @@ fi
#default snappy and leveldb install path #default snappy and leveldb install path
#you may change yourself #you may change yourself
export SNAPPY_DIR=/usr/local/snappy SNAPPY_DIR=/usr/local/snappy
export LEVELDB_DIR=/usr/local/leveldb LEVELDB_DIR=/usr/local/leveldb
ROCKSDB_DIR=
function add_path() function add_path()
{ {
@ -26,14 +27,38 @@ function add_path()
export GOPATH=$(add_path $GOPATH $VTROOT) export GOPATH=$(add_path $GOPATH $VTROOT)
export CGO_CFLAGS="-I$LEVELDB_DIR/include -I$SNAPPY_DIR/include" # check snappy
export CGO_CXXFLAGS="-I$LEVELDB_DIR/include -I$SNAPPY_DIR/include" if [ -f $SNAPPY_DIR/lib/libsnappy.a ]; then
export CGO_LDFLAGS="-L$LEVELDB_DIR/lib -L$SNAPPY_DIR/lib -lsnappy" 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 # check leveldb
export LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $SNAPPY_DIR/lib) if [ -f $LEVELDB_DIR/lib/libleveldb.a ]; then
export LD_LIBRARY_PATH=$(add_path $LD_LIBRARY_PATH $LEVELDB_DIR/lib) 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 # check rocksdb
export DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $SNAPPY_DIR/lib) if [ -f $ROCKSDB_DIR/lib/libleveldb.a ]; then
export DYLD_LIBRARY_PATH=$(add_path $DYLD_LIBRARY_PATH $LEVELDB_DIR/lib) 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

View File

@ -1,6 +1,7 @@
package ledis package ledis
import ( import (
"fmt"
"github.com/siddontang/copier" "github.com/siddontang/copier"
"github.com/siddontang/ledisdb/store" "github.com/siddontang/ledisdb/store"
"path" "path"
@ -10,6 +11,7 @@ type Config struct {
DataDir string `json:"data_dir"` DataDir string `json:"data_dir"`
DB struct { DB struct {
Name string `json:"name"`
Compression bool `json:"compression"` Compression bool `json:"compression"`
BlockSize int `json:"block_size"` BlockSize int `json:"block_size"`
WriteBufferSize int `json:"write_buffer_size"` WriteBufferSize int `json:"write_buffer_size"`
@ -26,10 +28,16 @@ type Config struct {
} }
func (cfg *Config) NewDBConfig() *store.Config { 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) dbCfg := new(store.Config)
copier.Copy(dbCfg, &cfg.DB) copier.Copy(dbCfg, &cfg.DB)
dbPath := path.Join(cfg.DataDir, fmt.Sprintf("%s_data", cfg.DB.Name))
dbCfg.Path = dbPath dbCfg.Path = dbPath
return dbCfg return dbCfg
} }

View File

@ -13,6 +13,7 @@ type Config struct {
DataDir string `json:"data_dir"` DataDir string `json:"data_dir"`
DB struct { DB struct {
Name string `json:"name"`
Compression bool `json:"compression"` Compression bool `json:"compression"`
BlockSize int `json:"block_size"` BlockSize int `json:"block_size"`
WriteBufferSize int `json:"write_buffer_size"` WriteBufferSize int `json:"write_buffer_size"`

View File

@ -6,7 +6,7 @@ import (
"os" "os"
) )
const DefaultStoreName = "lmdb" const DefaultStoreName = "goleveldb"
type Store interface { type Store interface {
Open(cfg *Config) (driver.IDB, error) Open(cfg *Config) (driver.IDB, error)
@ -17,24 +17,33 @@ var dbs = map[string]Store{}
func Register(name string, store Store) { func Register(name string, store Store) {
if _, ok := dbs[name]; ok { if _, ok := dbs[name]; ok {
panic(fmt.Errorf("db %s is registered", name)) panic(fmt.Errorf("store %s is registered", name))
} }
dbs[name] = store dbs[name] = store
} }
func Open(cfg *Config) (*DB, error) { func ListStores() []string {
if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil { s := []string{}
return nil, err for k, _ := range dbs {
s = append(s, k)
} }
return s
}
func Open(cfg *Config) (*DB, error) {
if len(cfg.Name) == 0 { if len(cfg.Name) == 0 {
cfg.Name = DefaultStoreName cfg.Name = DefaultStoreName
} }
s, ok := dbs[cfg.Name] s, ok := dbs[cfg.Name]
if !ok { 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) idb, err := s.Open(cfg)