Skip to main content

harmos_domain/model/storage/
schema_input.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17
18// Third Party Imports
19
20// Local Imports
21use crate::model::storage::codec::{Nested, product};
22use crate::model::storage::{SchemaKey, StorageCursor};
23use crate::schema::graph::Schema;
24use crate::{Error, MemorySize, Result};
25
26/* === Constants === */
27
28/* === Macros === */
29
30product!(SchemaInput { key: Nested<SchemaKey> => 1, graph: Nested<Schema> => 2 });
31product!(SchemaInfo { position: Nested<StorageCursor> => 1, schema: Nested<SchemaInput> => 2 });
32
33/* === Type Aliases === */
34
35/* === Structs === */
36
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct SchemaInput {
39    pub key: SchemaKey,
40    graph: Schema,
41}
42
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub struct SchemaInfo {
45    pub position: StorageCursor,
46    pub schema: SchemaInput,
47}
48
49/* === Enums === */
50
51/* === Traits === */
52
53/* === Implementations === */
54
55impl SchemaInput {
56    pub fn new(key: SchemaKey, graph: &Schema) -> Result<Self> {
57        graph
58            .prepare(&crate::config::SchemaSettings::default())
59            .map_err(|_| Error::InvalidInput { field: "storage schema", reason: "invalid finite schema graph" })?;
60        Ok(Self { key, graph: graph.clone() })
61    }
62
63    pub fn graph(&self) -> &Schema {
64        &self.graph
65    }
66}
67impl MemorySize for SchemaInput {
68    fn memory_size(&self) -> usize {
69        std::mem::size_of::<Self>() + self.key.memory_size() - std::mem::size_of::<SchemaKey>()
70            + self.graph.memory_size().saturating_sub(std::mem::size_of::<Schema>())
71    }
72}
73impl MemorySize for SchemaInfo {
74    fn memory_size(&self) -> usize {
75        std::mem::size_of::<Self>() + self.position.memory_size() - std::mem::size_of::<StorageCursor>() + self.schema.memory_size()
76            - std::mem::size_of::<SchemaInput>()
77    }
78}
79
80/* === Public Functions === */
81
82/* === Private Functions === */
83
84/* === Tests === */