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.

71 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package remote_storage
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "strings"
  6. "sync"
  7. )
  8. type RemoteStorageLocation string
  9. func (remote RemoteStorageLocation) NameBucketPath() (storageName, bucket, remotePath string) {
  10. if strings.HasSuffix(string(remote), "/") {
  11. remote = remote[:len(remote)-1]
  12. }
  13. parts := strings.SplitN(string(remote), "/", 3)
  14. if len(parts)>=1 {
  15. storageName = parts[0]
  16. }
  17. if len(parts)>=2 {
  18. bucket = parts[1]
  19. }
  20. remotePath = string(remote[len(storageName)+1+len(bucket):])
  21. if remotePath == "" {
  22. remotePath = "/"
  23. }
  24. return
  25. }
  26. type VisitFunc func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error
  27. type RemoteStorageClient interface {
  28. Traverse(remote RemoteStorageLocation, visitFn VisitFunc) error
  29. }
  30. type RemoteStorageClientMaker interface {
  31. Make(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error)
  32. }
  33. var (
  34. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  35. remoteStorageClients = make(map[string]RemoteStorageClient)
  36. remoteStorageClientsLock sync.Mutex
  37. )
  38. func makeRemoteStorageClient(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  39. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  40. if !found {
  41. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  42. }
  43. return maker.Make(remoteConf)
  44. }
  45. func GetRemoteStorage(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  46. remoteStorageClientsLock.Lock()
  47. defer remoteStorageClientsLock.Unlock()
  48. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  49. if found {
  50. return existingRemoteStorageClient, nil
  51. }
  52. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  53. if err != nil {
  54. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  55. }
  56. remoteStorageClients[remoteConf.Name] = newRemoteStorageClient
  57. return newRemoteStorageClient, nil
  58. }