-
Notifications
You must be signed in to change notification settings - Fork 543
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
Adding basic version of DUMP and RESTORE commands #899
Open
s3w3nofficial
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
s3w3nofficial:s3w3nofficial/add-dump-and-restore-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename redis -> resp, e.g., RespLengthEncodingUtils to clarify that this is related to the RESP protocol. |
||
{ | ||
/// <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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for the custom CRC64 implementation? Have you tried using System.IO.Hashing.Crc64.Hash(data) instead?