Skip to content

Commit

Permalink
Refactor out metadata Byte copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefffrey committed May 12, 2024
1 parent d86f99b commit 4786e40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/reader/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ enum CompressionHeader {
/// compressed or not.
fn decode_header(bytes: [u8; 3]) -> CompressionHeader {
let bytes = [bytes[0], bytes[1], bytes[2], 0];
let length = u32::from_le_bytes(bytes);
let is_original = length & 1 == 1;
// to clear the is_original bit
let length = length >> 1;
let length_and_flag = u32::from_le_bytes(bytes);
let is_original = length_and_flag & 1 == 1;
let length = length_and_flag >> 1;
if is_original {
CompressionHeader::Original(length)
} else {
Expand Down
18 changes: 8 additions & 10 deletions src/reader/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ pub fn read_metadata<R: ChunkReader>(reader: &mut R) -> Result<FileMetadata> {
};

let footer = deserialize_footer(
&tail_bytes[tail_bytes.len() - footer_length as usize..],
tail_bytes.slice(tail_bytes.len() - footer_length as usize..),
compression,
)?;
tail_bytes.truncate(tail_bytes.len() - footer_length as usize);

let metadata = deserialize_footer_metadata(
&tail_bytes[tail_bytes.len() - metadata_length as usize..],
tail_bytes.slice(tail_bytes.len() - metadata_length as usize..),
compression,
)?;

Expand Down Expand Up @@ -263,32 +263,30 @@ pub async fn read_metadata_async<R: super::AsyncChunkReader>(
};

let footer = deserialize_footer(
&tail_bytes[tail_bytes.len() - footer_length as usize..],
tail_bytes.slice(tail_bytes.len() - footer_length as usize..),
compression,
)?;
tail_bytes.truncate(tail_bytes.len() - footer_length as usize);

let metadata = deserialize_footer_metadata(
&tail_bytes[tail_bytes.len() - metadata_length as usize..],
tail_bytes.slice(tail_bytes.len() - metadata_length as usize..),
compression,
)?;

FileMetadata::from_proto(&postscript, &footer, &metadata)
}

fn deserialize_footer(bytes: &[u8], compression: Option<Compression>) -> Result<Footer> {
fn deserialize_footer(bytes: Bytes, compression: Option<Compression>) -> Result<Footer> {
let mut buffer = vec![];
// TODO: refactor to not need Bytes::copy_from_slice
Decompressor::new(Bytes::copy_from_slice(bytes), compression, vec![])
Decompressor::new(bytes, compression, vec![])
.read_to_end(&mut buffer)
.context(error::IoSnafu)?;
Footer::decode(buffer.as_slice()).context(error::DecodeProtoSnafu)
}

fn deserialize_footer_metadata(bytes: &[u8], compression: Option<Compression>) -> Result<Metadata> {
fn deserialize_footer_metadata(bytes: Bytes, compression: Option<Compression>) -> Result<Metadata> {
let mut buffer = vec![];
// TODO: refactor to not need Bytes::copy_from_slice
Decompressor::new(Bytes::copy_from_slice(bytes), compression, vec![])
Decompressor::new(bytes, compression, vec![])
.read_to_end(&mut buffer)
.context(error::IoSnafu)?;
Metadata::decode(buffer.as_slice()).context(error::DecodeProtoSnafu)
Expand Down

0 comments on commit 4786e40

Please sign in to comment.