seaweedfs/go/directory/file_id.go

39 lines
1 KiB
Go
Raw Normal View History

package directory
import (
"code.google.com/p/weed-fs/go/storage"
"code.google.com/p/weed-fs/go/util"
2013-02-27 06:54:22 +00:00
"encoding/hex"
2013-01-17 08:56:56 +00:00
"strings"
)
type FileId struct {
2012-08-24 05:56:14 +00:00
VolumeId storage.VolumeId
Key uint64
Hashcode uint32
}
2012-08-24 05:56:14 +00:00
func NewFileId(VolumeId storage.VolumeId, Key uint64, Hashcode uint32) *FileId {
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
}
2013-01-17 08:56:56 +00:00
func ParseFileId(fid string) *FileId {
a := strings.Split(fid, ",")
if len(a) != 2 {
2012-08-24 08:15:27 +00:00
println("Invalid fid", fid, ", split length", len(a))
return nil
}
vid_string, key_hash_string := a[0], a[1]
2013-01-17 08:56:56 +00:00
volumeId, _ := storage.NewVolumeId(vid_string)
key, hash := storage.ParseKeyHash(key_hash_string)
2012-08-24 06:14:54 +00:00
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}
}
func (n *FileId) String() string {
bytes := make([]byte, 12)
util.Uint64toBytes(bytes[0:8], n.Key)
util.Uint32toBytes(bytes[8:12], n.Hashcode)
nonzero_index := 0
for ; bytes[nonzero_index] == 0; nonzero_index++ {
}
2012-08-24 06:14:54 +00:00
return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
}