Skip to content

Commit 4dd7c88

Browse files
authored
Cleancode (#17)
* change ip to host * tests * tests, support for JSON column type in mysql 5.7 (wip), some clean up * wip json binary decoding * wip json * clean up * clean up * Scrutinizer * Scrutinizer 2 * json fix some refactoring * json fix some refactoring #14 * cache change * cache change
1 parent 0105225 commit 4dd7c88

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

src/MySQLReplication/Config/Config.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class Config
7070
* @var string
7171
*/
7272
private $mariaDbGtid;
73+
/**
74+
* @var int
75+
*/
76+
private $tableCacheSize;
7377

7478
/**
7579
* Config constructor.
@@ -88,6 +92,7 @@ class Config
8892
* @param array $eventsIgnore
8993
* @param array $tablesOnly
9094
* @param array $databasesOnly
95+
* @param int $tableCacheSize
9196
*/
9297
public function __construct(
9398
$user,
@@ -104,7 +109,8 @@ public function __construct(
104109
array $eventsOnly,
105110
array $eventsIgnore,
106111
array $tablesOnly,
107-
array $databasesOnly
112+
array $databasesOnly,
113+
$tableCacheSize
108114
) {
109115
$this->user = $user;
110116
$this->host = $host;
@@ -121,6 +127,7 @@ public function __construct(
121127
$this->tablesOnly = $tablesOnly;
122128
$this->databasesOnly = $databasesOnly;
123129
$this->mariaDbGtid = $mariaGtid;
130+
$this->tableCacheSize = $tableCacheSize;
124131
}
125132

126133
/**
@@ -182,6 +189,10 @@ public function validate()
182189
{
183190
throw new ConfigException(ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE);
184191
}
192+
if (false === filter_var($this->tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
193+
{
194+
throw new ConfigException(ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE);
195+
}
185196
}
186197

187198
/**
@@ -303,4 +314,12 @@ public function getDatabasesOnly()
303314
{
304315
return $this->databasesOnly;
305316
}
317+
318+
/**
319+
* @return int
320+
*/
321+
public function getTableCacheSize()
322+
{
323+
return $this->tableCacheSize;
324+
}
306325
}

src/MySQLReplication/Config/ConfigBuilder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class ConfigBuilder
6868
* @var string
6969
*/
7070
private $mariaDbGtid;
71+
/**
72+
* @var int
73+
*/
74+
private $tableCacheSize = 128;
7175

7276
/**
7377
* @param string $user
@@ -234,6 +238,14 @@ public function withMariaDbGtid($mariaDbGtid)
234238
return $this;
235239
}
236240

241+
/**
242+
* @param int $tableCacheSize
243+
*/
244+
public function withTableCacheSize($tableCacheSize)
245+
{
246+
$this->tableCacheSize = $tableCacheSize;
247+
}
248+
237249
/**
238250
* @return Config
239251
*/
@@ -254,8 +266,8 @@ public function build()
254266
$this->eventsOnly,
255267
$this->eventsIgnore,
256268
$this->tablesOnly,
257-
$this->databasesOnly
269+
$this->databasesOnly,
270+
$this->tableCacheSize
258271
);
259272
}
260-
261273
}

src/MySQLReplication/Config/ConfigService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public function makeConfigFromArray(array $config)
7777
{
7878
$configBuilder->withMariaDbGtid($v);
7979
}
80+
if ('tableCacheSize' === $k)
81+
{
82+
$configBuilder->withTableCacheSize($v);
83+
}
8084
}
8185

8286
return $configBuilder->build();

src/MySQLReplication/Config/Exception/ConfigException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ class ConfigException extends MySQLReplicationException
3232
const BIN_LOG_FILE_POSITION_ERROR_CODE = 10;
3333
const MARIADBGTID_ERROR_MESSAGE = 'Maria gtid must be string';
3434
const MARIADBGTID_ERROR_CODE = 11;
35+
const TABLE_CACHE_SIZE_ERROR_MESSAGE = 'Table cache must be integer';
36+
const TABLE_CACHE_SIZE_ERROR_CODE = 12;
3537
}

