2011-12-20 17:00:01 +08:00
|
|
|
package directory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2011-12-22 12:04:47 +08:00
|
|
|
"log"
|
2012-06-29 15:53:47 +08:00
|
|
|
"pkg/storage"
|
2011-12-20 17:00:01 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2012-06-29 15:53:47 +08:00
|
|
|
"pkg/util"
|
2011-12-20 17:00:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileId struct {
|
2012-08-24 13:56:14 +08:00
|
|
|
VolumeId storage.VolumeId
|
2011-12-20 17:00:01 +08:00
|
|
|
Key uint64
|
|
|
|
Hashcode uint32
|
|
|
|
}
|
|
|
|
|
2012-08-24 13:56:14 +08:00
|
|
|
func NewFileId(VolumeId storage.VolumeId, Key uint64, Hashcode uint32) *FileId {
|
2011-12-20 17:00:01 +08:00
|
|
|
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
|
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
func ParseFileId(fid string) *FileId{
|
2011-12-22 12:04:47 +08:00
|
|
|
a := strings.Split(fid, ",")
|
2011-12-20 17:00:01 +08:00
|
|
|
if len(a) != 2 {
|
2012-06-29 15:53:47 +08:00
|
|
|
log.Println("Invalid fid", fid, ", split length", len(a))
|
2011-12-20 17:00:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
vid_string, key_hash_string := a[0], a[1]
|
2012-06-29 15:53:47 +08:00
|
|
|
vid, _ := strconv.ParseUint(vid_string, 10, 64)
|
2011-12-22 12:04:47 +08:00
|
|
|
key, hash := storage.ParseKeyHash(key_hash_string)
|
2012-08-24 13:56:14 +08:00
|
|
|
return &FileId{VolumeId: storage.VolumeId(vid), Key: key, Hashcode: hash}
|
2011-12-20 17:00:01 +08:00
|
|
|
}
|
|
|
|
func (n *FileId) String() string {
|
|
|
|
bytes := make([]byte, 12)
|
2011-12-25 13:30:57 +08:00
|
|
|
util.Uint64toBytes(bytes[0:8], n.Key)
|
|
|
|
util.Uint32toBytes(bytes[8:12], n.Hashcode)
|
2011-12-20 17:00:01 +08:00
|
|
|
nonzero_index := 0
|
|
|
|
for ; bytes[nonzero_index] == 0; nonzero_index++ {
|
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
return strconv.FormatUint(uint64(n.VolumeId), 10) + "," + hex.EncodeToString(bytes[nonzero_index:])
|
2011-12-20 17:00:01 +08:00
|
|
|
}
|