You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package backend
  2. import (
  3. "io"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "github.com/spf13/viper"
  9. )
  10. type BackendStorageFile interface {
  11. io.ReaderAt
  12. io.WriterAt
  13. Truncate(off int64) error
  14. io.Closer
  15. GetStat() (datSize int64, modTime time.Time, err error)
  16. String() string
  17. Instantiate(src *os.File) error
  18. }
  19. type BackendStorage interface {
  20. Name() string
  21. NewStorageFile(key string) BackendStorageFile
  22. }
  23. type StorageType string
  24. type BackendStorageFactory interface {
  25. StorageType() StorageType
  26. BuildStorage(configuration util.Configuration, id string) (BackendStorage, error)
  27. }
  28. var (
  29. BackendStorageFactories = make(map[StorageType]BackendStorageFactory)
  30. BackendStorages = make(map[string]BackendStorage)
  31. )
  32. func LoadConfiguration(config *viper.Viper) {
  33. StorageBackendPrefix := "storage.backend"
  34. backendSub := config.Sub(StorageBackendPrefix)
  35. for backendTypeName, _ := range config.GetStringMap(StorageBackendPrefix) {
  36. backendStorageFactory, found := BackendStorageFactories[StorageType(backendTypeName)]
  37. if !found {
  38. glog.Fatalf("backend storage type %s not found", backendTypeName)
  39. }
  40. backendTypeSub := backendSub.Sub(backendTypeName)
  41. for backendStorageId, _ := range backendSub.GetStringMap(backendTypeName) {
  42. backendStorage, buildErr := backendStorageFactory.BuildStorage(backendTypeSub.Sub(backendStorageId), backendStorageId)
  43. if buildErr != nil {
  44. glog.Fatalf("fail to create backend storage %s.%s", backendTypeName, backendStorageId)
  45. }
  46. BackendStorages[backendTypeName+"."+backendStorageId] = backendStorage
  47. if backendStorageId == "default" {
  48. BackendStorages[backendTypeName] = backendStorage
  49. }
  50. }
  51. }
  52. }