2019-04-19 04:43:36 +00:00
|
|
|
package needle
|
2011-12-20 09:00:01 +00:00
|
|
|
|
|
|
|
import (
|
2013-02-27 06:54:22 +00:00
|
|
|
"encoding/hex"
|
2019-04-20 10:39:06 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-04-19 04:43:36 +00:00
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2011-12-20 09:00:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileId struct {
|
2013-04-16 07:10:21 +00:00
|
|
|
VolumeId VolumeId
|
2018-07-08 09:28:04 +00:00
|
|
|
Key NeedleId
|
|
|
|
Cookie Cookie
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 04:57:10 +00:00
|
|
|
func NewFileIdFromNeedle(VolumeId VolumeId, n *Needle) *FileId {
|
2018-07-08 09:28:04 +00:00
|
|
|
return &FileId{VolumeId: VolumeId, Key: n.Id, Cookie: n.Cookie}
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
|
|
|
|
func NewFileId(VolumeId VolumeId, key uint64, cookie uint32) *FileId {
|
|
|
|
return &FileId{VolumeId: VolumeId, Key: Uint64ToNeedleId(key), Cookie: Uint32ToCookie(cookie)}
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
|
2019-04-20 10:39:06 +00:00
|
|
|
// Deserialize the file id
|
|
|
|
func ParseFileIdFromString(fid string) (*FileId, error) {
|
|
|
|
vid, needleKeyCookie, err := splitVolumeId(fid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
volumeId, err := NewVolumeId(vid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nid, cookie, err := ParseNeedleIdCookie(needleKeyCookie)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fileId := &FileId{VolumeId: volumeId, Key: nid, Cookie: cookie}
|
|
|
|
return fileId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FileId) GetVolumeId() VolumeId {
|
|
|
|
return n.VolumeId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FileId) GetNeedleId() NeedleId {
|
|
|
|
return n.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FileId) GetCookie() Cookie {
|
|
|
|
return n.Cookie
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *FileId) GetNeedleIdCookie() string {
|
|
|
|
return formatNeedleIdCookie(n.Key, n.Cookie)
|
|
|
|
}
|
|
|
|
|
2011-12-20 09:00:01 +00:00
|
|
|
func (n *FileId) String() string {
|
2018-07-22 00:41:21 +00:00
|
|
|
return n.VolumeId.String() + "," + formatNeedleIdCookie(n.Key, n.Cookie)
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatNeedleIdCookie(key NeedleId, cookie Cookie) string {
|
2018-07-08 09:28:04 +00:00
|
|
|
bytes := make([]byte, NeedleIdSize+CookieSize)
|
2018-07-22 00:41:21 +00:00
|
|
|
NeedleIdToBytes(bytes[0:NeedleIdSize], key)
|
|
|
|
CookieToBytes(bytes[NeedleIdSize:NeedleIdSize+CookieSize], cookie)
|
2011-12-20 09:00:01 +00:00
|
|
|
nonzero_index := 0
|
2020-09-15 05:57:23 +00:00
|
|
|
for ; bytes[nonzero_index] == 0 && nonzero_index < NeedleIdSize; nonzero_index++ {
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
2018-07-22 00:41:21 +00:00
|
|
|
return hex.EncodeToString(bytes[nonzero_index:])
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
2019-04-20 10:39:06 +00:00
|
|
|
|
|
|
|
// copied from operation/delete_content.go, to cut off cycle dependency
|
|
|
|
func splitVolumeId(fid string) (vid string, key_cookie string, err error) {
|
|
|
|
commaIndex := strings.Index(fid, ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
return "", "", fmt.Errorf("wrong fid format")
|
|
|
|
}
|
|
|
|
return fid[:commaIndex], fid[commaIndex+1:], nil
|
|
|
|
}
|