-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ZLib compression using System.IO.Compression (#238)
* Add ZLib compression using System.IO.Compression * Update benchmark with builtin zlib compression * Add test for BuiltinZlibCompression * Add Apache header
- Loading branch information
Showing
7 changed files
with
144 additions
and
1 deletion.
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
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
77 changes: 77 additions & 0 deletions
77
src/DotPulsar/Internal/Compression/BuiltinZlibCompression.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,77 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace DotPulsar.Internal.Compression; | ||
|
||
using DotPulsar.Internal.Abstractions; | ||
using System.Buffers; | ||
using System.Reflection; | ||
|
||
public static class BuiltinZlibCompression | ||
{ | ||
public static bool TryLoading(out ICompressorFactory? compressorFactory, out IDecompressorFactory? decompressorFactory) | ||
{ | ||
try | ||
{ | ||
var assembly = Assembly.Load("System.IO.Compression"); | ||
var types = assembly.GetTypes(); | ||
|
||
// Find the ZLibStream | ||
var zlibStreamType = types.FirstOrDefault(t => t.FullName == "System.IO.Compression.ZLibStream"); | ||
|
||
// Find the enum values for the ZLibStream constructors | ||
var compressionLevelType = types.FirstOrDefault(t => t.FullName == "System.IO.Compression.CompressionLevel"); | ||
var compressionLevelOptimal = compressionLevelType?.GetEnumValues().GetValue(0); | ||
var compressionModeType = types.FirstOrDefault(t => t.FullName == "System.IO.Compression.CompressionMode"); | ||
var compressionModeDecompress = compressionModeType?.GetEnumValues().GetValue(0); | ||
|
||
compressorFactory = new CompressorFactory(PulsarApi.CompressionType.Zlib, () => new Compressor(CreateCompressor(zlibStreamType, compressionLevelOptimal))); | ||
decompressorFactory = new DecompressorFactory(PulsarApi.CompressionType.Zlib, () => new Decompressor(CreateDecompressor(zlibStreamType, compressionModeDecompress))); | ||
|
||
return CompressionTester.TestCompression(compressorFactory, decompressorFactory); | ||
} | ||
catch | ||
{ | ||
// Ignore | ||
} | ||
|
||
compressorFactory = null; | ||
decompressorFactory = null; | ||
|
||
return false; | ||
} | ||
|
||
private static Func<ReadOnlySequence<byte>, ReadOnlySequence<byte>> CreateCompressor(Type? zlibStreamType, object? compressionLevelOptimal) | ||
=> data => | ||
{ | ||
using var dataStream = new MemoryStream(data.ToArray()); | ||
using var compressedStream = new MemoryStream(); | ||
using var compressor = (Stream) Activator.CreateInstance(zlibStreamType!, new object[] { compressedStream, compressionLevelOptimal! })!; | ||
dataStream.CopyTo(compressor); | ||
compressor.Close(); | ||
return new ReadOnlySequence<byte>(compressedStream.ToArray()); | ||
}; | ||
|
||
private static Func<ReadOnlySequence<byte>, int, ReadOnlySequence<byte>> CreateDecompressor(Type? zlibStreamType, object? compressionModeDecompress) | ||
=> (data, _) => | ||
{ | ||
using var dataStream = new MemoryStream(data.ToArray()); | ||
using var decompressedStream = new MemoryStream(); | ||
using var decompressor = (Stream) Activator.CreateInstance(zlibStreamType!, new object[] { dataStream, compressionModeDecompress! })!; | ||
decompressor.CopyTo(decompressedStream); | ||
decompressor.Close(); | ||
decompressedStream.Position = 0; | ||
return new ReadOnlySequence<byte>(decompressedStream.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
37 changes: 37 additions & 0 deletions
37
tests/DotPulsar.Tests/Internal/Compression/BuiltinZlibCompressionTests.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,37 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace DotPulsar.Tests.Internal.Compression; | ||
|
||
using DotPulsar.Internal.Compression; | ||
|
||
[Trait("Category", "Unit")] | ||
public class BuiltinZlibCompressionTests | ||
{ | ||
[Fact] | ||
public void Compression_GivenDataToCompressAndDecompress_ShouldReturnOriginalData() | ||
{ | ||
// Arrange | ||
var couldLoad = BuiltinZlibCompression.TryLoading(out var compressorFactory, out var decompressorFactory); | ||
couldLoad.Should().BeTrue(); | ||
using var compressor = compressorFactory!.Create(); | ||
using var decompressor = decompressorFactory!.Create(); | ||
|
||
// Act | ||
var compressionWorks = CompressionTester.TestCompression(compressorFactory, decompressorFactory); | ||
|
||
// Assert | ||
compressionWorks.Should().BeTrue(); | ||
} | ||
} |