-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprot_binlog.go
1223 lines (959 loc) · 24.7 KB
/
prot_binlog.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The MIT License (MIT)
Copyright (c) 2015 Nirbhay Choubey
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.
*/
package mysql
import (
"encoding/binary"
"io"
"os"
"strconv"
"strings"
"time"
)
const (
_LOG_EVENT_BINLOG_IN_USE_F = 0x1
_LOG_EVENT_THREAD_SPECIFIC_F = 0x4
_LOG_EVENT_SUPPRESS_USE_F = 0x8
_LOG_EVENT_ARTIFICIAL_F = 0x20
_LOG_EVENT_RELAY_LOG_F = 0x40
_LOG_EVENT_SKIP_REPLICATION_F = 0x8000
)
const (
_EVENT_TYPE_OFFSET = 4
_FLAGS_OFFSET = 17
)
type netReader struct {
conn *Conn
slave binlogSlave
nonBlocking bool
first bool
eof bool
closed bool
e error
nextEvent []byte
}
// init
func (nr *netReader) init(p properties) error {
var (
err error
port uint64
)
// initialize slave structure
v := strings.Split(p.address, ":")
nr.slave.host = v[0]
if port, err = strconv.ParseUint(v[1], 10, 16); err != nil {
return myError(ErrInvalidDSN, err)
} else {
nr.slave.port = uint16(port)
}
nr.slave.id = p.binlogSlaveId
nr.slave.username = p.username
nr.slave.password = p.password
nr.slave.replicationRank = 0
nr.slave.masterId = 0
nr.nonBlocking = p.binlogDumpNonBlock
// establish a connection with the master server
if nr.conn, err = open(p); err != nil {
nr.closed = true
return err
}
// notify master about checksum awareness
if p.binlogVerifyChecksum {
if err = notifyChecksumAwareness(nr.conn); err != nil {
return err
}
}
// send COM_REGISTER_SLAVE to (master) server
if err = nr.registerSlave(); err != nil {
return err
}
return nil
}
type binlogSlave struct {
id uint32
host string
username string
password string
port uint16
replicationRank uint32 // ??
masterId uint32 // ??
}
type event struct {
header eventHeader
body []byte
}
func (nr *netReader) begin(index binlogIndex) error {
return nr.binlogDump(index)
}
func (nr *netReader) binlogDump(index binlogIndex) error {
var (
b []byte
err error
)
c := nr.conn
// reset the protocol packet sequence number
c.resetSeqno()
if b, err = c.createComBinlogDump(nr.slave, index, nr.nonBlocking); err != nil {
return err
}
// send COM_BINLOG_DUMP packet to (master) server
if err = c.writePacket(b); err != nil {
return err
}
if err = nr.readEvent(); err != nil {
nr.eof = true
return err
}
nr.first = true
return nil
}
func (nr *netReader) close() error {
if err := nr.conn.Close(); err != nil {
return err
}
nr.closed = true
return nil
}
func (nr *netReader) registerSlave() error {
var (
err error
b []byte
)
c := nr.conn
// reset the protocol packet sequence number
c.resetSeqno()
if b, err = c.createComRegisterSlave(nr.slave); err != nil {
return err
}
// send COM_REGISTER_SLAVE packet to (master) server
if err = c.writePacket(b); err != nil {
return err
}
if b, err = c.readPacket(); err != nil {
return err
}
switch b[0] {
case _PACKET_OK: //expected
// parse OK packet
if warn := c.parseOkPacket(b); warn {
return &c.e
}
case _PACKET_ERR: //expected
// parse err packet
c.parseErrPacket(b)
return &c.e
default: // unexpected
return myError(ErrInvalidPacket)
}
return nil
}
func (nr *netReader) next() bool {
var err error
// reset last error
nr.e = nil
if nr.closed || nr.eof {
return false
}
if nr.first { // first event has already been read
nr.first = false
} else if err = nr.readEvent(); err != nil { // read the next event
nr.eof = true
if err != io.EOF {
nr.e = err
}
// no more events to read
return false
}
return true
}
func (nr *netReader) event() []byte {
return nr.nextEvent
}
func (nr *netReader) error() error {
return nr.e
}
func (c *Conn) createComRegisterSlave(s binlogSlave) ([]byte, error) {
var (
b []byte
off, payloadLength int
err error
)
payloadLength = 18 + len(s.host) + len(s.username) + len(s.password)
if b, err = c.buff.Reset(4 + payloadLength); err != nil {
return nil, err
}
off += 4 // placeholder for protocol packet header
b[off] = _COM_REGISTER_SLAVE
off++
binary.LittleEndian.PutUint32(b[off:off+4], s.id)
off += 4
b[off] = uint8(len(s.host))
off++
copy(b[off:], s.host)
off += len(s.host)
b[off] = uint8(len(s.username))
off++
copy(b[off:], s.username)
off += len(s.username)
b[off] = uint8(len(s.password))
off++
copy(b[off:], s.password)
off += len(s.password)
binary.LittleEndian.PutUint16(b[off:off+2], s.port)
off += 2
binary.LittleEndian.PutUint32(b[off:off+4], s.replicationRank)
off += 4
binary.LittleEndian.PutUint32(b[off:off+4], s.masterId)
off += 4
return b[0:off], nil
}
func (c *Conn) createComBinlogDump(slave binlogSlave, index binlogIndex,
nonBlocking bool) ([]byte, error) {
var (
b []byte
off, payloadLength int
err error
)
payloadLength = 11 + len(index.file)
if b, err = c.buff.Reset(4 + payloadLength); err != nil {
return nil, err
}
off += 4 // placeholder for protocol packet header
b[off] = _COM_BINLOG_DUMP
off++
binary.LittleEndian.PutUint32(b[off:off+4], index.position)
off += 4
if nonBlocking {
// flags (0x01 : BINLOG_DUMP_NON_BLOCK)
binary.LittleEndian.PutUint16(b[off:off+2], uint16(0x01))
} else {
binary.LittleEndian.PutUint16(b[off:off+2], uint16(0))
}
off += 2
binary.LittleEndian.PutUint32(b[off:off+4], slave.id)
off += 4
off += copy(b[off:], index.file)
return b[0:off], nil
}
func parseEventHeader(b []byte) (eventHeader, int) {
var (
off int
header eventHeader
)
header.timestamp = binary.LittleEndian.Uint32(b[off : off+4])
off += 4
header.type_ = b[off]
off++
header.serverId = binary.LittleEndian.Uint32(b[off : off+4])
off += 4
header.size = binary.LittleEndian.Uint32(b[off : off+4])
off += 4
header.position = binary.LittleEndian.Uint32(b[off : off+4])
off += 4
header.flags = binary.LittleEndian.Uint16(b[off : off+2])
off += 2
return header, off
}
func (nr *netReader) readEvent() error {
var (
err error
b []byte
)
c := nr.conn
if b, err = c.readPacket(); err != nil {
return err
}
switch b[0] {
case _PACKET_OK: // expected
// move past [00]
nr.nextEvent = b[1:]
case _PACKET_ERR: //expected
// handle err packet
c.parseErrPacket(b)
return &c.e
case _PACKET_EOF: // expected
if warn := c.parseEOFPacket(b); warn {
// save warning (if any)
nr.e = &c.e
}
return io.EOF
default: //unexpected
return myError(ErrInvalidPacket)
}
return nil
}
func (b *Binlog) parseStartEventV3(buf []byte, ev *StartEventV3) (err error) {
var off int
ev.binlogVersion = binary.LittleEndian.Uint16(buf)
off += 2
ev.serverVersion = string(buf[off : off+50])
off += 50
ev.creationTime = time.Unix(int64(binary.LittleEndian.Uint32(buf[off:])), 0)
return
}
func (b *Binlog) parseQueryEvent(buf []byte, ev *QueryEvent) (err error) {
var (
off int
schemaLength int
varLength int
)
ev.slaveProxyId = binary.LittleEndian.Uint32(buf)
off += 4
ev.executionTime = time.Unix(int64(binary.LittleEndian.Uint32(buf[off:])), 0)
off += 4
// move past schema length
schemaLength = int(buf[off])
off++
ev.errorCode = binary.LittleEndian.Uint16(buf[off:])
off += 2
if b.desc.binlogVersion >= 4 {
varLength = int(binary.LittleEndian.Uint16(buf[off:]))
off += 2
}
ev.statusVars = string(buf[off : off+varLength])
off += varLength
ev.schema = string(buf[off : off+schemaLength])
off += schemaLength
off++
ev.query = string(buf[off:])
return nil
}
func (b *Binlog) parseRotateEvent(buf []byte, ev *RotateEvent) (err error) {
var off int
ev.position = binary.LittleEndian.Uint64(buf)
off += 8
ev.file = string(buf[off:])
return
}
func (b *Binlog) parseIntvarEvent(buf []byte, ev *IntvarEvent) (err error) {
var off int
ev.type_ = uint8(buf[0])
off++
ev.value = binary.LittleEndian.Uint64(buf[off:])
return
}
// parseLoadEvent parses LOAD_EVENT as well as NEW_LOAD_EVENT
func (b *Binlog) parseLoadEvent(buf []byte, ev *LoadEvent) (err error) {
var off int
ev.slaveProxyId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.executionTime = time.Unix(int64(binary.LittleEndian.Uint32(buf[off:])), 0)
off += 4
ev.skipLines = binary.LittleEndian.Uint32(buf[off:])
off += 4
// table name length
off++
// schema name length
off++
ev.fieldCount = binary.LittleEndian.Uint32(buf[off:])
off += 4
if ev.header.type_ == LOAD_EVENT {
ev.fieldTerminator = string(buf[off])
off++
ev.enclosedBy = string(buf[off])
off++
ev.lineTerminator = string(buf[off])
off++
ev.lineStart = string(buf[off])
off++
ev.escapedBy = string(buf[off])
off++
ev.optFlags = make([]byte, 1)
ev.optFlags[0] = buf[off]
off++
ev.emptyFlags = uint8(buf[off])
off++
} else { // NEW_LOAD_EVENT
var length int
length = int(buf[off])
off++
ev.fieldTerminator = string(buf[off : off+length])
off += length
length = int(buf[off])
off++
ev.enclosedBy = string(buf[off : off+length])
off += length
length = int(buf[off])
off++
ev.lineTerminator = string(buf[off : off+length])
off += length
length = int(buf[off])
off++
ev.lineStart = string(buf[off : off+length])
off += length
length = int(buf[off])
off++
ev.escapedBy = string(buf[off : off+length])
off += length
length = int(buf[off])
off++
ev.optFlags = make([]byte, length)
copy(ev.optFlags, buf[off:off+length])
off += length
}
/*
we do not really need individual field name lengths as
field names are null-terminated.
*/
off += int(ev.fieldCount)
ev.fields = make([]string, ev.fieldCount)
var i, n int
for i = 0; i < int(ev.fieldCount); i++ {
ev.fields[i], n = getNullTerminatedString(buf[off:])
off += n
}
ev.table, n = getNullTerminatedString(buf[off:])
off += n
ev.schema, n = getNullTerminatedString(buf[off:])
off += n
ev.file, n = getNullTerminatedString(buf[off:])
off += n
return
}
func (b *Binlog) parseSlaveEvent(buf []byte, ev *SlaveEvent) (err error) {
var off int
ev.masterPosition = binary.LittleEndian.Uint64(buf[off:])
off += 8
ev.masterPort = binary.LittleEndian.Uint16(buf[off:])
off += 2
var n int
ev.masterHost, n = getNullTerminatedString(buf[off:])
off += n
ev.masterLog, n = getNullTerminatedString(buf[off:])
off += n
return
}
func (b *Binlog) parseCreateFileEvent(buf []byte, ev *CreateFileEvent) (err error) {
var off int
ev.fileId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.data = buf[off:]
return
}
func (b *Binlog) parseAppendBlockEvent(buf []byte, ev *AppendBlockEvent) (err error) {
var off int
ev.fieldId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.data = buf[off:]
return
}
func (b *Binlog) parseExecLoadEvent(buf []byte, ev *ExecLoadEvent) (err error) {
var off int
ev.fileId = binary.LittleEndian.Uint32(buf[off:])
return
}
func (b *Binlog) parseDeleteFileEvent(buf []byte, ev *DeleteFileEvent) (err error) {
ev.fileId = binary.LittleEndian.Uint32(buf[0:])
return
}
func (b *Binlog) parseRandEvent(buf []byte, ev *RandEvent) (err error) {
var off int
ev.seed1 = binary.LittleEndian.Uint64(buf[off:])
off += 8
ev.seed2 = binary.LittleEndian.Uint64(buf[off:])
return
}
func (b *Binlog) parseUserVarEvent(buf []byte, ev *UserVarEvent) (err error) {
var off int
// name length
length := int(binary.LittleEndian.Uint32(buf[off:]))
off += 4
ev.name = string(buf[off : off+length])
off += length
if buf[off] != 0 {
ev.null = true
}
off++
if !ev.null {
ev.type_ = uint8(buf[off])
off++
ev.charset = binary.LittleEndian.Uint32(buf[off:])
off += 4
var valueLen int
valueLen = int(binary.LittleEndian.Uint32(buf[off:]))
off += 4
ev.value = make([]byte, valueLen)
copy(ev.value, buf[off:off+valueLen])
off += valueLen
}
// more data?
if len(buf[off:]) > 0 {
ev.flags = uint8(buf[off])
}
return
}
func (b *Binlog) parseFormatDescriptionEvent(buf []byte, ev *FormatDescriptionEvent) (err error) {
var off int
ev.binlogVersion = binary.LittleEndian.Uint16(buf)
off += 2
ev.serverVersion = string(buf[off : off+50])
off += 50
ev.creationTime = time.Unix(int64(binary.LittleEndian.Uint32(buf[off:])), 0)
off += 4
ev.commonHeaderLength = uint8(buf[off])
off++
// TODO: check server version and/or binlog version to see if it
// supports event checksum. For now consider and store rest of
// unread buffer to postHeaderLength.
ev.postHeaderLength = buf[off:]
// Checksum algorithm descriptor (1 byte), its placed right before the
// checksum value (4 bytes), excluded by the caller
ev.checksumAlg = uint8(ev.postHeaderLength[len(ev.postHeaderLength)-1])
return
}
func (b *Binlog) parseRowsQueryLogEvent(buf []byte, ev *RowsQueryLogEvent) (err error) {
length := buf[0]
ev.query = string(buf[1:length])
return
}
func (b *Binlog) parseXidEvent(buf []byte, ev *XidEvent) (err error) {
ev.xid = binary.LittleEndian.Uint64(buf[0:])
return
}
func (b *Binlog) parseBeginLoadQueryEvent(buf []byte, ev *BeginLoadQueryEvent) (err error) {
var off int
ev.fileId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.data = buf[off:]
return
}
func (b *Binlog) parseExecuteLoadQueryEvent(buf []byte, ev *ExecuteLoadQueryEvent) (err error) {
var off int
ev.slaveProxyId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.executionTime = time.Unix(int64(binary.LittleEndian.Uint32(buf[off:])), 0)
off += 4
ev.schemaLength = buf[off]
off++
ev.errorCode = binary.LittleEndian.Uint16(buf[off:])
off += 2
ev.statusVarsLength = binary.LittleEndian.Uint16(buf[off:])
off += 4
ev.fileId = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.startPosition = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.endPosition = binary.LittleEndian.Uint32(buf[off:])
off += 4
ev.dupHandlingFlags = uint8(buf[off])
return
}
func (b *Binlog) parseTableMapEvent(buf []byte, ev *TableMapEvent) (err error) {
var (
off int
length int
i uint64
)
if b.desc.postHeaderLength[ev.header.type_-1] == 6 {
ev.tableId = uint64(binary.LittleEndian.Uint32(buf[off:]))
off += 4
} else {
ev.tableId = getUint48(buf)
off += 6
}
ev.flags = binary.LittleEndian.Uint16(buf[off:])
off += 2
length = int(buf[off])
off++
ev.schema = string(buf[off : off+length])
off += length
off++
length = int(buf[off])
off++
ev.table = string(buf[off : off+length])
off += length
off++
ev.columnCount, length = getLenencInt(buf[off:])
off += length
ev.columns = make([]EventColumn, ev.columnCount)
// field type
for i = 0; i < ev.columnCount; i++ {
ev.columns[i].type_ = uint8(buf[off])
off++
}
// field meta data
var meta uint16
_, length = getLenencInt(buf[off:])
off += length
// read meta data and store them into the respective columns
// TODO: verify that buffer consumed is equal to the meta data size
for i = 0; i < ev.columnCount; i++ {
switch getMetaDataSize(ev.columns[i].type_) {
case 2:
meta = binary.LittleEndian.Uint16(buf[off:])
off += 2
case 1:
meta = uint16(buf[off])
off++
default:
meta = 0
}
ev.columns[i].meta = meta
}
// null bitmap
nullBitmapSize := int((ev.columnCount + 7) / 8)
nullBitmap := buf[off : off+nullBitmapSize]
for i = 0; i < ev.columnCount; i++ {
if isNull(nullBitmap, uint16(i), 0) {
ev.columns[i].nullable = true
}
}
return
}
func getMetaDataSize(type_ uint8) uint8 {
switch type_ {
case _TYPE_TINY_BLOB, _TYPE_BLOB, _TYPE_MEDIUM_BLOB, _TYPE_LONG_BLOB,
_TYPE_DOUBLE, _TYPE_FLOAT, _TYPE_GEOMETRY, _TYPE_TIME2,
_TYPE_DATETIME2, _TYPE_TIMESTAMP2:
return 1
case _TYPE_SET, _TYPE_ENUM, _TYPE_STRING, _TYPE_BIT, _TYPE_VARCHAR, _TYPE_NEW_DECIMAL:
return 2
default:
}
return 0
}
func (b *Binlog) parseIncidentEvent(buf []byte, ev *IncidentEvent) (err error) {
var (
off int
length int
)
ev.type_ = binary.LittleEndian.Uint16(buf[off:])
off += 2
length = int(buf[off])
off++
ev.message = string(buf[off : off+length])
return
}
// Note: There was no after-image in v0.
func (b *Binlog) parseRowsEvent(buf []byte, ev *RowsEvent) (err error) {
var (
off int
length int
)
if b.desc.postHeaderLength[ev.header.type_-1] == 6 {
ev.tableId = uint64(binary.LittleEndian.Uint32(buf[off:]))
off += 4
} else {
ev.tableId = getUint48(buf)
off += 6
}
ev.flags = binary.LittleEndian.Uint16(buf[off:])
off += 2
if b.desc.postHeaderLength[ev.header.type_-1] == 10 {
length = int(binary.LittleEndian.Uint16(buf[off:])) - 2
off += 2
ev.extraData = buf[off : off+length]
off += length
}
ev.columnCount, length = getLenencInt(buf[off:])
off += length
length = int((ev.columnCount + 7) / 8)
ev.columnsPresentBitmap1 = buf[off : off+length]
off += length
if (ev.header.type_ == UPDATE_ROWS_EVENT_V1) ||
(ev.header.type_ == UPDATE_ROWS_EVENT) {
ev.columnsPresentBitmap2 = buf[off : off+length]
off += length
}
ev.rows1.Rows = make([]EventRow, 0)
if (ev.header.type_ == UPDATE_ROWS_EVENT_V1) ||
(ev.header.type_ == UPDATE_ROWS_EVENT) {
ev.rows2.Rows = make([]EventRow, 0)
}
var (
n int
r EventRow
)
for off < len(buf) {
r, n = b.parseEventRow(buf[off:], ev.columnCount,
ev.columnsPresentBitmap1)
ev.rows1.Rows = append(ev.rows1.Rows, r)
off += n
if (ev.header.type_ == UPDATE_ROWS_EVENT_V1) ||
(ev.header.type_ == UPDATE_ROWS_EVENT) {
r, n = b.parseEventRow(buf[off:], ev.columnCount,
ev.columnsPresentBitmap2)
ev.rows2.Rows = append(ev.rows2.Rows, r)
off += n
}
}
return
}
func (b *Binlog) parseEventRow(buf []byte, columnCount uint64,
columnsPresentBitmap []byte) (EventRow, int) {
var (
off int
r EventRow
)
r.Columns = make([]interface{}, 0, columnCount)
nullBitmapSize := int((setBitCount(columnsPresentBitmap) + 7) / 8)
nullBitmap := buf[off : off+nullBitmapSize]
off += nullBitmapSize
for i := uint64(0); i < columnCount; i++ {
if isNull(nullBitmap, uint16(i), 0) == true {
r.Columns = append(r.Columns, nil)
} else {
switch b.tableMap.columns[i].type_ {
// string
case _TYPE_VARCHAR, _TYPE_VARSTRING:
v, n := parseString2(buf[off:], b.tableMap.columns[i].meta)
r.Columns = append(r.Columns, v)
off += n
case _TYPE_STRING, _TYPE_ENUM,
_TYPE_SET, _TYPE_BLOB,
_TYPE_TINY_BLOB, _TYPE_MEDIUM_BLOB,
_TYPE_LONG_BLOB, _TYPE_GEOMETRY,
_TYPE_BIT, _TYPE_DECIMAL:
v, n := parseString(buf[off:])
r.Columns = append(r.Columns, v)
off += n
case _TYPE_NEW_DECIMAL:
v, n := parseNewDecimal(buf[off:], b.tableMap.columns[i].meta)
r.Columns = append(r.Columns, v)
off += n
// int64
case _TYPE_LONG_LONG:
r.Columns = append(r.Columns, parseInt64(buf[off:off+8]))
off += 8
// int32
case _TYPE_LONG, _TYPE_INT24:
r.Columns = append(r.Columns, parseInt32(buf[off:off+4]))
off += 4
// int16
case _TYPE_SHORT, _TYPE_YEAR:
r.Columns = append(r.Columns, parseInt16(buf[off:off+2]))
off += 2
// int8
case _TYPE_TINY:
r.Columns = append(r.Columns, parseInt8(buf[off:off+1]))
off++
// float64
case _TYPE_DOUBLE:
r.Columns = append(r.Columns, parseDouble(buf[off:off+8]))
off += 8
// float32
case _TYPE_FLOAT:
r.Columns = append(r.Columns, parseFloat(buf[off:off+4]))
off += 4
// time.Time
case _TYPE_DATE, _TYPE_DATETIME,
_TYPE_TIMESTAMP:
v, n := parseDate(buf[off:])
r.Columns = append(r.Columns, v)
off += n
// time.Duration
case _TYPE_TIME:
v, n := parseTime(buf[off:])
r.Columns = append(r.Columns, v)
off += n
// TODO: map the following unhandled types accordingly
case _TYPE_NEW_DATE, _TYPE_TIMESTAMP2,
_TYPE_DATETIME2, _TYPE_TIME2,
_TYPE_NULL:
fallthrough
default:
}
}
}
return r, off
}
func (b *Binlog) parseGtidLogEvent(buf []byte, ev *GtidLogEvent) {
var off int