-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grain storage): added compression modules (#20)
Co-authored-by: Derek Grech <derek.grech@river.tech>
- Loading branch information
Showing
18 changed files
with
409 additions
and
36 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
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,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Orleans.Persistence.Redis\Orleans.Persistence.Redis.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,22 @@ | ||
using Orleans.Persistence.Redis.Compression; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace DeflateCompressionTest | ||
{ | ||
public class DeflateCompressionUnitTest | ||
{ | ||
[Fact] | ||
public void DeflateCompressionTest1() | ||
{ | ||
const string test = "hello world"; | ||
var deflateCompression = new DeflateCompression(); | ||
|
||
var compressed = deflateCompression.Compress(Encoding.UTF8.GetBytes(test)); | ||
var decompressed = deflateCompression.Decompress(compressed); | ||
|
||
Assert.NotNull(decompressed); | ||
Assert.Equal(test, Encoding.UTF8.GetString(decompressed)); | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
Orleans.Persistence.Redis/Compression/BrotliCompression.cs
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,31 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
|
||
namespace Orleans.Persistence.Redis.Compression | ||
{ | ||
public class BrotliCompression : ICompression | ||
{ | ||
public byte[] Decompress(byte[] bytes) | ||
{ | ||
using var memoryStream = new MemoryStream(bytes); | ||
using var outputStream = new MemoryStream(); | ||
using (var decompressStream = new Brotli.BrotliStream(memoryStream, CompressionMode.Decompress)) | ||
{ | ||
decompressStream.CopyTo(outputStream); | ||
} | ||
|
||
return outputStream.ToArray(); | ||
} | ||
|
||
public byte[] Compress(byte[] bytes) | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
using (var brotliStream = new Brotli.BrotliStream(memoryStream, CompressionMode.Compress)) | ||
{ | ||
brotliStream.Write(bytes, 0, bytes.Length); | ||
} | ||
|
||
return memoryStream.ToArray(); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
Orleans.Persistence.Redis/Compression/DeflateCompression.cs
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,75 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
|
||
namespace Orleans.Persistence.Redis.Compression | ||
{ | ||
public class DeflateCompression : ICompression | ||
{ | ||
public byte[] Decompress(byte[] buffer) | ||
{ | ||
var tempBuffer = new byte[buffer.Length - 6]; // 2 header + 4 checksum | ||
if (buffer[0] != 0x78 && buffer[0] != 0x9c) | ||
return null; | ||
|
||
Array.Copy(buffer, 2, tempBuffer, 0, tempBuffer.Length); | ||
|
||
using var memoryStream = new MemoryStream(tempBuffer); | ||
using var outputStream = new MemoryStream(); | ||
using (var decompressStream = new DeflateStream(memoryStream, CompressionMode.Decompress)) | ||
{ | ||
decompressStream.CopyTo(outputStream); | ||
} | ||
|
||
var decompressed = outputStream.ToArray(); | ||
|
||
var checksum = CalculateChecksum(decompressed); | ||
|
||
return checksum.Where((t, i) => t != buffer[buffer.Length - 4 + i]).Any() | ||
? null | ||
: decompressed; | ||
} | ||
|
||
public byte[] Compress(byte[] buffer) | ||
{ | ||
byte[] tempData; | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
// header | ||
memoryStream.WriteByte(0x78); | ||
memoryStream.WriteByte(0x9C); | ||
using (var compressStream = new DeflateStream(memoryStream, CompressionMode.Compress)) | ||
compressStream.Write(buffer, 0, buffer.Length); | ||
|
||
tempData = memoryStream.ToArray(); | ||
} | ||
|
||
var n = tempData.Length; | ||
Array.Resize(ref tempData, n + 4); | ||
var checksum = CalculateChecksum(buffer); | ||
Array.Copy(checksum, 0, tempData, n, checksum.Length); | ||
return tempData; | ||
} | ||
|
||
private byte[] CalculateChecksum(byte[] buffer) | ||
{ | ||
var a1 = 1; | ||
var a2 = 0; | ||
var checksum = new byte[4]; | ||
|
||
foreach (var b in buffer) | ||
{ | ||
a1 = (a1 + b) % 65521; | ||
a2 = (a2 + a1) % 65521; | ||
} | ||
|
||
checksum[0] = (byte)(a2 >> 8); | ||
checksum[1] = (byte)a2; | ||
checksum[2] = (byte)(a1 >> 8); | ||
checksum[3] = (byte)a1; | ||
|
||
return checksum; | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
|
||
namespace Orleans.Persistence.Redis.Compression | ||
{ | ||
public class GZipCompression : ICompression | ||
{ | ||
public byte[] Decompress(byte[] buffer) | ||
{ | ||
using var memoryStream = new MemoryStream(buffer); | ||
using var outputStream = new MemoryStream(); | ||
using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress)) | ||
{ | ||
decompressStream.CopyTo(outputStream); | ||
} | ||
|
||
return outputStream.ToArray(); | ||
} | ||
|
||
public byte[] Compress(byte[] buffer) | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
using (var compressStream = new GZipStream(memoryStream, CompressionMode.Compress)) | ||
{ | ||
compressStream.Write(buffer, 0, buffer.Length); | ||
} | ||
|
||
return memoryStream.ToArray(); | ||
} | ||
} | ||
} |
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,8 @@ | ||
namespace Orleans.Persistence.Redis.Compression | ||
{ | ||
public interface ICompression | ||
{ | ||
byte[] Decompress(byte[] buffer); | ||
byte[] Compress(byte[] buffer); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Orleans.Persistence.Redis/Compression/RawDeflateCompression.cs
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,31 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
|
||
namespace Orleans.Persistence.Redis.Compression | ||
{ | ||
public class RawDeflateCompression : ICompression | ||
{ | ||
public byte[] Decompress(byte[] bytes) | ||
{ | ||
using var memoryStream = new MemoryStream(bytes); | ||
using var outputStream = new MemoryStream(); | ||
using (var decompressStream = new DeflateStream(memoryStream, CompressionMode.Decompress)) | ||
{ | ||
decompressStream.CopyTo(outputStream); | ||
} | ||
|
||
return outputStream.ToArray(); | ||
} | ||
|
||
public byte[] Compress(byte[] bytes) | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
using (var compressStream = new DeflateStream(memoryStream, CompressionMode.Compress)) | ||
{ | ||
compressStream.Write(bytes, 0, bytes.Length); | ||
} | ||
|
||
return memoryStream.ToArray(); | ||
} | ||
} | ||
} |
Oops, something went wrong.