2019-10-29 07:35:16 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-11-27 11:09:42 +00:00
|
|
|
"os"
|
2019-10-29 07:35:16 +00:00
|
|
|
"time"
|
2019-11-29 02:33:18 +00:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/spf13/viper"
|
2019-10-29 07:35:16 +00:00
|
|
|
)
|
|
|
|
|
2019-11-29 02:33:18 +00:00
|
|
|
type BackendStorageFile interface {
|
2019-10-29 07:35:16 +00:00
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
Truncate(off int64) error
|
|
|
|
io.Closer
|
|
|
|
GetStat() (datSize int64, modTime time.Time, err error)
|
|
|
|
String() string
|
2019-11-27 11:09:42 +00:00
|
|
|
Instantiate(src *os.File) error
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
2019-11-19 03:24:34 +00:00
|
|
|
|
2019-11-29 02:33:18 +00:00
|
|
|
type BackendStorage interface {
|
|
|
|
Name() string
|
|
|
|
NewStorageFile(key string) BackendStorageFile
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageType string
|
|
|
|
type BackendStorageFactory interface {
|
|
|
|
StorageType() StorageType
|
|
|
|
BuildStorage(configuration util.Configuration, id string) (BackendStorage, error)
|
|
|
|
}
|
|
|
|
|
2019-11-19 03:24:34 +00:00
|
|
|
var (
|
2019-11-29 02:33:18 +00:00
|
|
|
BackendStorageFactories = make(map[StorageType]BackendStorageFactory)
|
|
|
|
BackendStorages = make(map[string]BackendStorage)
|
2019-11-19 03:24:34 +00:00
|
|
|
)
|
2019-11-29 02:33:18 +00:00
|
|
|
|
|
|
|
func LoadConfiguration(config *viper.Viper) {
|
|
|
|
|
|
|
|
StorageBackendPrefix := "storage.backend"
|
|
|
|
|
|
|
|
backendSub := config.Sub(StorageBackendPrefix)
|
|
|
|
|
|
|
|
for backendTypeName, _ := range config.GetStringMap(StorageBackendPrefix) {
|
|
|
|
backendStorageFactory, found := BackendStorageFactories[StorageType(backendTypeName)]
|
|
|
|
if !found {
|
|
|
|
glog.Fatalf("backend storage type %s not found", backendTypeName)
|
|
|
|
}
|
|
|
|
backendTypeSub := backendSub.Sub(backendTypeName)
|
|
|
|
for backendStorageId, _ := range backendSub.GetStringMap(backendTypeName) {
|
|
|
|
backendStorage, buildErr := backendStorageFactory.BuildStorage(backendTypeSub.Sub(backendStorageId), backendStorageId)
|
|
|
|
if buildErr != nil {
|
|
|
|
glog.Fatalf("fail to create backend storage %s.%s", backendTypeName, backendStorageId)
|
|
|
|
}
|
|
|
|
BackendStorages[backendTypeName+"."+backendStorageId] = backendStorage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|