volume_grpc_batch_delete.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. )
  10. func (vs *VolumeServer) BatchDelete(ctx context.Context, req *volume_server_pb.BatchDeleteRequest) (*volume_server_pb.BatchDeleteResponse, error) {
  11. resp := &volume_server_pb.BatchDeleteResponse{}
  12. now := uint64(time.Now().Unix())
  13. for _, fid := range req.FileIds {
  14. vid, id_cookie, err := operation.ParseFileId(fid)
  15. if err != nil {
  16. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  17. FileId: fid,
  18. Status: http.StatusBadRequest,
  19. Error: err.Error()})
  20. continue
  21. }
  22. n := new(needle.Needle)
  23. volumeId, _ := needle.NewVolumeId(vid)
  24. n.ParsePath(id_cookie)
  25. cookie := n.Cookie
  26. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  27. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  28. FileId: fid,
  29. Status: http.StatusNotFound,
  30. Error: err.Error(),
  31. })
  32. continue
  33. }
  34. if n.IsChunkedManifest() {
  35. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  36. FileId: fid,
  37. Status: http.StatusNotAcceptable,
  38. Error: "ChunkManifest: not allowed in batch delete mode.",
  39. })
  40. continue
  41. }
  42. if n.Cookie != cookie {
  43. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  44. FileId: fid,
  45. Status: http.StatusBadRequest,
  46. Error: "File Random Cookie does not match.",
  47. })
  48. break
  49. }
  50. n.LastModified = now
  51. if size, err := vs.store.DeleteVolumeNeedle(volumeId, n); err != nil {
  52. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  53. FileId: fid,
  54. Status: http.StatusInternalServerError,
  55. Error: err.Error()},
  56. )
  57. } else {
  58. resp.Results = append(resp.Results, &volume_server_pb.DeleteResult{
  59. FileId: fid,
  60. Status: http.StatusAccepted,
  61. Size: size},
  62. )
  63. }
  64. }
  65. return resp, nil
  66. }