1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use pancake_db_idl::dtype::DataType;
use q_compress::data_types::TimestampMicros;

use crate::errors::{CoreError, CoreResult};
use crate::primitives::Primitive;

use super::{Q_COMPRESS, ZSTD};
use super::ValueCodec;

pub fn new_codec(
  dtype: DataType,
  codec: &str,
) -> CoreResult<Box<dyn ValueCodec>> {
  let maybe_res: Option<Box<dyn ValueCodec>> = match dtype {
    DataType::STRING => String::new_value_codec(codec),
    DataType::INT64 => i64::new_value_codec(codec),
    DataType::BYTES => Vec::<u8>::new_value_codec(codec),
    DataType::BOOL => bool::new_value_codec(codec),
    DataType::FLOAT32 => f32::new_value_codec(codec),
    DataType::FLOAT64 => f64::new_value_codec(codec),
    DataType::TIMESTAMP_MICROS => TimestampMicros::new_value_codec(codec),
  };

  match maybe_res {
    Some(res) => Ok(res),
    None => Err(CoreError::invalid(&format!(
      "compression codec {} unavailable for data type {:?}",
      codec,
      dtype,
    )))
  }
}

pub fn choose_codec(dtype: DataType) -> String {
  match dtype {
    DataType::INT64 => Q_COMPRESS.to_string(),
    DataType::STRING => ZSTD.to_string(),
    DataType::BYTES => ZSTD.to_string(),
    DataType::FLOAT32 => Q_COMPRESS.to_string(),
    DataType::FLOAT64 => Q_COMPRESS.to_string(),
    DataType::BOOL => Q_COMPRESS.to_string(),
    DataType::TIMESTAMP_MICROS => Q_COMPRESS.to_string(),
  }
}