pancake_db_core/primitives/
bytess.rs

1use pancake_db_idl::dml::field_value::Value;
2use pancake_db_idl::dtype::DataType;
3
4use crate::compression::Codec;
5use crate::compression::ZSTD;
6use crate::compression::zstd_codec::ZstdCodec;
7use crate::errors::{CoreError, CoreResult};
8use crate::primitives::{Atom, Primitive};
9
10impl Atom for u8 {
11  const BYTE_SIZE: usize = 1;
12
13  fn to_bytes(&self) -> Vec<u8> {
14    vec![*self]
15  }
16
17  fn try_from_bytes(bytes: &[u8]) -> CoreResult<u8> {
18    Ok(bytes[0])
19  }
20}
21
22impl Primitive for Vec<u8> {
23  type A = u8;
24  const DTYPE: DataType = DataType::Bytes;
25
26  const IS_ATOMIC: bool = false;
27
28  fn to_value(&self) -> Value {
29    Value::BytesVal(self.clone())
30  }
31
32  fn try_from_value(v: &Value) -> CoreResult<Vec<u8>> {
33    match v {
34      Value::BytesVal(res) => Ok(res.clone()),
35      _ => Err(CoreError::invalid("unable to extract string from value"))
36    }
37  }
38
39  fn to_atoms(&self) -> Vec<u8> {
40    self.to_vec()
41  }
42
43  fn try_from_atoms(atoms: &[u8]) -> CoreResult<Self> {
44    Ok(atoms.to_vec())
45  }
46
47  fn new_codec(codec: &str) -> Option<Box<dyn Codec<P=Self>>> {
48    if codec == ZSTD {
49      Some(Box::new(ZstdCodec::<Vec<u8>>::default()))
50    } else {
51      None
52    }
53  }
54}