decouple from viper for filer store

This commit is contained in:
Chris Lu 2018-06-17 13:01:57 -07:00
parent 0467195f07
commit eb2acd11c2
8 changed files with 39 additions and 35 deletions

View file

@ -5,7 +5,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/gocql/gocql" "github.com/gocql/gocql"
"github.com/spf13/viper"
) )
func init() { func init() {
@ -21,10 +20,10 @@ func (store *CassandraStore) GetName() string {
return "cassandra" return "cassandra"
} }
func (store *CassandraStore) Initialize(viper *viper.Viper) (err error) { func (store *CassandraStore) Initialize(configuration filer2.Configuration) (err error) {
return store.initialize( return store.initialize(
viper.GetString("keyspace"), configuration.GetString("keyspace"),
viper.GetStringSlice("hosts"), configuration.GetStringSlice("hosts"),
) )
} }

View file

@ -124,3 +124,13 @@ func (f *Filer) LoadConfiguration() {
os.Exit(-1) os.Exit(-1)
} }
// A simplified interface to decouple from Viper
type Configuration interface {
GetString(key string) string
GetBool(key string) bool
GetInt(key string) int
GetInt64(key string) int64
GetFloat64(key string) float64
GetStringSlice(key string) []string
}

View file

@ -2,12 +2,12 @@ package filer2
import ( import (
"errors" "errors"
"github.com/spf13/viper"
) )
type FilerStore interface { type FilerStore interface {
GetName() string GetName() string
Initialize(viper *viper.Viper) error // Initialize initializes the file store
Initialize(configuration Configuration) error
InsertEntry(*Entry) error InsertEntry(*Entry) error
UpdateEntry(*Entry) (err error) UpdateEntry(*Entry) (err error)
FindEntry(FullPath) (entry *Entry, err error) FindEntry(FullPath) (entry *Entry, err error)

View file

@ -7,7 +7,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
weed_util "github.com/chrislusf/seaweedfs/weed/util" weed_util "github.com/chrislusf/seaweedfs/weed/util"
"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
leveldb_util "github.com/syndtr/goleveldb/leveldb/util" leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
) )
@ -28,8 +27,8 @@ func (store *LevelDBStore) GetName() string {
return "leveldb" return "leveldb"
} }
func (store *LevelDBStore) Initialize(viper *viper.Viper) (err error) { func (store *LevelDBStore) Initialize(configuration filer2.Configuration) (err error) {
dir := viper.GetString("dir") dir := configuration.GetString("dir")
return store.initialize(dir) return store.initialize(dir)
} }

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/google/btree" "github.com/google/btree"
"github.com/spf13/viper"
"strings" "strings"
) )
@ -28,7 +27,7 @@ func (store *MemDbStore) GetName() string {
return "memory" return "memory"
} }
func (store *MemDbStore) Initialize(viper *viper.Viper) (err error) { func (store *MemDbStore) Initialize(configuration filer2.Configuration) (err error) {
store.tree = btree.New(8) store.tree = btree.New(8)
return nil return nil
} }

View file

@ -7,7 +7,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/filer2/abstract_sql" "github.com/chrislusf/seaweedfs/weed/filer2/abstract_sql"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
) )
const ( const (
@ -26,15 +25,15 @@ func (store *MysqlStore) GetName() string {
return "mysql" return "mysql"
} }
func (store *MysqlStore) Initialize(viper *viper.Viper) (err error) { func (store *MysqlStore) Initialize(configuration filer2.Configuration) (err error) {
return store.initialize( return store.initialize(
viper.GetString("username"), configuration.GetString("username"),
viper.GetString("password"), configuration.GetString("password"),
viper.GetString("hostname"), configuration.GetString("hostname"),
viper.GetInt("port"), configuration.GetInt("port"),
viper.GetString("database"), configuration.GetString("database"),
viper.GetInt("connection_max_idle"), configuration.GetInt("connection_max_idle"),
viper.GetInt("connection_max_open"), configuration.GetInt("connection_max_open"),
) )
} }

View file

@ -7,7 +7,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/filer2/abstract_sql" "github.com/chrislusf/seaweedfs/weed/filer2/abstract_sql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/spf13/viper"
) )
const ( const (
@ -26,16 +25,16 @@ func (store *PostgresStore) GetName() string {
return "postgres" return "postgres"
} }
func (store *PostgresStore) Initialize(viper *viper.Viper) (err error) { func (store *PostgresStore) Initialize(configuration filer2.Configuration) (err error) {
return store.initialize( return store.initialize(
viper.GetString("username"), configuration.GetString("username"),
viper.GetString("password"), configuration.GetString("password"),
viper.GetString("hostname"), configuration.GetString("hostname"),
viper.GetInt("port"), configuration.GetInt("port"),
viper.GetString("database"), configuration.GetString("database"),
viper.GetString("sslmode"), configuration.GetString("sslmode"),
viper.GetInt("connection_max_idle"), configuration.GetInt("connection_max_idle"),
viper.GetInt("connection_max_open"), configuration.GetInt("connection_max_open"),
) )
} }

View file

@ -5,7 +5,6 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/spf13/viper"
"sort" "sort"
"strings" "strings"
) )
@ -26,11 +25,11 @@ func (store *RedisStore) GetName() string {
return "redis" return "redis"
} }
func (store *RedisStore) Initialize(viper *viper.Viper) (err error) { func (store *RedisStore) Initialize(configuration filer2.Configuration) (err error) {
return store.initialize( return store.initialize(
viper.GetString("address"), configuration.GetString("address"),
viper.GetString("password"), configuration.GetString("password"),
viper.GetInt("database"), configuration.GetInt("database"),
) )
} }