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
2.0 KiB

3 years ago
3 years ago
3 years ago
3 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. func ParseLocation(remote string) (loc *filer_pb.RemoteStorageLocation) {
  9. loc = &filer_pb.RemoteStorageLocation{}
  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. loc.Name = parts[0]
  16. }
  17. if len(parts) >= 2 {
  18. loc.Bucket = parts[1]
  19. }
  20. loc.Path = string(remote[len(loc.Name)+1+len(loc.Bucket):])
  21. if loc.Path == "" {
  22. loc.Path = "/"
  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(loc *filer_pb.RemoteStorageLocation, visitFn VisitFunc) error
  29. ReadFile(loc *filer_pb.RemoteStorageLocation, offset int64, size int64) (data[]byte, err error)
  30. }
  31. type RemoteStorageClientMaker interface {
  32. Make(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error)
  33. }
  34. var (
  35. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  36. remoteStorageClients = make(map[string]RemoteStorageClient)
  37. remoteStorageClientsLock sync.Mutex
  38. )
  39. func makeRemoteStorageClient(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  40. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  41. if !found {
  42. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  43. }
  44. return maker.Make(remoteConf)
  45. }
  46. func GetRemoteStorage(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  47. remoteStorageClientsLock.Lock()
  48. defer remoteStorageClientsLock.Unlock()
  49. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  50. if found {
  51. return existingRemoteStorageClient, nil
  52. }
  53. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  54. if err != nil {
  55. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  56. }
  57. remoteStorageClients[remoteConf.Name] = newRemoteStorageClient
  58. return newRemoteStorageClient, nil
  59. }