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

Flat file blocks #6976

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Flat file blocks #6976

wants to merge 4 commits into from

Conversation

benaadams
Copy link
Member

@benaadams benaadams commented May 3, 2024

Changes

  • Save receipts and blocks as flatfiles
image

Types of changes

What types of changes does your code introduce?

  • Optimization

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Notes on testing

Optional. Remove if not applicable.

Documentation

Requires documentation update

  • Yes
  • No

If yes, link the PR to the docs update or the issue with the details labeled docs. Remove if not applicable.

Requires explanation in Release Notes

  • Yes
  • No

If yes, fill in the details here. Remove if not applicable.

Remarks

Optional. Remove if not applicable.

private void SaveToFile(Block block)
{
(string directory, string filename, int lockId) = GetBlockDirectoryAndPath(block.Number, _basePath);
RecyclableMemoryStream output = RecyclableStream.GetStream(filename);
Copy link
Member

Choose a reason for hiding this comment

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

also using?

Directory.CreateDirectory(directory);
}

FileStream file = new(path, mode: FileMode.OpenOrCreate, access: FileAccess.Write, share: FileShare.Read);
Copy link
Member

Choose a reason for hiding this comment

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

you could FIleShare ReadWrite if you read non-locked block numbers?

RecyclableRlpStream rlpStream;
if (item is null)
{
rlpStream = new RecyclableRlpStream(tag);
Copy link
Member

Choose a reason for hiding this comment

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

this can be moved outside if?

Comment on lines +266 to +299
path = Path.Combine(directory, filename);
}
if (path is not null && File.Exists(path))
{
using FileStream file = new(path, mode: FileMode.Open, access: FileAccess.Read, share: FileShare.ReadWrite);
var fileHandle = file.SafeFileHandle;
long headerOffset = (blockNumber / FileSplit) % BlocksPerFile;

Vector128<long> headerEntry = default;
Span<byte> headerData = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref headerEntry, 1));
long read = RandomAccess.Read(fileHandle, headerData, Vector128<byte>.Count * headerOffset);
if (read == Vector128<byte>.Count && headerEntry != default)
{
long offset = headerEntry.GetElement(0);
int length = (int)headerEntry.GetElement(1);

var array = ArrayPool<byte>.Shared.Rent(length);
try
{
var input = array.AsSpan(0, length);
read = RandomAccess.Read(fileHandle, input, offset);
if (read == length)
{
using RecyclableMemoryStream outputStream = RecyclableStream.GetStream(filename);
var compressed = new MemoryStream(array, 0, length, writable: false);
using (SnappyStream decompressor = new(compressed, CompressionMode.Decompress))
{
decompressor.CopyTo(outputStream);
}

ArrayPool<byte>.Shared.Return(array);
array = null;

outputStream.Position = 0;
Copy link
Member

Choose a reason for hiding this comment

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

code duplication?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants