Skip to content

Commit

Permalink
add method to check is valkey (#72)
Browse files Browse the repository at this point in the history
Signed-off-by: catcherwong <catcher_hwq@outlook.com>
  • Loading branch information
catcherwong authored Oct 3, 2024
1 parent 3427613 commit 702d263
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/RDBParser/BinaryReaderRDBParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RDBParser
Expand All @@ -14,6 +16,8 @@ public partial class BinaryReaderRDBParser : IRDBParser
private ulong _idle = 0;
private int _freq = 0;
private int _mem_policy = -1; // 1 - lru | 2 - lfu
private int _version = -1;
private HashSet<string> _auxKey = new HashSet<string>();

public BinaryReaderRDBParser(IReaderCallback callback, ParserFilter filter = null)
{
Expand All @@ -32,6 +36,7 @@ public void Parse(string path)

var versionBytes = br.ReadBytes(Constant.MagicCount.VERSION);
var version = BinaryReaderBasicVerify.CheckAndGetRDBVersion(versionBytes);
_version = version;
_callback.StartRDB(version);

ulong db = 0;
Expand Down Expand Up @@ -99,6 +104,10 @@ public void Parse(string path)
var auxKey = br.ReadStr();
var auxVal = br.ReadStr();
_callback.AuxField(auxKey, auxVal);

var k = Encoding.UTF8.GetString(auxKey);
_auxKey.Add(k);

continue;
}

Expand Down Expand Up @@ -188,6 +197,9 @@ public void Parse(string path)
public Task ParseAsync(string path)
=> Task.Run(() => Parse(path));

public bool IsValkey()
=> _auxKey.Contains("valkey-ver");

private bool MatchFilter(int database = -1, int dataType = -1, byte[] key = null)
{
if (_filter == null) return true;
Expand Down
32 changes: 32 additions & 0 deletions tests/RDBParserTests/ValkeySimpleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using RDBParser;
using Xunit;
using Xunit.Abstractions;

namespace RDBParserTests
{
public class ValkeySimpleTests
{
private ITestOutputHelper _output;

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

[Fact]
public void TestIsValkey()
{
// set mykey v1
// bgsave
var path = TestHelper.GetRDBPath("valkey_80_normal.rdb");

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


Assert.True(parser.IsValkey());
}
}

}
Binary file added tests/dumps/valkey_80_normal.rdb
Binary file not shown.

0 comments on commit 702d263

Please sign in to comment.