-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic version of DUMP and RESTORE redis commands
- Loading branch information
1 parent
47ac84f
commit 2954d6e
Showing
16 changed files
with
787 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Garnet.common; | ||
|
||
/// <summary> | ||
/// Port of redis crc64 from https://github.com/redis/redis/blob/7.2/src/crc64.c | ||
/// </summary> | ||
public static class Crc64 | ||
{ | ||
/// <summary> | ||
/// Polynomial (same as redis) | ||
/// </summary> | ||
private const ulong POLY = 0xad93d23594c935a9UL; | ||
|
||
/// <summary> | ||
/// Reverse all bits in a 64-bit value (bit reflection). | ||
/// Only used for data_len == 64 in this code. | ||
/// </summary> | ||
private static ulong Reflect64(ulong data) | ||
{ | ||
// swap odd/even bits | ||
data = ((data >> 1) & 0x5555555555555555UL) | ((data & 0x5555555555555555UL) << 1); | ||
// swap consecutive pairs | ||
data = ((data >> 2) & 0x3333333333333333UL) | ((data & 0x3333333333333333UL) << 2); | ||
// swap nibbles | ||
data = ((data >> 4) & 0x0F0F0F0F0F0F0F0FUL) | ((data & 0x0F0F0F0F0F0F0F0FUL) << 4); | ||
// swap bytes, then 2-byte pairs, then 4-byte pairs | ||
data = System.Buffers.Binary.BinaryPrimitives.ReverseEndianness(data); | ||
return data; | ||
} | ||
|
||
/// <summary> | ||
/// A direct bit-by-bit CRC64 calculation (like _crc64 in C). | ||
/// </summary> | ||
private static ulong Crc64Bitwise(ReadOnlySpan<byte> data) | ||
{ | ||
ulong crc = 0; | ||
|
||
foreach (var c in data) | ||
{ | ||
for (byte i = 1; i != 0; i <<= 1) | ||
{ | ||
// interpret the top bit of 'crc' and current bit of 'c' | ||
var bitSet = (crc & 0x8000000000000000UL) != 0; | ||
var cbit = (c & i) != 0; | ||
|
||
// if cbit flips the sense, invert bitSet | ||
if (cbit) | ||
bitSet = !bitSet; | ||
|
||
// shift | ||
crc <<= 1; | ||
|
||
// apply polynomial if needed | ||
if (bitSet) | ||
crc ^= POLY; | ||
} | ||
|
||
// ensure it stays in 64 bits | ||
crc &= 0xffffffffffffffffUL; | ||
} | ||
|
||
// reflect and XOR, per standard | ||
crc &= 0xffffffffffffffffUL; | ||
crc = Reflect64(crc) ^ 0x0000000000000000UL; | ||
return crc; | ||
} | ||
|
||
/// <summary> | ||
/// Computes crc64 | ||
/// </summary> | ||
/// <param name="data"></param> | ||
/// <returns></returns> | ||
public static byte[] Hash(ReadOnlySpan<byte> data) | ||
{ | ||
var bitwiseCrc = Crc64Bitwise(data); | ||
return BitConverter.GetBytes(bitwiseCrc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Garnet.common; | ||
|
||
/// <summary> | ||
/// Utils for working with redis length encoding | ||
/// </summary> | ||
public static class RedisLengthEncodingUtils | ||
{ | ||
/// <summary> | ||
/// Decodes the redis length encoded length and returns payload start | ||
/// </summary> | ||
/// <param name="buff"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentException"></exception> | ||
public static (long length, byte payloadStart) DecodeLength(ref ReadOnlySpan<byte> buff) | ||
{ | ||
// remove the value type byte | ||
var encoded = buff.Slice(1); | ||
|
||
if (encoded.Length == 0) | ||
throw new ArgumentException("Encoded length cannot be empty.", nameof(encoded)); | ||
|
||
var firstByte = encoded[0]; | ||
return (firstByte >> 6) switch | ||
{ | ||
// 6-bit encoding | ||
0 => (firstByte & 0x3F, 1), | ||
// 14-bit encoding | ||
1 when encoded.Length < 2 => throw new ArgumentException("Not enough bytes for 14-bit encoding."), | ||
1 => (((firstByte & 0x3F) << 8) | encoded[1], 2), | ||
// 32-bit encoding | ||
2 when encoded.Length < 5 => throw new ArgumentException("Not enough bytes for 32-bit encoding."), | ||
2 => ((long)((encoded[1] << 24) | (encoded[2] << 16) | (encoded[3] << 8) | encoded[4]), 5), | ||
_ => throw new ArgumentException("Invalid encoding type.", nameof(encoded)) | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Encoded payload length to redis encoded payload length | ||
/// </summary> | ||
/// <param name="length"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public static byte[] EncodeLength(long length) | ||
{ | ||
switch (length) | ||
{ | ||
// 6-bit encoding (length ≤ 63) | ||
case < 1 << 6: | ||
return [(byte)(length & 0x3F)]; // 00xxxxxx | ||
// 14-bit encoding (64 ≤ length ≤ 16,383) | ||
case < 1 << 14: | ||
{ | ||
var firstByte = (byte)(((length >> 8) & 0x3F) | (1 << 6)); // 01xxxxxx | ||
var secondByte = (byte)(length & 0xFF); | ||
return [firstByte, secondByte]; | ||
} | ||
// 32-bit encoding (length ≤ 4,294,967,295) | ||
case <= 0xFFFFFFFF: | ||
{ | ||
var firstByte = (byte)(2 << 6); // 10xxxxxx | ||
var lengthBytes = BitConverter.GetBytes((uint)length); // Ensure unsigned | ||
if (BitConverter.IsLittleEndian) | ||
{ | ||
Array.Reverse(lengthBytes); // Convert to big-endian | ||
} | ||
return new[] { firstByte }.Concat(lengthBytes).ToArray(); | ||
} | ||
default: | ||
throw new ArgumentOutOfRangeException("Length exceeds maximum allowed for Redis encoding (4,294,967,295)."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.