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.

80 lines
2.5 KiB

4 years ago
4 years ago
3 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. "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. func FormatLocation(loc *filer_pb.RemoteStorageLocation) string {
  28. return fmt.Sprintf("%s/%s%s", loc.Name, loc.Bucket, loc.Path)
  29. }
  30. type VisitFunc func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error
  31. type RemoteStorageClient interface {
  32. Traverse(loc *filer_pb.RemoteStorageLocation, visitFn VisitFunc) error
  33. ReadFile(loc *filer_pb.RemoteStorageLocation, offset int64, size int64) (data []byte, err error)
  34. WriteDirectory(loc *filer_pb.RemoteStorageLocation, entry *filer_pb.Entry) (err error)
  35. WriteFile(loc *filer_pb.RemoteStorageLocation, entry *filer_pb.Entry, reader io.Reader) (remoteEntry *filer_pb.RemoteEntry, err error)
  36. UpdateFileMetadata(loc *filer_pb.RemoteStorageLocation, oldEntry *filer_pb.Entry, newEntry *filer_pb.Entry) (err error)
  37. DeleteFile(loc *filer_pb.RemoteStorageLocation) (err error)
  38. }
  39. type RemoteStorageClientMaker interface {
  40. Make(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error)
  41. }
  42. var (
  43. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  44. remoteStorageClients = make(map[string]RemoteStorageClient)
  45. remoteStorageClientsLock sync.Mutex
  46. )
  47. func makeRemoteStorageClient(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  48. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  49. if !found {
  50. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  51. }
  52. return maker.Make(remoteConf)
  53. }
  54. func GetRemoteStorage(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  55. remoteStorageClientsLock.Lock()
  56. defer remoteStorageClientsLock.Unlock()
  57. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  58. if found {
  59. return existingRemoteStorageClient, nil
  60. }
  61. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  62. if err != nil {
  63. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  64. }
  65. remoteStorageClients[remoteConf.Name] = newRemoteStorageClient
  66. return newRemoteStorageClient, nil
  67. }