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/* === Modules === */
11
12pub mod aggregate;
13pub mod downsample;
14pub mod filter;
15pub mod histogram;
16pub mod project;
17pub mod sort;
18
19/* === Re-Exports === */
20
21/* === Imports === */
22
23// Standard Library Imports
24
25// Third Party Imports
26
27// Local Imports
28use crate::MemorySize;
29use crate::model::storage::codec::{Many, Nested, Text, product, variants};
30
31/* === Constants === */
32
33/* === Macros === */
34
35product!(FieldPath { fields: Many<Text> => 1 });
36variants!(Transform {
37    0, 1, "filter": Self::Filter(value) => { value: Nested<filter::Filter> => 1 } => Self::Filter(value);
38    1, 2, "project": Self::Project(value) => { value: Nested<project::Project> => 1 } => Self::Project(value);
39    2, 3, "aggregate": Self::Aggregate(value) => { value: Nested<aggregate::Aggregate> => 1 } => Self::Aggregate(value);
40    3, 4, "histogram": Self::Histogram(value) => { value: Nested<histogram::Histogram> => 1 } => Self::Histogram(value);
41    4, 5, "downsample": Self::Downsample(value) => { value: Nested<downsample::Downsample> => 1 } => Self::Downsample(value);
42    5, 6, "sort": Self::Sort(value) => { value: Nested<sort::Sort> => 1 } => Self::Sort(value);
43});
44
45/* === Type Aliases === */
46
47/* === Structs === */
48
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub struct FieldPath {
51    pub fields: Vec<String>,
52}
53
54/* === Enums === */
55
56#[derive(Clone, Debug)]
57pub enum Transform {
58    Filter(filter::Filter),
59    Project(project::Project),
60    Aggregate(aggregate::Aggregate),
61    Histogram(histogram::Histogram),
62    Downsample(downsample::Downsample),
63    Sort(sort::Sort),
64}
65
66/* === Traits === */
67
68/* === Implementations === */
69
70impl From<&str> for FieldPath {
71    fn from(name: &str) -> Self {
72        Self {
73            fields: name.split('.').map(str::to_owned).collect(),
74        }
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 === */