From c5a42b0afcb1207fc046828ed6cbc36bf387467a Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 15:49:26 +0100 Subject: [PATCH 1/7] compatibility --- src/MySQLReplication/BinLog/BinLogConnect.php | 1 + .../BinLog/BinLogServerInfo.php | 32 +++++++++++++++++++ .../Definitions/ConstEventType.php | 1 + src/MySQLReplication/Event/QueryEvent.php | 16 +++++++++- src/MySQLReplication/Event/RotateEvent.php | 20 ++++++++++-- .../Repository/MySQLRepository.php | 18 +++++++++-- 6 files changed, 83 insertions(+), 5 deletions(-) mode change 100644 => 100755 src/MySQLReplication/BinLog/BinLogConnect.php mode change 100644 => 100755 src/MySQLReplication/BinLog/BinLogServerInfo.php mode change 100644 => 100755 src/MySQLReplication/Definitions/ConstEventType.php mode change 100644 => 100755 src/MySQLReplication/Event/QueryEvent.php mode change 100644 => 100755 src/MySQLReplication/Event/RotateEvent.php mode change 100644 => 100755 src/MySQLReplication/Repository/MySQLRepository.php diff --git a/src/MySQLReplication/BinLog/BinLogConnect.php b/src/MySQLReplication/BinLog/BinLogConnect.php old mode 100644 new mode 100755 index 7098667..7b9d482 --- a/src/MySQLReplication/BinLog/BinLogConnect.php +++ b/src/MySQLReplication/BinLog/BinLogConnect.php @@ -112,6 +112,7 @@ public function connectToStream() private function serverInfo() { BinLogServerInfo::parsePackage($this->getPacket(false)); + BinLogServerInfo::parseVersion($this->mySQLRepository->getVersion()); } /** diff --git a/src/MySQLReplication/BinLog/BinLogServerInfo.php b/src/MySQLReplication/BinLog/BinLogServerInfo.php old mode 100644 new mode 100755 index b69becc..155802d --- a/src/MySQLReplication/BinLog/BinLogServerInfo.php +++ b/src/MySQLReplication/BinLog/BinLogServerInfo.php @@ -3,11 +3,15 @@ namespace MySQLReplication\BinLog; /** + * todo ugly class, for refactoring * Class BinLogServerInfo * @package MySQLReplication\BinLog */ class BinLogServerInfo { + const MYSQL_VERSION_MARIADB = 'MariaDB'; + const MYSQL_VERSION_PERCONA = 'Percona'; + const MYSQL_VERSION_GENERIC = 'MySQL'; /** * @var array */ @@ -91,6 +95,7 @@ public static function parsePackage($pack) { self::$serverInfo['auth_plugin_name'] .= $pack[$j]; } + self::$serverInfo['version_name'] = self::MYSQL_VERSION_GENERIC; } /** @@ -100,4 +105,31 @@ public static function getSalt() { return self::$serverInfo['salt']; } + + /** + * @see http://stackoverflow.com/questions/37317869/determine-if-mysql-or-percona-or-mariadb + * @param string $version + */ + public static function parseVersion($version) + { + if ('' !== $version) + { + if (false !== strpos($version, self::MYSQL_VERSION_MARIADB)) + { + self::$serverInfo['version_name'] = self::MYSQL_VERSION_MARIADB; + } + else if (false !== strpos($version, self::MYSQL_VERSION_PERCONA)) + { + self::$serverInfo['version_name'] = self::MYSQL_VERSION_PERCONA; + } + } + } + + /** + * @return string + */ + public static function getVersion() + { + return self::$serverInfo['version_name']; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Definitions/ConstEventType.php b/src/MySQLReplication/Definitions/ConstEventType.php old mode 100644 new mode 100755 index f781796..bc9f842 --- a/src/MySQLReplication/Definitions/ConstEventType.php +++ b/src/MySQLReplication/Definitions/ConstEventType.php @@ -5,6 +5,7 @@ /** * Class ConstEventType * @package MySQLReplication\Definitions + * @see https://dev.mysql.com/doc/internals/en/binlog-event-type.html */ class ConstEventType { diff --git a/src/MySQLReplication/Event/QueryEvent.php b/src/MySQLReplication/Event/QueryEvent.php old mode 100644 new mode 100755 index 2b55bc4..f1e1fab --- a/src/MySQLReplication/Event/QueryEvent.php +++ b/src/MySQLReplication/Event/QueryEvent.php @@ -3,11 +3,13 @@ namespace MySQLReplication\Event; use MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException; +use MySQLReplication\BinLog\BinLogServerInfo; use MySQLReplication\Event\DTO\QueryDTO; /** * Class QueryEvent * @package MySQLReplication\Event + * @see https://dev.mysql.com/doc/internals/en/query-event.html */ class QueryEvent extends EventCommon { @@ -26,7 +28,7 @@ public function makeQueryDTO() $schema = $this->binaryDataReader->read($schemaLength); $this->binaryDataReader->advance(1); $query = $this->binaryDataReader->read( - $this->eventInfo->getSize() - 13 - $statusVarsLength - $schemaLength - 1 + $this->eventInfo->getSize() - $this->getSizeToRemoveByVersion() - $statusVarsLength - $schemaLength - 1 ); return new QueryDTO( @@ -36,4 +38,16 @@ public function makeQueryDTO() $query ); } + + /** + * @return int + */ + private function getSizeToRemoveByVersion() + { + if (BinLogServerInfo::MYSQL_VERSION_MARIADB === BinLogServerInfo::getVersion()) + { + return 13; + } + return 36; + } } diff --git a/src/MySQLReplication/Event/RotateEvent.php b/src/MySQLReplication/Event/RotateEvent.php old mode 100644 new mode 100755 index 36d076b..53592c9 --- a/src/MySQLReplication/Event/RotateEvent.php +++ b/src/MySQLReplication/Event/RotateEvent.php @@ -2,22 +2,26 @@ namespace MySQLReplication\Event; +use MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException; +use MySQLReplication\BinLog\BinLogServerInfo; use MySQLReplication\Event\DTO\RotateDTO; /** * Class RotateEvent * @package MySQLReplication\Event + * @see https://dev.mysql.com/doc/internals/en/rotate-event.html */ -class RotateEvent extends EventCommon +class RotateEvent extends EventCommon { /** + * @throws BinaryDataReaderException * @return RotateDTO */ public function makeRotateEventDTO() { $pos = $this->binaryDataReader->readUInt64(); $binFileName = $this->binaryDataReader->read( - $this->eventInfo->getSizeNoHeader() - 8 + $this->eventInfo->getSizeNoHeader() - $this->getSizeToRemoveByVersion() ); return new RotateDTO( @@ -26,4 +30,16 @@ public function makeRotateEventDTO() $binFileName ); } + + /** + * @return int + */ + private function getSizeToRemoveByVersion() + { + if (BinLogServerInfo::MYSQL_VERSION_MARIADB !== BinLogServerInfo::getVersion()) + { + return 8; + } + return 0; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Repository/MySQLRepository.php b/src/MySQLReplication/Repository/MySQLRepository.php old mode 100644 new mode 100755 index 1622c39..7332364 --- a/src/MySQLReplication/Repository/MySQLRepository.php +++ b/src/MySQLReplication/Repository/MySQLRepository.php @@ -56,7 +56,8 @@ public function getFields($schema, $table) */ public function getConnection() { - if (false === $this->connection->ping()) { + if (false === $this->connection->ping()) + { $this->connection->close(); $this->connection->connect(); } @@ -74,6 +75,19 @@ public function isCheckSum() return isset($res['Value']); } + /** + * @return string + */ + public function getVersion() + { + $res = $this->getConnection()->fetchAssoc('SHOW VARIABLES LIKE "version_comment"'); + if (!empty($res['Value'])) + { + return $res['Value']; + } + return ''; + } + /** * File * Position @@ -87,4 +101,4 @@ public function getMasterStatus() { return $this->getConnection()->fetchAssoc('SHOW MASTER STATUS'); } -} +} \ No newline at end of file From 8f3e6c335187fb3ef7b6c63b8f99a9ad2bba8514 Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:11:37 +0100 Subject: [PATCH 2/7] compatibility --- src/MySQLReplication/Event/QueryEvent.php | 2 ++ src/MySQLReplication/Repository/MySQLRepository.php | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/MySQLReplication/Event/QueryEvent.php b/src/MySQLReplication/Event/QueryEvent.php index f1e1fab..0fcf4fb 100755 --- a/src/MySQLReplication/Event/QueryEvent.php +++ b/src/MySQLReplication/Event/QueryEvent.php @@ -44,6 +44,8 @@ public function makeQueryDTO() */ private function getSizeToRemoveByVersion() { + + var_dump(BinLogServerInfo::getVersion()); if (BinLogServerInfo::MYSQL_VERSION_MARIADB === BinLogServerInfo::getVersion()) { return 13; diff --git a/src/MySQLReplication/Repository/MySQLRepository.php b/src/MySQLReplication/Repository/MySQLRepository.php index 7332364..db5d10a 100755 --- a/src/MySQLReplication/Repository/MySQLRepository.php +++ b/src/MySQLReplication/Repository/MySQLRepository.php @@ -80,12 +80,16 @@ public function isCheckSum() */ public function getVersion() { - $res = $this->getConnection()->fetchAssoc('SHOW VARIABLES LIKE "version_comment"'); - if (!empty($res['Value'])) + $r = ''; + $versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"'); + if (is_array($versions) && 0 !== count($versions)) { - return $res['Value']; + foreach ($versions as $version) + { + $r .= $version['Value']; + } } - return ''; + return $r; } /** From 2cd991f8f46477479c5759dbf6c8c81e9c543cbb Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:17:17 +0100 Subject: [PATCH 3/7] clean debug --- src/MySQLReplication/Event/QueryEvent.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/MySQLReplication/Event/QueryEvent.php b/src/MySQLReplication/Event/QueryEvent.php index 0fcf4fb..f1e1fab 100755 --- a/src/MySQLReplication/Event/QueryEvent.php +++ b/src/MySQLReplication/Event/QueryEvent.php @@ -44,8 +44,6 @@ public function makeQueryDTO() */ private function getSizeToRemoveByVersion() { - - var_dump(BinLogServerInfo::getVersion()); if (BinLogServerInfo::MYSQL_VERSION_MARIADB === BinLogServerInfo::getVersion()) { return 13; From 913c014fb4cb0ffe6f97916c778aefeeaa0d8d01 Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:18:18 +0100 Subject: [PATCH 4/7] execute flag fix (windows suxxx) --- src/MySQLReplication/BinLog/BinLogConnect.php | 0 src/MySQLReplication/BinLog/BinLogServerInfo.php | 0 src/MySQLReplication/Definitions/ConstEventType.php | 0 src/MySQLReplication/Event/QueryEvent.php | 0 src/MySQLReplication/Event/RotateEvent.php | 0 src/MySQLReplication/Repository/MySQLRepository.php | 0 6 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/MySQLReplication/BinLog/BinLogConnect.php mode change 100755 => 100644 src/MySQLReplication/BinLog/BinLogServerInfo.php mode change 100755 => 100644 src/MySQLReplication/Definitions/ConstEventType.php mode change 100755 => 100644 src/MySQLReplication/Event/QueryEvent.php mode change 100755 => 100644 src/MySQLReplication/Event/RotateEvent.php mode change 100755 => 100644 src/MySQLReplication/Repository/MySQLRepository.php diff --git a/src/MySQLReplication/BinLog/BinLogConnect.php b/src/MySQLReplication/BinLog/BinLogConnect.php old mode 100755 new mode 100644 diff --git a/src/MySQLReplication/BinLog/BinLogServerInfo.php b/src/MySQLReplication/BinLog/BinLogServerInfo.php old mode 100755 new mode 100644 diff --git a/src/MySQLReplication/Definitions/ConstEventType.php b/src/MySQLReplication/Definitions/ConstEventType.php old mode 100755 new mode 100644 diff --git a/src/MySQLReplication/Event/QueryEvent.php b/src/MySQLReplication/Event/QueryEvent.php old mode 100755 new mode 100644 diff --git a/src/MySQLReplication/Event/RotateEvent.php b/src/MySQLReplication/Event/RotateEvent.php old mode 100755 new mode 100644 diff --git a/src/MySQLReplication/Repository/MySQLRepository.php b/src/MySQLReplication/Repository/MySQLRepository.php old mode 100755 new mode 100644 From 1aec5dce6579e989e2e6a63c5ad5dabdb274c90c Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:19:40 +0100 Subject: [PATCH 5/7] end of line clean --- .gitignore | 18 +- LICENSE | 42 +- example/benchmark.php | 328 +++---- example/dump_events.php | 102 +- phpunit.xml | 40 +- .../BinLog/Exception/BinLogException.php | 24 +- .../BinaryDataReader/BinaryDataReader.php | 874 +++++++++--------- .../BinaryDataReaderBuilder.php | 52 +- .../BinaryDataReaderService.php | 42 +- .../Exception/BinaryDataReaderException.php | 24 +- src/MySQLReplication/Config/ConfigService.php | 166 ++-- .../Config/Exception/ConfigException.php | 68 +- .../Definitions/ConstEventsNames.php | 38 +- .../Event/DTO/DeleteRowsDTO.php | 48 +- src/MySQLReplication/Event/DTO/EventDTO.php | 110 +-- src/MySQLReplication/Event/DTO/GTIDLogDTO.php | 184 ++-- .../Event/DTO/MariaDbGtidLogDTO.php | 214 ++--- src/MySQLReplication/Event/DTO/QueryDTO.php | 220 ++--- src/MySQLReplication/Event/DTO/RotateDTO.php | 184 ++-- src/MySQLReplication/Event/DTO/RowsDTO.php | 194 ++-- .../Event/DTO/TableMapDTO.php | 160 ++-- .../Event/DTO/UpdateRowsDTO.php | 48 +- .../Event/DTO/WriteRowsDTO.php | 46 +- src/MySQLReplication/Event/DTO/XidDTO.php | 152 +-- src/MySQLReplication/Event/EventCommon.php | 68 +- src/MySQLReplication/Event/EventInfo.php | 316 +++---- .../Event/EventSubscribers.php | 268 +++--- .../Event/Exception/EventException.php | 24 +- src/MySQLReplication/Event/GtidEvent.php | 54 +- .../Event/MariaDbGtidEvent.php | 56 +- src/MySQLReplication/Event/QueryEvent.php | 106 +-- src/MySQLReplication/Event/RotateEvent.php | 88 +- .../Event/RowEvent/TableMap.php | 208 ++--- src/MySQLReplication/Event/XidEvent.php | 44 +- .../Exception/MySQLReplicationException.php | 20 +- src/MySQLReplication/Gtid/Gtid.php | 146 +-- src/MySQLReplication/Gtid/GtidCollection.php | 84 +- src/MySQLReplication/Gtid/GtidException.php | 24 +- src/MySQLReplication/Gtid/GtidService.php | 64 +- .../MySQLReplicationFactory.php | 228 ++--- 40 files changed, 2588 insertions(+), 2588 deletions(-) diff --git a/.gitignore b/.gitignore index 1e18be3..9409266 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ -.idea -out.log -log -/vendor -composer.phar -composer.lock -.php_cs.cache -.DS_Store -Thumbs.db +.idea +out.log +log +/vendor +composer.phar +composer.lock +.php_cs.cache +.DS_Store +Thumbs.db /example/profiler.php \ No newline at end of file diff --git a/LICENSE b/LICENSE index ff3f8cd..d2c1ce3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -The MIT License (MIT) - -Copyright (c) 2016 krowinski - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +The MIT License (MIT) + +Copyright (c) 2016 krowinski + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/example/benchmark.php b/example/benchmark.php index 0444eac..27b417d 100644 --- a/example/benchmark.php +++ b/example/benchmark.php @@ -1,164 +1,164 @@ -start = microtime(true); - } - - public function onUpdate(UpdateRowsDTO $event) - { - ++$this->counter; - if (0 === ($this->counter % 1000)) - { - echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL; - } - } -} - -/** - * Class Benchmark - * @package example - */ -class Benchmark -{ - /** - * @var string - */ - private $database = 'mysqlreplication_test'; - - /** - * Benchmark constructor. - * @throws DBALException - * @throws ConfigException - * @throws BinLogException - * @throws MySQLReplicationException - */ - public function __construct() - { - $conn = $this->getConnection(); - $conn->exec('DROP DATABASE IF EXISTS ' . $this->database); - $conn->exec('CREATE DATABASE ' . $this->database); - $conn->exec('USE ' . $this->database); - $conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY'); - $conn->exec('INSERT INTO test VALUES(1)'); - $conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY'); - $conn->exec('INSERT INTO test2 VALUES(1)'); - $conn->exec('RESET MASTER'); - - $this->binLogStream = new MySQLReplicationFactory( - (new ConfigService())->makeConfigFromArray([ - 'user' => 'root', - 'ip' => '127.0.0.1', - 'password' => 'root', - // we only interest in update row event - 'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], - 'slaveId' => 9999 - ]) - ); - - // register benchmark subscriber handler - $this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers()); - } - - /** - * @return Connection - * @throws DBALException - */ - private function getConnection() - { - return DriverManager::getConnection([ - 'user' => 'root', - 'password' => 'root', - 'host' => '127.0.0.1', - 'port' => 3306, - 'driver' => 'pdo_mysql', - 'dbname' => $this->database - ]); - } - - /** - * @throws MySQLReplicationException - * @throws DBALException - */ - public function run() - { - $pid = pcntl_fork(); - if ($pid === -1) - { - die('could not fork'); - } - else if ($pid) - { - $this->consume(); - pcntl_wait($status); - } - else - { - $this->produce(); - } - } - - /** - * @throws MySQLReplicationException - */ - private function consume() - { - while(1) { - $this->binLogStream->binLogEvent(); - } - } - - /** - * @throws DBALException - */ - private function produce() - { - $conn = $this->getConnection(); - - echo 'Start insert data' . PHP_EOL; - while (1) - { - $conn->exec('UPDATE test SET i = i + 1;'); - $conn->exec('UPDATE test2 SET i = i + 1;'); - } - - $conn->close(); - die; - } -} - -(new Benchmark())->run(); +start = microtime(true); + } + + public function onUpdate(UpdateRowsDTO $event) + { + ++$this->counter; + if (0 === ($this->counter % 1000)) + { + echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL; + } + } +} + +/** + * Class Benchmark + * @package example + */ +class Benchmark +{ + /** + * @var string + */ + private $database = 'mysqlreplication_test'; + + /** + * Benchmark constructor. + * @throws DBALException + * @throws ConfigException + * @throws BinLogException + * @throws MySQLReplicationException + */ + public function __construct() + { + $conn = $this->getConnection(); + $conn->exec('DROP DATABASE IF EXISTS ' . $this->database); + $conn->exec('CREATE DATABASE ' . $this->database); + $conn->exec('USE ' . $this->database); + $conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY'); + $conn->exec('INSERT INTO test VALUES(1)'); + $conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY'); + $conn->exec('INSERT INTO test2 VALUES(1)'); + $conn->exec('RESET MASTER'); + + $this->binLogStream = new MySQLReplicationFactory( + (new ConfigService())->makeConfigFromArray([ + 'user' => 'root', + 'ip' => '127.0.0.1', + 'password' => 'root', + // we only interest in update row event + 'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], + 'slaveId' => 9999 + ]) + ); + + // register benchmark subscriber handler + $this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers()); + } + + /** + * @return Connection + * @throws DBALException + */ + private function getConnection() + { + return DriverManager::getConnection([ + 'user' => 'root', + 'password' => 'root', + 'host' => '127.0.0.1', + 'port' => 3306, + 'driver' => 'pdo_mysql', + 'dbname' => $this->database + ]); + } + + /** + * @throws MySQLReplicationException + * @throws DBALException + */ + public function run() + { + $pid = pcntl_fork(); + if ($pid === -1) + { + die('could not fork'); + } + else if ($pid) + { + $this->consume(); + pcntl_wait($status); + } + else + { + $this->produce(); + } + } + + /** + * @throws MySQLReplicationException + */ + private function consume() + { + while(1) { + $this->binLogStream->binLogEvent(); + } + } + + /** + * @throws DBALException + */ + private function produce() + { + $conn = $this->getConnection(); + + echo 'Start insert data' . PHP_EOL; + while (1) + { + $conn->exec('UPDATE test SET i = i + 1;'); + $conn->exec('UPDATE test2 SET i = i + 1;'); + } + + $conn->close(); + die; + } +} + +(new Benchmark())->run(); diff --git a/example/dump_events.php b/example/dump_events.php index e638b08..09a1743 100644 --- a/example/dump_events.php +++ b/example/dump_events.php @@ -1,52 +1,52 @@ -makeConfigFromArray([ - 'user' => 'root', - 'ip' => '127.0.0.1', - 'password' => 'root', - //'mariaDbGtid' => '1-1-3,0-1-88', - //'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592', - ]) -); - -/** - * Class BenchmarkEventSubscribers - * @package example - */ -class MyEventSubscribers extends EventSubscribers -{ - /** - * @param EventDTO $event (your own handler more in EventSubscribers class ) - */ - public function allEvents(EventDTO $event) - { - // all events got __toString() implementation - echo $event; - - // all events got JsonSerializable implementation - //echo json_encode($result, JSON_PRETTY_PRINT); - - echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL; - } -} - -// register your events handler here -$binLogStream->registerSubscriber(new MyEventSubscribers()); - -// start consuming events -while (1) { - $binLogStream->binLogEvent(); +makeConfigFromArray([ + 'user' => 'root', + 'ip' => '127.0.0.1', + 'password' => 'root', + //'mariaDbGtid' => '1-1-3,0-1-88', + //'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592', + ]) +); + +/** + * Class BenchmarkEventSubscribers + * @package example + */ +class MyEventSubscribers extends EventSubscribers +{ + /** + * @param EventDTO $event (your own handler more in EventSubscribers class ) + */ + public function allEvents(EventDTO $event) + { + // all events got __toString() implementation + echo $event; + + // all events got JsonSerializable implementation + //echo json_encode($result, JSON_PRETTY_PRINT); + + echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL; + } +} + +// register your events handler here +$binLogStream->registerSubscriber(new MyEventSubscribers()); + +// start consuming events +while (1) { + $binLogStream->binLogEvent(); } \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 6c68af8..fcc3183 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,21 +1,21 @@ - - - - - ./tests/ - - - - - src/ - - + + + + + ./tests/ + + + + + src/ + + \ No newline at end of file diff --git a/src/MySQLReplication/BinLog/Exception/BinLogException.php b/src/MySQLReplication/BinLog/Exception/BinLogException.php index bf1735e..724d73e 100644 --- a/src/MySQLReplication/BinLog/Exception/BinLogException.php +++ b/src/MySQLReplication/BinLog/Exception/BinLogException.php @@ -1,13 +1,13 @@ -binaryData = $binaryData; - } - - /** - * @param int $length - */ - public function advance($length) - { - $length = (int)$length; - $this->readBytes += $length; - $this->binaryData = substr($this->binaryData, $length); - } - - /** - * @param int $length - * @return string - * @throws BinaryDataReaderException - */ - public function read($length) - { - $length = (int)$length; - $return = substr($this->binaryData, 0, $length); - $this->readBytes += $length; - $this->binaryData = substr($this->binaryData, $length); - - return $return; - } - - /** - * @return int - */ - public function readInt16() - { - return unpack('s', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; - } - - /** - * Push again data in data buffer. It's use when you want - * to extract a bit from a value a let the rest of the code normally - * read the data - * - * @param string $data - */ - public function unread($data) - { - $this->readBytes -= strlen($data); - $this->binaryData = $data . $this->binaryData; - } - - /** - * Read a 'Length Coded Binary' number from the data buffer. - * Length coded numbers can be anywhere from 1 to 9 bytes depending - * on the value of the first byte. - * From PyMYSQL source code - * - * @return int|string - * @throws BinaryDataReaderException - */ - public function readCodedBinary() - { - $c = ord($this->read(self::UNSIGNED_CHAR_LENGTH)); - if ($c == self::NULL_COLUMN) - { - return ''; - } - if ($c < self::UNSIGNED_CHAR_COLUMN) - { - return $c; - } - elseif ($c == self::UNSIGNED_SHORT_COLUMN) - { - return $this->readUInt16(); - - } - elseif ($c == self::UNSIGNED_INT24_COLUMN) - { - return $this->readUInt24(); - } - elseif ($c == self::UNSIGNED_INT64_COLUMN) - { - return $this->readUInt64(); - } - - throw new BinaryDataReaderException('Column num ' . $c . ' not handled'); - } - - /** - * @return int - */ - public function readUInt16() - { - return unpack('v', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; - } - - /** - * @return int - */ - public function readUInt24() - { - $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); - return $data[1] + ($data[2] << 8) + ($data[3] << 16); - } - - /** - * @return int - */ - public function readUInt64() - { - return $this->unpackUInt64($this->read(self::UNSIGNED_INT64_LENGTH)); - } - - /** - * @param string $data - * @return string - */ - public function unpackUInt64($data) - { - $data = unpack('V*', $data); - return bcadd($data[1], bcmul($data[2], bcpow(2, 32))); - } - - /** - * @return int - */ - public function readInt24() - { - $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); - - $res = $data[1] | ($data[2] << 8) | ($data[3] << 16); - if ($res >= 0x800000) - { - $res -= 0x1000000; - } - return $res; - } - - /** - * @return string - */ - public function readInt64() - { - $data = unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH)); - return bcadd($data[1], ($data[2] << 32)); - } - - /** - * @param int $size - * @return string - * @throws BinaryDataReaderException - */ - public function readLengthCodedPascalString($size) - { - return $this->read($this->readUIntBySize($size)); - } - - /** - * Read a little endian integer values based on byte number - * - * @param $size - * @return mixed - * @throws BinaryDataReaderException - */ - public function readUIntBySize($size) - { - if ($size == self::UNSIGNED_CHAR_LENGTH) - { - return $this->readUInt8(); - } - elseif ($size == self::UNSIGNED_SHORT_LENGTH) - { - return $this->readUInt16(); - } - elseif ($size == self::UNSIGNED_INT24_LENGTH) - { - return $this->readUInt24(); - } - elseif ($size == self::UNSIGNED_INT32_LENGTH) - { - return $this->readUInt32(); - } - elseif ($size == self::UNSIGNED_INT40_LENGTH) - { - return $this->readUInt40(); - } - elseif ($size == self::UNSIGNED_INT48_LENGTH) - { - return $this->readUInt48(); - } - elseif ($size == self::UNSIGNED_INT56_LENGTH) - { - return $this->readUInt56(); - } - elseif ($size == self::UNSIGNED_INT64_LENGTH) - { - return $this->readUInt64(); - } - - throw new BinaryDataReaderException('$size ' . $size . ' not handled'); - } - - /** - * @return int - */ - public function readUInt8() - { - return unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; - } - - /** - * @return int - */ - public function readUInt32() - { - return unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; - } - - /** - * @return mixed - */ - public function readUInt40() - { - $data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; - $data2 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; - - return $data1 + ($data2 << 8); - } - - /** - * @return mixed - */ - public function readUInt48() - { - $data = unpack('v3', $this->read(self::UNSIGNED_INT48_LENGTH)); - - return $data[1] + ($data[2] << 16) + ($data[3] << 32); - } - - /** - * @return mixed - */ - public function readUInt56() - { - $data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; - $data2 = unpack('S', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; - $data3 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; - - return $data1 + ($data2 << 8) + ($data3 << 24); - } - - /** - * Read a big endian integer values based on byte number - * - * @param int $size - * @return int - * @throws BinaryDataReaderException - */ - public function readIntBeBySize($size) - { - if ($size == self::UNSIGNED_CHAR_LENGTH) - { - return $this->readInt8(); - } - elseif ($size == self::UNSIGNED_SHORT_LENGTH) - { - return $this->readInt16Be(); - } - elseif ($size == self::UNSIGNED_INT24_LENGTH) - { - return $this->readInt24Be(); - } - elseif ($size == self::UNSIGNED_INT32_LENGTH) - { - return $this->readInt32Be(); - } - elseif ($size == self::UNSIGNED_INT40_LENGTH) - { - return $this->readInt40Be(); - } - - throw new BinaryDataReaderException('$size ' . $size . ' not handled'); - } - - /** - * @return int - */ - public function readInt8() - { - return unpack('c', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; - } - - /** - * @return mixed - */ - public function readInt16Be() - { - return unpack('n', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; - } - - /** - * @return int - */ - public function readInt24Be() - { - $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); - $res = ($data[1] << 16) | ($data[2] << 8) | $data[3]; - if ($res >= 0x800000) - { - $res -= 0x1000000; - } - - return $res; - } - - /** - * @return int - */ - public function readInt32Be() - { - return unpack('i', strrev($this->read(self::UNSIGNED_INT32_LENGTH)))[1]; - } - - /** - * @return int - */ - public function readInt40Be() - { - $data1 = unpack('N', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; - $data2 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; - - return $data2 + ($data1 << 8); - } - - /** - * @return int - */ - public function readInt32() - { - return unpack('i', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; - } - - /** - * @return float - */ - public function readFloat() - { - return unpack('f', $this->read(self::UNSIGNED_FLOAT_LENGTH))[1]; - } - - /** - * @return double - */ - public function readDouble() - { - return unpack('d', $this->read(self::UNSIGNED_DOUBLE_LENGTH))[1]; - } - - /** - * @return string - */ - public function readTableId() - { - return $this->unpackUInt64($this->read(self::UNSIGNED_INT48_LENGTH) . chr(0) . chr(0)); - } - - /** - * @param int $size - * @return bool - */ - public function isComplete($size) - { - if ($this->readBytes + 1 - 20 < $size) - { - return false; - } - return true; - } - - /** - * @param int $value - * @return string - */ - public static function pack64bit($value) - { - return pack('C8', - ($value >> 0) & 0xFF, - ($value >> 8) & 0xFF, - ($value >> 16) & 0xFF, - ($value >> 24) & 0xFF, - ($value >> 32) & 0xFF, - ($value >> 40) & 0xFF, - ($value >> 48) & 0xFF, - ($value >> 56) & 0xFF - ); - } +binaryData = $binaryData; + } + + /** + * @param int $length + */ + public function advance($length) + { + $length = (int)$length; + $this->readBytes += $length; + $this->binaryData = substr($this->binaryData, $length); + } + + /** + * @param int $length + * @return string + * @throws BinaryDataReaderException + */ + public function read($length) + { + $length = (int)$length; + $return = substr($this->binaryData, 0, $length); + $this->readBytes += $length; + $this->binaryData = substr($this->binaryData, $length); + + return $return; + } + + /** + * @return int + */ + public function readInt16() + { + return unpack('s', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; + } + + /** + * Push again data in data buffer. It's use when you want + * to extract a bit from a value a let the rest of the code normally + * read the data + * + * @param string $data + */ + public function unread($data) + { + $this->readBytes -= strlen($data); + $this->binaryData = $data . $this->binaryData; + } + + /** + * Read a 'Length Coded Binary' number from the data buffer. + * Length coded numbers can be anywhere from 1 to 9 bytes depending + * on the value of the first byte. + * From PyMYSQL source code + * + * @return int|string + * @throws BinaryDataReaderException + */ + public function readCodedBinary() + { + $c = ord($this->read(self::UNSIGNED_CHAR_LENGTH)); + if ($c == self::NULL_COLUMN) + { + return ''; + } + if ($c < self::UNSIGNED_CHAR_COLUMN) + { + return $c; + } + elseif ($c == self::UNSIGNED_SHORT_COLUMN) + { + return $this->readUInt16(); + + } + elseif ($c == self::UNSIGNED_INT24_COLUMN) + { + return $this->readUInt24(); + } + elseif ($c == self::UNSIGNED_INT64_COLUMN) + { + return $this->readUInt64(); + } + + throw new BinaryDataReaderException('Column num ' . $c . ' not handled'); + } + + /** + * @return int + */ + public function readUInt16() + { + return unpack('v', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; + } + + /** + * @return int + */ + public function readUInt24() + { + $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); + return $data[1] + ($data[2] << 8) + ($data[3] << 16); + } + + /** + * @return int + */ + public function readUInt64() + { + return $this->unpackUInt64($this->read(self::UNSIGNED_INT64_LENGTH)); + } + + /** + * @param string $data + * @return string + */ + public function unpackUInt64($data) + { + $data = unpack('V*', $data); + return bcadd($data[1], bcmul($data[2], bcpow(2, 32))); + } + + /** + * @return int + */ + public function readInt24() + { + $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); + + $res = $data[1] | ($data[2] << 8) | ($data[3] << 16); + if ($res >= 0x800000) + { + $res -= 0x1000000; + } + return $res; + } + + /** + * @return string + */ + public function readInt64() + { + $data = unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH)); + return bcadd($data[1], ($data[2] << 32)); + } + + /** + * @param int $size + * @return string + * @throws BinaryDataReaderException + */ + public function readLengthCodedPascalString($size) + { + return $this->read($this->readUIntBySize($size)); + } + + /** + * Read a little endian integer values based on byte number + * + * @param $size + * @return mixed + * @throws BinaryDataReaderException + */ + public function readUIntBySize($size) + { + if ($size == self::UNSIGNED_CHAR_LENGTH) + { + return $this->readUInt8(); + } + elseif ($size == self::UNSIGNED_SHORT_LENGTH) + { + return $this->readUInt16(); + } + elseif ($size == self::UNSIGNED_INT24_LENGTH) + { + return $this->readUInt24(); + } + elseif ($size == self::UNSIGNED_INT32_LENGTH) + { + return $this->readUInt32(); + } + elseif ($size == self::UNSIGNED_INT40_LENGTH) + { + return $this->readUInt40(); + } + elseif ($size == self::UNSIGNED_INT48_LENGTH) + { + return $this->readUInt48(); + } + elseif ($size == self::UNSIGNED_INT56_LENGTH) + { + return $this->readUInt56(); + } + elseif ($size == self::UNSIGNED_INT64_LENGTH) + { + return $this->readUInt64(); + } + + throw new BinaryDataReaderException('$size ' . $size . ' not handled'); + } + + /** + * @return int + */ + public function readUInt8() + { + return unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; + } + + /** + * @return int + */ + public function readUInt32() + { + return unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; + } + + /** + * @return mixed + */ + public function readUInt40() + { + $data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; + $data2 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; + + return $data1 + ($data2 << 8); + } + + /** + * @return mixed + */ + public function readUInt48() + { + $data = unpack('v3', $this->read(self::UNSIGNED_INT48_LENGTH)); + + return $data[1] + ($data[2] << 16) + ($data[3] << 32); + } + + /** + * @return mixed + */ + public function readUInt56() + { + $data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; + $data2 = unpack('S', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; + $data3 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; + + return $data1 + ($data2 << 8) + ($data3 << 24); + } + + /** + * Read a big endian integer values based on byte number + * + * @param int $size + * @return int + * @throws BinaryDataReaderException + */ + public function readIntBeBySize($size) + { + if ($size == self::UNSIGNED_CHAR_LENGTH) + { + return $this->readInt8(); + } + elseif ($size == self::UNSIGNED_SHORT_LENGTH) + { + return $this->readInt16Be(); + } + elseif ($size == self::UNSIGNED_INT24_LENGTH) + { + return $this->readInt24Be(); + } + elseif ($size == self::UNSIGNED_INT32_LENGTH) + { + return $this->readInt32Be(); + } + elseif ($size == self::UNSIGNED_INT40_LENGTH) + { + return $this->readInt40Be(); + } + + throw new BinaryDataReaderException('$size ' . $size . ' not handled'); + } + + /** + * @return int + */ + public function readInt8() + { + return unpack('c', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; + } + + /** + * @return mixed + */ + public function readInt16Be() + { + return unpack('n', $this->read(self::UNSIGNED_SHORT_LENGTH))[1]; + } + + /** + * @return int + */ + public function readInt24Be() + { + $data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH)); + $res = ($data[1] << 16) | ($data[2] << 8) | $data[3]; + if ($res >= 0x800000) + { + $res -= 0x1000000; + } + + return $res; + } + + /** + * @return int + */ + public function readInt32Be() + { + return unpack('i', strrev($this->read(self::UNSIGNED_INT32_LENGTH)))[1]; + } + + /** + * @return int + */ + public function readInt40Be() + { + $data1 = unpack('N', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; + $data2 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1]; + + return $data2 + ($data1 << 8); + } + + /** + * @return int + */ + public function readInt32() + { + return unpack('i', $this->read(self::UNSIGNED_INT32_LENGTH))[1]; + } + + /** + * @return float + */ + public function readFloat() + { + return unpack('f', $this->read(self::UNSIGNED_FLOAT_LENGTH))[1]; + } + + /** + * @return double + */ + public function readDouble() + { + return unpack('d', $this->read(self::UNSIGNED_DOUBLE_LENGTH))[1]; + } + + /** + * @return string + */ + public function readTableId() + { + return $this->unpackUInt64($this->read(self::UNSIGNED_INT48_LENGTH) . chr(0) . chr(0)); + } + + /** + * @param int $size + * @return bool + */ + public function isComplete($size) + { + if ($this->readBytes + 1 - 20 < $size) + { + return false; + } + return true; + } + + /** + * @param int $value + * @return string + */ + public static function pack64bit($value) + { + return pack('C8', + ($value >> 0) & 0xFF, + ($value >> 8) & 0xFF, + ($value >> 16) & 0xFF, + ($value >> 24) & 0xFF, + ($value >> 32) & 0xFF, + ($value >> 40) & 0xFF, + ($value >> 48) & 0xFF, + ($value >> 56) & 0xFF + ); + } } \ No newline at end of file diff --git a/src/MySQLReplication/BinaryDataReader/BinaryDataReaderBuilder.php b/src/MySQLReplication/BinaryDataReader/BinaryDataReaderBuilder.php index f2a51a5..113ce11 100644 --- a/src/MySQLReplication/BinaryDataReader/BinaryDataReaderBuilder.php +++ b/src/MySQLReplication/BinaryDataReader/BinaryDataReaderBuilder.php @@ -1,27 +1,27 @@ -binaryData = $binaryData; - } - - public function build() - { - return new BinaryDataReader( - $this->binaryData - ); - } +binaryData = $binaryData; + } + + public function build() + { + return new BinaryDataReader( + $this->binaryData + ); + } } \ No newline at end of file diff --git a/src/MySQLReplication/BinaryDataReader/BinaryDataReaderService.php b/src/MySQLReplication/BinaryDataReader/BinaryDataReaderService.php index f49218b..73286ce 100644 --- a/src/MySQLReplication/BinaryDataReader/BinaryDataReaderService.php +++ b/src/MySQLReplication/BinaryDataReader/BinaryDataReaderService.php @@ -1,22 +1,22 @@ -withBinaryData($binaryData); - - return $packageBuilder->build(); - } +withBinaryData($binaryData); + + return $packageBuilder->build(); + } } \ No newline at end of file diff --git a/src/MySQLReplication/BinaryDataReader/Exception/BinaryDataReaderException.php b/src/MySQLReplication/BinaryDataReader/Exception/BinaryDataReaderException.php index 5bc351d..0988496 100644 --- a/src/MySQLReplication/BinaryDataReader/Exception/BinaryDataReaderException.php +++ b/src/MySQLReplication/BinaryDataReader/Exception/BinaryDataReaderException.php @@ -1,13 +1,13 @@ - $v) - { - if ('user' === $k) - { - $configBuilder->withUser($v); - } - if ('ip' === $k) - { - $configBuilder->withHost($v); - } - if ('port' === $k) - { - $configBuilder->withPort($v); - } - if ('password' === $k) - { - $configBuilder->withPassword($v); - } - if ('dbName' === $k) - { - $configBuilder->withDbName($v); - } - if ('charset' === $k) - { - $configBuilder->withCharset($v); - } - if ('gtid' === $k) - { - $configBuilder->withGtid($v); - } - if ('slaveId' === $k) - { - $configBuilder->withSlaveId($v); - } - if ('binLogFileName' === $k) - { - $configBuilder->withBinLogFileName($v); - } - if ('binLogPosition' === $k) - { - $configBuilder->withBinLogPosition($v); - } - if ('eventsOnly' === $k) - { - $configBuilder->withEventsOnly($v); - } - if ('eventsIgnore' === $k) - { - $configBuilder->withEventsIgnore($v); - } - if ('tablesOnly' === $k) - { - $configBuilder->withTablesOnly($v); - } - if ('databasesOnly' === $k) - { - $configBuilder->withDatabasesOnly($v); - } - if ('mariaDbGtid' === $k) - { - $configBuilder->withMariaDbGtid($v); - } - } - - return $configBuilder->build(); - } + $v) + { + if ('user' === $k) + { + $configBuilder->withUser($v); + } + if ('ip' === $k) + { + $configBuilder->withHost($v); + } + if ('port' === $k) + { + $configBuilder->withPort($v); + } + if ('password' === $k) + { + $configBuilder->withPassword($v); + } + if ('dbName' === $k) + { + $configBuilder->withDbName($v); + } + if ('charset' === $k) + { + $configBuilder->withCharset($v); + } + if ('gtid' === $k) + { + $configBuilder->withGtid($v); + } + if ('slaveId' === $k) + { + $configBuilder->withSlaveId($v); + } + if ('binLogFileName' === $k) + { + $configBuilder->withBinLogFileName($v); + } + if ('binLogPosition' === $k) + { + $configBuilder->withBinLogPosition($v); + } + if ('eventsOnly' === $k) + { + $configBuilder->withEventsOnly($v); + } + if ('eventsIgnore' === $k) + { + $configBuilder->withEventsIgnore($v); + } + if ('tablesOnly' === $k) + { + $configBuilder->withTablesOnly($v); + } + if ('databasesOnly' === $k) + { + $configBuilder->withDatabasesOnly($v); + } + if ('mariaDbGtid' === $k) + { + $configBuilder->withMariaDbGtid($v); + } + } + + return $configBuilder->build(); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Config/Exception/ConfigException.php b/src/MySQLReplication/Config/Exception/ConfigException.php index 3c93c9b..0a52b6b 100644 --- a/src/MySQLReplication/Config/Exception/ConfigException.php +++ b/src/MySQLReplication/Config/Exception/ConfigException.php @@ -1,35 +1,35 @@ -type; - } +type; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/EventDTO.php b/src/MySQLReplication/Event/DTO/EventDTO.php index 05224a8..acf41d6 100644 --- a/src/MySQLReplication/Event/DTO/EventDTO.php +++ b/src/MySQLReplication/Event/DTO/EventDTO.php @@ -1,56 +1,56 @@ -eventInfo = $eventInfo; - } - - /** - * @return EventInfo - */ - public function getEventInfo() - { - return $this->eventInfo; - } - - /** - * @return string - */ - abstract public function getType(); - - /** - * @return string - */ - abstract public function __toString(); - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - abstract public function jsonSerialize(); +eventInfo = $eventInfo; + } + + /** + * @return EventInfo + */ + public function getEventInfo() + { + return $this->eventInfo; + } + + /** + * @return string + */ + abstract public function getType(); + + /** + * @return string + */ + abstract public function __toString(); + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + abstract public function jsonSerialize(); } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/GTIDLogDTO.php b/src/MySQLReplication/Event/DTO/GTIDLogDTO.php index 11a3d6e..f0f9e20 100644 --- a/src/MySQLReplication/Event/DTO/GTIDLogDTO.php +++ b/src/MySQLReplication/Event/DTO/GTIDLogDTO.php @@ -1,93 +1,93 @@ -commit = $commit; - $this->gtid = $gtid; - } - - /** - * @return boolean - */ - public function isCommit() - { - return $this->commit; - } - - /** - * @return string - */ - public function getGtid() - { - return $this->gtid; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Commit: ' . var_export($this->commit, true) . PHP_EOL . - 'GTID NEXT: ' . $this->gtid . PHP_EOL; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +commit = $commit; + $this->gtid = $gtid; + } + + /** + * @return boolean + */ + public function isCommit() + { + return $this->commit; + } + + /** + * @return string + */ + public function getGtid() + { + return $this->gtid; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Commit: ' . var_export($this->commit, true) . PHP_EOL . + 'GTID NEXT: ' . $this->gtid . PHP_EOL; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/MariaDbGtidLogDTO.php b/src/MySQLReplication/Event/DTO/MariaDbGtidLogDTO.php index db7ae49..89dc9fd 100644 --- a/src/MySQLReplication/Event/DTO/MariaDbGtidLogDTO.php +++ b/src/MySQLReplication/Event/DTO/MariaDbGtidLogDTO.php @@ -1,108 +1,108 @@ -flag = $flag; - $this->domainId = $domainId; - $this->sequenceNumber = $sequenceNumber; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Flag: ' . var_export($this->flag, true) . PHP_EOL . - 'Domain Id: ' . $this->domainId . PHP_EOL . - 'Sequence Number: ' . $this->sequenceNumber . PHP_EOL; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } - - /** - * @return int - */ - public function getFlag() - { - return $this->flag; - } - - /** - * @return int - */ - public function getSequenceNumber() - { - return $this->sequenceNumber; - } - - /** - * @return int - */ - public function getDomainId() - { - return $this->domainId; - } +flag = $flag; + $this->domainId = $domainId; + $this->sequenceNumber = $sequenceNumber; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Flag: ' . var_export($this->flag, true) . PHP_EOL . + 'Domain Id: ' . $this->domainId . PHP_EOL . + 'Sequence Number: ' . $this->sequenceNumber . PHP_EOL; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } + + /** + * @return int + */ + public function getFlag() + { + return $this->flag; + } + + /** + * @return int + */ + public function getSequenceNumber() + { + return $this->sequenceNumber; + } + + /** + * @return int + */ + public function getDomainId() + { + return $this->domainId; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/QueryDTO.php b/src/MySQLReplication/Event/DTO/QueryDTO.php index 4370575..5b3ded6 100644 --- a/src/MySQLReplication/Event/DTO/QueryDTO.php +++ b/src/MySQLReplication/Event/DTO/QueryDTO.php @@ -1,110 +1,110 @@ -executionTime = $executionTime; - $this->query = $query; - $this->database = $database; - } - - /** - * @return string - */ - public function getDatabase() - { - return $this->database; - } - - /** - * @return int - */ - public function getExecutionTime() - { - return $this->executionTime; - } - - /** - * @return string - */ - public function getQuery() - { - return $this->query; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Database: ' . $this->database . PHP_EOL . - 'Execution time: ' . $this->executionTime . PHP_EOL . - 'Query: ' . $this->query . PHP_EOL; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } -} - +executionTime = $executionTime; + $this->query = $query; + $this->database = $database; + } + + /** + * @return string + */ + public function getDatabase() + { + return $this->database; + } + + /** + * @return int + */ + public function getExecutionTime() + { + return $this->executionTime; + } + + /** + * @return string + */ + public function getQuery() + { + return $this->query; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Database: ' . $this->database . PHP_EOL . + 'Execution time: ' . $this->executionTime . PHP_EOL . + 'Query: ' . $this->query . PHP_EOL; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } +} + diff --git a/src/MySQLReplication/Event/DTO/RotateDTO.php b/src/MySQLReplication/Event/DTO/RotateDTO.php index 6684e14..5d73ab7 100644 --- a/src/MySQLReplication/Event/DTO/RotateDTO.php +++ b/src/MySQLReplication/Event/DTO/RotateDTO.php @@ -1,93 +1,93 @@ -position = $position; - $this->next_binlog = $next_binlog; - } - - /** - * @return int - */ - public function getPosition() - { - return $this->position; - } - - /** - * @return string - */ - public function getNextBinlog() - { - return $this->next_binlog; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Binlog position: ' . $this->position . PHP_EOL . - 'Binlog filename: ' . $this->next_binlog . PHP_EOL; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +position = $position; + $this->next_binlog = $next_binlog; + } + + /** + * @return int + */ + public function getPosition() + { + return $this->position; + } + + /** + * @return string + */ + public function getNextBinlog() + { + return $this->next_binlog; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Binlog position: ' . $this->position . PHP_EOL . + 'Binlog filename: ' . $this->next_binlog . PHP_EOL; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/RowsDTO.php b/src/MySQLReplication/Event/DTO/RowsDTO.php index d41f1a2..f2a0725 100644 --- a/src/MySQLReplication/Event/DTO/RowsDTO.php +++ b/src/MySQLReplication/Event/DTO/RowsDTO.php @@ -1,98 +1,98 @@ -changedRows = $changedRows; - $this->values = $values; - $this->tableMap = $tableMap; - } - - /** - * @return TableMap - */ - public function getTableMap() - { - return $this->tableMap; - } - - /** - * @return int - */ - public function getChangedRows() - { - return $this->changedRows; - } - - /** - * @return array - */ - public function getValues() - { - return $this->values; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Table: ' . $this->tableMap->getTable() . PHP_EOL . - 'Affected columns: ' . $this->tableMap->getColumnsAmount() . PHP_EOL . - 'Changed rows: ' . $this->changedRows . PHP_EOL . - 'Values: ' . print_r($this->values, true) . PHP_EOL; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +changedRows = $changedRows; + $this->values = $values; + $this->tableMap = $tableMap; + } + + /** + * @return TableMap + */ + public function getTableMap() + { + return $this->tableMap; + } + + /** + * @return int + */ + public function getChangedRows() + { + return $this->changedRows; + } + + /** + * @return array + */ + public function getValues() + { + return $this->values; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Table: ' . $this->tableMap->getTable() . PHP_EOL . + 'Affected columns: ' . $this->tableMap->getColumnsAmount() . PHP_EOL . + 'Changed rows: ' . $this->changedRows . PHP_EOL . + 'Values: ' . print_r($this->values, true) . PHP_EOL; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/TableMapDTO.php b/src/MySQLReplication/Event/DTO/TableMapDTO.php index 775a081..5c342e5 100644 --- a/src/MySQLReplication/Event/DTO/TableMapDTO.php +++ b/src/MySQLReplication/Event/DTO/TableMapDTO.php @@ -1,81 +1,81 @@ -tableMap = $tableMap; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Table: ' . $this->tableMap->getTable() . PHP_EOL . - 'Database: ' . $this->tableMap->getDatabase() . PHP_EOL . - 'Table Id: ' . $this->tableMap->getTableId() . PHP_EOL . - 'Columns amount: ' . $this->tableMap->getColumnsAmount() . PHP_EOL; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } - - /** - * @return TableMap - */ - public function getTableMap() - { - return $this->tableMap; - } +tableMap = $tableMap; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Table: ' . $this->tableMap->getTable() . PHP_EOL . + 'Database: ' . $this->tableMap->getDatabase() . PHP_EOL . + 'Table Id: ' . $this->tableMap->getTableId() . PHP_EOL . + 'Columns amount: ' . $this->tableMap->getColumnsAmount() . PHP_EOL; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } + + /** + * @return TableMap + */ + public function getTableMap() + { + return $this->tableMap; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/UpdateRowsDTO.php b/src/MySQLReplication/Event/DTO/UpdateRowsDTO.php index ac40f22..6575ad2 100644 --- a/src/MySQLReplication/Event/DTO/UpdateRowsDTO.php +++ b/src/MySQLReplication/Event/DTO/UpdateRowsDTO.php @@ -1,25 +1,25 @@ -type; - } +type; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/WriteRowsDTO.php b/src/MySQLReplication/Event/DTO/WriteRowsDTO.php index 4e542a2..16a9df0 100644 --- a/src/MySQLReplication/Event/DTO/WriteRowsDTO.php +++ b/src/MySQLReplication/Event/DTO/WriteRowsDTO.php @@ -1,24 +1,24 @@ -type; - } +type; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/DTO/XidDTO.php b/src/MySQLReplication/Event/DTO/XidDTO.php index b93f08f..883caa8 100644 --- a/src/MySQLReplication/Event/DTO/XidDTO.php +++ b/src/MySQLReplication/Event/DTO/XidDTO.php @@ -1,77 +1,77 @@ -xid = $xid; - } - - /** - * @return string - */ - public function getXid() - { - return $this->xid; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return string - */ - public function __toString() - { - return PHP_EOL . - '=== Event ' . $this->getType() . ' === ' . PHP_EOL . - 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . - 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . - 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . - 'Transaction ID: ' . $this->xid . PHP_EOL; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +xid = $xid; + } + + /** + * @return string + */ + public function getXid() + { + return $this->xid; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function __toString() + { + return PHP_EOL . + '=== Event ' . $this->getType() . ' === ' . PHP_EOL . + 'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL . + 'Log position: ' . $this->eventInfo->getPos() . PHP_EOL . + 'Event size: ' . $this->eventInfo->getSize() . PHP_EOL . + 'Transaction ID: ' . $this->xid . PHP_EOL; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/EventCommon.php b/src/MySQLReplication/Event/EventCommon.php index efe977d..b48588c 100644 --- a/src/MySQLReplication/Event/EventCommon.php +++ b/src/MySQLReplication/Event/EventCommon.php @@ -1,35 +1,35 @@ -eventInfo = $eventInfo; - $this->binaryDataReader = $binaryDataReader; - } +eventInfo = $eventInfo; + $this->binaryDataReader = $binaryDataReader; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/EventInfo.php b/src/MySQLReplication/Event/EventInfo.php index 117b99c..3b1863f 100644 --- a/src/MySQLReplication/Event/EventInfo.php +++ b/src/MySQLReplication/Event/EventInfo.php @@ -1,159 +1,159 @@ -timestamp = $timestamp; - $this->type = $type; - $this->id = $id; - $this->size = $size; - $this->pos = $pos; - $this->flag = $flag; - $this->checkSum = $checkSum; - } - - /** - * @return string - */ - public function getDateTime() - { - if (empty($this->dateTime)) - { - $this->dateTime = date('c', $this->timestamp); - } - return $this->dateTime; - } - - /** - * @return string - */ - public function getSizeNoHeader() - { - if (empty($this->sizeNoHeader)) - { - $this->sizeNoHeader = (true === $this->checkSum ? $this->size - 23 : $this->size - 19); - } - return $this->sizeNoHeader; - } - - /** - * @return int - */ - public function getTimestamp() - { - return $this->timestamp; - } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * @return int - */ - public function getId() - { - return $this->id; - } - - /** - * @return int - */ - public function getSize() - { - return $this->size; - } - - /** - * @return int - */ - public function getPos() - { - return $this->pos; - } - - /** - * @return string - */ - public function getFlag() - { - return $this->flag; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +timestamp = $timestamp; + $this->type = $type; + $this->id = $id; + $this->size = $size; + $this->pos = $pos; + $this->flag = $flag; + $this->checkSum = $checkSum; + } + + /** + * @return string + */ + public function getDateTime() + { + if (empty($this->dateTime)) + { + $this->dateTime = date('c', $this->timestamp); + } + return $this->dateTime; + } + + /** + * @return string + */ + public function getSizeNoHeader() + { + if (empty($this->sizeNoHeader)) + { + $this->sizeNoHeader = (true === $this->checkSum ? $this->size - 23 : $this->size - 19); + } + return $this->sizeNoHeader; + } + + /** + * @return int + */ + public function getTimestamp() + { + return $this->timestamp; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return int + */ + public function getSize() + { + return $this->size; + } + + /** + * @return int + */ + public function getPos() + { + return $this->pos; + } + + /** + * @return string + */ + public function getFlag() + { + return $this->flag; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/EventSubscribers.php b/src/MySQLReplication/Event/EventSubscribers.php index 37e641e..a571548 100644 --- a/src/MySQLReplication/Event/EventSubscribers.php +++ b/src/MySQLReplication/Event/EventSubscribers.php @@ -1,135 +1,135 @@ - 'methodName') - * * array('eventName' => array('methodName', $priority)) - * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) - * - * @return array The event names to listen to - */ - public static function getSubscribedEvents() - { - return [ - ConstEventsNames::TABLE_MAP => 'onTableMap', - ConstEventsNames::UPDATE => 'onUpdate', - ConstEventsNames::DELETE => 'onDelete', - ConstEventsNames::GTID => 'onGTID', - ConstEventsNames::QUERY => 'onQuery', - ConstEventsNames::ROTATE => 'onRotate', - ConstEventsNames::XID => 'onXID', - ConstEventsNames::WRITE => 'onWrite', - ConstEventsNames::MARIADB_GTID => 'onMariaDbGtid', - ]; - } - - /** - * @param UpdateRowsDTO $event - */ - public function onUpdate(UpdateRowsDTO $event) - { - $this->allEvents($event); - } - - /** - * @param EventDTO $event - */ - protected function allEvents(EventDTO $event) - { - } - - /** - * @param TableMapDTO $event - */ - public function onTableMap(TableMapDTO $event) - { - $this->allEvents($event); - } - - /** - * @param DeleteRowsDTO $event - */ - public function onDelete(DeleteRowsDTO $event) - { - $this->allEvents($event); - } - - /** - * @param GTIDLogDTO $event - */ - public function onGTID(GTIDLogDTO $event) - { - $this->allEvents($event); - } - - /** - * @param QueryDTO $event - */ - public function onQuery(QueryDTO $event) - { - $this->allEvents($event); - } - - /** - * @param RotateDTO $event - */ - public function onRotate(RotateDTO $event) - { - $this->allEvents($event); - } - - /** - * @param XidDTO $event - */ - public function onXID(XidDTO $event) - { - $this->allEvents($event); - } - - /** - * @param WriteRowsDTO $event - */ - public function onWrite(WriteRowsDTO $event) - { - $this->allEvents($event); - } - - /** - * @param MariaDbGtidLogDTO $event - */ - public function onMariaDbGtid(MariaDbGtidLogDTO $event) - { - $this->allEvents($event); - } + 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) + * + * @return array The event names to listen to + */ + public static function getSubscribedEvents() + { + return [ + ConstEventsNames::TABLE_MAP => 'onTableMap', + ConstEventsNames::UPDATE => 'onUpdate', + ConstEventsNames::DELETE => 'onDelete', + ConstEventsNames::GTID => 'onGTID', + ConstEventsNames::QUERY => 'onQuery', + ConstEventsNames::ROTATE => 'onRotate', + ConstEventsNames::XID => 'onXID', + ConstEventsNames::WRITE => 'onWrite', + ConstEventsNames::MARIADB_GTID => 'onMariaDbGtid', + ]; + } + + /** + * @param UpdateRowsDTO $event + */ + public function onUpdate(UpdateRowsDTO $event) + { + $this->allEvents($event); + } + + /** + * @param EventDTO $event + */ + protected function allEvents(EventDTO $event) + { + } + + /** + * @param TableMapDTO $event + */ + public function onTableMap(TableMapDTO $event) + { + $this->allEvents($event); + } + + /** + * @param DeleteRowsDTO $event + */ + public function onDelete(DeleteRowsDTO $event) + { + $this->allEvents($event); + } + + /** + * @param GTIDLogDTO $event + */ + public function onGTID(GTIDLogDTO $event) + { + $this->allEvents($event); + } + + /** + * @param QueryDTO $event + */ + public function onQuery(QueryDTO $event) + { + $this->allEvents($event); + } + + /** + * @param RotateDTO $event + */ + public function onRotate(RotateDTO $event) + { + $this->allEvents($event); + } + + /** + * @param XidDTO $event + */ + public function onXID(XidDTO $event) + { + $this->allEvents($event); + } + + /** + * @param WriteRowsDTO $event + */ + public function onWrite(WriteRowsDTO $event) + { + $this->allEvents($event); + } + + /** + * @param MariaDbGtidLogDTO $event + */ + public function onMariaDbGtid(MariaDbGtidLogDTO $event) + { + $this->allEvents($event); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/Exception/EventException.php b/src/MySQLReplication/Event/Exception/EventException.php index aa761ea..095bd85 100644 --- a/src/MySQLReplication/Event/Exception/EventException.php +++ b/src/MySQLReplication/Event/Exception/EventException.php @@ -1,13 +1,13 @@ -binaryDataReader->readUInt8() == 1; - $sid = unpack('H*', $this->binaryDataReader->read(16))[1]; - $gno = $this->binaryDataReader->readUInt64(); - - return new GTIDLogDTO( - $this->eventInfo, - $commit_flag, - vsprintf('%s%s%s%s%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s%s%s%s%s%s%s%s%s', str_split($sid)) . ':' . $gno - ); - } +binaryDataReader->readUInt8() == 1; + $sid = unpack('H*', $this->binaryDataReader->read(16))[1]; + $gno = $this->binaryDataReader->readUInt64(); + + return new GTIDLogDTO( + $this->eventInfo, + $commit_flag, + vsprintf('%s%s%s%s%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s%s%s%s%s%s%s%s%s', str_split($sid)) . ':' . $gno + ); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/MariaDbGtidEvent.php b/src/MySQLReplication/Event/MariaDbGtidEvent.php index 03214dc..ab1793e 100644 --- a/src/MySQLReplication/Event/MariaDbGtidEvent.php +++ b/src/MySQLReplication/Event/MariaDbGtidEvent.php @@ -1,29 +1,29 @@ -binaryDataReader->readUInt64(); - $domainId = $this->binaryDataReader->readUInt32(); - $flag = $this->binaryDataReader->readUInt8(); - - return new MariaDbGtidLogDTO( - $this->eventInfo, - $flag, - $domainId, - $sequenceNumber - ); - } +binaryDataReader->readUInt64(); + $domainId = $this->binaryDataReader->readUInt32(); + $flag = $this->binaryDataReader->readUInt8(); + + return new MariaDbGtidLogDTO( + $this->eventInfo, + $flag, + $domainId, + $sequenceNumber + ); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/QueryEvent.php b/src/MySQLReplication/Event/QueryEvent.php index f1e1fab..c37825e 100644 --- a/src/MySQLReplication/Event/QueryEvent.php +++ b/src/MySQLReplication/Event/QueryEvent.php @@ -1,53 +1,53 @@ -binaryDataReader->advance(4); - $executionTime = $this->binaryDataReader->readUInt32(); - $schemaLength = $this->binaryDataReader->readUInt8(); - $this->binaryDataReader->advance(2); - $statusVarsLength = $this->binaryDataReader->readUInt16(); - $this->binaryDataReader->advance($statusVarsLength); - $schema = $this->binaryDataReader->read($schemaLength); - $this->binaryDataReader->advance(1); - $query = $this->binaryDataReader->read( - $this->eventInfo->getSize() - $this->getSizeToRemoveByVersion() - $statusVarsLength - $schemaLength - 1 - ); - - return new QueryDTO( - $this->eventInfo, - $schema, - $executionTime, - $query - ); - } - - /** - * @return int - */ - private function getSizeToRemoveByVersion() - { - if (BinLogServerInfo::MYSQL_VERSION_MARIADB === BinLogServerInfo::getVersion()) - { - return 13; - } - return 36; - } -} +binaryDataReader->advance(4); + $executionTime = $this->binaryDataReader->readUInt32(); + $schemaLength = $this->binaryDataReader->readUInt8(); + $this->binaryDataReader->advance(2); + $statusVarsLength = $this->binaryDataReader->readUInt16(); + $this->binaryDataReader->advance($statusVarsLength); + $schema = $this->binaryDataReader->read($schemaLength); + $this->binaryDataReader->advance(1); + $query = $this->binaryDataReader->read( + $this->eventInfo->getSize() - $this->getSizeToRemoveByVersion() - $statusVarsLength - $schemaLength - 1 + ); + + return new QueryDTO( + $this->eventInfo, + $schema, + $executionTime, + $query + ); + } + + /** + * @return int + */ + private function getSizeToRemoveByVersion() + { + if (BinLogServerInfo::MYSQL_VERSION_MARIADB === BinLogServerInfo::getVersion()) + { + return 13; + } + return 36; + } +} diff --git a/src/MySQLReplication/Event/RotateEvent.php b/src/MySQLReplication/Event/RotateEvent.php index 53592c9..c800840 100644 --- a/src/MySQLReplication/Event/RotateEvent.php +++ b/src/MySQLReplication/Event/RotateEvent.php @@ -1,45 +1,45 @@ -binaryDataReader->readUInt64(); - $binFileName = $this->binaryDataReader->read( - $this->eventInfo->getSizeNoHeader() - $this->getSizeToRemoveByVersion() - ); - - return new RotateDTO( - $this->eventInfo, - $pos, - $binFileName - ); - } - - /** - * @return int - */ - private function getSizeToRemoveByVersion() - { - if (BinLogServerInfo::MYSQL_VERSION_MARIADB !== BinLogServerInfo::getVersion()) - { - return 8; - } - return 0; - } +binaryDataReader->readUInt64(); + $binFileName = $this->binaryDataReader->read( + $this->eventInfo->getSizeNoHeader() - $this->getSizeToRemoveByVersion() + ); + + return new RotateDTO( + $this->eventInfo, + $pos, + $binFileName + ); + } + + /** + * @return int + */ + private function getSizeToRemoveByVersion() + { + if (BinLogServerInfo::MYSQL_VERSION_MARIADB !== BinLogServerInfo::getVersion()) + { + return 8; + } + return 0; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/RowEvent/TableMap.php b/src/MySQLReplication/Event/RowEvent/TableMap.php index f3a2645..f396fd7 100644 --- a/src/MySQLReplication/Event/RowEvent/TableMap.php +++ b/src/MySQLReplication/Event/RowEvent/TableMap.php @@ -1,105 +1,105 @@ -database = $database; - $this->table = $table; - $this->tableId = $tableId; - $this->columnsAmount = $columnsAmount; - $this->fields = $fields; - } - - /** - * @return string - */ - public function getDatabase() - { - return $this->database; - } - - /** - * @return string - */ - public function getTable() - { - return $this->table; - } - - /** - * @return int - */ - public function getTableId() - { - return $this->tableId; - } - - /** - * @return int - */ - public function getColumnsAmount() - { - return $this->columnsAmount; - } - - /** - * @return array - */ - public function getFields() - { - return $this->fields; - } - - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - return get_object_vars($this); - } +database = $database; + $this->table = $table; + $this->tableId = $tableId; + $this->columnsAmount = $columnsAmount; + $this->fields = $fields; + } + + /** + * @return string + */ + public function getDatabase() + { + return $this->database; + } + + /** + * @return string + */ + public function getTable() + { + return $this->table; + } + + /** + * @return int + */ + public function getTableId() + { + return $this->tableId; + } + + /** + * @return int + */ + public function getColumnsAmount() + { + return $this->columnsAmount; + } + + /** + * @return array + */ + public function getFields() + { + return $this->fields; + } + + /** + * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Event/XidEvent.php b/src/MySQLReplication/Event/XidEvent.php index a466bfc..a26d129 100644 --- a/src/MySQLReplication/Event/XidEvent.php +++ b/src/MySQLReplication/Event/XidEvent.php @@ -1,23 +1,23 @@ -eventInfo, - $this->binaryDataReader->readUInt64() - ); - } +eventInfo, + $this->binaryDataReader->readUInt64() + ); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Exception/MySQLReplicationException.php b/src/MySQLReplication/Exception/MySQLReplicationException.php index a441020..3348564 100644 --- a/src/MySQLReplication/Exception/MySQLReplicationException.php +++ b/src/MySQLReplication/Exception/MySQLReplicationException.php @@ -1,11 +1,11 @@ -sid = $matches[1]; - foreach (array_filter(explode(':', $matches[2])) as $k) - { - $this->intervals[] = explode('-', $k); - } - $this->sid = str_replace('-', '', $this->sid); - } - - /** - * @return string - */ - public function getEncoded() - { - $buffer = pack('H*', $this->sid); - $buffer .= BinaryDataReader::pack64bit(count($this->intervals)); - - foreach ($this->intervals as $interval) - { - if (count($interval) !== 1) - { - $buffer .= BinaryDataReader::pack64bit($interval[0]); - $buffer .= BinaryDataReader::pack64bit($interval[1]); - } - else - { - $buffer .= BinaryDataReader::pack64bit($interval[0]); - $buffer .= BinaryDataReader::pack64bit($interval[0] + 1); - } - } - - return $buffer; - } - - /** - * @return int - */ - public function getEncodedLength() - { - return (40 * count($this->intervals)); - } +sid = $matches[1]; + foreach (array_filter(explode(':', $matches[2])) as $k) + { + $this->intervals[] = explode('-', $k); + } + $this->sid = str_replace('-', '', $this->sid); + } + + /** + * @return string + */ + public function getEncoded() + { + $buffer = pack('H*', $this->sid); + $buffer .= BinaryDataReader::pack64bit(count($this->intervals)); + + foreach ($this->intervals as $interval) + { + if (count($interval) !== 1) + { + $buffer .= BinaryDataReader::pack64bit($interval[0]); + $buffer .= BinaryDataReader::pack64bit($interval[1]); + } + else + { + $buffer .= BinaryDataReader::pack64bit($interval[0]); + $buffer .= BinaryDataReader::pack64bit($interval[0] + 1); + } + } + + return $buffer; + } + + /** + * @return int + */ + public function getEncodedLength() + { + return (40 * count($this->intervals)); + } } \ No newline at end of file diff --git a/src/MySQLReplication/Gtid/GtidCollection.php b/src/MySQLReplication/Gtid/GtidCollection.php index b9f938a..fbebac4 100644 --- a/src/MySQLReplication/Gtid/GtidCollection.php +++ b/src/MySQLReplication/Gtid/GtidCollection.php @@ -1,43 +1,43 @@ -toArray() as $gtid) - { - $l += $gtid->getEncodedLength(); - } - - return $l; - } - - /** - * @return string - */ - public function getEncoded() - { - $s = BinaryDataReader::pack64bit($this->count()); - /** @var Gtid $gtid */ - foreach ($this->toArray() as $gtid) - { - $s .= $gtid->getEncoded(); - } - - return $s; - } +toArray() as $gtid) + { + $l += $gtid->getEncodedLength(); + } + + return $l; + } + + /** + * @return string + */ + public function getEncoded() + { + $s = BinaryDataReader::pack64bit($this->count()); + /** @var Gtid $gtid */ + foreach ($this->toArray() as $gtid) + { + $s .= $gtid->getEncoded(); + } + + return $s; + } } \ No newline at end of file diff --git a/src/MySQLReplication/Gtid/GtidException.php b/src/MySQLReplication/Gtid/GtidException.php index b2383da..46eede9 100644 --- a/src/MySQLReplication/Gtid/GtidException.php +++ b/src/MySQLReplication/Gtid/GtidException.php @@ -1,13 +1,13 @@ -GtidCollection = new GtidCollection(); - } - - /** - * @param string $gtids - * @return GtidCollection - * @throws GtidException - */ - public function makeCollectionFromString($gtids) - { - foreach (array_filter(explode(',', $gtids)) as $gtid) - { - $this->GtidCollection->add(new Gtid($gtid)); - } - - return $this->GtidCollection; - } +GtidCollection = new GtidCollection(); + } + + /** + * @param string $gtids + * @return GtidCollection + * @throws GtidException + */ + public function makeCollectionFromString($gtids) + { + foreach (array_filter(explode(',', $gtids)) as $gtid) + { + $this->GtidCollection->add(new Gtid($gtid)); + } + + return $this->GtidCollection; + } } \ No newline at end of file diff --git a/src/MySQLReplication/MySQLReplicationFactory.php b/src/MySQLReplication/MySQLReplicationFactory.php index 0816afa..baf738f 100644 --- a/src/MySQLReplication/MySQLReplicationFactory.php +++ b/src/MySQLReplication/MySQLReplicationFactory.php @@ -1,114 +1,114 @@ -validate(); - - $this->connection = DriverManager::getConnection([ - 'user' => $config->getUser(), - 'password' => $config->getPassword(), - 'host' => $config->getIp(), - 'port' => $config->getPort(), - 'driver' => 'pdo_mysql', - 'charset' => $config->getCharset() - ]); - $this->binLogAuth = new BinLogAuth(); - $this->MySQLRepository = new MySQLRepository($this->connection); - $this->GtiService = new GtidService(); - $this->binLogConnect = new BinLogConnect($config, $this->MySQLRepository, $this->binLogAuth, $this->GtiService); - $this->binLogConnect->connectToStream(); - $this->binaryDataReaderService = new BinaryDataReaderService(); - $this->rowEventService = new RowEventService($config, $this->MySQLRepository); - $this->eventDispatcher = new EventDispatcher(); - $this->event = new Event($config, $this->binLogConnect, $this->binaryDataReaderService, $this->rowEventService, $this->eventDispatcher); - } - - /** - * @param EventSubscribers $eventSubscribers - */ - public function registerSubscriber(EventSubscribers $eventSubscribers) - { - $this->eventDispatcher->addSubscriber($eventSubscribers); - } - - /** - * @return Connection - */ - public function getDbConnection() - { - return $this->connection; - } - - /** - * @throws MySQLReplicationException - */ - public function binLogEvent() - { - $this->event->consume(); - } -} +validate(); + + $this->connection = DriverManager::getConnection([ + 'user' => $config->getUser(), + 'password' => $config->getPassword(), + 'host' => $config->getIp(), + 'port' => $config->getPort(), + 'driver' => 'pdo_mysql', + 'charset' => $config->getCharset() + ]); + $this->binLogAuth = new BinLogAuth(); + $this->MySQLRepository = new MySQLRepository($this->connection); + $this->GtiService = new GtidService(); + $this->binLogConnect = new BinLogConnect($config, $this->MySQLRepository, $this->binLogAuth, $this->GtiService); + $this->binLogConnect->connectToStream(); + $this->binaryDataReaderService = new BinaryDataReaderService(); + $this->rowEventService = new RowEventService($config, $this->MySQLRepository); + $this->eventDispatcher = new EventDispatcher(); + $this->event = new Event($config, $this->binLogConnect, $this->binaryDataReaderService, $this->rowEventService, $this->eventDispatcher); + } + + /** + * @param EventSubscribers $eventSubscribers + */ + public function registerSubscriber(EventSubscribers $eventSubscribers) + { + $this->eventDispatcher->addSubscriber($eventSubscribers); + } + + /** + * @return Connection + */ + public function getDbConnection() + { + return $this->connection; + } + + /** + * @throws MySQLReplicationException + */ + public function binLogEvent() + { + $this->event->consume(); + } +} From 5cc64f813a072f558431bf6ce7e6cede82641e2c Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:29:23 +0100 Subject: [PATCH 6/7] cleanup --- example/benchmark.php | 6 +++--- example/dump_events.php | 3 +-- src/MySQLReplication/BinLog/BinLogServerInfo.php | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/example/benchmark.php b/example/benchmark.php index 27b417d..8bbf72b 100644 --- a/example/benchmark.php +++ b/example/benchmark.php @@ -3,7 +3,6 @@ namespace example; error_reporting(E_ALL); -ini_set('display_errors', 1); date_default_timezone_set('UTC'); include __DIR__ . '/../vendor/autoload.php'; @@ -18,6 +17,7 @@ use MySQLReplication\Config\ConfigService; use MySQLReplication\Definitions\ConstEventType; use MySQLReplication\Event\DTO\UpdateRowsDTO; +use SebastianBergmann\RecursionContext\InvalidArgumentException; /** * Class BenchmarkEventSubscribers @@ -113,13 +113,14 @@ private function getConnection() /** * @throws MySQLReplicationException * @throws DBALException + * @throws \InvalidArgumentException */ public function run() { $pid = pcntl_fork(); if ($pid === -1) { - die('could not fork'); + throw new \InvalidArgumentException('Could not fork'); } else if ($pid) { @@ -157,7 +158,6 @@ private function produce() } $conn->close(); - die; } } diff --git a/example/dump_events.php b/example/dump_events.php index 09a1743..f60d6e4 100644 --- a/example/dump_events.php +++ b/example/dump_events.php @@ -3,7 +3,6 @@ namespace example; error_reporting(E_ALL); -ini_set('display_errors', 1); date_default_timezone_set('UTC'); include __DIR__ . '/../vendor/autoload.php'; @@ -37,7 +36,7 @@ public function allEvents(EventDTO $event) echo $event; // all events got JsonSerializable implementation - //echo json_encode($result, JSON_PRETTY_PRINT); + echo json_encode($event, JSON_PRETTY_PRINT); echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL; } diff --git a/src/MySQLReplication/BinLog/BinLogServerInfo.php b/src/MySQLReplication/BinLog/BinLogServerInfo.php index 155802d..1169e5f 100644 --- a/src/MySQLReplication/BinLog/BinLogServerInfo.php +++ b/src/MySQLReplication/BinLog/BinLogServerInfo.php @@ -3,7 +3,6 @@ namespace MySQLReplication\BinLog; /** - * todo ugly class, for refactoring * Class BinLogServerInfo * @package MySQLReplication\BinLog */ From ed8837851a5dd7c557c323ad988b18aa1dc23a0f Mon Sep 17 00:00:00 2001 From: fazi Date: Sat, 5 Nov 2016 16:36:34 +0100 Subject: [PATCH 7/7] sensiolabs advice --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 9409266..798e27c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,7 @@ -.idea out.log log /vendor composer.phar composer.lock .php_cs.cache -.DS_Store -Thumbs.db /example/profiler.php \ No newline at end of file