2013-04-16 07:10:21 +00:00
|
|
|
package storage
|
2011-12-20 09:00:01 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-06 09:03:32 +00:00
|
|
|
"encoding/binary"
|
2013-02-27 06:54:22 +00:00
|
|
|
"encoding/hex"
|
2013-08-14 07:31:02 +00:00
|
|
|
"errors"
|
2013-01-17 08:56:56 +00:00
|
|
|
"strings"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-04-16 19:18:06 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
2011-12-20 09:00:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileId struct {
|
2013-04-16 07:10:21 +00:00
|
|
|
VolumeId VolumeId
|
2011-12-20 09:00:01 +00:00
|
|
|
Key uint64
|
|
|
|
Hashcode uint32
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:57:10 +00:00
|
|
|
func NewFileIdFromNeedle(VolumeId VolumeId, n *Needle) *FileId {
|
|
|
|
return &FileId{VolumeId: VolumeId, Key: n.Id, Hashcode: n.Cookie}
|
|
|
|
}
|
2013-04-16 07:10:21 +00:00
|
|
|
func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId {
|
2011-12-20 09:00:01 +00:00
|
|
|
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
|
|
|
|
}
|
2013-08-14 07:31:02 +00:00
|
|
|
func ParseFileId(fid string) (*FileId, error) {
|
2011-12-22 04:04:47 +00:00
|
|
|
a := strings.Split(fid, ",")
|
2011-12-20 09:00:01 +00:00
|
|
|
if len(a) != 2 {
|
2013-08-14 07:31:02 +00:00
|
|
|
glog.V(1).Infoln("Invalid fid ", fid, ", split length ", len(a))
|
|
|
|
return nil, errors.New("Invalid fid " + fid)
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
|
|
|
vid_string, key_hash_string := a[0], a[1]
|
2013-04-16 07:10:21 +00:00
|
|
|
volumeId, _ := NewVolumeId(vid_string)
|
2013-12-09 21:53:24 +00:00
|
|
|
key, hash, e := ParseKeyHash(key_hash_string)
|
|
|
|
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, e
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
|
|
|
func (n *FileId) String() string {
|
|
|
|
bytes := make([]byte, 12)
|
2016-04-06 09:03:32 +00:00
|
|
|
binary.BigEndian.PutUint64(bytes[0:8], n.Key)
|
|
|
|
binary.BigEndian.PutUint32(bytes[8:12], n.Hashcode)
|
2011-12-20 09:00:01 +00:00
|
|
|
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:])
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|