Skip to main content

harmos_domain/model/storage/
mutations.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
17use std::time::SystemTime;
18
19// Third Party Imports
20use serde::{Deserialize, Serialize};
21
22// Local Imports
23use crate::model::storage::{FileSession, StorageCursor, StoreHistory};
24use crate::{Identifier, MemorySize};
25
26/* === Constants === */
27
28/* === Macros === */
29
30crate::model::storage::codec::product!(MutationId {
31    window: crate::model::storage::codec::Id => 1,
32    key: crate::model::storage::codec::Id => 2,
33});
34crate::model::storage::codec::product!(MutationWindow {
35    history: crate::model::storage::codec::Nested<StoreHistory> => 1,
36    id: crate::model::storage::codec::Id => 2,
37    expires_at: crate::model::storage::codec::Time => 3,
38});
39crate::model::storage::codec::product!(FileProducer {
40    session: crate::model::storage::codec::Nested<FileSession> => 1,
41    length: crate::model::storage::codec::Unsigned => 2,
42});
43
44crate::model::storage::codec::product!(StorageInfo {
45    accepted: crate::model::storage::codec::Nested<StorageCursor> => 1,
46    saved: crate::model::storage::codec::Nested<StorageCursor> => 2,
47    mutations: crate::model::storage::codec::Optional<crate::model::storage::codec::Nested<MutationWindow>> => 3,
48    file_batch_bytes: crate::model::storage::codec::Unsigned => 4,
49});
50
51/* === Type Aliases === */
52
53/* === Structs === */
54
55#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
56#[serde(deny_unknown_fields)]
57pub struct MutationId {
58    pub window: Identifier,
59    pub key: Identifier,
60}
61
62#[derive(Clone, Debug, PartialEq, Eq)]
63pub struct MutationWindow {
64    pub history: StoreHistory,
65    pub id: Identifier,
66    pub expires_at: SystemTime,
67}
68
69#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(deny_unknown_fields)]
71pub struct FileProducer {
72    pub session: FileSession,
73    pub length: u64,
74}
75
76#[derive(Clone, Debug, PartialEq, Eq)]
77pub struct StorageInfo {
78    pub accepted: StorageCursor,
79    pub saved: StorageCursor,
80    pub mutations: Option<MutationWindow>,
81    pub file_batch_bytes: u64,
82}
83
84/* === Enums === */
85
86/* === Traits === */
87
88/* === Implementations === */
89
90impl MemorySize for MutationId {
91    fn memory_size(&self) -> usize {
92        std::mem::size_of::<Self>() + self.window.memory_size() - std::mem::size_of::<Identifier>() + self.key.memory_size()
93            - std::mem::size_of::<Identifier>()
94    }
95}
96impl MemorySize for MutationWindow {
97    fn memory_size(&self) -> usize {
98        std::mem::size_of::<Self>() + self.history.memory_size() - std::mem::size_of::<StoreHistory>() + self.id.memory_size()
99            - std::mem::size_of::<Identifier>()
100    }
101}
102impl MemorySize for FileProducer {
103    fn memory_size(&self) -> usize {
104        std::mem::size_of::<Self>() + self.session.memory_size() - std::mem::size_of::<FileSession>()
105    }
106}
107
108impl MemorySize for StorageInfo {
109    fn memory_size(&self) -> usize {
110        std::mem::size_of::<Self>() + self.accepted.memory_size() - std::mem::size_of::<StorageCursor>() + self.saved.memory_size()
111            - std::mem::size_of::<StorageCursor>()
112            + self.mutations.memory_size()
113            - std::mem::size_of::<Option<MutationWindow>>()
114    }
115}
116
117/* === Public Functions === */
118
119/* === Private Functions === */
120
121/* === Tests === */