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.

39 lines
1.0 KiB

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