Skip to main content

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