garage/src/block/block.rs

116 lines
2.9 KiB
Rust
Raw Permalink Normal View History

use std::path::PathBuf;
2022-07-18 15:18:47 +00:00
use bytes::Bytes;
2022-03-15 11:12:12 +00:00
use serde::{Deserialize, Serialize};
use zstd::stream::{decode_all as zstd_decode, Encoder};
use garage_util::data::*;
use garage_util::error::*;
2022-07-22 16:20:27 +00:00
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub enum DataBlockHeader {
Plain,
Compressed,
}
2022-03-15 11:12:12 +00:00
/// A possibly compressed block of data
pub enum DataBlock {
/// Uncompressed data
2022-07-22 16:20:27 +00:00
Plain(Bytes),
2022-03-15 11:12:12 +00:00
/// Data compressed with zstd
2022-07-22 16:20:27 +00:00
Compressed(Bytes),
2022-03-15 11:12:12 +00:00
}
#[derive(Debug)]
pub enum DataBlockPath {
/// Uncompressed data fail
Plain(PathBuf),
/// Compressed data fail
Compressed(PathBuf),
}
2022-03-15 11:12:12 +00:00
impl DataBlock {
/// Query whether this block is compressed
pub fn is_compressed(&self) -> bool {
matches!(self, DataBlock::Compressed(_))
}
/// Get the inner, possibly compressed buffer. You should probably use [`DataBlock::verify_get`]
/// instead
pub fn inner_buffer(&self) -> &[u8] {
use DataBlock::*;
let (Plain(ref res) | Compressed(ref res)) = self;
res
}
/// Get the buffer, possibly decompressing it, and verify it's integrity.
/// For Plain block, data is compared to hash, for Compressed block, zstd checksumming system
/// is used instead.
2022-07-22 16:20:27 +00:00
pub fn verify_get(self, hash: Hash) -> Result<Bytes, Error> {
2022-03-15 11:12:12 +00:00
match self {
DataBlock::Plain(data) => {
if blake2sum(&data) == hash {
Ok(data)
} else {
Err(Error::CorruptData(hash))
}
}
2022-07-22 16:20:27 +00:00
DataBlock::Compressed(data) => zstd_decode(&data[..])
.map_err(|_| Error::CorruptData(hash))
.map(Bytes::from),
2022-03-15 11:12:12 +00:00
}
}
/// Verify data integrity. Allocate less than [`DataBlock::verify_get`] and don't consume self, but
/// does not return the buffer content.
pub fn verify(&self, hash: Hash) -> Result<(), Error> {
match self {
DataBlock::Plain(data) => {
if blake2sum(data) == hash {
Ok(())
} else {
Err(Error::CorruptData(hash))
}
}
DataBlock::Compressed(data) => zstd::stream::copy_decode(&data[..], std::io::sink())
.map_err(|_| Error::CorruptData(hash)),
}
}
2022-07-18 15:18:47 +00:00
pub async fn from_buffer(data: Bytes, level: Option<i32>) -> DataBlock {
tokio::task::spawn_blocking(move || {
if let Some(level) = level {
if let Ok(data) = zstd_encode(&data[..], level) {
2022-07-22 16:20:27 +00:00
return DataBlock::Compressed(data.into());
2022-07-18 15:18:47 +00:00
}
2022-03-15 11:12:12 +00:00
}
2022-07-22 16:20:27 +00:00
DataBlock::Plain(data)
2022-07-18 15:18:47 +00:00
})
.await
.unwrap()
2022-03-15 11:12:12 +00:00
}
2022-07-22 16:20:27 +00:00
pub fn into_parts(self) -> (DataBlockHeader, Bytes) {
match self {
DataBlock::Plain(data) => (DataBlockHeader::Plain, data),
DataBlock::Compressed(data) => (DataBlockHeader::Compressed, data),
}
}
pub fn from_parts(h: DataBlockHeader, bytes: Bytes) -> Self {
match h {
DataBlockHeader::Plain => DataBlock::Plain(bytes),
DataBlockHeader::Compressed => DataBlock::Compressed(bytes),
}
}
2022-03-15 11:12:12 +00:00
}
fn zstd_encode<R: std::io::Read>(mut source: R, level: i32) -> std::io::Result<Vec<u8>> {
let mut result = Vec::<u8>::new();
let mut encoder = Encoder::new(&mut result, level)?;
encoder.include_checksum(true)?;
std::io::copy(&mut source, &mut encoder)?;
encoder.finish()?;
Ok(result)
}