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.

79 lines
1.6 KiB

  1. package filer_pb
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  4. )
  5. func toFileIdObject(fileIdStr string) (*FileId, error) {
  6. t, err := needle.ParseFileIdFromString(fileIdStr)
  7. if err != nil {
  8. return nil, err
  9. }
  10. return &FileId{
  11. VolumeId: uint32(t.VolumeId),
  12. Cookie: uint32(t.Cookie),
  13. FileKey: uint64(t.Key),
  14. }, nil
  15. }
  16. func (fid *FileId) toFileIdString() string {
  17. return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
  18. }
  19. func ChunkEquals(this, that *FileChunk) bool {
  20. if this.Fid == nil{
  21. this.Fid, _ = toFileIdObject(this.FileId)
  22. }
  23. if that.Fid == nil{
  24. that.Fid, _ = toFileIdObject(that.FileId)
  25. }
  26. return this.Fid.FileKey == that.Fid.FileKey && this.Fid.VolumeId == that.Fid.VolumeId && this.Fid.Cookie == that.Fid.Cookie
  27. }
  28. func (c *FileChunk) GetFileIdString() string {
  29. if c.FileId != "" {
  30. return c.FileId
  31. }
  32. if c.Fid != nil {
  33. c.FileId = c.Fid.toFileIdString()
  34. return c.FileId
  35. }
  36. return ""
  37. }
  38. func BeforeEntrySerialization(chunks []*FileChunk) {
  39. for _, chunk := range chunks {
  40. if chunk.FileId != "" {
  41. if fid, err := toFileIdObject(chunk.FileId); err == nil {
  42. chunk.Fid = fid
  43. chunk.FileId = ""
  44. }
  45. }
  46. if chunk.SourceFileId != "" {
  47. if fid, err := toFileIdObject(chunk.SourceFileId); err == nil {
  48. chunk.SourceFid = fid
  49. chunk.SourceFileId = ""
  50. }
  51. }
  52. }
  53. }
  54. func AfterEntryDeserialization(chunks []*FileChunk) {
  55. for _, chunk := range chunks {
  56. if chunk.Fid != nil && chunk.FileId == "" {
  57. chunk.FileId = chunk.Fid.toFileIdString()
  58. }
  59. if chunk.SourceFid != nil && chunk.SourceFileId == "" {
  60. chunk.SourceFileId = chunk.SourceFid.toFileIdString()
  61. }
  62. }
  63. }