Skip to main content

harmos_domain/operations/storage/
query.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// A Declarative Storage Query: A Source At A Full Cursor, Ordered Transforms
11// And A Final Row Slice. Results Cross The Wire As Arrow IPC.
12
13/* === Modules === */
14
15/* === Re-Exports === */
16
17/* === Imports === */
18
19// Standard Library Imports
20
21// Third Party Imports
22
23// Local Imports
24use super::IO_POLICY;
25use crate::model::storage::StorageCursor;
26use crate::model::storage::codec::{Nested, Repeated, product};
27use crate::model::storage::query::transform::Transform;
28use crate::model::storage::query::{QueryCompletion, QueryOutput, RowSlice, Source};
29use crate::operations::{ReplaySafety, Visibility};
30use crate::{MemorySize, Result};
31
32/* === Constants === */
33
34/* === Macros === */
35
36product!(Query {
37    at: Nested<StorageCursor> => 1,
38    source: Nested<Source> => 2,
39    transforms: Repeated<Transform> => 3,
40    slice: Nested<RowSlice> => 4,
41});
42
43crate::declare!(Query {
44    id: "storage.query",
45    version: 1,
46    policy: IO_POLICY,
47    response: QueryCompletion,
48    output: QueryOutput,
49    visibility: Visibility::Shared,
50    replay: ReplaySafety::Idempotent,
51    grpc: ("harmos.storage.v1", "Storage", "Query"),
52    grant: |request: &Query| Some(crate::plugin::Grant::of("storage.query", request.source.grant_scope(),)),
53});
54
55/* === Type Aliases === */
56
57/* === Structs === */
58
59#[derive(Clone, Debug)]
60pub struct Query {
61    pub at: StorageCursor,
62    pub source: Source,
63    pub transforms: Vec<Transform>,
64    pub slice: RowSlice,
65}
66
67/* === Enums === */
68
69/* === Traits === */
70
71/* === Implementations === */
72
73impl Query {
74    pub fn validate(&self) -> Result<()> {
75        self.source.validate()?;
76        if self.transforms.len() > 64 {
77            return Err(crate::model::storage::query::transform::invalid("too many query transforms"));
78        }
79        Ok(())
80    }
81}
82
83impl MemorySize for Query {
84    fn memory_size(&self) -> usize {
85        std::mem::size_of::<Self>()
86            .saturating_add(self.at.memory_size().saturating_sub(std::mem::size_of_val(&self.at)))
87            .saturating_add(self.source.memory_size().saturating_sub(std::mem::size_of_val(&self.source)))
88            .saturating_add(self.transforms.memory_size().saturating_sub(std::mem::size_of_val(&self.transforms)))
89            .saturating_add(self.slice.memory_size().saturating_sub(std::mem::size_of_val(&self.slice)))
90    }
91}
92
93/* === Public Functions === */
94
95/* === Private Functions === */
96
97/* === Tests === */