2023-09-05 12:27:39 +00:00
|
|
|
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};
|
2024-02-23 10:47:44 +00:00
|
|
|
use zstd::stream::Encoder;
|
2022-03-15 11:12:12 +00:00
|
|
|
|
|
|
|
use garage_util::data::*;
|
|
|
|
use garage_util::error::*;
|
|
|
|
|
2024-02-23 11:22:29 +00:00
|
|
|
use garage_net::stream::ByteStream;
|
|
|
|
|
2022-07-22 16:20:27 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
|
|
|
|
pub enum DataBlockHeader {
|
|
|
|
Plain,
|
|
|
|
Compressed,
|
|
|
|
}
|
|
|
|
|
2024-02-23 11:11:14 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DataBlockElem<T> {
|
|
|
|
header: DataBlockHeader,
|
|
|
|
elem: T,
|
2022-03-15 11:12:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 11:11:14 +00:00
|
|
|
/// A possibly compressed block of data
|
|
|
|
pub type DataBlock = DataBlockElem<Bytes>;
|
|
|
|
|
|
|
|
/// A path to a possibly compressed block of data
|
|
|
|
pub type DataBlockPath = DataBlockElem<PathBuf>;
|
|
|
|
|
2024-02-23 11:22:29 +00:00
|
|
|
/// A stream of possibly compressed block data
|
|
|
|
pub type DataBlockStream = DataBlockElem<ByteStream>;
|
|
|
|
|
2024-02-23 11:11:14 +00:00
|
|
|
impl DataBlockHeader {
|
|
|
|
pub fn is_compressed(&self) -> bool {
|
|
|
|
matches!(self, DataBlockHeader::Compressed)
|
|
|
|
}
|
2023-09-05 12:27:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 11:11:14 +00:00
|
|
|
impl<T> DataBlockElem<T> {
|
|
|
|
pub fn from_parts(header: DataBlockHeader, elem: T) -> Self {
|
|
|
|
Self { header, elem }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plain(elem: T) -> Self {
|
|
|
|
Self {
|
|
|
|
header: DataBlockHeader::Plain,
|
|
|
|
elem,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compressed(elem: T) -> Self {
|
|
|
|
Self {
|
|
|
|
header: DataBlockHeader::Compressed,
|
|
|
|
elem,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_parts(self) -> (DataBlockHeader, T) {
|
|
|
|
(self.header, self.elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_parts_ref(&self) -> (DataBlockHeader, &T) {
|
|
|
|
(self.header, &self.elem)
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 11:12:12 +00:00
|
|
|
|
2024-02-23 11:11:14 +00:00
|
|
|
impl DataBlock {
|
2024-02-23 10:47:44 +00:00
|
|
|
/// Verify data integrity. Does not return the buffer content.
|
2022-03-15 11:12:12 +00:00
|
|
|
pub fn verify(&self, hash: Hash) -> Result<(), Error> {
|
2024-02-23 11:11:14 +00:00
|
|
|
match self.header {
|
|
|
|
DataBlockHeader::Plain => {
|
|
|
|
if blake2sum(&self.elem) == hash {
|
2022-03-15 11:12:12 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::CorruptData(hash))
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 11:11:14 +00:00
|
|
|
DataBlockHeader::Compressed => {
|
|
|
|
zstd::stream::copy_decode(&self.elem[..], std::io::sink())
|
|
|
|
.map_err(|_| Error::CorruptData(hash))
|
|
|
|
}
|
2022-03-15 11:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-02-23 11:11:14 +00:00
|
|
|
if let Ok(data_compressed) = zstd_encode(&data[..], level) {
|
2024-02-23 11:26:35 +00:00
|
|
|
return DataBlock::compressed(data_compressed.into());
|
2022-07-18 15:18:47 +00:00
|
|
|
}
|
2022-03-15 11:12:12 +00:00
|
|
|
}
|
2024-02-23 11:26:35 +00:00
|
|
|
DataBlock::plain(data.into())
|
2022-07-18 15:18:47 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
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)
|
|
|
|
}
|