-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
94 lines (82 loc) · 1.86 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package lang
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/google/uuid"
)
const (
// Random string generation
letterBytes = "abcdefghijklmnopqrstuvwxyz1234567890"
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
var (
// Random source by nano time
randSrc = rand.NewSource(time.Now().UnixNano())
)
// NVL is null value logic
func NVL(str string, def string) string {
if len(str) == 0 {
return def
}
return str
}
// get store cluster key
func GetStoreClusterKey(namespace string, clusterName string) string {
if clusterName == "" {
return fmt.Sprintf("/ns/%s/clusters", namespace)
} else {
return fmt.Sprintf("/ns/%s/clusters/%s", namespace, clusterName)
}
}
// get store node key
func GetStoreNodeKey(namespace string, clusterName string, nodeName string) string {
if nodeName == "" {
return fmt.Sprintf("/ns/%s/clusters/%s/nodes", namespace, clusterName)
} else {
return fmt.Sprintf("/ns/%s/clusters/%s/nodes/%s", namespace, clusterName, nodeName)
}
}
// get uuid
func GetUid() string {
return uuid.New().String()
}
// Random string generation
func GetRandomString(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, randSrc.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = randSrc.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// get node name
func GetNodeName(clusterName string, role string, idx int) string {
return fmt.Sprintf("%s-%s-%d-%s", clusterName, role[:1], idx, GetRandomString(5))
}
func GetIdxToInt(idx string) int {
i, err := strconv.Atoi(idx)
if err != nil {
i = 0
}
return i
}
func GetMaxNumber(arr []int) int {
max := 0
for _, val := range arr {
if val > max {
max = val
}
}
return max
}