Skip to main content

harmos_domain/model/storage/
position.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12pub mod codec;
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 crate::{Error, Identifier, MemorySize, Result};
24
25/* === Constants === */
26
27/* === Macros === */
28
29/* === Type Aliases === */
30
31/* === Structs === */
32
33#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
34#[serde(deny_unknown_fields)]
35pub struct StoreHistory {
36    pub store: Identifier,
37    pub epoch: Identifier,
38}
39
40#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
41#[serde(deny_unknown_fields)]
42pub struct StorageCursor {
43    pub history: StoreHistory,
44    pub sequence: u64,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
48#[serde(deny_unknown_fields)]
49pub struct RecoveryPosition {
50    pub history: StoreHistory,
51    pub sequence: u64,
52}
53
54/* === Enums === */
55
56/* === Traits === */
57
58/* === Implementations === */
59
60impl MemorySize for StoreHistory {
61    fn memory_size(&self) -> usize {
62        std::mem::size_of::<Self>()
63            .saturating_add(self.store.memory_size().saturating_sub(std::mem::size_of::<Identifier>()))
64            .saturating_add(self.epoch.memory_size().saturating_sub(std::mem::size_of::<Identifier>()))
65    }
66}
67
68impl MemorySize for StorageCursor {
69    fn memory_size(&self) -> usize {
70        std::mem::size_of::<Self>().saturating_add(self.history.memory_size().saturating_sub(std::mem::size_of::<StoreHistory>()))
71    }
72}
73
74impl StoreHistory {
75    pub fn check(&self, other: &Self) -> Result<()> {
76        if self == other { Ok(()) } else { Err(Error::DifferentHistory) }
77    }
78}
79
80impl StorageCursor {
81    pub fn covers(&self, other: &Self) -> Result<bool> {
82        self.history.check(&other.history)?;
83        Ok(self.sequence >= other.sequence)
84    }
85}
86
87impl RecoveryPosition {
88    pub fn covers(&self, other: &Self) -> Result<bool> {
89        self.history.check(&other.history)?;
90        Ok(self.sequence >= other.sequence)
91    }
92}
93
94/* === Public Functions === */
95
96/* === Private Functions === */
97
98/* === Tests === */