From 953454ae44b64aaa3620895f564eb9b66084c2ae Mon Sep 17 00:00:00 2001 From: Geonui Date: Sun, 10 Dec 2023 16:18:13 +0900 Subject: [PATCH 1/2] feat: Add RandEvent, RandEventParser --- src/MySqlCdc/Events/RandEvent.cs | 22 ++++++++++++++ src/MySqlCdc/Parsers/RandEventParser.cs | 23 +++++++++++++++ .../Parsers/RandEventParserTests.cs | 29 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/MySqlCdc/Events/RandEvent.cs create mode 100644 src/MySqlCdc/Parsers/RandEventParser.cs create mode 100644 tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs diff --git a/src/MySqlCdc/Events/RandEvent.cs b/src/MySqlCdc/Events/RandEvent.cs new file mode 100644 index 0000000..712b8b2 --- /dev/null +++ b/src/MySqlCdc/Events/RandEvent.cs @@ -0,0 +1,22 @@ +namespace MySqlCdc.Events; + + /// + /// Represents two seed values that set the rand_seed1 and rand_seed2 system variables that are used to compute the random number. + /// See more + /// + /// + /// Creates a new . + /// + public record RandEvent(long Seed1, long Seed2) : IBinlogEvent + { + /// + /// Gets the rand_seed1 + /// + public long Seed1 {get; } = Seed1; + + /// + /// Gets the rand_seed2 + /// + public long Seed2 {get; } = Seed2; + + } \ No newline at end of file diff --git a/src/MySqlCdc/Parsers/RandEventParser.cs b/src/MySqlCdc/Parsers/RandEventParser.cs new file mode 100644 index 0000000..3594989 --- /dev/null +++ b/src/MySqlCdc/Parsers/RandEventParser.cs @@ -0,0 +1,23 @@ +using MySqlCdc.Events; +using MySqlCdc.Protocol; + +namespace MySqlCdc.Parsers; + + /// + /// Parses events. + /// Supports all versions of MariaDB and MySQL. + /// + + public class RandEventParser : IEventParser + { + /// + /// Parses from the buffer. + /// + public IBinlogEvent ParseEvent(EventHeader header, ref PacketReader reader) + { + var seed1 = reader.ReadInt64LittleEndian(); + var seed2 = reader.ReadInt64LittleEndian(); + + return new RandEvent(seed1, seed2); + } + } diff --git a/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs b/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs new file mode 100644 index 0000000..e9cbbca --- /dev/null +++ b/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs @@ -0,0 +1,29 @@ + +using MySqlCdc.Events; +using MySqlCdc.Parsers; +using MySqlCdc.Protocol; +using Xunit; + +namespace MySqlCdc.Tests.Parsers; + +public class RandEventParserTests +{ + private static byte[] Payload = { + 0x5f, 0x56, 0x75, 0x65, 0x0d, 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, + 0x00, 0x00, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xdb, 0x85, 0x2d, 0x00, 0x00, + 0x00, 0x00, 0x07, 0x97, 0xbc, 0x04, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x6e, 0x46, 0xdd + }; + + [Fact] + public void Test_RandEvent_ReturnsEvent() + { + var reader = new PacketReader(Payload); + var eventHeader = EventHeader.Read(ref reader); + + var parser = new RandEventParser(); + var @event = (RandEvent)parser.ParseEvent(eventHeader, ref reader); + ; + Assert.Equal(763747084, @event.Seed1); + Assert.Equal(79468295, @event.Seed2); + } +} \ No newline at end of file From 605be732c5925433c3b8a2c6763f830b374fdf1a Mon Sep 17 00:00:00 2001 From: dongwook-chan Date: Sun, 10 Dec 2023 22:59:02 +0900 Subject: [PATCH 2/2] Update type of seeds to be unsigned long instead of long --- src/MySqlCdc/Events/RandEvent.cs | 7 +++---- src/MySqlCdc/Parsers/RandEventParser.cs | 4 ++-- src/MySqlCdc/Protocol/PacketReader.cs | 10 ++++++++++ tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/MySqlCdc/Events/RandEvent.cs b/src/MySqlCdc/Events/RandEvent.cs index 712b8b2..cbdc9e3 100644 --- a/src/MySqlCdc/Events/RandEvent.cs +++ b/src/MySqlCdc/Events/RandEvent.cs @@ -7,16 +7,15 @@ namespace MySqlCdc.Events; /// /// Creates a new . /// - public record RandEvent(long Seed1, long Seed2) : IBinlogEvent + public record RandEvent(ulong Seed1, ulong Seed2) : IBinlogEvent { /// /// Gets the rand_seed1 /// - public long Seed1 {get; } = Seed1; + public ulong Seed1 {get; } = Seed1; /// /// Gets the rand_seed2 /// - public long Seed2 {get; } = Seed2; - + public ulong Seed2 { get; } = Seed2; } \ No newline at end of file diff --git a/src/MySqlCdc/Parsers/RandEventParser.cs b/src/MySqlCdc/Parsers/RandEventParser.cs index 3594989..2d7a5c5 100644 --- a/src/MySqlCdc/Parsers/RandEventParser.cs +++ b/src/MySqlCdc/Parsers/RandEventParser.cs @@ -15,8 +15,8 @@ public class RandEventParser : IEventParser /// public IBinlogEvent ParseEvent(EventHeader header, ref PacketReader reader) { - var seed1 = reader.ReadInt64LittleEndian(); - var seed2 = reader.ReadInt64LittleEndian(); + var seed1 = reader.ReadUInt64LittleEndian(); + var seed2 = reader.ReadUInt64LittleEndian(); return new RandEvent(seed1, seed2); } diff --git a/src/MySqlCdc/Protocol/PacketReader.cs b/src/MySqlCdc/Protocol/PacketReader.cs index 4f9e0d3..892b434 100644 --- a/src/MySqlCdc/Protocol/PacketReader.cs +++ b/src/MySqlCdc/Protocol/PacketReader.cs @@ -66,6 +66,16 @@ public UInt32 ReadUInt32BigEndian() return result; } + /// + /// Reads 64-bit long written in little-endian format. + /// + public UInt64 ReadUInt64LittleEndian() + { + UInt64 result = BinaryPrimitives.ReadUInt64LittleEndian(_span.Slice(_offset)); + _offset += 8; + return result; + } + /// /// Reads 64-bit long written in little-endian format. /// diff --git a/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs b/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs index e9cbbca..6f66d24 100644 --- a/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs +++ b/tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs @@ -23,7 +23,7 @@ public void Test_RandEvent_ReturnsEvent() var parser = new RandEventParser(); var @event = (RandEvent)parser.ParseEvent(eventHeader, ref reader); ; - Assert.Equal(763747084, @event.Seed1); - Assert.Equal(79468295, @event.Seed2); + Assert.Equal(763747084UL, @event.Seed1); + Assert.Equal(79468295UL, @event.Seed2); } } \ No newline at end of file