Skip to content

Commit

Permalink
support slotinfo 244 (#73)
Browse files Browse the repository at this point in the history
* support slotinfo 244

Signed-off-by: catcherwong <[email protected]>

* fix slot info

Signed-off-by: catcherwong <[email protected]>

* remove useless code

Signed-off-by: catcherwong <[email protected]>

---------

Signed-off-by: catcherwong <[email protected]>
  • Loading branch information
catcherwong authored Oct 6, 2024
1 parent 702d263 commit 8a8ab76
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/RDBParser/BinaryReaderRDBParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class BinaryReaderRDBParser : IRDBParser
private int _freq = 0;
private int _mem_policy = -1; // 1 - lru | 2 - lfu
private int _version = -1;
private ulong _slotId = 0;
private HashSet<string> _auxKey = new HashSet<string>();

public BinaryReaderRDBParser(IReaderCallback callback, ParserFilter filter = null)
Expand Down Expand Up @@ -88,6 +89,19 @@ public void Parse(string path)
}
}

if (opType == Constant.OpCode.SLOTINFO)
{
// cluster keyslot yourkey
_slotId = br.ReadLength();

// slotSize
_ = br.ReadLength();

// expireSlotSize
_ = br.ReadLength();
continue;
}

if (opType == Constant.OpCode.SELECTDB)
{
if (!isFirstDb)
Expand Down Expand Up @@ -174,6 +188,7 @@ public void Parse(string path)
{
info.Idle = _idle;
info.Freq = _freq;
info.SlotId = _slotId;

ReadObject(br, _key, opType, _expiry, info);
}
Expand Down
2 changes: 2 additions & 0 deletions src/RDBParser/Callbacks/Models/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Info

public ulong Zips { get; set; }

public ulong SlotId { get; set; }

public override string ToString()
{
return $"Info{{Encoding={Encoding},Idle={Idle},Freq={Freq},SizeOfValue={SizeOfValue},Zips={Zips}}}";
Expand Down
4 changes: 4 additions & 0 deletions src/RDBParser/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static class MagicCount

public static class OpCode
{
/// <summary>
/// Individual slot info, such as slot id and size (cluster mode only).
/// </summary>
public const int SLOTINFO = 244;
public const int FUNCTION2 = 245;
public const int FUNCTION = 246;
public const int MODULE_AUX = 247;
Expand Down
35 changes: 35 additions & 0 deletions tests/RDBParserTests/ClusterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using RDBParser;
using System.Text;
using Xunit;
using Xunit.Abstractions;

namespace RDBParserTests
{
public class ClusterTests
{
private ITestOutputHelper _output;

public ClusterTests(ITestOutputHelper output)
{
this._output = output;
}

[Fact]
public void TestSoltInfo()
{
// set key{v1} v1
// set key{v12} v12
// bgsave
var path = TestHelper.GetRDBPath("redis74_cluster_slotinfo.rdb");

var callback = new TestReaderCallback(_output);
var parser = new BinaryReaderRDBParser(callback);
parser.Parse(path);

var infos = callback.GetInfos();

Assert.Equal(1165, (int)infos[0][Encoding.UTF8.GetBytes("key{v1}")].SlotId);
Assert.Equal(2589, (int)infos[0][Encoding.UTF8.GetBytes("key{v12}")].SlotId);
}
}
}
Binary file added tests/dumps/redis74_cluster_slotinfo.rdb
Binary file not shown.

0 comments on commit 8a8ab76

Please sign in to comment.