Skip to main content

harmos_storage/
writes.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12pub mod convert;
13mod decoded;
14
15/* === Re-Exports === */
16
17pub use decoded::DecodedBatch;
18pub use harmos_domain::model::storage::{BatchReceipt, DataBatch, WriteCompletion, WriteReceipt};
19
20/* === Imports === */
21
22// Standard Library Imports
23// Third Party Imports
24// Local Imports
25use super::manager::ENTRY_OVERHEAD;
26use super::{SchemaKey, StorageCursor, StoreHistory};
27use crate::schema::graph::Schema;
28use crate::{Error, MemorySize, Result};
29
30/* === Constants === */
31
32/* === Macros === */
33
34/* === Type Aliases === */
35
36/* === Structs === */
37
38#[derive(Clone, Debug, PartialEq, Eq)]
39pub struct StorageSchema {
40    pub position: StorageCursor,
41    pub key: SchemaKey,
42    pub schema: Schema,
43}
44
45/* === Enums === */
46
47/* === Traits === */
48
49/* === Implementations === */
50
51impl MemorySize for StorageSchema {
52    fn memory_size(&self) -> usize {
53        std::mem::size_of::<Self>()
54            .saturating_add(self.position.memory_size().saturating_sub(std::mem::size_of::<StorageCursor>()))
55            .saturating_add(self.key.memory_size().saturating_sub(std::mem::size_of::<SchemaKey>()))
56            .saturating_add(self.schema.memory_size().saturating_sub(std::mem::size_of::<Schema>()))
57    }
58}
59
60impl StorageSchema {
61    pub fn new(position: StorageCursor, key: SchemaKey, schema: Schema) -> Self {
62        Self { position, key, schema }
63    }
64
65    pub(super) fn resident_bytes(key: &SchemaKey, schema: &Schema, history: &StoreHistory) -> Result<usize> {
66        let allocations =
67            schema.clone_memory_bytes().map_err(|_| size_error())?.checked_sub(std::mem::size_of::<Schema>()).ok_or_else(size_error)?;
68        [
69            std::mem::size_of::<Self>(),
70            ENTRY_OVERHEAD,
71            key.id.as_str().len(),
72            history.store.as_str().len(),
73            history.epoch.as_str().len(),
74            allocations,
75        ]
76        .into_iter()
77        .try_fold(0usize, |bytes, next| bytes.checked_add(next))
78        .ok_or_else(size_error)
79    }
80}
81
82/* === Public Functions === */
83
84/* === Private Functions === */
85
86fn size_error() -> Error {
87    Error::InvalidInput { field: "storage schema", reason: "size overflow" }
88}
89
90/* === Tests === */