file_id.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package storage
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. type FileId struct {
  10. VolumeId VolumeId
  11. Key uint64
  12. Hashcode uint32
  13. }
  14. func NewFileIdFromNeedle(VolumeId VolumeId, n *Needle) *FileId {
  15. return &FileId{VolumeId: VolumeId, Key: n.Id, Hashcode: n.Cookie}
  16. }
  17. func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId {
  18. return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
  19. }
  20. func ParseFileId(fid string) (*FileId, error) {
  21. a := strings.Split(fid, ",")
  22. if len(a) != 2 {
  23. glog.V(1).Infoln("Invalid fid ", fid, ", split length ", len(a))
  24. return nil, errors.New("Invalid fid " + fid)
  25. }
  26. vid_string, key_hash_string := a[0], a[1]
  27. volumeId, _ := NewVolumeId(vid_string)
  28. key, hash, e := ParseKeyHash(key_hash_string)
  29. return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, e
  30. }
  31. func (n *FileId) String() string {
  32. bytes := make([]byte, 12)
  33. util.Uint64toBytes(bytes[0:8], n.Key)
  34. util.Uint32toBytes(bytes[8:12], n.Hashcode)
  35. nonzero_index := 0
  36. for ; bytes[nonzero_index] == 0; nonzero_index++ {
  37. }
  38. return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
  39. }