use super::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Algo {
Snappy,
Lz4,
Zstd,
Deflate,
GDeflate,
Bitcomp {
data_type: BitcompDataType,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BitcompDataType {
Char,
Uint8,
Uint16,
Uint32,
Uint64,
Int8,
Int16,
Int32,
Int64,
Float32,
Float64,
BFloat16,
}
impl BitcompDataType {
pub const fn for_postings_uint32() -> Self {
BitcompDataType::Uint32
}
pub const fn name(self) -> &'static str {
match self {
BitcompDataType::Char => "char",
BitcompDataType::Uint8 => "uint8",
BitcompDataType::Uint16 => "uint16",
BitcompDataType::Uint32 => "uint32",
BitcompDataType::Uint64 => "uint64",
BitcompDataType::Int8 => "int8",
BitcompDataType::Int16 => "int16",
BitcompDataType::Int32 => "int32",
BitcompDataType::Int64 => "int64",
BitcompDataType::Float32 => "float32",
BitcompDataType::Float64 => "float64",
BitcompDataType::BFloat16 => "bfloat16",
}
}
pub fn parse(s: &str) -> Result<Self> {
match s {
"char" => Ok(BitcompDataType::Char),
"uint8" => Ok(BitcompDataType::Uint8),
"uint16" => Ok(BitcompDataType::Uint16),
"uint32" => Ok(BitcompDataType::Uint32),
"uint64" => Ok(BitcompDataType::Uint64),
"int8" => Ok(BitcompDataType::Int8),
"int16" => Ok(BitcompDataType::Int16),
"int32" => Ok(BitcompDataType::Int32),
"int64" => Ok(BitcompDataType::Int64),
"float32" => Ok(BitcompDataType::Float32),
"float64" => Ok(BitcompDataType::Float64),
"bfloat16" => Ok(BitcompDataType::BFloat16),
other => Err(Error::UnknownAlgo(format!("bitcomp:{other}"))),
}
}
}
impl Algo {
pub const fn for_postings_uint32() -> Self {
Algo::Bitcomp {
data_type: BitcompDataType::Uint32,
}
}
pub fn name(self) -> &'static str {
match self {
Algo::Snappy => "snappy",
Algo::Lz4 => "lz4",
Algo::Zstd => "zstd",
Algo::Deflate => "deflate",
Algo::GDeflate => "gdeflate",
Algo::Bitcomp { .. } => "bitcomp",
}
}
pub fn parse(s: &str) -> Result<Self> {
match s {
"snappy" => Ok(Algo::Snappy),
"lz4" => Ok(Algo::Lz4),
"zstd" => Ok(Algo::Zstd),
"deflate" => Ok(Algo::Deflate),
"gdeflate" => Ok(Algo::GDeflate),
"bitcomp" => Ok(Algo::for_postings_uint32()),
other if other.starts_with("bitcomp:") || other.starts_with("bitcomp-") => {
let suffix = &other[8..];
let dt = BitcompDataType::parse(suffix)?;
Ok(Algo::Bitcomp { data_type: dt })
}
other => Err(Error::UnknownAlgo(other.to_string())),
}
}
pub fn is_first_class(self) -> bool {
matches!(self, Algo::Snappy | Algo::Lz4 | Algo::Zstd)
}
}
impl std::fmt::Display for Algo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Algo::Bitcomp { data_type } => write!(f, "bitcomp-{}", data_type.name()),
other => f.write_str(other.name()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Tier {
Hot,
Warm,
Cold,
Frozen,
Wire,
}
impl Tier {
pub fn default_algo(self) -> Algo {
match self {
Tier::Hot => Algo::Snappy,
Tier::Warm | Tier::Cold | Tier::Frozen => Algo::Zstd,
Tier::Wire => Algo::Lz4,
}
}
pub fn name(self) -> &'static str {
match self {
Tier::Hot => "hot",
Tier::Warm => "warm",
Tier::Cold => "cold",
Tier::Frozen => "frozen",
Tier::Wire => "wire",
}
}
}
impl std::fmt::Display for Tier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}