Skip to main content

molframe_core/execution/
storage.rs

1//! Policies for reusable scratch memory and deterministic spill storage.
2
3use std::path::{Path, PathBuf};
4
5/// Upper bound for reusable in-memory scratch owned by one stage.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct ScratchPolicy {
8    max_bytes: usize,
9}
10
11impl ScratchPolicy {
12    /// Creates a scratch policy bounded by `max_bytes`.
13    #[must_use]
14    pub const fn new(max_bytes: usize) -> Self {
15        Self { max_bytes }
16    }
17
18    /// Returns the maximum reusable scratch capacity.
19    #[must_use]
20    pub const fn max_bytes(self) -> usize {
21        self.max_bytes
22    }
23}
24
25impl Default for ScratchPolicy {
26    fn default() -> Self {
27        Self::new(8_000_000)
28    }
29}
30
31/// Policy controlling whether and where execution may spill temporary data.
32#[derive(Clone, Debug, Default, PartialEq, Eq)]
33pub enum TempStoragePolicy {
34    /// Operations that cannot remain within memory must return an error.
35    #[default]
36    Disabled,
37    /// Spill files may be created below one explicit directory.
38    Directory {
39        /// Root below which implementations create execution-specific files.
40        root: PathBuf,
41        /// Maximum bytes retained on disk by one execution context.
42        max_bytes: u64,
43    },
44}
45
46impl TempStoragePolicy {
47    /// Enables bounded spill below `root`.
48    #[must_use]
49    pub fn directory(root: impl Into<PathBuf>, max_bytes: u64) -> Self {
50        Self::Directory {
51            root: root.into(),
52            max_bytes,
53        }
54    }
55
56    /// Returns the configured spill root, when disk storage is enabled.
57    #[must_use]
58    pub fn root(&self) -> Option<&Path> {
59        match self {
60            Self::Disabled => None,
61            Self::Directory { root, .. } => Some(root),
62        }
63    }
64
65    /// Returns the disk-space ceiling. Disabled storage has a zero ceiling.
66    #[must_use]
67    pub const fn max_bytes(&self) -> u64 {
68        match self {
69            Self::Disabled => 0,
70            Self::Directory { max_bytes, .. } => *max_bytes,
71        }
72    }
73}
74
75#[cfg(test)]
76#[path = "storage_tests.rs"]
77mod tests;