pancake_db_core/compression/
utils.rs

1use pancake_db_idl::dtype::DataType;
2use q_compress::data_types::TimestampMicros;
3
4use crate::errors::{CoreError, CoreResult};
5use crate::primitives::Primitive;
6
7use super::{Q_COMPRESS, ZSTD};
8use super::ValueCodec;
9
10pub fn new_codec(
11  dtype: DataType,
12  codec: &str,
13) -> CoreResult<Box<dyn ValueCodec>> {
14  let maybe_res: Option<Box<dyn ValueCodec>> = match dtype {
15    DataType::String => String::new_value_codec(codec),
16    DataType::Int64 => i64::new_value_codec(codec),
17    DataType::Bytes => Vec::<u8>::new_value_codec(codec),
18    DataType::Bool => bool::new_value_codec(codec),
19    DataType::Float32 => f32::new_value_codec(codec),
20    DataType::Float64 => f64::new_value_codec(codec),
21    DataType::TimestampMicros => TimestampMicros::new_value_codec(codec),
22  };
23
24  match maybe_res {
25    Some(res) => Ok(res),
26    None => Err(CoreError::invalid(&format!(
27      "compression codec {} unavailable for data type {:?}",
28      codec,
29      dtype,
30    )))
31  }
32}
33
34pub fn choose_codec(dtype: DataType) -> String {
35  match dtype {
36    DataType::Int64 => Q_COMPRESS.to_string(),
37    DataType::String => ZSTD.to_string(),
38    DataType::Bytes => ZSTD.to_string(),
39    DataType::Float32 => Q_COMPRESS.to_string(),
40    DataType::Float64 => Q_COMPRESS.to_string(),
41    DataType::Bool => Q_COMPRESS.to_string(),
42    DataType::TimestampMicros => Q_COMPRESS.to_string(),
43  }
44}
45