Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to check is valkey #72

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.