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.

105 lines
2.5 KiB

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