-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRdbReader.cs
More file actions
85 lines (69 loc) · 2.49 KB
/
Copy pathRdbReader.cs
File metadata and controls
85 lines (69 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Redis.Cache;
using Redis.Common;
namespace Redis.Rdb;
public static class RdbReader
{
public static void ReadRdb(string filePath)
{
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using var binaryReader = new BinaryReader(fileStream);
binaryReader.ValidateRedisMagicString();
var version = binaryReader.GetRdbVersion();
ulong? database = null;
long expiry = 0;
while (fileStream.Position < fileStream.Length)
{
var opCode = binaryReader.ReadByte();
if (opCode == RdbConstants.OpCodes.SelectDb)
{
database = binaryReader.ReadLength();
continue;
}
if (opCode == RdbConstants.OpCodes.ResizeDb)
{
var dbSize = binaryReader.ReadLength();
var expireSize = binaryReader.ReadLength();
Console.WriteLine($"DbSize: {dbSize} - {expireSize}");
continue;
}
if (opCode == RdbConstants.OpCodes.Aux)
{
var auxKey = binaryReader.ReadStr();
var auxKeyStr = auxKey.AsString();
var auxVal = binaryReader.ReadStr();
var auxValStr = auxVal.AsString();
Console.WriteLine($"AuxField: {auxKeyStr} - {auxValStr}");
continue;
}
if (opCode == RdbConstants.OpCodes.Eof)
{
if (version >= 5)
{
binaryReader.ReadBytes(RdbConstants.Verification.Checksum);
}
break;
}
if (opCode == RdbConstants.OpCodes.ExpireTimeMs)
{
expiry = binaryReader.ReadInt64();
opCode = binaryReader.ReadByte();
}
if (opCode == RdbConstants.OpCodes.ExpireTime)
{
// expiry = binaryReader.ReadInt32();
// expireTime = binaryReader.ReadInt32().ToString();
opCode = binaryReader.ReadByte();
}
if (!database.HasValue)
{
continue;
}
if (opCode == RdbConstants.DataTypes.String)
{
var keyBytes = binaryReader.ReadStr();
var valueBytes = binaryReader.ReadStr();
DataCache.Set(keyBytes.AsString(), valueBytes.AsString(), expiry);
}
}
}
}