github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/binlog/event/sid_mysql.go (about) 1 // Copyright 2019 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // binlog events generator for MySQL used to generate some binlog events for tests. 15 // Readability takes precedence over performance. 16 17 package event 18 19 import ( 20 "encoding/hex" 21 22 "github.com/go-mysql-org/go-mysql/replication" 23 "github.com/pingcap/tiflow/dm/pkg/terror" 24 ) 25 26 // SID represents a SERVER_UUID in GTIDEvent/PrevGTIDEvent. 27 type SID [replication.SidLength]byte 28 29 // Bytes returns the byte slices representation of SID. 30 func (sid SID) Bytes() []byte { 31 return sid[:] 32 } 33 34 func (sid SID) String() string { 35 dst := []byte("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") 36 hex.Encode(dst, sid[:4]) 37 hex.Encode(dst[9:], sid[4:6]) 38 hex.Encode(dst[14:], sid[6:8]) 39 hex.Encode(dst[19:], sid[8:10]) 40 hex.Encode(dst[24:], sid[10:16]) 41 return string(dst) 42 } 43 44 // ParseSID parses a SID from its string representation. 45 // ref https://github.com/vitessio/vitess/blob/869543aa2c4c467f146ba7d77f6d090ca7f8a862/go/mysql/mysql56_gtid.go#L66. 46 func ParseSID(s string) (SID, error) { 47 var sid SID 48 if len(s) != 36 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { 49 return sid, terror.ErrBinlogParseSID.Generatef("SID string %s not valid", s) 50 } 51 52 // drop the dashes so we can just check the error of Decode once. 53 b := make([]byte, 0, 32) 54 b = append(b, s[:8]...) 55 b = append(b, s[9:13]...) 56 b = append(b, s[14:18]...) 57 b = append(b, s[19:23]...) 58 b = append(b, s[24:]...) 59 60 if _, err := hex.Decode(sid[:], b); err != nil { 61 return sid, terror.ErrBinlogParseSID.SetMessage("decode % X").Delegate(err, b) 62 } 63 return sid, nil 64 }