Skip to main content

grafeo_common/memory/buffer/
region.rs

1//! Memory region definitions for the unified buffer manager.
2
3/// Memory region identifiers for budget partitioning.
4///
5/// The buffer manager divides its budget across these regions,
6/// allowing for differentiated eviction policies and pressure tracking.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum MemoryRegion {
10    /// Graph storage: nodes, edges, properties, adjacency lists.
11    GraphStorage,
12    /// Index structures: btree, hash, trie indexes.
13    IndexBuffers,
14    /// Query execution: DataChunks, hash tables, sort buffers.
15    ExecutionBuffers,
16    /// Spill staging area for operators under memory pressure.
17    SpillStaging,
18}
19
20impl MemoryRegion {
21    /// Returns the array index for this region (for per-region tracking).
22    #[must_use]
23    pub const fn index(&self) -> usize {
24        match self {
25            Self::GraphStorage => 0,
26            Self::IndexBuffers => 1,
27            Self::ExecutionBuffers => 2,
28            Self::SpillStaging => 3,
29        }
30    }
31
32    /// Returns a human-readable name for this region.
33    #[must_use]
34    pub const fn name(&self) -> &'static str {
35        match self {
36            Self::GraphStorage => "Graph Storage",
37            Self::IndexBuffers => "Index Buffers",
38            Self::ExecutionBuffers => "Execution Buffers",
39            Self::SpillStaging => "Spill Staging",
40        }
41    }
42
43    /// Returns all memory regions.
44    #[must_use]
45    pub const fn all() -> [Self; 4] {
46        [
47            Self::GraphStorage,
48            Self::IndexBuffers,
49            Self::ExecutionBuffers,
50            Self::SpillStaging,
51        ]
52    }
53}
54
55impl std::fmt::Display for MemoryRegion {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        write!(f, "{}", self.name())
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_region_indices() {
67        assert_eq!(MemoryRegion::GraphStorage.index(), 0);
68        assert_eq!(MemoryRegion::IndexBuffers.index(), 1);
69        assert_eq!(MemoryRegion::ExecutionBuffers.index(), 2);
70        assert_eq!(MemoryRegion::SpillStaging.index(), 3);
71    }
72
73    #[test]
74    fn test_region_names() {
75        assert_eq!(MemoryRegion::GraphStorage.name(), "Graph Storage");
76        assert_eq!(MemoryRegion::ExecutionBuffers.name(), "Execution Buffers");
77    }
78
79    #[test]
80    fn test_region_all() {
81        let all = MemoryRegion::all();
82        assert_eq!(all.len(), 4);
83    }
84}