pancake_db_core/primitives/
ints.rs1use pancake_db_idl::dml::field_value::Value;
2use pancake_db_idl::dtype::DataType;
3
4use crate::compression::Codec;
5use crate::compression::q_codec::I64QCodec;
6use crate::compression::Q_COMPRESS;
7use crate::errors::{CoreError, CoreResult};
8use crate::primitives::{Atom, Primitive};
9use crate::utils;
10
11impl Atom for i64 {
12 const BYTE_SIZE: usize = 8;
13
14 fn to_bytes(&self) -> Vec<u8> {
15 self.to_be_bytes().to_vec()
16 }
17
18 fn try_from_bytes(bytes: &[u8]) -> CoreResult<Self> {
19 let byte_array = utils::try_byte_array::<8>(bytes)?;
20 Ok(i64::from_be_bytes(byte_array))
21 }
22}
23
24impl Primitive for i64 {
25 type A = Self;
26 const DTYPE: DataType = DataType::Int64;
27
28 const IS_ATOMIC: bool = true;
29
30 fn to_value(&self) -> Value {
31 Value::Int64Val(*self)
32 }
33
34 fn try_from_value(v: &Value) -> CoreResult<i64> {
35 match v {
36 Value::Int64Val(res) => Ok(*res),
37 _ => Err(CoreError::invalid("cannot read i64 from value")),
38 }
39 }
40
41 fn to_atoms(&self) -> Vec<Self> {
42 vec![*self]
43 }
44
45 fn try_from_atoms(atoms: &[Self]) -> CoreResult<Self> {
46 Ok(atoms[0])
47 }
48
49 fn new_codec(codec: &str) -> Option<Box<dyn Codec<P=Self>>> {
50 if codec == Q_COMPRESS {
51 Some(Box::new(I64QCodec {}))
52 } else {
53 None
54 }
55 }
56}
57