forked from krowinski/php-mysql-replication
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJsonBinaryDecoderService.php
More file actions
383 lines (332 loc) · 13.3 KB
/
JsonBinaryDecoderService.php
File metadata and controls
383 lines (332 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
declare(strict_types=1);
namespace MySQLReplication\JsonBinaryDecoder;
use InvalidArgumentException;
use LengthException;
use MySQLReplication\BinaryDataReader\BinaryDataReader;
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
/**
* @see https://github.com/mysql/mysql-server/blob/5.7/sql/json_binary.cc
* @see https://github.com/mysql/mysql-server/blob/8.0/sql/json_binary.cc
* @see https://github.com/shyiko/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/json/JsonBinary.java
*/
class JsonBinaryDecoderService
{
public const SMALL_OBJECT = 0;
public const LARGE_OBJECT = 1;
public const SMALL_ARRAY = 2;
public const LARGE_ARRAY = 3;
public const LITERAL = 4;
public const INT16 = 5;
public const UINT16 = 6;
public const INT32 = 7;
public const UINT32 = 8;
public const INT64 = 9;
public const UINT64 = 10;
public const DOUBLE = 11;
public const STRING = 12;
//public const OPAQUE = 15;
public const LITERAL_NULL = 0;
public const LITERAL_TRUE = 1;
public const LITERAL_FALSE = 2;
public const SMALL_OFFSET_SIZE = 2;
public const LARGE_OFFSET_SIZE = 4;
public const KEY_ENTRY_SIZE_SMALL = 2 + self::SMALL_OFFSET_SIZE;
public const KEY_ENTRY_SIZE_LARGE = 2 + self::LARGE_OFFSET_SIZE;
public const VALUE_ENTRY_SIZE_SMALL = 1 + self::SMALL_OFFSET_SIZE;
public const VALUE_ENTRY_SIZE_LARGE = 1 + self::LARGE_OFFSET_SIZE;
public const OBJECT = 1;
public const ARRAY = 2;
public const SCALAR = 3;
private $binaryDataReader;
private $jsonBinaryDecoderFormatter;
private $dataLength;
public function __construct(
BinaryDataReader $binaryDataReader,
JsonBinaryDecoderFormatter $jsonBinaryDecoderFormatter
) {
$this->binaryDataReader = $binaryDataReader;
$this->jsonBinaryDecoderFormatter = $jsonBinaryDecoderFormatter;
$this->dataLength = $this->binaryDataReader->getBinaryDataLength();
}
public static function makeJsonBinaryDecoder(string $data): self
{
return new self(
new BinaryDataReader($data),
new JsonBinaryDecoderFormatter()
);
}
/**
* @throws BinaryDataReaderException
* @throws JsonBinaryDecoderException
*/
public function parseToString(): string
{
// Sometimes, we can insert a NULL JSON even we set the JSON field as NOT NULL.
// If we meet this case, we can return a 'null' value.
if ($this->binaryDataReader->getBinaryDataLength() === 0) {
return 'null';
}
$this->parseJson($this->binaryDataReader->readUInt8());
return $this->jsonBinaryDecoderFormatter->getJsonString();
}
/**
* @throws JsonBinaryDecoderException
* @throws BinaryDataReaderException
*/
private function parseJson(int $type): void
{
$results = [];
if (self::SMALL_OBJECT === $type) {
$results[self::OBJECT] = $this->parseArrayOrObject(self::OBJECT, self::SMALL_OFFSET_SIZE);
} elseif (self::LARGE_OBJECT === $type) {
$results[self::OBJECT] = $this->parseArrayOrObject(self::OBJECT, self::LARGE_OFFSET_SIZE);
} elseif (self::SMALL_ARRAY === $type) {
$results[self::ARRAY] = $this->parseArrayOrObject(self::ARRAY, self::SMALL_OFFSET_SIZE);
} elseif (self::LARGE_ARRAY === $type) {
$results[self::ARRAY] = $this->parseArrayOrObject(self::ARRAY, self::LARGE_OFFSET_SIZE);
} else {
$results[self::SCALAR][] = [
'name' => null,
'value' => $this->parseScalar($type)
];
}
$this->parseToJson($results);
}
/**
* @throws BinaryDataReaderException
* @throws JsonBinaryDecoderException
*/
private function parseToJson(array $results): void
{
foreach ($results as $dataType => $entities) {
if (self::OBJECT === $dataType) {
$this->jsonBinaryDecoderFormatter->formatBeginObject();
} elseif (self::ARRAY === $dataType) {
$this->jsonBinaryDecoderFormatter->formatBeginArray();
}
foreach ($entities as $i => $entity) {
if ($dataType === self::SCALAR) {
if (null === $entity['value']->getValue()) {
$this->jsonBinaryDecoderFormatter->formatValue('null');
} elseif (is_bool($entity['value']->getValue())) {
$this->jsonBinaryDecoderFormatter->formatValueBool($entity['value']->getValue());
} else {
$this->jsonBinaryDecoderFormatter->formatValue($entity['value']->getValue());
}
continue;
}
if ($i !== 0) {
$this->jsonBinaryDecoderFormatter->formatNextEntry();
}
if (null !== $entity['name']) {
$this->jsonBinaryDecoderFormatter->formatName($entity['name']);
}
$this->assignValues($entity['value']);
}
if (self::OBJECT === $dataType) {
$this->jsonBinaryDecoderFormatter->formatEndObject();
} elseif (self::ARRAY === $dataType) {
$this->jsonBinaryDecoderFormatter->formatEndArray();
}
}
}
/**
* @throws BinaryDataReaderException
* @throws JsonBinaryDecoderException
*/
private function parseArrayOrObject(int $type, int $intSize): array
{
$large = $intSize === self::LARGE_OFFSET_SIZE;
$offsetSize = self::offsetSize($large);
if ($this->dataLength < 2 * $offsetSize) {
throw new InvalidArgumentException('Document is not long enough to contain the two length fields');
}
$elementCount = $this->binaryDataReader->readUIntBySize($intSize);
$bytes = $this->binaryDataReader->readUIntBySize($intSize);
if ($bytes > $this->dataLength) {
throw new InvalidArgumentException(
'The value can\'t have more bytes than what\'s available in the data buffer.'
);
}
$keyEntrySize = self::keyEntrySize($large);
$valueEntrySize = self::valueEntrySize($large);
$headerSize = 2 * $offsetSize;
if ($type === self::OBJECT) {
$headerSize += $elementCount * $keyEntrySize;
}
$headerSize += $elementCount * $valueEntrySize;
if ($headerSize > $bytes) {
throw new InvalidArgumentException('Header is larger than the full size of the value.');
}
$keyLengths = [];
if ($type === self::OBJECT) {
// Read each key-entry, consisting of the offset and length of each key ...
for ($i = 0; $i !== $elementCount; ++$i) {
$keyOffset = $this->binaryDataReader->readUIntBySize($intSize);
$keyLengths[$i] = $this->binaryDataReader->readUInt16();
if ($keyOffset < $headerSize) {
throw new InvalidArgumentException('Invalid key offset');
}
}
}
$entries = [];
for ($i = 0; $i !== $elementCount; ++$i) {
$entries[$i] = $this->getOffsetOrInLinedValue($bytes, $intSize, $valueEntrySize);
}
$keys = [];
if ($type === self::OBJECT) {
for ($i = 0; $i !== $elementCount; ++$i) {
$keys[$i] = $this->binaryDataReader->read($keyLengths[$i]);
}
}
$results = [];
for ($i = 0; $i !== $elementCount; ++$i) {
$results[] = [
'name' => $keys[$i] ?? null,
'value' => $entries[$i]
];
}
return $results;
}
private static function offsetSize(bool $large): int
{
return $large ? self::LARGE_OFFSET_SIZE : self::SMALL_OFFSET_SIZE;
}
private static function keyEntrySize(bool $large): int
{
return $large ? self::KEY_ENTRY_SIZE_LARGE : self::KEY_ENTRY_SIZE_SMALL;
}
private static function valueEntrySize(bool $large): int
{
return $large ? self::VALUE_ENTRY_SIZE_LARGE : self::VALUE_ENTRY_SIZE_SMALL;
}
/**
* @throws BinaryDataReaderException
* @throws JsonBinaryDecoderException
*/
private function getOffsetOrInLinedValue(int $bytes, int $intSize, int $valueEntrySize): JsonBinaryDecoderValue
{
$type = $this->binaryDataReader->readUInt8();
if (self::isInLinedType($type, $intSize)) {
$scalar = $this->parseScalar($type);
// In binlog format, JSON arrays are fixed width elements, even though type value can be smaller.
// In order to properly process this case, we need to move cursor to the next element, which is on position 1 + $valueEntrySize (1 is length of type)
if ($type === self::UINT16 || $type === self::INT16) {
$readNextBytes = $valueEntrySize - 2 - 1;
$this->binaryDataReader->read($readNextBytes);
}
return $scalar;
}
$offset = $this->binaryDataReader->readUIntBySize($intSize);
if ($offset > $bytes) {
throw new LengthException(
'The offset for the value in the JSON binary document is ' . $offset . ', which is larger than the binary form of the JSON document (' . $bytes . ' bytes)'
);
}
return new JsonBinaryDecoderValue(false, null, $type, $offset);
}
private static function isInLinedType(int $type, int $intSize): bool
{
switch ($type) {
case self::LITERAL:
case self::INT16:
case self::UINT16:
return true;
case self::INT32:
case self::UINT32:
return self::LARGE_OFFSET_SIZE === $intSize;
default:
return false;
}
}
/**
* @throws JsonBinaryDecoderException
*/
private function parseScalar(int $type): JsonBinaryDecoderValue
{
if (self::LITERAL === $type) {
$data = $this->readLiteral();
} elseif (self::INT16 === $type) {
$data = $this->binaryDataReader->readInt16();
} elseif (self::INT32 === $type) {
$data = ($this->binaryDataReader->readInt32());
} elseif (self::INT64 === $type) {
$data = $this->binaryDataReader->readInt64();
} elseif (self::UINT16 === $type) {
$data = ($this->binaryDataReader->readUInt16());
} elseif (self::UINT64 === $type) {
$data = ($this->binaryDataReader->readUInt64());
} elseif (self::DOUBLE === $type) {
$data = ($this->binaryDataReader->readDouble());
} elseif (self::STRING === $type) {
$data = ($this->binaryDataReader->read($this->readVariableInt()));
// } else if (self::OPAQUE === $type) {
} else {
throw new JsonBinaryDecoderException(
JsonBinaryDecoderException::UNKNOWN_JSON_TYPE_MESSAGE . $type,
JsonBinaryDecoderException::UNKNOWN_JSON_TYPE_CODE
);
}
return new JsonBinaryDecoderValue(true, $data, $type);
}
private function readLiteral(): ?bool
{
$literal = ord($this->binaryDataReader->read(BinaryDataReader::UNSIGNED_SHORT_LENGTH));
if (self::LITERAL_NULL === $literal) {
return null;
}
if (self::LITERAL_TRUE === $literal) {
return true;
}
if (self::LITERAL_FALSE === $literal) {
return false;
}
return null;
}
private function readVariableInt(): int
{
$maxBytes = min($this->binaryDataReader->getBinaryDataLength(), 5);
$len = 0;
for ($i = 0; $i < $maxBytes; ++$i) {
$size = $this->binaryDataReader->readUInt8();
// Get the next 7 bits of the length.
$len |= ($size & 0x7f) << (7 * $i);
if (($size & 0x80) === 0) {
// This was the last byte. Return successfully.
return $len;
}
}
return $len;
}
/**
* @throws JsonBinaryDecoderException
* @throws BinaryDataReaderException
*/
private function assignValues(JsonBinaryDecoderValue $jsonBinaryDecoderValue): void
{
if (false === $jsonBinaryDecoderValue->isIsResolved()) {
$this->ensureOffset($jsonBinaryDecoderValue->getOffset());
$this->parseJson($jsonBinaryDecoderValue->getType());
} elseif (null === $jsonBinaryDecoderValue->getValue()) {
$this->jsonBinaryDecoderFormatter->formatValueNull();
} elseif (is_bool($jsonBinaryDecoderValue->getValue())) {
$this->jsonBinaryDecoderFormatter->formatValueBool($jsonBinaryDecoderValue->getValue());
} elseif (is_numeric($jsonBinaryDecoderValue->getValue())) {
$this->jsonBinaryDecoderFormatter->formatValueNumeric($jsonBinaryDecoderValue->getValue());
}
}
private function ensureOffset(?int $ensureOffset): void
{
if (null === $ensureOffset) {
return;
}
$pos = $this->binaryDataReader->getReadBytes();
if ($pos !== $ensureOffset) {
if ($ensureOffset < $pos) {
return;
}
$this->binaryDataReader->advance($ensureOffset + 1 - $pos);
}
}
}