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.

76 lines
2.4 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. "io"
  6. "strings"
  7. "sync"
  8. )
  9. func ParseLocation(remote string) (loc *filer_pb.RemoteStorageLocation) {
  10. loc = &filer_pb.RemoteStorageLocation{}
  11. if strings.HasSuffix(string(remote), "/") {
  12. remote = remote[:len(remote)-1]
  13. }
  14. parts := strings.SplitN(string(remote), "/", 3)
  15. if len(parts) >= 1 {
  16. loc.Name = parts[0]
  17. }
  18. if len(parts) >= 2 {
  19. loc.Bucket = parts[1]
  20. }
  21. loc.Path = string(remote[len(loc.Name)+1+len(loc.Bucket):])
  22. if loc.Path == "" {
  23. loc.Path = "/"
  24. }
  25. return
  26. }
  27. type VisitFunc func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error
  28. type RemoteStorageClient interface {
  29. Traverse(loc *filer_pb.RemoteStorageLocation, visitFn VisitFunc) error
  30. ReadFile(loc *filer_pb.RemoteStorageLocation, offset int64, size int64) (data []byte, err error)
  31. WriteDirectory(loc *filer_pb.RemoteStorageLocation, entry *filer_pb.Entry) (err error)
  32. WriteFile(loc *filer_pb.RemoteStorageLocation, entry *filer_pb.Entry, reader io.Reader) (remoteEntry *filer_pb.RemoteEntry, err error)
  33. UpdateFileMetadata(loc *filer_pb.RemoteStorageLocation, entry *filer_pb.Entry) (err error)
  34. DeleteFile(loc *filer_pb.RemoteStorageLocation) (err error)
  35. }
  36. type RemoteStorageClientMaker interface {
  37. Make(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error)
  38. }
  39. var (
  40. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  41. remoteStorageClients = make(map[string]RemoteStorageClient)
  42. remoteStorageClientsLock sync.Mutex
  43. )
  44. func makeRemoteStorageClient(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  45. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  46. if !found {
  47. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  48. }
  49. return maker.Make(remoteConf)
  50. }
  51. func GetRemoteStorage(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  52. remoteStorageClientsLock.Lock()
  53. defer remoteStorageClientsLock.Unlock()
  54. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  55. if found {
  56. return existingRemoteStorageClient, nil
  57. }
  58. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  59. if err != nil {
  60. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  61. }
  62. remoteStorageClients[remoteConf.Name] = newRemoteStorageClient
  63. return newRemoteStorageClient, nil
  64. }