1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MySQLReplication\Event; |
4
|
|
|
|
5
|
|
|
use MySQLReplication\BinaryDataReader\BinaryDataReaderService; |
6
|
|
|
use MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException; |
7
|
|
|
use MySQLReplication\BinLog\Exception\BinLogException; |
8
|
|
|
use MySQLReplication\BinLog\BinLogSocketConnectInterface; |
9
|
|
|
use MySQLReplication\Config\Config; |
10
|
|
|
use MySQLReplication\Config\Exception\ConfigException; |
11
|
|
|
use MySQLReplication\Definitions\ConstEventsNames; |
12
|
|
|
use MySQLReplication\Definitions\ConstEventType; |
13
|
|
|
use MySQLReplication\Event\Exception\EventException; |
14
|
|
|
use MySQLReplication\Event\RowEvent\RowEventService; |
15
|
|
|
use MySQLReplication\Exception\MySQLReplicationException; |
16
|
|
|
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Event |
21
|
|
|
* @package MySQLReplication\Event |
22
|
|
|
*/ |
23
|
|
|
class Event |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var BinLogSocketConnectInterface |
27
|
|
|
*/ |
28
|
|
|
private $socketConnect; |
29
|
|
|
/** |
30
|
|
|
* @var BinaryDataReaderService |
31
|
|
|
*/ |
32
|
|
|
private $packageService; |
33
|
|
|
/** |
34
|
|
|
* @var RowEventService |
35
|
|
|
*/ |
36
|
|
|
private $rowEventService; |
37
|
|
|
/** |
38
|
|
|
* @var Config |
39
|
|
|
*/ |
40
|
|
|
private $config; |
41
|
|
|
/** |
42
|
|
|
* @var EventDispatcher |
43
|
|
|
*/ |
44
|
|
|
private $eventDispatcher; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* BinLogPack constructor. |
48
|
|
|
* @param Config $config |
49
|
|
|
* @param BinLogSocketConnectInterface $socketConnect |
50
|
|
|
* @param BinaryDataReaderService $packageService |
51
|
|
|
* @param RowEventService $rowEventService |
52
|
|
|
* @param EventDispatcher $eventDispatcher |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
Config $config, |
56
|
|
|
BinLogSocketConnectInterface $socketConnect, |
57
|
|
|
BinaryDataReaderService $packageService, |
58
|
|
|
RowEventService $rowEventService, |
59
|
|
|
EventDispatcher $eventDispatcher |
60
|
|
|
) { |
61
|
|
|
$this->config = $config; |
62
|
|
|
$this->socketConnect = $socketConnect; |
63
|
|
|
$this->packageService = $packageService; |
64
|
|
|
$this->rowEventService = $rowEventService; |
65
|
|
|
$this->eventDispatcher = $eventDispatcher; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws BinaryDataReaderException |
70
|
|
|
* @throws BinLogException |
71
|
|
|
* @throws ConfigException |
72
|
|
|
* @throws EventException |
73
|
|
|
* @throws MySQLReplicationException |
74
|
|
|
* @throws JsonBinaryDecoderException |
75
|
|
|
*/ |
76
|
|
|
public function consume() |
77
|
|
|
{ |
78
|
|
|
$binaryDataReader = $this->packageService->makePackageFromBinaryData( |
79
|
|
|
$this->socketConnect->getPacket(false) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// "ok" value on first byte continue |
83
|
|
|
$binaryDataReader->advance(1); |
84
|
|
|
|
85
|
|
|
// decode all events data |
86
|
|
|
$eventInfo = new EventInfo( |
87
|
|
|
$binaryDataReader->readInt32(), |
88
|
|
|
$binaryDataReader->readUInt8(), |
89
|
|
|
$binaryDataReader->readInt32(), |
90
|
|
|
$binaryDataReader->readInt32(), |
91
|
|
|
$binaryDataReader->readInt32(), |
92
|
|
|
$binaryDataReader->readUInt16(), |
93
|
|
|
$this->socketConnect->getCheckSum() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (ConstEventType::TABLE_MAP_EVENT === $eventInfo->getType()) |
97
|
|
|
{ |
98
|
|
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeTableMapDTO(); |
99
|
|
|
if ($event !== null) |
100
|
|
|
{ |
101
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::TABLE_MAP, $event); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
else |
105
|
|
|
{ |
106
|
|
|
if ([] !== $this->config->getEventsOnly() && !in_array($eventInfo->getType(), $this->config->getEventsOnly(), true)) |
107
|
|
|
{ |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (in_array($eventInfo->getType(), $this->config->getEventsIgnore(), true)) |
112
|
|
|
{ |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (in_array($eventInfo->getType(), [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], true)) |
117
|
|
|
{ |
118
|
|
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeUpdateRowsDTO(); |
119
|
|
|
if ($event !== null) |
120
|
|
|
{ |
121
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::UPDATE, $event); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
elseif (in_array($eventInfo->getType(), [ConstEventType::WRITE_ROWS_EVENT_V1, ConstEventType::WRITE_ROWS_EVENT_V2], true)) |
125
|
|
|
{ |
126
|
|
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeWriteRowsDTO(); |
127
|
|
|
if ($event !== null) |
128
|
|
|
{ |
129
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::WRITE, $event); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
elseif (in_array($eventInfo->getType(), [ConstEventType::DELETE_ROWS_EVENT_V1, ConstEventType::DELETE_ROWS_EVENT_V2], true)) |
133
|
|
|
{ |
134
|
|
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeDeleteRowsDTO(); |
135
|
|
|
if ($event !== null) |
136
|
|
|
{ |
137
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::DELETE, $event); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
elseif (ConstEventType::XID_EVENT === $eventInfo->getType()) |
141
|
|
|
{ |
142
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::XID, (new XidEvent($eventInfo, $binaryDataReader))->makeXidDTO()); |
143
|
|
|
} |
144
|
|
|
elseif (ConstEventType::ROTATE_EVENT === $eventInfo->getType()) |
145
|
|
|
{ |
146
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::ROTATE, (new RotateEvent($eventInfo, $binaryDataReader))->makeRotateEventDTO()); |
147
|
|
|
} |
148
|
|
|
elseif (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) |
149
|
|
|
{ |
150
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::GTID, (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO()); |
151
|
|
|
} |
152
|
|
|
elseif (ConstEventType::QUERY_EVENT === $eventInfo->getType()) |
153
|
|
|
{ |
154
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::QUERY, (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO()); |
155
|
|
|
} |
156
|
|
|
elseif (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) |
157
|
|
|
{ |
158
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::MARIADB_GTID, (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|