Пример #1
0
 private static async Task PrintEventAsync(IBinlogEvent binlogEvent)
 {
     var json = JsonConvert.SerializeObject(binlogEvent, Formatting.Indented,
                                            new JsonSerializerSettings()
     {
         Converters = new List <JsonConverter> {
             new StringEnumConverter()
         }
     });
     await Console.Out.WriteLineAsync(json);
 }
Пример #2
0
        private void UpdateBinlogPosition(IBinlogEvent binlogEvent)
        {
            // Rows event depends on preceding TableMapEvent & we change the position
            // after we read them atomically to prevent missing mapping on reconnect.
            // Figure out something better as TableMapEvent can be followed by several row events.
            if (binlogEvent is TableMapEvent tableMapEvent)
            {
                return;
            }

            if (binlogEvent is RotateEvent rotateEvent)
            {
                _options.Binlog.Filename = rotateEvent.BinlogFilename;
                _options.Binlog.Position = rotateEvent.BinlogPosition;
            }
            else if (binlogEvent.Header.NextEventPosition > 0)
            {
                _options.Binlog.Position = binlogEvent.Header.NextEventPosition;
            }
        }
Пример #3
0
        private void UpdateGtidPosition(IBinlogEvent binlogEvent)
        {
            if (_options.Binlog.StartingStrategy != StartingStrategy.FromGtid)
            {
                return;
            }

            if (binlogEvent is GtidEvent gtidEvent)
            {
                _gtid = gtidEvent.Gtid;
            }
            else if (binlogEvent is XidEvent xidEvent)
            {
                CommitGtid();
            }
            else if (binlogEvent is QueryEvent queryEvent)
            {
                if (string.IsNullOrWhiteSpace(queryEvent.SqlStatement))
                {
                    return;
                }

                if (queryEvent.SqlStatement == "BEGIN")
                {
                    _transaction = true;
                }
                else if (queryEvent.SqlStatement == "COMMIT" || queryEvent.SqlStatement == "ROLLBACK")
                {
                    CommitGtid();
                }
                else if (!_transaction)
                {
                    // Auto-commit query like DDL
                    CommitGtid();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Constructs a <see cref="IBinlogEvent"/> from packet buffer.
        /// </summary>
        public virtual IBinlogEvent DeserializeEvent(ref PacketReader reader)
        {
            var eventHeader = new EventHeader(ref reader);

            // Consider verifying checksum
            // ChecksumType.Verify(eventBuffer, checksumBuffer);
            reader.SliceFromEnd(ChecksumStrategy.Length);

            IBinlogEvent binlogEvent = null;

            if (EventParsers.TryGetValue(eventHeader.EventType, out var eventParser))
            {
                binlogEvent = eventParser.ParseEvent(eventHeader, ref reader);
            }
            else
            {
                binlogEvent = new UnknownEvent(eventHeader);
            }

            if (binlogEvent is FormatDescriptionEvent formatEvent)
            {
                ChecksumStrategy = formatEvent.ChecksumType switch
                {
                    ChecksumType.NONE => new NoneChecksum(),
                    ChecksumType.CRC32 => new Crc32Checksum(),
                    _ => throw new InvalidOperationException("The master checksum type is not supported.")
                };
            }
            if (binlogEvent is TableMapEvent tableMapEvent)
            {
                TableMapCache[tableMapEvent.TableId] = tableMapEvent;
            }

            return(binlogEvent);
        }
    }