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.

43 lines
1.2 KiB

11 years ago
  1. package storage
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "strings"
  6. "github.com/chrislusf/weed-fs/go/glog"
  7. "github.com/chrislusf/weed-fs/go/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. }