cubefs

Форк
0
77 строк · 1.8 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14

15
package proto
16

17
import (
18
	"encoding/binary"
19
	"fmt"
20
	"strconv"
21
	"strings"
22
)
23

24
// basic type for all module
25
type (
26
	DiskID    uint32
27
	BlobID    uint64
28
	Vid       uint32
29
	ClusterID uint32
30
)
31

32
func (id DiskID) Encode() []byte {
33
	key := make([]byte, 4)
34
	binary.BigEndian.PutUint32(key, uint32(id))
35
	return key
36
}
37

38
func (id *DiskID) Decode(b []byte) DiskID {
39
	key := binary.BigEndian.Uint32(b)
40
	*id = DiskID(key)
41
	return *id
42
}
43

44
func (id DiskID) ToString() string {
45
	return strconv.FormatUint(uint64(id), 10)
46
}
47

48
func (vid Vid) ToString() string {
49
	return strconv.FormatUint(uint64(vid), 10)
50
}
51

52
func (id ClusterID) ToString() string {
53
	return strconv.FormatUint(uint64(id), 10)
54
}
55

56
const seqToken = ";"
57

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))
61
}
62

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)
66
	if len(parts) != 2 {
67
		err = fmt.Errorf("invalid token %s", token)
68
		return
69
	}
70
	host = parts[0]
71
	vidU32, err := strconv.ParseUint(parts[1], 10, 32)
72
	if err != nil {
73
		return
74
	}
75
	vid = Vid(vidU32)
76
	return
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.