src/MySQLReplication/Event/RowEvent/Columns.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Columns
2121
* @param string $columnType
2222
* @param array $columnSchema
2323
* @param BinaryDataReader $binaryDataReader
24+
* @throws ConfigException
2425
* @return array
2526
*/
2627
public static function parse($columnType, array $columnSchema, BinaryDataReader $binaryDataReader)
@@ -31,34 +32,36 @@ public static function parse($columnType, array $columnSchema, BinaryDataReader
3132
self::$field['collation_name'] = $columnSchema['COLLATION_NAME'];
3233
self::$field['character_set_name'] = $columnSchema['CHARACTER_SET_NAME'];
3334
self::$field['comment'] = $columnSchema['COLUMN_COMMENT'];
34-
self::$field['unsigned'] = stripos($columnSchema['COLUMN_TYPE'], 'unsigned') === false ? false : true;
35+
self::$field['unsigned'] = !(stripos($columnSchema['COLUMN_TYPE'], 'unsigned') === false);
3536
self::$field['type_is_bool'] = false;
3637
self::$field['is_primary'] = $columnSchema['COLUMN_KEY'] === 'PRI';
3738

38-
if (self::$field['type'] == ConstFieldType::VARCHAR) {
39+
if (self::$field['type'] === ConstFieldType::VARCHAR) {
3940
self::$field['max_length'] = $binaryDataReader->readInt16();
40-
} elseif (self::$field['type'] == ConstFieldType::DOUBLE) {
41+
} elseif (self::$field['type'] === ConstFieldType::DOUBLE) {
4142
self::$field['size'] = $binaryDataReader->readUInt8();
42-
} elseif (self::$field['type'] == ConstFieldType::FLOAT) {
43+
} elseif (self::$field['type'] === ConstFieldType::FLOAT) {
4344
self::$field['size'] = $binaryDataReader->readUInt8();
44-
} elseif (self::$field['type'] == ConstFieldType::TIMESTAMP2) {
45+
} elseif (self::$field['type'] === ConstFieldType::TIMESTAMP2) {
4546
self::$field['fsp'] = $binaryDataReader->readUInt8();
46-
} elseif (self::$field['type'] == ConstFieldType::DATETIME2) {
47+
} elseif (self::$field['type'] === ConstFieldType::DATETIME2) {
4748
self::$field['fsp'] = $binaryDataReader->readUInt8();
48-
} elseif (self::$field['type'] == ConstFieldType::TIME2) {
49+
} elseif (self::$field['type'] === ConstFieldType::TIME2) {
4950
self::$field['fsp'] = $binaryDataReader->readUInt8();
50-
} elseif (self::$field['type'] == ConstFieldType::TINY && $columnSchema['COLUMN_TYPE'] === 'tinyint(1)') {
51+
} elseif (self::$field['type'] === ConstFieldType::TINY && $columnSchema['COLUMN_TYPE'] === 'tinyint(1)') {
5152
self::$field['type_is_bool'] = true;
52-
} elseif (self::$field['type'] == ConstFieldType::VAR_STRING || self::$field['type'] == ConstFieldType::STRING) {
53+
} elseif (self::$field['type'] === ConstFieldType::VAR_STRING || self::$field['type'] === ConstFieldType::STRING) {
5354
self::getFieldSpecial($binaryDataReader, $columnSchema);
54-
} elseif (self::$field['type'] == ConstFieldType::BLOB) {
55+
} elseif (self::$field['type'] === ConstFieldType::BLOB) {
56+
self::$field['length_size'] = $binaryDataReader->readUInt8();
57+
} elseif (self::$field['type'] === ConstFieldType::GEOMETRY) {
5558
self::$field['length_size'] = $binaryDataReader->readUInt8();
56-
} elseif (self::$field['type'] == ConstFieldType::GEOMETRY) {
59+
} elseif (self::$field['type'] === ConstFieldType::JSON) {
5760
self::$field['length_size'] = $binaryDataReader->readUInt8();
58-
} elseif (self::$field['type'] == ConstFieldType::NEWDECIMAL) {
61+
} elseif (self::$field['type'] === ConstFieldType::NEWDECIMAL) {
5962
self::$field['precision'] = $binaryDataReader->readUInt8();
6063
self::$field['decimals'] = $binaryDataReader->readUInt8();
61-
} elseif (self::$field['type'] == ConstFieldType::BIT) {
64+
} elseif (self::$field['type'] === ConstFieldType::BIT) {
6265
$bits = $binaryDataReader->readUInt8();
6366
$bytes = $binaryDataReader->readUInt8();
6467
self::$field['bits'] = ($bytes * 8) + $bits;
@@ -77,7 +80,7 @@ private static function getFieldSpecial(BinaryDataReader $packet, array $columnS
7780
{
7881
$metadata = ($packet->readUInt8() << 8) + $packet->readUInt8();
7982
$real_type = $metadata >> 8;
80-
if ($real_type == ConstFieldType::SET || $real_type == ConstFieldType::ENUM) {
83+
if ($real_type === ConstFieldType::SET || $real_type === ConstFieldType::ENUM) {
8184
self::$field['type'] = $real_type;
8285
self::$field['size'] = $metadata & 0x00ff;
8386
self::getFieldSpecialValues($columnSchema);
@@ -92,9 +95,9 @@ private static function getFieldSpecial(BinaryDataReader $packet, array $columnS
9295
*/
9396
private static function getFieldSpecialValues(array $columnSchema)
9497
{
95-
if (self::$field['type'] == ConstFieldType::ENUM) {
98+
if (self::$field['type'] === ConstFieldType::ENUM) {
9699
self::$field['enum_values'] = explode(',', str_replace(['enum(', ')', '\''], '', $columnSchema['COLUMN_TYPE']));
97-
} else if (self::$field['type'] == ConstFieldType::SET) {
100+
} else if (self::$field['type'] === ConstFieldType::SET) {
98101
self::$field['set_values'] = explode(',', str_replace(['set(', ')', '\''], '', $columnSchema['COLUMN_TYPE']));
99102
} else {
100103
throw new ConfigException('Type not handled! - ' . self::$field['type']);

src/MySQLReplication/Event/RowEvent/RowEvent.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MySQLReplication\BinaryDataReader\BinaryDataReader;
66
use MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException;
77
use MySQLReplication\Config\Config;
8+
use MySQLReplication\Config\Exception\ConfigException;
89
use MySQLReplication\Definitions\ConstEventType;
910
use MySQLReplication\Definitions\ConstFieldType;
1011
use MySQLReplication\Event\DTO\DeleteRowsDTO;
@@ -97,6 +98,7 @@ public function __construct(
9798
*
9899
* @return TableMapDTO
99100
* @throws BinaryDataReaderException
101+
* @throws ConfigException
100102
*/
101103
public function makeTableMapDTO()
102104
{
@@ -125,9 +127,9 @@ public function makeTableMapDTO()
125127
$data['column_types'] = $this->binaryDataReader->read($data['columns_amount']);
126128

127129
// automatically clear table cache to save memory
128-
if (count(self::$tableMapCache) >= 128)
130+
if (count(self::$tableMapCache) > $this->config->getTableCacheSize())
129131
{
130-
self::$tableMapCache = array_slice(self::$tableMapCache, 64, -1, true);
132+
self::$tableMapCache = array_slice(self::$tableMapCache, ceil($this->config->getTableCacheSize() / 2), null, true);
131133
}
132134

133135
// already in cache don't parse
@@ -443,7 +445,9 @@ private function getColumnData($colsBitmap)
443445
}
444446
elseif ($column['type'] === ConstFieldType::JSON)
445447
{
446-
$values[$name] = $this->jsonBinaryDecoderFactory->makeJsonBinaryDecoder($this->getString(BinaryDataReader::UNSIGNED_INT32_LENGTH, $column))->parseToString();
448+
$values[$name] = $this->jsonBinaryDecoderFactory->makeJsonBinaryDecoder(
449+
$this->binaryDataReader->readLengthCodedPascalString($column['length_size'])
450+
)->parseToString();
447451
}
448452
else
449453
{

0 commit comments

Comments
 (0)