-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_gtid_of_binlog.go
62 lines (51 loc) · 1.13 KB
/
get_gtid_of_binlog.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
package mysql_binlog_utils
import (
"encoding/binary"
"io"
"os"
"strconv"
gtid "github.com/ikarishinjieva/go-gtid"
)
func GetGtidOfBinlog(binlogPath string) (gtidDesc string, err error) {
file, err := os.Open(binlogPath)
if nil != err {
return "", err
}
defer file.Close()
p := int64(4)
headerBs := make([]byte, 19)
gtidBs := make([]byte, 25)
for {
if _, err := file.Seek(p, 0); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
if _, err := io.ReadFull(file, headerBs); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
length := binary.LittleEndian.Uint32(headerBs[9:13])
eventType := int(headerBs[4])
if GTID_LOG_EVENT != eventType {
p += int64(length)
continue
}
if _, err := io.ReadFull(file, gtidBs); nil != err {
if "EOF" == err.Error() {
break
}
return gtidDesc, err
}
uuid := bytesToUuid(gtidBs[1:17])
number := bytesToUint64(gtidBs[17:])
if gtidDesc, err = gtid.GtidAdd(gtidDesc, uuid+":"+strconv.FormatUint(number, 10)); nil != err {
return gtidDesc, err
}
p += int64(length)
}
return gtidDesc, nil
}