pancake_db_core/primitives/
timestamps.rs1use std::time::SystemTime;
2use pancake_db_idl::dml::field_value::Value;
3use pancake_db_idl::dtype::DataType;
4use q_compress::data_types::{NumberLike, TimestampMicros};
5
6use prost_types::Timestamp;
7
8use crate::compression::Codec;
9use crate::compression::q_codec::TimestampMicrosQCodec;
10use crate::compression::Q_COMPRESS;
11use crate::errors::{CoreError, CoreResult};
12use crate::primitives::{Atom, Primitive};
13
14impl Atom for TimestampMicros {
15 const BYTE_SIZE: usize = 12;
16
17 fn to_bytes(&self) -> Vec<u8> {
18 NumberLike::to_bytes(*self)
19 }
20
21 fn try_from_bytes(bytes: &[u8]) -> CoreResult<Self> {
22 Ok(TimestampMicros::from_bytes(bytes.to_vec())?)
23 }
24}
25
26impl Primitive for TimestampMicros {
27 type A = Self;
28 const DTYPE: DataType = DataType::TimestampMicros;
29
30 const IS_ATOMIC: bool = true;
31
32 fn to_value(&self) -> Value {
33 let mut t = Timestamp::from(SystemTime::now());
34 let (secs, nanos) = self.to_secs_and_nanos();
35 t.seconds = secs;
36 t.nanos = nanos as i32;
37 Value::TimestampVal(t)
38 }
39
40 fn try_from_value(v: &Value) -> CoreResult<TimestampMicros> {
41 match v {
42 Value::TimestampVal(res) => Ok(TimestampMicros::from_secs_and_nanos(res.seconds, res.nanos as u32)),
43 _ => Err(CoreError::invalid("cannot read timestamp from value")),
44 }
45 }
46
47 fn to_atoms(&self) -> Vec<Self> {
48 vec![*self]
49 }
50
51 fn try_from_atoms(atoms: &[Self]) -> CoreResult<Self> {
52 Ok(atoms[0])
53 }
54
55 fn new_codec(codec: &str) -> Option<Box<dyn Codec<P=Self>>> {
56 if codec == Q_COMPRESS {
57 Some(Box::new(TimestampMicrosQCodec {}))
58 } else {
59 None
60 }
61 }
62}
63