Skip to content
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

Unsure to merge: Compress blocks and txs as well #761

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Libplanet.Tests/Store/CompressedDefaultStoreTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit.Abstractions;

namespace Libplanet.Tests.Store
{
public class CompressedDefaultStoreTest : DefaultStoreTest
{
public CompressedDefaultStoreTest(ITestOutputHelper testOutputHelper)
: base(testOutputHelper, compress: true)
{
}
}
}
9 changes: 7 additions & 2 deletions Libplanet.Tests/Store/DefaultStoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Libplanet.Tests.Store
{
public class DefaultStoreFixture : StoreFixture, IDisposable
{
public DefaultStoreFixture(bool memory = false)
public DefaultStoreFixture(bool memory = false, bool compress = false)
{
if (memory)
{
Expand All @@ -20,7 +20,12 @@ public DefaultStoreFixture(bool memory = false)
);
}

Store = new DefaultStore(Path, blockCacheSize: 2, txCacheSize: 2);
Store = new DefaultStore(
Path,
compress: compress,
blockCacheSize: 2,
txCacheSize: 2
);
}

public string Path { get; }
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Store/DefaultStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class DefaultStoreTest : StoreTest, IDisposable
{
private readonly DefaultStoreFixture _fx;

public DefaultStoreTest(ITestOutputHelper testOutputHelper)
public DefaultStoreTest(ITestOutputHelper testOutputHelper, bool compress = false)
{
TestOutputHelper = testOutputHelper;
Fx = _fx = new DefaultStoreFixture();
Fx = _fx = new DefaultStoreFixture(compress: compress);
}

public void Dispose()
Expand Down
72 changes: 65 additions & 7 deletions Libplanet/Store/DefaultStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ public override Transaction<T> GetTransaction<T>(TxId txid)
return null;
}

if (_compress)
{
using (var inputBuffer = new MemoryStream(bytes))
{
bytes = Decompress(inputBuffer);
}
}

Transaction<T> tx = Transaction<T>.Deserialize(bytes);
_txCache.AddOrUpdate(txid, tx);
return tx;
Expand Down Expand Up @@ -819,28 +827,73 @@ internal static string FormatChainId(Guid chainId) =>
return null;
}

RawBlock rawBlock;
IValue value;
try
{
var value = new Codec().Decode(_blocks.ReadAllBytes(path));
if (!(value is Bencodex.Types.Dictionary dict))
byte[] bytes;
if (_compress)
{
throw new DecodingException(
$"Expected {typeof(Bencodex.Types.Dictionary)} but " +
$"{value.GetType()}");
using (var f = _blocks.OpenFile(path, System.IO.FileMode.Open, FileAccess.Read))
{
bytes = Decompress(f);
}
}
else
{
bytes = _blocks.ReadAllBytes(path);
}

rawBlock = new RawBlock(dict);
value = new Codec().Decode(bytes);
}
catch (FileNotFoundException)
{
return null;
}

if (!(value is Bencodex.Types.Dictionary dict))
{
throw new DecodingException(
$"Expected {typeof(Bencodex.Types.Dictionary)} but " +
$"{value.GetType()}");
}

RawBlock rawBlock = new RawBlock(dict);
_blockCache.AddOrUpdate(blockHash, rawBlock);
return rawBlock;
}

private static byte[] CompressBytes(byte[] input)
{
using (var buffer = new MemoryStream())
using (var deflate = new DeflateStream(buffer, CompressionLevel.Fastest, true))
{
deflate.Write(input, 0, input.Length);
deflate.Flush();
buffer.Flush();
int length = (int)buffer.Position;
var output = new byte[length];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use buffer.ToArray() instead of temp variable(output)?

buffer.Seek(0, SeekOrigin.Begin);
buffer.Read(output, 0, output.Length);
return output;
}
}

private static byte[] Decompress(Stream inputBuffer)
{
using (var outputBuffer = new MemoryStream())
using (var deflate = new DeflateStream(inputBuffer, CompressionMode.Decompress))
{
deflate.CopyTo(outputBuffer);
deflate.Flush();
outputBuffer.Flush();
int length = (int)outputBuffer.Position;
var output = new byte[length];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use outputBuffer.ToArray() instead of temp variable(output)?

outputBuffer.Seek(0, SeekOrigin.Begin);
outputBuffer.Read(output, 0, output.Length);
return output;
}
}

private static void CreateDirectoryRecursively(IFileSystem fs, UPath path)
{
if (!fs.DirectoryExists(path))
Expand All @@ -862,6 +915,11 @@ private void WriteContentAddressableFile(IFileSystem fs, UPath path, byte[] cont
return;
}

if (_compress)
{
contents = CompressBytes(contents);
}

// For atomicity, writes bytes into an intermediate temp file,
// and then renames it to the final destination.
UPath tmpPath = dirPath / $".{Guid.NewGuid():N}.tmp";
Expand Down