seaweedfs/weed/storage/types/needle_id_type.go

44 lines
878 B
Go
Raw Normal View History

2018-07-09 07:22:50 +00:00
package types
import (
2018-07-22 00:39:10 +00:00
"fmt"
2018-07-09 07:22:50 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
"strconv"
)
type NeedleId uint64
const (
NeedleIdSize = 8
2018-08-01 06:25:26 +00:00
NeedleIdEmpty = 0
2018-07-09 07:22:50 +00:00
)
func NeedleIdToBytes(bytes []byte, needleId NeedleId) {
util.Uint64toBytes(bytes, uint64(needleId))
}
// NeedleIdToUint64 used to send max needle id to master
func NeedleIdToUint64(needleId NeedleId) uint64 {
return uint64(needleId)
}
2018-07-22 00:39:10 +00:00
func Uint64ToNeedleId(needleId uint64) NeedleId {
2018-07-09 07:22:50 +00:00
return NeedleId(needleId)
}
2018-07-22 00:39:10 +00:00
func BytesToNeedleId(bytes []byte) NeedleId {
2018-07-09 07:22:50 +00:00
return NeedleId(util.BytesToUint64(bytes))
}
func (k NeedleId) String() string {
2018-08-24 05:44:30 +00:00
return strconv.FormatUint(uint64(k), 16)
2018-07-09 07:22:50 +00:00
}
func ParseNeedleId(idString string) (NeedleId, error) {
2018-08-24 05:44:30 +00:00
key, err := strconv.ParseUint(idString, 16, 64)
2018-07-09 07:22:50 +00:00
if err != nil {
return 0, fmt.Errorf("needle id %s format error: %v", idString, err)
}
return NeedleId(key), nil
}