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.

81 lines
1.9 KiB

6 years ago
6 years ago
11 years ago
  1. package needle
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "strings"
  6. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  7. )
  8. type FileId struct {
  9. VolumeId VolumeId
  10. Key NeedleId
  11. Cookie Cookie
  12. }
  13. func NewFileIdFromNeedle(VolumeId VolumeId, n *Needle) *FileId {
  14. return &FileId{VolumeId: VolumeId, Key: n.Id, Cookie: n.Cookie}
  15. }
  16. func NewFileId(VolumeId VolumeId, key uint64, cookie uint32) *FileId {
  17. return &FileId{VolumeId: VolumeId, Key: Uint64ToNeedleId(key), Cookie: Uint32ToCookie(cookie)}
  18. }
  19. // Deserialize the file id
  20. func ParseFileIdFromString(fid string) (*FileId, error) {
  21. vid, needleKeyCookie, err := splitVolumeId(fid)
  22. if err != nil {
  23. return nil, err
  24. }
  25. volumeId, err := NewVolumeId(vid)
  26. if err != nil {
  27. return nil, err
  28. }
  29. nid, cookie, err := ParseNeedleIdCookie(needleKeyCookie)
  30. if err != nil {
  31. return nil, err
  32. }
  33. fileId := &FileId{VolumeId: volumeId, Key: nid, Cookie: cookie}
  34. return fileId, nil
  35. }
  36. func (n *FileId) GetVolumeId() VolumeId {
  37. return n.VolumeId
  38. }
  39. func (n *FileId) GetNeedleId() NeedleId {
  40. return n.Key
  41. }
  42. func (n *FileId) GetCookie() Cookie {
  43. return n.Cookie
  44. }
  45. func (n *FileId) GetNeedleIdCookie() string {
  46. return formatNeedleIdCookie(n.Key, n.Cookie)
  47. }
  48. func (n *FileId) String() string {
  49. return n.VolumeId.String() + "," + formatNeedleIdCookie(n.Key, n.Cookie)
  50. }
  51. func formatNeedleIdCookie(key NeedleId, cookie Cookie) string {
  52. bytes := make([]byte, NeedleIdSize+CookieSize)
  53. NeedleIdToBytes(bytes[0:NeedleIdSize], key)
  54. CookieToBytes(bytes[NeedleIdSize:NeedleIdSize+CookieSize], cookie)
  55. nonzero_index := 0
  56. for ; bytes[nonzero_index] == 0; nonzero_index++ {
  57. }
  58. return hex.EncodeToString(bytes[nonzero_index:])
  59. }
  60. // copied from operation/delete_content.go, to cut off cycle dependency
  61. func splitVolumeId(fid string) (vid string, key_cookie string, err error) {
  62. commaIndex := strings.Index(fid, ",")
  63. if commaIndex <= 0 {
  64. return "", "", fmt.Errorf("wrong fid format")
  65. }
  66. return fid[:commaIndex], fid[commaIndex+1:], nil
  67. }