1pub mod convert;
13mod decoded;
14
15pub use decoded::DecodedBatch;
18pub use harmos_domain::model::storage::{BatchReceipt, DataBatch, WriteCompletion, WriteReceipt};
19
20use super::manager::ENTRY_OVERHEAD;
26use super::{SchemaKey, StorageCursor, StoreHistory};
27use crate::schema::graph::Schema;
28use crate::{Error, MemorySize, Result};
29
30#[derive(Clone, Debug, PartialEq, Eq)]
39pub struct StorageSchema {
40 pub position: StorageCursor,
41 pub key: SchemaKey,
42 pub schema: Schema,
43}
44
45impl 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
82fn size_error() -> Error {
87 Error::InvalidInput { field: "storage schema", reason: "size overflow" }
88}
89
90