Skip to main content

harmos_domain/model/storage/
identities.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Stored Stream And Object Identities The Paged Catalog Lists.
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19
20// Third Party Imports
21use serde::{Deserialize, Serialize};
22// Local Imports
23use super::{ObjectKey, SchemaKey, StorageCursor};
24use crate::{Identifier, MemorySize};
25
26/* === Constants === */
27
28/* === Macros === */
29
30/* === Type Aliases === */
31
32/* === Structs === */
33
34#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
35pub struct IdentitySelection {
36    pub kind: IdentityKind,
37    pub schema: Option<Identifier>,
38}
39
40/* === Enums === */
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44pub enum IdentityKind {
45    Streams,
46    Objects,
47}
48
49#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
51pub enum StoredIdentity {
52    Stream { schema: SchemaKey, scope: Option<Identifier>, item: Identifier, stream: Identifier, first: StorageCursor },
53    Object { schema: SchemaKey, key: ObjectKey, first: StorageCursor },
54}
55
56/* === Traits === */
57
58/* === Implementations === */
59
60impl MemorySize for IdentitySelection {
61    fn memory_size(&self) -> usize {
62        std::mem::size_of::<Self>()
63            .saturating_add(self.schema.as_ref().map_or(0, |schema| schema.memory_size().saturating_sub(std::mem::size_of::<Identifier>())))
64    }
65}
66
67impl MemorySize for StoredIdentity {
68    fn memory_size(&self) -> usize {
69        let base = std::mem::size_of::<Self>()
70            .saturating_add(self.schema().memory_size().saturating_sub(std::mem::size_of::<SchemaKey>()))
71            .saturating_add(self.first().memory_size().saturating_sub(std::mem::size_of::<StorageCursor>()));
72        match self {
73            Self::Stream { scope, item, stream, .. } => base
74                .saturating_add(scope.as_ref().map_or(0, |scope| scope.memory_size().saturating_sub(std::mem::size_of::<Identifier>())))
75                .saturating_add(item.memory_size().saturating_sub(std::mem::size_of::<Identifier>()))
76                .saturating_add(stream.memory_size().saturating_sub(std::mem::size_of::<Identifier>())),
77            Self::Object { key, .. } => base.saturating_add(key.memory_size().saturating_sub(std::mem::size_of::<ObjectKey>())),
78        }
79    }
80}
81
82impl StoredIdentity {
83    pub fn first(&self) -> &StorageCursor {
84        match self {
85            Self::Stream { first, .. } | Self::Object { first, .. } => first,
86        }
87    }
88
89    pub fn schema(&self) -> &SchemaKey {
90        match self {
91            Self::Stream { schema, .. } | Self::Object { schema, .. } => schema,
92        }
93    }
94
95    pub fn kind(&self) -> IdentityKind {
96        match self {
97            Self::Stream { .. } => IdentityKind::Streams,
98            Self::Object { .. } => IdentityKind::Objects,
99        }
100    }
101}
102
103impl IdentitySelection {
104    pub fn matches(&self, identity: &StoredIdentity) -> bool {
105        identity.kind() == self.kind && self.schema.as_ref().is_none_or(|schema| schema == &identity.schema().id)
106    }
107}
108
109/* === Public Functions === */
110
111/* === Private Functions === */
112
113/* === Tests === */