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.

38 lines
1.0 KiB

13 years ago
  1. package directory
  2. import (
  3. "encoding/hex"
  4. "code.google.com/p/weed-fs/weed/storage"
  5. "code.google.com/p/weed-fs/weed/util"
  6. "strings"
  7. )
  8. type FileId struct {
  9. VolumeId storage.VolumeId
  10. Key uint64
  11. Hashcode uint32
  12. }
  13. func NewFileId(VolumeId storage.VolumeId, Key uint64, Hashcode uint32) *FileId {
  14. return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
  15. }
  16. func ParseFileId(fid string) *FileId {
  17. a := strings.Split(fid, ",")
  18. if len(a) != 2 {
  19. println("Invalid fid", fid, ", split length", len(a))
  20. return nil
  21. }
  22. vid_string, key_hash_string := a[0], a[1]
  23. volumeId, _ := storage.NewVolumeId(vid_string)
  24. key, hash := storage.ParseKeyHash(key_hash_string)
  25. return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}
  26. }
  27. func (n *FileId) String() string {
  28. bytes := make([]byte, 12)
  29. util.Uint64toBytes(bytes[0:8], n.Key)
  30. util.Uint32toBytes(bytes[8:12], n.Hashcode)
  31. nonzero_index := 0
  32. for ; bytes[nonzero_index] == 0; nonzero_index++ {
  33. }
  34. return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
  35. }