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.

86 lines
1.9 KiB

5 years ago
  1. package filer_pb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. )
  8. func toFileIdObject(fileIdStr string) (*FileId, error) {
  9. t, err := needle.ParseFileIdFromString(fileIdStr)
  10. if err != nil {
  11. return nil, err
  12. }
  13. return &FileId{
  14. VolumeId: uint32(t.VolumeId),
  15. Cookie: uint32(t.Cookie),
  16. FileKey: uint64(t.Key),
  17. }, nil
  18. }
  19. func (fid *FileId) toFileIdString() string {
  20. return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
  21. }
  22. func (c *FileChunk) GetFileIdString() string {
  23. if c.FileId != "" {
  24. return c.FileId
  25. }
  26. if c.Fid != nil {
  27. c.FileId = c.Fid.toFileIdString()
  28. return c.FileId
  29. }
  30. return ""
  31. }
  32. func BeforeEntrySerialization(chunks []*FileChunk) {
  33. for _, chunk := range chunks {
  34. if chunk.FileId != "" {
  35. if fid, err := toFileIdObject(chunk.FileId); err == nil {
  36. chunk.Fid = fid
  37. chunk.FileId = ""
  38. }
  39. }
  40. if chunk.SourceFileId != "" {
  41. if fid, err := toFileIdObject(chunk.SourceFileId); err == nil {
  42. chunk.SourceFid = fid
  43. chunk.SourceFileId = ""
  44. }
  45. }
  46. }
  47. }
  48. func AfterEntryDeserialization(chunks []*FileChunk) {
  49. for _, chunk := range chunks {
  50. if chunk.Fid != nil && chunk.FileId == "" {
  51. chunk.FileId = chunk.Fid.toFileIdString()
  52. }
  53. if chunk.SourceFid != nil && chunk.SourceFileId == "" {
  54. chunk.SourceFileId = chunk.SourceFid.toFileIdString()
  55. }
  56. }
  57. }
  58. func CreateEntry(client SeaweedFilerClient, request *CreateEntryRequest) error {
  59. resp, err := client.CreateEntry(context.Background(), request)
  60. if err != nil {
  61. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
  62. return fmt.Errorf("CreateEntry: %v", err)
  63. }
  64. if resp.Error != "" {
  65. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
  66. return fmt.Errorf("CreateEntry : %v", resp.Error)
  67. }
  68. return nil
  69. }