mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactoring
This commit is contained in:
parent
5b1fd374be
commit
d0982cafa7
42
weed/storage/types/needle_id_type.go
Normal file
42
weed/storage/types/needle_id_type.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/chrislusf/seaweedfs/weed/util"
|
||||||
|
"strconv"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NeedleId uint64
|
||||||
|
|
||||||
|
const (
|
||||||
|
NeedleIdSize = 8
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint64ToNeedleId(needleId uint64) (NeedleId) {
|
||||||
|
return NeedleId(needleId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BytesToNeedleId(bytes []byte) (NeedleId) {
|
||||||
|
return NeedleId(util.BytesToUint64(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k NeedleId) String() string {
|
||||||
|
return strconv.FormatUint(uint64(k), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseNeedleId(idString string) (NeedleId, error) {
|
||||||
|
key, err := strconv.ParseUint(idString, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("needle id %s format error: %v", idString, err)
|
||||||
|
}
|
||||||
|
return NeedleId(key), nil
|
||||||
|
}
|
|
@ -7,12 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NeedleId uint64
|
|
||||||
type Offset uint32
|
type Offset uint32
|
||||||
type Cookie uint32
|
type Cookie uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NeedleIdSize = 8
|
|
||||||
OffsetSize = 4
|
OffsetSize = 4
|
||||||
SizeSize = 4 // uint32 size
|
SizeSize = 4 // uint32 size
|
||||||
NeedleEntrySize = NeedleIdSize + OffsetSize + SizeSize
|
NeedleEntrySize = NeedleIdSize + OffsetSize + SizeSize
|
||||||
|
@ -22,23 +20,6 @@ const (
|
||||||
CookieSize = 4
|
CookieSize = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Uint64ToNeedleId(needleId uint64) (NeedleId) {
|
|
||||||
return NeedleId(needleId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BytesToNeedleId(bytes []byte) (NeedleId) {
|
|
||||||
return NeedleId(util.BytesToUint64(bytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
func CookieToBytes(bytes []byte, cookie Cookie) {
|
func CookieToBytes(bytes []byte, cookie Cookie) {
|
||||||
util.Uint32toBytes(bytes, uint32(cookie))
|
util.Uint32toBytes(bytes, uint32(cookie))
|
||||||
}
|
}
|
||||||
|
@ -50,6 +31,14 @@ func BytesToCookie(bytes []byte) (Cookie) {
|
||||||
return Cookie(util.BytesToUint32(bytes[0:4]))
|
return Cookie(util.BytesToUint32(bytes[0:4]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseCookie(cookieString string) (Cookie, error) {
|
||||||
|
cookie, err := strconv.ParseUint(cookieString, 16, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("needle cookie %s format error: %v", cookieString, err)
|
||||||
|
}
|
||||||
|
return Cookie(cookie), nil
|
||||||
|
}
|
||||||
|
|
||||||
func OffsetToBytes(bytes []byte, offset Offset) {
|
func OffsetToBytes(bytes []byte, offset Offset) {
|
||||||
util.Uint32toBytes(bytes, uint32(offset))
|
util.Uint32toBytes(bytes, uint32(offset))
|
||||||
}
|
}
|
||||||
|
@ -61,23 +50,3 @@ func Uint32ToOffset(offset uint32) (Offset) {
|
||||||
func BytesToOffset(bytes []byte) (Offset) {
|
func BytesToOffset(bytes []byte) (Offset) {
|
||||||
return Offset(util.BytesToUint32(bytes[0:4]))
|
return Offset(util.BytesToUint32(bytes[0:4]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k NeedleId) String() string {
|
|
||||||
return strconv.FormatUint(uint64(k), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseNeedleId(idString string) (NeedleId, error) {
|
|
||||||
key, err := strconv.ParseUint(idString, 16, 64)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("needle id %s format error: %v", idString, err)
|
|
||||||
}
|
|
||||||
return NeedleId(key), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseCookie(cookieString string) (Cookie, error) {
|
|
||||||
cookie, err := strconv.ParseUint(cookieString, 16, 32)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("needle cookie %s format error: %v", cookieString, err)
|
|
||||||
}
|
|
||||||
return Cookie(cookie), nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue