forked from GoDannyLai/binlog_rollback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comfuncs.go
186 lines (159 loc) · 4.03 KB
/
comfuncs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"dannytools/ehand"
"dannytools/logging"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
MyPos "github.com/siddontang/go-mysql/mysql"
sliceKits "github.com/toolkits/slice"
)
/*
func CheckErr(err error, errMsg string, errCode int, ifExt bool) {
if err != nil {
if errMsg != "" {
fmt.Printf("%s: %s\n", errMsg, err)
} else {
fmt.Println(err)
}
if ifExt {
if errCode < 1 {
errCode = 1
}
os.Exit(errCode)
}
}
}
*/
func IntSliceToString(iArr []int, sep string, prefix string) string {
sArr := make([]string, len(iArr))
for _, v := range iArr {
sArr = append(sArr, string(v))
}
return prefix + " " + strings.Join(sArr, sep)
}
func StrSliceToString(sArr []string, sep, prefix string) string {
return prefix + " " + strings.Join(sArr, sep)
}
func CheckElementOfSliceStr(arr []string, element string, prefix string, ifExt bool) bool {
if sliceKits.ContainsString(arr, element) {
return true
} else {
if ifExt {
gLogger.WriteToLogByFieldsExitMsgNoErr(fmt.Sprintf("%s, %s",
prefix, StrSliceToString(arr, ",", "valid args are: ")), logging.ERROR, ehand.ERR_INVALID_OPTION)
}
return false
}
}
func CheckElementOfSliceInt(arr []int, element int, prefix string, ifExt bool) bool {
if sliceKits.ContainsInt(arr, element) {
return true
} else {
if ifExt {
gLogger.WriteToLogByFieldsExitMsgNoErr(fmt.Sprintf("%s, %s", prefix,
IntSliceToString(arr, ",", "valid args are: ")), logging.ERROR, ehand.ERR_INVALID_OPTION)
}
return false
}
}
func CompareBinlogPos(sBinFile string, sPos uint, eBinFile string, ePos uint) int {
// 1: greater, -1: less, 0: equal
sp := MyPos.Position{Name: sBinFile, Pos: uint32(sPos)}
ep := MyPos.Position{Name: eBinFile, Pos: uint32(ePos)}
result := sp.Compare(ep)
return result
}
func CheckIsDir(fd string) (bool, string) {
fs, err := os.Stat(fd)
if err != nil {
return false, fd + " not exists"
}
if fs.IsDir() {
return true, ""
} else {
return false, fd + " is not a dir"
}
}
func GetBinlogBasenameAndIndex(binlog string) (string, int) {
binlogFile := filepath.Base(binlog)
arr := strings.Split(binlogFile, ".")
cnt := len(arr)
n, err := strconv.ParseUint(arr[cnt-1], 10, 32)
gLogger.WriteToLogByFieldsErrorExtramsgExit(err, "parse binlog file index number error", logging.ERROR, ehand.ERR_NUMBER_PARSE)
indx := int(n)
baseName := strings.Join(arr[0:cnt-1], "")
return baseName, indx
}
func GetNextBinlog(baseName string, indx int) string {
indx++
//idxStr := strconv.Itoa(indx)
idxStr := fmt.Sprintf("%06d", indx)
return baseName + "." + idxStr
}
func GetDatetimeStr(sec int64, nsec int64, timeFmt string) string {
return time.Unix(sec, nsec).Format(timeFmt)
}
func CommaSeparatedListToArray(str string) []string {
var arr []string
for _, item := range strings.Split(str, ",") {
item = strings.TrimSpace(item)
if item != "" {
arr = append(arr, item)
}
}
return arr
}
func GetAbsTableName(schema, table string) string {
return fmt.Sprintf("%s%s%s", schema, KEY_DB_TABLE_SEP, table)
}
func GetDbTbFromAbsTbName(name string) (string, string) {
arr := strings.Split(name, KEY_DB_TABLE_SEP)
return arr[0], arr[1]
}
func GetBinlogPosAsKey(binlog string, spos, epos uint32) string {
arr := []string{binlog, strconv.FormatUint(uint64(spos), 10), strconv.FormatUint(uint64(epos), 10)}
return strings.Join(arr, KEY_BINLOG_POS_SEP)
}
func GetMaxValue(nums ...int) int {
max := nums[0]
for _, v := range nums {
if v > max {
max = v
}
}
return max
}
func GetMinValue(nums ...int) int {
min := nums[0]
for _, v := range nums {
if v < min {
min = v
}
}
return min
}
func GetLineHeaderStrFromColumnNamesArr(arr []string, sep string) string {
return strings.Join(arr, sep)
}
func ConvertStrArrToIntferfaceArrForPrint(arr []string) []interface{} {
tmp := make([]interface{}, len(arr))
for i, v := range arr {
tmp[i] = v
}
return tmp
}
func CompareEquelByteSlice(s1 []byte, s2 []byte) bool {
if len(s1) != len(s2) {
return false
}
for i, v := range s1 {
if v != s2[i] {
return false
}
}
return true
}