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 {
75            fields: name.split('.').map(str::to_owned).collect(),
76        }
77    }
78}
79impl Transform {
80    pub fn is_streaming(&self) -> bool {
81        matches!(self, Self::Filter(_) | Self::Project(_))
82    }
83}
84impl MemorySize for FieldPath {
85    fn memory_size(&self) -> usize {
86        std::mem::size_of::<Self>() + self.fields.memory_size().saturating_sub(std::mem::size_of_val(&self.fields))
87    }
88}
89impl MemorySize for Transform {
90    fn memory_size(&self) -> usize {
91        std::mem::size_of::<Self>()
92            + match self {
93                Self::Filter(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
94                Self::Project(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
95                Self::Aggregate(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
96                Self::Histogram(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
97                Self::Downsample(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
98                Self::Sort(value) => value.memory_size().saturating_sub(std::mem::size_of_val(value)),
99            }
100    }
101}
102
103/* === Public Functions === */
104
105pub fn invalid(reason: &'static str) -> crate::Error {
106    crate::Error::InvalidInput { field: "storage query transform", reason }
107}
108
109/* === Private Functions === */
110
111/* === Tests === */