chore(storage): support set db path for badger driver (#499)

* chore(storage): support set db path for badger driver

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* fix path

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* fix readme

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* update default value

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-04-23 10:34:35 +08:00 committed by GitHub
parent ccc73947ad
commit 4e05611577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 6 deletions

View File

@ -170,6 +170,8 @@ stat:
path: "bunt.db"
leveldb:
path: "level.db"
badgerdb:
path: "badger.db"
```
## Memory Usage

View File

@ -88,6 +88,8 @@ stat:
path: "bunt.db"
leveldb:
path: "level.db"
badgerdb:
path: "badger.db"
`)
// ConfYaml is config structure.
@ -174,11 +176,12 @@ type SectionLog struct {
// SectionStat is sub section of config.
type SectionStat struct {
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
LevelDB SectionLevelDB `yaml:"leveldb"`
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
LevelDB SectionLevelDB `yaml:"leveldb"`
BadgerDB SectionBadgerDB `yaml:"badgerdb"`
}
// SectionRedis is sub section of config.
@ -204,6 +207,11 @@ type SectionLevelDB struct {
Path string `yaml:"path"`
}
// SectionBadgerDB is sub section of config.
type SectionBadgerDB struct {
Path string `yaml:"path"`
}
// SectionPID is sub section of config.
type SectionPID struct {
Enabled bool `yaml:"enabled"`
@ -321,6 +329,7 @@ func LoadConf(confPath string) (ConfYaml, error) {
conf.Stat.BoltDB.Bucket = viper.GetString("stat.boltdb.bucket")
conf.Stat.BuntDB.Path = viper.GetString("stat.buntdb.path")
conf.Stat.LevelDB.Path = viper.GetString("stat.leveldb.path")
conf.Stat.BadgerDB.Path = viper.GetString("stat.badgerdb.path")
// gRPC Server
conf.GRPC.Enabled = viper.GetBool("grpc.enabled")

View File

@ -105,6 +105,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "bunt.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorushDefault.Stat.LevelDB.Path)
assert.Equal(suite.T(), "badger.db", suite.ConfGorushDefault.Stat.BadgerDB.Path)
// gRPC
assert.Equal(suite.T(), false, suite.ConfGorushDefault.GRPC.Enabled)
@ -180,6 +181,7 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "bunt.db", suite.ConfGorush.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorush.Stat.LevelDB.Path)
assert.Equal(suite.T(), "badger.db", suite.ConfGorush.Stat.BadgerDB.Path)
// gRPC
assert.Equal(suite.T(), false, suite.ConfGorush.GRPC.Enabled)

View File

@ -75,3 +75,5 @@ stat:
path: "bunt.db"
leveldb:
path: "level.db"
badgerdb:
path: "badger.db"

View File

@ -30,7 +30,12 @@ type Storage struct {
// Init client storage.
func (s *Storage) Init() error {
s.name = "badger"
s.opts = badger.DefaultOptions(os.TempDir() + "badger")
dbPath := s.config.Stat.BadgerDB.Path
if dbPath == "" {
dbPath = os.TempDir() + "badger"
}
s.opts = badger.DefaultOptions(dbPath)
return nil
}