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.

81 lines
2.6 KiB

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