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

feat: add lz4 to SnapshotSegment::Headers #6487

Merged
merged 4 commits into from
Feb 14, 2024
Merged
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
5 changes: 5 additions & 0 deletions crates/primitives/src/snapshot/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ impl SnapshotSegment {

Some((segment, SegmentRangeInclusive::new(block_start, block_end)))
}

/// Returns `true` if the segment is `SnapshotSegment::Headers`.
pub fn is_headers(&self) -> bool {
matches!(self, SnapshotSegment::Headers)
}
}

/// A segment header that contains information common to all segments. Used for storage.
Expand Down
52 changes: 34 additions & 18 deletions crates/storage/provider/src/providers/snapshot/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use reth_primitives::{
BlockHash, BlockNumber, Header, Receipt, SnapshotSegment, TransactionSignedNoHash, TxNumber,
U256,
};
use std::{ops::Deref, path::PathBuf, sync::Arc};
use std::{
ops::Deref,
path::{Path, PathBuf},
sync::Arc,
};

/// Mutable reference to a dashmap element of [`SnapshotProviderRW`].
pub type SnapshotProviderRWRefMut<'a> = RefMut<'a, SnapshotSegment, SnapshotProviderRW<'static>>;
Expand Down Expand Up @@ -51,21 +55,8 @@ impl<'a> SnapshotProviderRW<'a> {
.get_highest_snapshot_tx(segment)
.map(|tx| SegmentRangeInclusive::new(tx, tx));
let path = reader.directory().join(segment.filename(&block_range));
(
NippyJar::new(
segment.columns(),
&path,
SegmentHeader::new(
SegmentRangeInclusive::new(
block_range.start(),
block_range.start(),
),
tx_range,
segment,
),
),
path,
)

(create_jar(segment, &path, block_range, tx_range), path)
}
Err(err) => return Err(err),
};
Expand Down Expand Up @@ -95,7 +86,7 @@ impl<'a> SnapshotProviderRW<'a> {
let user_header = self.writer.user_header();
let mut max_block = Some(user_header.block_end());

if matches!(self.writer.user_header().segment(), SnapshotSegment::Headers) {
if self.writer.user_header().segment().is_headers() {
// This can be a scenario where we pruned all blocks from the static file, including the
// genesis block.
if user_header.block_end() == 0 && self.writer.rows() == 0 {
Expand All @@ -113,7 +104,7 @@ impl<'a> SnapshotProviderRW<'a> {
pub fn increment_block(&mut self, segment: SnapshotSegment) -> ProviderResult<BlockNumber> {
debug_assert!(
(self.writer.rows() as u64 + self.writer.user_header().block_end()) != 0
|| !matches!(segment, SnapshotSegment::Headers),
|| !segment.is_headers(),
"This function should only be called by append_header when dealing with SnapshotSegment::Headers.");

let last_block = self.writer.user_header().block_end();
Expand Down Expand Up @@ -348,3 +339,28 @@ impl<'a> Deref for SnapshotProviderRW<'a> {
&self.reader
}
}

fn create_jar(
segment: SnapshotSegment,
path: &Path,
block_range: SegmentRangeInclusive,
tx_range: Option<SegmentRangeInclusive>,
) -> NippyJar<SegmentHeader> {
let mut jar = NippyJar::new(
segment.columns(),
path,
SegmentHeader::new(
SegmentRangeInclusive::new(block_range.start(), block_range.start()),
tx_range,
segment,
),
);

// Transaction and Receipt already have the compression scheme used natively in its encoding.
// (zstd-dictionary)
if segment.is_headers() {
jar = jar.with_lz4();
}

jar
}
Loading