From 1eb972b1ac8951faac234f56a30b34100d6ecff2 Mon Sep 17 00:00:00 2001 From: trinity-1686a Date: Wed, 15 Dec 2021 11:26:43 +0100 Subject: Add compression using zstd (#173) fix #27 Co-authored-by: Trinity Pointard Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/173 Co-authored-by: trinity-1686a Co-committed-by: trinity-1686a --- src/util/config.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/util/config.rs') diff --git a/src/util/config.rs b/src/util/config.rs index 33802012..61a749e3 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -30,6 +30,13 @@ pub struct Config { // (we can add more aliases for this later) pub replication_mode: String, + /// Zstd compression level used on data blocks + #[serde( + deserialize_with = "deserialize_compression", + default = "default_compression" + )] + pub compression_level: Option, + /// RPC secret key: 32 bytes hex encoded pub rpc_secret: String, @@ -123,3 +130,57 @@ where Ok(ret) } + +fn default_compression() -> Option { + Some(1) +} + +fn deserialize_compression<'de, D>(deserializer: D) -> Result, D::Error> +where + D: de::Deserializer<'de>, +{ + use std::convert::TryFrom; + + struct OptionVisitor; + + impl<'de> serde::de::Visitor<'de> for OptionVisitor { + type Value = Option; + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("int or 'none'") + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + if value.eq_ignore_ascii_case("none") { + Ok(None) + } else { + Err(E::custom(format!( + "Invalid compression level: '{}', should be a number, or 'none'", + value + ))) + } + } + + fn visit_i64(self, v: i64) -> Result + where + E: de::Error, + { + i32::try_from(v) + .map(Some) + .map_err(|_| E::custom("Compression level out of bound".to_owned())) + } + + fn visit_u64(self, v: u64) -> Result + where + E: de::Error, + { + i32::try_from(v) + .map(Some) + .map_err(|_| E::custom("Compression level out of bound".to_owned())) + } + } + + deserializer.deserialize_any(OptionVisitor) +} -- cgit v1.2.3