Skip to content

Commit

Permalink
Merge pull request #32 from HongGeonUi/randEvent
Browse files Browse the repository at this point in the history
feat: Add RandEvent, RandEventParser
  • Loading branch information
rusuly authored Dec 11, 2023
2 parents a9f23d0 + 605be73 commit 49fc4aa
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/MySqlCdc/Events/RandEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace MySqlCdc.Events;

/// <summary>
/// Represents two seed values that set the rand_seed1 and rand_seed2 system variables that are used to compute the random number.
/// <a href="https://mariadb.com/kb/en/rand_event/">See more</a>
/// </summary>
/// <remarks>
/// Creates a new <see cref="RandEvent"/>.
/// </remarks>
public record RandEvent(ulong Seed1, ulong Seed2) : IBinlogEvent
{
/// <summary>
/// Gets the rand_seed1
/// </summary>
public ulong Seed1 {get; } = Seed1;

/// <summary>
/// Gets the rand_seed2
/// </summary>
public ulong Seed2 { get; } = Seed2;
}
23 changes: 23 additions & 0 deletions src/MySqlCdc/Parsers/RandEventParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MySqlCdc.Events;
using MySqlCdc.Protocol;

namespace MySqlCdc.Parsers;

/// <summary>
/// Parses <see cref="RandEvent"/> events.
/// Supports all versions of MariaDB and MySQL.
/// </summary>

public class RandEventParser : IEventParser
{
/// <summary>
/// Parses <see cref="RandEvent"/> from the buffer.
/// </summary>
public IBinlogEvent ParseEvent(EventHeader header, ref PacketReader reader)
{
var seed1 = reader.ReadUInt64LittleEndian();
var seed2 = reader.ReadUInt64LittleEndian();

return new RandEvent(seed1, seed2);
}
}
10 changes: 10 additions & 0 deletions src/MySqlCdc/Protocol/PacketReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public UInt32 ReadUInt32BigEndian()
return result;
}

/// <summary>
/// Reads 64-bit long written in little-endian format.
/// </summary>
public UInt64 ReadUInt64LittleEndian()
{
UInt64 result = BinaryPrimitives.ReadUInt64LittleEndian(_span.Slice(_offset));
_offset += 8;
return result;
}

/// <summary>
/// Reads 64-bit long written in little-endian format.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions tests/MySqlCdc.Tests/Parsers/RandEventParserTests.cs
Original file line number Diff line number Diff line change
@@ -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(763747084UL, @event.Seed1);
Assert.Equal(79468295UL, @event.Seed2);
}
}

0 comments on commit 49fc4aa

Please sign in to comment.