mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-12-30 04:57:55 +08:00
b39c384d6d
This reverts commit3523ad5239
, reversing changes made to5d100994b1
.
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
|
)
|
|
|
|
type FileId struct {
|
|
VolumeId VolumeId
|
|
Key uint64
|
|
Hashcode uint32
|
|
}
|
|
|
|
func NewFileIdFromNeedle(VolumeId VolumeId, n *Needle) *FileId {
|
|
return &FileId{VolumeId: VolumeId, Key: n.Id, Hashcode: n.Cookie}
|
|
}
|
|
func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId {
|
|
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
|
|
}
|
|
func ParseFileId(fid string) (*FileId, error) {
|
|
a := strings.Split(fid, ",")
|
|
if len(a) != 2 {
|
|
glog.V(1).Infoln("Invalid fid ", fid, ", split length ", len(a))
|
|
return nil, errors.New("Invalid fid " + fid)
|
|
}
|
|
vid_string, key_hash_string := a[0], a[1]
|
|
volumeId, _ := NewVolumeId(vid_string)
|
|
key, hash, e := ParseKeyHash(key_hash_string)
|
|
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, e
|
|
}
|
|
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++ {
|
|
}
|
|
return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
|
|
}
|