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
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) ./...

View File

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

47
dev.sh
View File

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

View File

@ -1,6 +1,7 @@
package ledis
import (
"fmt"
"github.com/siddontang/copier"
"github.com/siddontang/ledisdb/store"
"path"
@ -10,6 +11,7 @@ type Config struct {
DataDir string `json:"data_dir"`
DB struct {
Name string `json:"name"`
Compression bool `json:"compression"`
BlockSize int `json:"block_size"`
WriteBufferSize int `json:"write_buffer_size"`
@ -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
}

View File

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

View File

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