-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
binlog_event_rbr.go
395 lines (332 loc) · 12 KB
/
binlog_event_rbr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
384
385
386
387
388
389
390
391
392
393
394
395
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"encoding/binary"
"vitess.io/vitess/go/mysql/binlog"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// TableMap implements BinlogEvent.TableMap().
//
// Expected format (L = total length of event data):
//
// # bytes field
// 4/6 table id
// 2 flags
// 1 schema name length sl
// sl schema name
// 1 [00]
// 1 table name length tl
// tl table name
// 1 [00]
// <var> column count cc (var-len encoded)
// cc column-def, one byte per column
// <var> column-meta-def (var-len encoded string)
// n NULL-bitmask, length: (cc + 7) / 8
func (ev binlogEvent) TableMap(f BinlogFormat) (*TableMap, error) {
data := ev.Bytes()[f.HeaderLength:]
result := &TableMap{}
pos := 6
if f.HeaderSize(eTableMapEvent) == 6 {
pos = 4
}
result.Flags = binary.LittleEndian.Uint16(data[pos : pos+2])
pos += 2
l := int(data[pos])
result.Database = string(data[pos+1 : pos+1+l])
pos += 1 + l + 1
l = int(data[pos])
result.Name = string(data[pos+1 : pos+1+l])
pos += 1 + l + 1
columnCount, read, ok := readLenEncInt(data, pos)
if !ok {
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "expected column count at position %v (data=%v)", pos, data)
}
pos = read
result.Types = data[pos : pos+int(columnCount)]
pos += int(columnCount)
metaLen, read, ok := readLenEncInt(data, pos)
if !ok {
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "expected metadata length at position %v (data=%v)", pos, data)
}
pos = read
// Allocate and parse / copy Metadata.
result.Metadata = make([]uint16, columnCount)
expectedEnd := pos + int(metaLen)
for c := uint64(0); c < columnCount; c++ {
var err error
result.Metadata[c], pos, err = metadataRead(data, pos, result.Types[c])
if err != nil {
return nil, err
}
}
if pos != expectedEnd {
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected metadata end: got %v was expecting %v (data=%v)", pos, expectedEnd, data)
}
// A bit array that says if each column can be NULL.
result.CanBeNull, _ = newBitmap(data, pos, int(columnCount))
return result, nil
}
// metadataLength returns how many bytes are used for metadata, based on a type.
func metadataLength(typ byte) int {
switch typ {
case binlog.TypeDecimal, binlog.TypeTiny, binlog.TypeShort, binlog.TypeLong, binlog.TypeNull, binlog.TypeTimestamp, binlog.TypeLongLong, binlog.TypeInt24, binlog.TypeDate, binlog.TypeTime, binlog.TypeDateTime, binlog.TypeYear, binlog.TypeNewDate:
// No data here.
return 0
case binlog.TypeFloat, binlog.TypeDouble, binlog.TypeTimestamp2, binlog.TypeDateTime2, binlog.TypeTime2, binlog.TypeJSON, binlog.TypeTinyBlob, binlog.TypeMediumBlob, binlog.TypeLongBlob, binlog.TypeBlob, binlog.TypeGeometry:
// One byte.
return 1
case binlog.TypeNewDecimal, binlog.TypeEnum, binlog.TypeSet, binlog.TypeString:
// Two bytes, Big Endian because of crazy encoding.
return 2
case binlog.TypeVarchar, binlog.TypeBit, binlog.TypeVarString:
// Two bytes, Little Endian
return 2
default:
// Unknown type. This is used in tests only, so panic.
panic(vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataLength: unhandled data type: %v", typ))
}
}
// metadataTotalLength returns the total size of the metadata for an
// array of types.
func metadataTotalLength(types []byte) int {
sum := 0
for _, t := range types {
sum += metadataLength(t)
}
return sum
}
// metadataRead reads a single value from the metadata string.
func metadataRead(data []byte, pos int, typ byte) (uint16, int, error) {
switch typ {
case binlog.TypeDecimal, binlog.TypeTiny, binlog.TypeShort, binlog.TypeLong, binlog.TypeNull, binlog.TypeTimestamp, binlog.TypeLongLong, binlog.TypeInt24, binlog.TypeDate, binlog.TypeTime, binlog.TypeDateTime, binlog.TypeYear, binlog.TypeNewDate:
// No data here.
return 0, pos, nil
case binlog.TypeFloat, binlog.TypeDouble, binlog.TypeTimestamp2, binlog.TypeDateTime2, binlog.TypeTime2, binlog.TypeJSON, binlog.TypeTinyBlob, binlog.TypeMediumBlob, binlog.TypeLongBlob, binlog.TypeBlob, binlog.TypeGeometry:
// One byte.
return uint16(data[pos]), pos + 1, nil
case binlog.TypeNewDecimal, binlog.TypeEnum, binlog.TypeSet, binlog.TypeString:
// Two bytes, Big Endian because of crazy encoding.
return uint16(data[pos])<<8 + uint16(data[pos+1]), pos + 2, nil
case binlog.TypeVarchar, binlog.TypeBit, binlog.TypeVarString:
// Two bytes, Little Endian
return uint16(data[pos]) + uint16(data[pos+1])<<8, pos + 2, nil
default:
// Unknown types, we can't go on.
return 0, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataRead: unhandled data type: %v", typ)
}
}
// metadataWrite writes a single value into the metadata string.
func metadataWrite(data []byte, pos int, typ byte, value uint16) int {
switch typ {
case binlog.TypeDecimal, binlog.TypeTiny, binlog.TypeShort, binlog.TypeLong, binlog.TypeNull, binlog.TypeTimestamp, binlog.TypeLongLong, binlog.TypeInt24, binlog.TypeDate, binlog.TypeTime, binlog.TypeDateTime, binlog.TypeYear, binlog.TypeNewDate:
// No data here.
return pos
case binlog.TypeFloat, binlog.TypeDouble, binlog.TypeTimestamp2, binlog.TypeDateTime2, binlog.TypeTime2, binlog.TypeJSON, binlog.TypeTinyBlob, binlog.TypeMediumBlob, binlog.TypeLongBlob, binlog.TypeBlob, binlog.TypeGeometry:
// One byte.
data[pos] = byte(value)
return pos + 1
case binlog.TypeNewDecimal, binlog.TypeEnum, binlog.TypeSet, binlog.TypeString:
// Two bytes, Big Endian because of crazy encoding.
data[pos] = byte(value >> 8)
data[pos+1] = byte(value)
return pos + 2
case binlog.TypeVarchar, binlog.TypeBit, binlog.TypeVarString:
// Two bytes, Little Endian
data[pos] = byte(value)
data[pos+1] = byte(value >> 8)
return pos + 2
default:
// Unknown type. This is used in tests only, so panic.
panic(vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataRead: unhandled data type: %v", typ))
}
}
// Rows implements BinlogEvent.TableMap().
//
// Expected format (L = total length of event data):
//
// # bytes field
// 4/6 table id
// 2 flags
// -- if version == 2
// 2 extra data length edl
// edl extra data
// -- endif
//
// <var> number of columns (var-len encoded)
// <var> identify bitmap
// <var> data bitmap
// -- for each row
// <var> null bitmap for identify for present rows
// <var> values for each identify field
// <var> null bitmap for data for present rows
// <var> values for each data field
// --
func (ev binlogEvent) Rows(f BinlogFormat, tm *TableMap) (Rows, error) {
typ := ev.Type()
data := ev.Bytes()[f.HeaderLength:]
hasIdentify := typ == eUpdateRowsEventV1 || typ == eUpdateRowsEventV2 ||
typ == eDeleteRowsEventV1 || typ == eDeleteRowsEventV2
hasData := typ == eWriteRowsEventV1 || typ == eWriteRowsEventV2 ||
typ == eUpdateRowsEventV1 || typ == eUpdateRowsEventV2
result := Rows{}
pos := 6
if f.HeaderSize(typ) == 6 {
pos = 4
}
result.Flags = binary.LittleEndian.Uint16(data[pos : pos+2])
pos += 2
// version=2 have extra data here.
if typ == eWriteRowsEventV2 || typ == eUpdateRowsEventV2 || typ == eDeleteRowsEventV2 {
// This extraDataLength contains the 2 bytes length.
extraDataLength := binary.LittleEndian.Uint16(data[pos : pos+2])
pos += int(extraDataLength)
}
columnCount, read, ok := readLenEncInt(data, pos)
if !ok {
return result, vterrors.Errorf(vtrpc.Code_INTERNAL, "expected column count at position %v (data=%v)", pos, data)
}
pos = read
numIdentifyColumns := 0
numDataColumns := 0
if hasIdentify {
// Bitmap of the columns used for identify.
result.IdentifyColumns, pos = newBitmap(data, pos, int(columnCount))
numIdentifyColumns = result.IdentifyColumns.BitCount()
}
if hasData {
// Bitmap of columns that are present.
result.DataColumns, pos = newBitmap(data, pos, int(columnCount))
numDataColumns = result.DataColumns.BitCount()
}
// One row at a time.
for pos < len(data) {
row := Row{}
if hasIdentify {
// Bitmap of identify columns that are null (amongst the ones that are present).
row.NullIdentifyColumns, pos = newBitmap(data, pos, numIdentifyColumns)
// Get the identify values.
startPos := pos
valueIndex := 0
for c := 0; c < int(columnCount); c++ {
if !result.IdentifyColumns.Bit(c) {
// This column is not represented.
continue
}
if row.NullIdentifyColumns.Bit(valueIndex) {
// This column is represented, but its value is NULL.
valueIndex++
continue
}
// This column is represented now. We need to skip its length.
l, err := binlog.CellLength(data, pos, tm.Types[c], tm.Metadata[c])
if err != nil {
return result, err
}
pos += l
valueIndex++
}
row.Identify = data[startPos:pos]
}
if hasData {
// Bitmap of columns that are null (amongst the ones that are present).
row.NullColumns, pos = newBitmap(data, pos, numDataColumns)
// Get the values.
startPos := pos
valueIndex := 0
for c := 0; c < int(columnCount); c++ {
if !result.DataColumns.Bit(c) {
// This column is not represented.
continue
}
if row.NullColumns.Bit(valueIndex) {
// This column is represented, but its value is NULL.
valueIndex++
continue
}
// This column is represented now. We need to skip its length.
l, err := binlog.CellLength(data, pos, tm.Types[c], tm.Metadata[c])
if err != nil {
return result, err
}
pos += l
valueIndex++
}
row.Data = data[startPos:pos]
}
result.Rows = append(result.Rows, row)
}
return result, nil
}
// StringValuesForTests is a helper method to return the string value
// of all columns in a row in a Row. Only use it in tests, as the
// returned values cannot be interpreted correctly without the schema.
// We assume everything is unsigned in this method.
func (rs *Rows) StringValuesForTests(tm *TableMap, rowIndex int) ([]string, error) {
var result []string
valueIndex := 0
data := rs.Rows[rowIndex].Data
pos := 0
for c := 0; c < rs.DataColumns.Count(); c++ {
if !rs.DataColumns.Bit(c) {
continue
}
if rs.Rows[rowIndex].NullColumns.Bit(valueIndex) {
// This column is represented, but its value is NULL.
result = append(result, "NULL")
valueIndex++
continue
}
// We have real data
value, l, err := binlog.CellValue(data, pos, tm.Types[c], tm.Metadata[c], &querypb.Field{Type: querypb.Type_UINT64})
if err != nil {
return nil, err
}
result = append(result, value.ToString())
pos += l
valueIndex++
}
return result, nil
}
// StringIdentifiesForTests is a helper method to return the string
// identify of all columns in a row in a Row. Only use it in tests, as the
// returned values cannot be interpreted correctly without the schema.
// We assume everything is unsigned in this method.
func (rs *Rows) StringIdentifiesForTests(tm *TableMap, rowIndex int) ([]string, error) {
var result []string
valueIndex := 0
data := rs.Rows[rowIndex].Identify
pos := 0
for c := 0; c < rs.IdentifyColumns.Count(); c++ {
if !rs.IdentifyColumns.Bit(c) {
continue
}
if rs.Rows[rowIndex].NullIdentifyColumns.Bit(valueIndex) {
// This column is represented, but its value is NULL.
result = append(result, "NULL")
valueIndex++
continue
}
// We have real data
value, l, err := binlog.CellValue(data, pos, tm.Types[c], tm.Metadata[c], &querypb.Field{Type: querypb.Type_UINT64})
if err != nil {
return nil, err
}
result = append(result, value.ToString())
pos += l
valueIndex++
}
return result, nil
}