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.

102 lines
2.5 KiB

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