Skip to main content

harmos_domain/model/storage/query/transform/
mod.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Storage Arrow Query Contract
11
12/* === Modules === */
13
14pub mod aggregate;
15pub mod downsample;
16pub mod filter;
17pub mod histogram;
18pub mod project;
19pub mod sort;
20
21/* === Re-Exports === */
22
23/* === Imports === */
24
25// Standard Library Imports
26
27// Third Party Imports
28
29// Local Imports
30use crate::MemorySize;
31use crate::model::storage::codec::{Many, Nested, Text, product, variants};
32
33/* === Constants === */
34
35/* === Macros === */
36
37product!(FieldPath { fields: Many<Text> => 1 });
38variants!(Transform {
39    0, 1, "filter": Self::Filter(value) => { value: Nested<filter::Filter> => 1 } => Self::Filter(value);
40    1, 2, "project": Self::Project(value) => { value: Nested<project::Project> => 1 } => Self::Project(value);
41    2, 3, "aggregate": Self::Aggregate(value) => { value: Nested<aggregate::Aggregate> => 1 } => Self::Aggregate(value);
42    3, 4, "histogram": Self::Histogram(value) => { value: Nested<histogram::Histogram> => 1 } => Self::Histogram(value);
43    4, 5, "downsample": Self::Downsample(value) => { value: Nested<downsample::Downsample> => 1 } => Self::Downsample(value);
44    5, 6, "sort": Self::Sort(value) => { value: Nested<sort::Sort> => 1 } => Self::Sort(value);
45});
46
47/* === Type Aliases === */
48
49/* === Structs === */
50
51#[derive(Clone, Debug, PartialEq, Eq)]
52pub struct FieldPath {
53    pub fields: Vec<String>,
54}
55
56/* === Enums === */
57
58#[derive(Clone, Debug)]
59pub enum Transform {
60    Filter(filter::Filter),
61    Project(project::Project),
62    Aggregate(aggregate::Aggregate),
63    Histogram(histogram::Histogram),
64    Downsample(downsample::Downsample),
65    Sort(sort::Sort),
66}
67
68/* === Traits === */
69
70/* === Implementations === */
71
72impl From<&str> for FieldPath {
73    fn from(name: &str) -> Self {
74        Self { fields: name.split('.').map(str::to_owned).collect() }
75    }
76}
77impl Transform {
78    pub fn is_streaming(&self) -> bool {
79        matches!(self, Self::Filter(_) | Self::Project(_))
80    }
81}
82impl MemorySize for FieldPath {
83    fn memory_size(&self) -> usize {
84        std::mem::size_of::<Self>() + self.fields.memory_size().saturating_sub(std::mem::size_of_val(&self.fields))
85    }
86}
87impl MemorySize for Transform {
88    fn memory_size(&self) -> usize {
89        std::mem::size_of::<Self>()
90            + match self {
91                Self::Filter(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
92                Self::Project(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
93                Self::Aggregate(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
94                Self::Histogram(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
95                Self::Downsample(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
96                Self::Sort(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
97            }
98    }
99}
100
101/* === Public Functions === */
102
103pub fn invalid(reason: &'static str) -> crate::Error {
104    crate::Error::InvalidInput { field: "storage query transform", reason }
105}
106
107/* === Private Functions === */
108
109/* === Tests === */