use ZST error type for snappy, use new snappy compression methods
This commit is contained in:
parent
53db9921d6
commit
0e917b9fa3
@ -229,7 +229,7 @@ pub enum Error {
|
|||||||
/// Io error.
|
/// Io error.
|
||||||
Io(::std::io::Error),
|
Io(::std::io::Error),
|
||||||
/// Snappy error.
|
/// Snappy error.
|
||||||
Snappy(::util::snappy::Error),
|
Snappy(::util::snappy::InvalidInput),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
@ -321,8 +321,8 @@ impl From<::std::io::Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<::util::snappy::Error> for Error {
|
impl From<::util::snappy::InvalidInput> for Error {
|
||||||
fn from(err: ::util::snappy::Error) -> Error {
|
fn from(err: ::util::snappy::InvalidInput) -> Error {
|
||||||
Error::Snappy(err)
|
Error::Snappy(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,41 +40,10 @@ mod block;
|
|||||||
// Try to have chunks be around 16MB (before compression)
|
// Try to have chunks be around 16MB (before compression)
|
||||||
const PREFERRED_CHUNK_SIZE: usize = 16 * 1024 * 1024;
|
const PREFERRED_CHUNK_SIZE: usize = 16 * 1024 * 1024;
|
||||||
|
|
||||||
// compresses the data into the buffer, resizing if necessary.
|
|
||||||
fn compression_helper(input: &[u8], output: &mut Vec<u8>) -> usize {
|
|
||||||
let max_size = snappy::max_compressed_len(input.len());
|
|
||||||
let buf_len = output.len();
|
|
||||||
|
|
||||||
// resize if necessary, but in reality this will probably almost never happen.
|
|
||||||
if max_size > buf_len {
|
|
||||||
output.resize(max_size, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
match snappy::compress_into(&input, output) {
|
|
||||||
Ok(size) => size,
|
|
||||||
Err(snappy::Error::BufferTooSmall) => panic!("buffer too small although capacity ensured?"),
|
|
||||||
Err(snappy::Error::InvalidInput) => panic!("invalid input error impossible in snappy_compress"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// decompresses the data into the buffer, resizing if necessary.
|
|
||||||
// may return invalid input error.
|
|
||||||
fn decompression_helper(input: &[u8], output: &mut Vec<u8>) -> Result<usize, snappy::Error> {
|
|
||||||
let size = try!(snappy::decompressed_len(input));
|
|
||||||
let buf_len = output.len();
|
|
||||||
|
|
||||||
// resize if necessary, slow path.
|
|
||||||
if size > buf_len {
|
|
||||||
output.resize(size, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
snappy::decompress_into(&input, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// shared portion of write_chunk
|
// shared portion of write_chunk
|
||||||
// returns either a (hash, compressed_size) pair or an io error.
|
// returns either a (hash, compressed_size) pair or an io error.
|
||||||
fn write_chunk(raw_data: &[u8], compression_buffer: &mut Vec<u8>, path: &Path) -> Result<(H256, usize), Error> {
|
fn write_chunk(raw_data: &[u8], compression_buffer: &mut Vec<u8>, path: &Path) -> Result<(H256, usize), Error> {
|
||||||
let compressed_size = compression_helper(raw_data, compression_buffer);
|
let compressed_size = snappy::compress_into(raw_data, compression_buffer);
|
||||||
let compressed = &compression_buffer[..compressed_size];
|
let compressed = &compression_buffer[..compressed_size];
|
||||||
let hash = compressed.sha3();
|
let hash = compressed.sha3();
|
||||||
|
|
||||||
@ -320,7 +289,7 @@ impl<'a> StateRebuilder<'a> {
|
|||||||
|
|
||||||
/// Feed a compressed state chunk into the rebuilder.
|
/// Feed a compressed state chunk into the rebuilder.
|
||||||
pub fn feed(&mut self, compressed: &[u8]) -> Result<(), Error> {
|
pub fn feed(&mut self, compressed: &[u8]) -> Result<(), Error> {
|
||||||
let len = try!(decompression_helper(compressed, &mut self.snappy_buffer));
|
let len = try!(snappy::decompress_into(compressed, &mut self.snappy_buffer));
|
||||||
let rlp = UntrustedRlp::new(&self.snappy_buffer[..len]);
|
let rlp = UntrustedRlp::new(&self.snappy_buffer[..len]);
|
||||||
|
|
||||||
for account_pair in rlp.iter() {
|
for account_pair in rlp.iter() {
|
||||||
|
@ -66,7 +66,7 @@ pub enum UtilError {
|
|||||||
/// Error from a bad input size being given for the needed output.
|
/// Error from a bad input size being given for the needed output.
|
||||||
BadSize,
|
BadSize,
|
||||||
/// Error from snappy.
|
/// Error from snappy.
|
||||||
Snappy(::snappy::Error),
|
Snappy(::snappy::InvalidInput),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for UtilError {
|
impl fmt::Display for UtilError {
|
||||||
@ -182,8 +182,8 @@ impl From<String> for UtilError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<::snappy::Error> for UtilError {
|
impl From<::snappy::InvalidInput> for UtilError {
|
||||||
fn from(err: ::snappy::Error) -> UtilError {
|
fn from(err: ::snappy::InvalidInput) -> UtilError {
|
||||||
UtilError::Snappy(err)
|
UtilError::Snappy(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,23 +53,13 @@ extern {
|
|||||||
) -> c_int;
|
) -> c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Errors that can occur during usage of snappy.
|
/// Attempted to decompress an uncompressed buffer.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub struct InvalidInput;
|
||||||
/// An invalid input was supplied. Usually means that you tried to decompress an uncompressed
|
|
||||||
/// buffer.
|
|
||||||
InvalidInput,
|
|
||||||
/// The output buffer supplied was too small. Make sure to provide buffers large enough to hold
|
|
||||||
/// all the output data.
|
|
||||||
BufferTooSmall,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for InvalidInput {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
write!(f, "Attempted snappy decompression with invalid input")
|
||||||
Error::InvalidInput => write!(f, "Snappy error (invalid input)"),
|
|
||||||
Error::BufferTooSmall => write!(f, "Snappy error (buffer too small)"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,30 +69,30 @@ pub fn max_compressed_len(len: usize) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// How large the given data will be when decompressed.
|
/// How large the given data will be when decompressed.
|
||||||
pub fn decompressed_len(compressed: &[u8]) -> Result<usize, Error> {
|
pub fn decompressed_len(compressed: &[u8]) -> Result<usize, InvalidInput> {
|
||||||
let mut size: size_t = 0;
|
let mut size: size_t = 0;
|
||||||
let len = compressed.len() as size_t;
|
let len = compressed.len() as size_t;
|
||||||
|
|
||||||
let status = unsafe { snappy_uncompressed_length(compressed.as_ptr() as *const c_char, len, &mut size) };
|
let status = unsafe { snappy_uncompressed_length(compressed.as_ptr() as *const c_char, len, &mut size) };
|
||||||
|
|
||||||
if status == SNAPPY_INVALID_INPUT {
|
if status == SNAPPY_INVALID_INPUT {
|
||||||
Err(Error::InvalidInput)
|
Err(InvalidInput)
|
||||||
} else {
|
} else {
|
||||||
Ok(len)
|
Ok(size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compress a buffer using snappy.
|
/// Compress a buffer using snappy.
|
||||||
pub fn compress(input: &[u8]) -> Vec<u8> {
|
pub fn compress(input: &[u8]) -> Vec<u8> {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
compress_into(input, &mut buf).expect("snappy compression failed");
|
compress_into(input, &mut buf);
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compress a buffer using snappy, writing the result into
|
/// Compress a buffer using snappy, writing the result into
|
||||||
/// the given output buffer, growing it if necessary.
|
/// the given output buffer, growing it if necessary.
|
||||||
/// Otherwise, returns the length of the compressed data.
|
/// Otherwise, returns the length of the compressed data.
|
||||||
pub fn compress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, Error> {
|
pub fn compress_into(input: &[u8], output: &mut Vec<u8>) -> usize {
|
||||||
let mut len = max_compressed_len(input.len());
|
let mut len = max_compressed_len(input.len());
|
||||||
|
|
||||||
if output.len() < len {
|
if output.len() < len {
|
||||||
@ -119,15 +109,15 @@ pub fn compress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, Error>
|
|||||||
};
|
};
|
||||||
|
|
||||||
match status {
|
match status {
|
||||||
SNAPPY_OK => Ok(len),
|
SNAPPY_OK => len,
|
||||||
SNAPPY_INVALID_INPUT => Err(Error::InvalidInput), // should never happen, but can't hurt!
|
SNAPPY_INVALID_INPUT => panic!("snappy compression has no concept of invalid input"),
|
||||||
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, the capacity was just ensured."),
|
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, the capacity was just ensured."),
|
||||||
_ => panic!("snappy returned unspecified status"),
|
_ => panic!("snappy returned unspecified status"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decompress a buffer using snappy. Will return an error if the buffer is not snappy-compressed.
|
/// Decompress a buffer using snappy. Will return an error if the buffer is not snappy-compressed.
|
||||||
pub fn decompress(input: &[u8]) -> Result<Vec<u8>, Error> {
|
pub fn decompress(input: &[u8]) -> Result<Vec<u8>, InvalidInput> {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
decompress_into(input, &mut v).map(|_| v)
|
decompress_into(input, &mut v).map(|_| v)
|
||||||
}
|
}
|
||||||
@ -136,7 +126,7 @@ pub fn decompress(input: &[u8]) -> Result<Vec<u8>, Error> {
|
|||||||
/// the given output buffer, growing it if necessary.
|
/// the given output buffer, growing it if necessary.
|
||||||
/// Will error if the input buffer is not snappy-compressed.
|
/// Will error if the input buffer is not snappy-compressed.
|
||||||
/// Otherwise, returns the length of the decompressed data.
|
/// Otherwise, returns the length of the decompressed data.
|
||||||
pub fn decompress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, Error> {
|
pub fn decompress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, InvalidInput> {
|
||||||
let mut len = try!(decompressed_len(input));
|
let mut len = try!(decompressed_len(input));
|
||||||
|
|
||||||
if output.len() < len {
|
if output.len() < len {
|
||||||
@ -154,7 +144,7 @@ pub fn decompress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, Erro
|
|||||||
|
|
||||||
match status {
|
match status {
|
||||||
SNAPPY_OK => Ok(len as usize),
|
SNAPPY_OK => Ok(len as usize),
|
||||||
SNAPPY_INVALID_INPUT => Err(Error::InvalidInput),
|
SNAPPY_INVALID_INPUT => Err(InvalidInput),
|
||||||
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, size was just set to large enough."),
|
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, size was just set to large enough."),
|
||||||
_ => panic!("snappy returned unspecified status"),
|
_ => panic!("snappy returned unspecified status"),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user