-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
binlog_event_mariadb.go
94 lines (78 loc) · 2.92 KB
/
binlog_event_mariadb.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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"encoding/binary"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
// mariadbBinlogEvent wraps a raw packet buffer and provides methods to examine
// it by implementing BinlogEvent. Some methods are pulled in from
// binlogEvent.
type mariadbBinlogEvent struct {
binlogEvent
semiSyncAckRequested bool
}
// NewMariadbBinlogEventWithSemiSyncInfo creates a BinlogEvent instance from given byte array
func NewMariadbBinlogEventWithSemiSyncInfo(buf []byte, semiSyncAckRequested bool) BinlogEvent {
return mariadbBinlogEvent{binlogEvent: binlogEvent(buf), semiSyncAckRequested: semiSyncAckRequested}
}
// NewMariadbBinlogEvent creates a BinlogEvent instance from given byte array
func NewMariadbBinlogEvent(buf []byte) BinlogEvent {
return mariadbBinlogEvent{binlogEvent: binlogEvent(buf)}
}
// IsSemiSyncAckRequested implements BinlogEvent.IsSemiSyncAckRequested().
func (ev mariadbBinlogEvent) IsSemiSyncAckRequested() bool {
return ev.semiSyncAckRequested
}
// IsGTID implements BinlogEvent.IsGTID().
func (ev mariadbBinlogEvent) IsGTID() bool {
return ev.Type() == eMariaGTIDEvent
}
// GTID implements BinlogEvent.GTID().
//
// Expected format:
//
// # bytes field
// 8 sequence number
// 4 domain ID
// 1 flags2
func (ev mariadbBinlogEvent) GTID(f BinlogFormat) (GTID, bool, error) {
const FLStandalone = 1
data := ev.Bytes()[f.HeaderLength:]
flags2 := data[8+4]
return MariadbGTID{
Sequence: binary.LittleEndian.Uint64(data[:8]),
Domain: binary.LittleEndian.Uint32(data[8 : 8+4]),
Server: ev.ServerID(),
}, flags2&FLStandalone == 0, nil
}
// PreviousGTIDs implements BinlogEvent.PreviousGTIDs().
func (ev mariadbBinlogEvent) PreviousGTIDs(f BinlogFormat) (Position, error) {
return Position{}, vterrors.Errorf(vtrpc.Code_INTERNAL, "MariaDB should not provide PREVIOUS_GTIDS_EVENT events")
}
// StripChecksum implements BinlogEvent.StripChecksum().
func (ev mariadbBinlogEvent) StripChecksum(f BinlogFormat) (BinlogEvent, []byte, error) {
switch f.ChecksumAlgorithm {
case BinlogChecksumAlgOff, BinlogChecksumAlgUndef:
// There is no checksum.
return ev, nil, nil
default:
// Checksum is the last 4 bytes of the event buffer.
data := ev.Bytes()
length := len(data)
checksum := data[length-4:]
data = data[:length-4]
return mariadbBinlogEvent{binlogEvent: binlogEvent(data)}, checksum, nil
}
}