Skip to content

Commit 12e8984

Browse files
cenkorepomelo2016
andauthored
add previousgtidsevent (go-mysql-org#469)
* add previousgtidsevent Co-authored-by: pomelo2016 <[email protected]>
1 parent a9add8d commit 12e8984

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

replication/event.go

+50
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package replication
22

33
import (
44
"encoding/binary"
5+
"encoding/hex"
56
"fmt"
67
"io"
78
"strconv"
@@ -217,6 +218,55 @@ func (e *RotateEvent) Dump(w io.Writer) {
217218
fmt.Fprintln(w)
218219
}
219220

221+
type PreviousGTIDsEvent struct {
222+
GTIDSets string
223+
}
224+
225+
func (e *PreviousGTIDsEvent) Decode(data []byte) error {
226+
var previousGTIDSets []string
227+
pos := 0
228+
uuidCount := binary.LittleEndian.Uint16(data[pos:pos+8])
229+
pos += 8
230+
231+
for i := uint16(0);i < uuidCount; i++ {
232+
uuid := e.decodeUuid(data[pos:pos+16])
233+
pos += 16
234+
sliceCount := binary.LittleEndian.Uint16(data[pos:pos+8])
235+
pos += 8
236+
var intervals []string
237+
for i := uint16(0);i < sliceCount; i++ {
238+
start := e.decodeInterval(data[pos:pos+8])
239+
pos += 8
240+
stop := e.decodeInterval(data[pos:pos+8])
241+
pos += 8
242+
interval := ""
243+
if stop == start+1 {
244+
interval = fmt.Sprintf("%d",start)
245+
}else {
246+
interval = fmt.Sprintf("%d-%d",start,stop-1)
247+
}
248+
intervals = append(intervals,interval)
249+
}
250+
previousGTIDSets = append(previousGTIDSets,fmt.Sprintf("%s:%s",uuid,strings.Join(intervals,":")))
251+
}
252+
e.GTIDSets = fmt.Sprintf("%s",strings.Join(previousGTIDSets,","))
253+
return nil
254+
}
255+
256+
func (e *PreviousGTIDsEvent) Dump(w io.Writer) {
257+
fmt.Fprintf(w, "Previous GTID Event: %s\n", e.GTIDSets)
258+
fmt.Fprintln(w)
259+
}
260+
261+
func (e *PreviousGTIDsEvent) decodeUuid(data []byte) string {
262+
return fmt.Sprintf("%s-%s-%s-%s-%s",hex.EncodeToString(data[0:4]),hex.EncodeToString(data[4:6]),
263+
hex.EncodeToString(data[6:8]),hex.EncodeToString(data[8:10]),hex.EncodeToString(data[10:]))
264+
}
265+
266+
func (e *PreviousGTIDsEvent) decodeInterval(data []byte) uint64 {
267+
return binary.LittleEndian.Uint64(data)
268+
}
269+
220270
type XIDEvent struct {
221271
XID uint64
222272

replication/parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ func (p *BinlogParser) parseEvent(h *EventHeader, data []byte, rawData []byte) (
271271
ee := &MariadbGTIDEvent{}
272272
ee.GTID.ServerID = h.ServerID
273273
e = ee
274+
case PREVIOUS_GTIDS_EVENT:
275+
e = &PreviousGTIDsEvent{}
274276
default:
275277
e = &GenericEvent{}
276278
}

0 commit comments

Comments
 (0)