1
// Copyright 2022 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
24
// basic type for all module
32
func (id DiskID) Encode() []byte {
33
key := make([]byte, 4)
34
binary.BigEndian.PutUint32(key, uint32(id))
38
func (id *DiskID) Decode(b []byte) DiskID {
39
key := binary.BigEndian.Uint32(b)
44
func (id DiskID) ToString() string {
45
return strconv.FormatUint(uint64(id), 10)
48
func (vid Vid) ToString() string {
49
return strconv.FormatUint(uint64(vid), 10)
52
func (id ClusterID) ToString() string {
53
return strconv.FormatUint(uint64(id), 10)
58
// EncodeToken encode host and vid to a string token.
59
func EncodeToken(host string, vid Vid) (token string) {
60
return fmt.Sprintf("%s%s%s", host, seqToken, strconv.FormatUint(uint64(vid), 10))
63
// DecodeToken decode host and vid from the token.
64
func DecodeToken(token string) (host string, vid Vid, err error) {
65
parts := strings.SplitN(token, seqToken, 2)
67
err = fmt.Errorf("invalid token %s", token)
71
vidU32, err := strconv.ParseUint(parts[1], 10, 32)