some refactoring on data read/write path #729

Merged
lx merged 6 commits from refactor-block into main 2024-02-23 15:49:44 +00:00
2 changed files with 5 additions and 22 deletions
Showing only changes of commit 6ee691e65f - Show all commits

View file

@ -62,20 +62,9 @@ impl<T> DataBlockElem<T> {
pub fn as_parts_ref(&self) -> (DataBlockHeader, &T) { pub fn as_parts_ref(&self) -> (DataBlockHeader, &T) {
(self.header, &self.elem) (self.header, &self.elem)
} }
/// Query whether this block is compressed
pub fn is_compressed(&self) -> bool {
self.header.is_compressed()
}
} }
impl DataBlock { impl DataBlock {
/// Get the inner, possibly compressed buffer. You should probably use [`DataBlock::verify_get`]
/// instead
pub fn inner_buffer(&self) -> &[u8] {
&self.elem
}
/// Verify data integrity. Does not return the buffer content. /// Verify data integrity. Does not return the buffer content.
pub fn verify(&self, hash: Hash) -> Result<(), Error> { pub fn verify(&self, hash: Hash) -> Result<(), Error> {
match self.header { match self.header {
@ -97,16 +86,10 @@ impl DataBlock {
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
if let Some(level) = level { if let Some(level) = level {
if let Ok(data_compressed) = zstd_encode(&data[..], level) { if let Ok(data_compressed) = zstd_encode(&data[..], level) {
return DataBlock { return DataBlock::compressed(data_compressed.into());
header: DataBlockHeader::Compressed,
elem: data_compressed.into(),
};
} }
} }
DataBlock { DataBlock::plain(data.into())
header: DataBlockHeader::Plain,
elem: data.into(),
}
}) })
.await .await
.unwrap() .unwrap()

View file

@ -690,8 +690,8 @@ impl BlockManagerLocked {
mgr: &BlockManager, mgr: &BlockManager,
existing_path: Option<DataBlockPath>, existing_path: Option<DataBlockPath>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let compressed = data.is_compressed(); let (header, data) = data.as_parts_ref();
let data = data.inner_buffer(); let compressed = header.is_compressed();
let directory = mgr.data_layout.load().primary_block_dir(hash); let directory = mgr.data_layout.load().primary_block_dir(hash);
@ -805,7 +805,7 @@ impl BlockManagerLocked {
let data = mgr.read_block_from(hash, &wrong_path).await?; let data = mgr.read_block_from(hash, &wrong_path).await?;
self.write_block_inner(hash, &data, mgr, Some(wrong_path)) self.write_block_inner(hash, &data, mgr, Some(wrong_path))
.await?; .await?;
Ok(data.inner_buffer().len()) Ok(data.as_parts_ref().1.len())
} }
} }