-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheventtype-formatdescription.go
58 lines (49 loc) · 1.87 KB
/
eventtype-formatdescription.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
package binlog
import (
"encoding/binary"
"errors"
)
// A format description event is the first event for binlog-version 4;
// it describes how the following events are structured.
type FormatDescriptionEvent struct {
BinlogVersion uint16
ServerVersion []byte
CreationTimestamp uint32
EventHeaderLength uint8
EventTypeHeaderLengths []byte
}
// Payload is structured as follows for MySQL v5.5:
// 2 bytes (uint16) for binlog version
// 50 bytes for server version string (padded with '\0's)
// 4 bytes (uint32) for created timestamp. Note that this value may be
// unpopulated
// 1 byte (uint8) for total header size, where total header size = common header
// size + extra headers size
// 1 byte per event type for event's fixed length data size. Note that unknown
// events doesn't have an entry
// 27 bytes for events' fixed size length (one uint8 entry per event type except
// unknown events)
func NewFormatDescriptionEvent(b []byte) (Event, error) {
e := new(FormatDescriptionEvent)
i := 0
// Version of this binlog format (2 bytes); should always be 4
e.BinlogVersion = binary.LittleEndian.Uint16(b[i : i+2])
i = i + 2
// Version of the MySQL server that created the binlog (string[50])
e.ServerVersion = make([]byte, 50)
copy(e.ServerVersion, b[i:i+50])
i = i + 50
// Seconds since Unix epoch when the binlog was created (4 bytes)
e.CreationTimestamp = binary.LittleEndian.Uint32(b[i : i+4])
i = i + 4
// Length of the binlog event header of following events; should always match
// const EventHeaderSize (1 byte)
e.EventHeaderLength = b[i]
i = i + 1
if e.EventHeaderLength != byte(EventHeaderSize) {
return nil, errors.New("invalid event header length")
}
// An array indexed by binlogeventtype - 1 to extract the length of the event-specific header (string[p])
e.EventTypeHeaderLengths = b[i:]
return e, nil
}