From 4e05611577f924091b27f35d34032be08772ea39 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 23 Apr 2020 10:34:35 +0800 Subject: [PATCH] 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 * fix path Signed-off-by: Bo-Yi Wu * fix readme Signed-off-by: Bo-Yi Wu * update default value Signed-off-by: Bo-Yi Wu --- README.md | 2 ++ config/config.go | 19 ++++++++++++++----- config/config_test.go | 2 ++ config/testdata/config.yml | 2 ++ storage/badger/badger.go | 7 ++++++- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ae90556..9b1d755 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,8 @@ stat: path: "bunt.db" leveldb: path: "level.db" + badgerdb: + path: "badger.db" ``` ## Memory Usage diff --git a/config/config.go b/config/config.go index 07ae8cd..905f5d5 100644 --- a/config/config.go +++ b/config/config.go @@ -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") diff --git a/config/config_test.go b/config/config_test.go index 23d2f55..54b1363 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) diff --git a/config/testdata/config.yml b/config/testdata/config.yml index a182302..4cbb214 100644 --- a/config/testdata/config.yml +++ b/config/testdata/config.yml @@ -75,3 +75,5 @@ stat: path: "bunt.db" leveldb: path: "level.db" + badgerdb: + path: "badger.db" diff --git a/storage/badger/badger.go b/storage/badger/badger.go index 25b995b..82c7fd6 100644 --- a/storage/badger/badger.go +++ b/storage/badger/badger.go @@ -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 }