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.

104 lines
2.5 KiB

10 years ago
10 years ago
  1. package operation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "strings"
  7. "sync"
  8. "github.com/chrislusf/seaweedfs/go/security"
  9. "github.com/chrislusf/seaweedfs/go/util"
  10. )
  11. type DeleteResult struct {
  12. Fid string `json:"fid"`
  13. Size int `json:"size"`
  14. Error string `json:"error,omitempty"`
  15. }
  16. func DeleteFile(master string, fileId string, jwt security.EncodedJwt) error {
  17. fileUrl, err := LookupFileId(master, fileId)
  18. if err != nil {
  19. return err
  20. }
  21. return util.Delete(fileUrl, jwt)
  22. }
  23. func ParseFileId(fid string) (vid string, key_cookie string, err error) {
  24. commaIndex := strings.Index(fid, ",")
  25. if commaIndex <= 0 {
  26. return "", "", errors.New("Wrong fid format.")
  27. }
  28. return fid[:commaIndex], fid[commaIndex+1:], nil
  29. }
  30. type DeleteFilesResult struct {
  31. Errors []string
  32. Results []DeleteResult
  33. }
  34. func DeleteFiles(master string, fileIds []string) (*DeleteFilesResult, error) {
  35. vid_to_fileIds := make(map[string][]string)
  36. ret := &DeleteFilesResult{}
  37. var vids []string
  38. for _, fileId := range fileIds {
  39. vid, _, err := ParseFileId(fileId)
  40. if err != nil {
  41. ret.Results = append(ret.Results, DeleteResult{Fid: vid, Error: err.Error()})
  42. continue
  43. }
  44. if _, ok := vid_to_fileIds[vid]; !ok {
  45. vid_to_fileIds[vid] = make([]string, 0)
  46. vids = append(vids, vid)
  47. }
  48. vid_to_fileIds[vid] = append(vid_to_fileIds[vid], fileId)
  49. }
  50. lookupResults, err := LookupVolumeIds(master, vids)
  51. if err != nil {
  52. return ret, err
  53. }
  54. server_to_fileIds := make(map[string][]string)
  55. for vid, result := range lookupResults {
  56. if result.Error != "" {
  57. ret.Errors = append(ret.Errors, result.Error)
  58. continue
  59. }
  60. for _, location := range result.Locations {
  61. if _, ok := server_to_fileIds[location.Url]; !ok {
  62. server_to_fileIds[location.Url] = make([]string, 0)
  63. }
  64. server_to_fileIds[location.Url] = append(
  65. server_to_fileIds[location.Url], vid_to_fileIds[vid]...)
  66. }
  67. }
  68. var wg sync.WaitGroup
  69. for server, fidList := range server_to_fileIds {
  70. wg.Add(1)
  71. go func(server string, fidList []string) {
  72. defer wg.Done()
  73. values := make(url.Values)
  74. for _, fid := range fidList {
  75. values.Add("fid", fid)
  76. }
  77. jsonBlob, err := util.Post("http://"+server+"/delete", values)
  78. if err != nil {
  79. ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
  80. return
  81. }
  82. var result []DeleteResult
  83. err = json.Unmarshal(jsonBlob, &result)
  84. if err != nil {
  85. ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
  86. return
  87. }
  88. ret.Results = append(ret.Results, result...)
  89. }(server, fidList)
  90. }
  91. wg.Wait()
  92. return ret, nil
  93. }