Skip to main content

akar_main/
storage_driver.rs

1use akar_catalog::Catalog;
2use akar_common::file_system::VirtualFileSystemRegistry;
3use akar_storage::{BufferInfo, FileInfo, FsmInfo, StorageInfo, StorageManager};
4use std::path::Path;
5use std::sync::{Arc, Mutex};
6
7/// High-level storage access API.
8///
9/// Provides programmatic access to storage-level metadata (page counts,
10/// buffer manager stats, file sizes, FSM state, table layout) without
11/// going through Cypher queries.
12///
13/// Obtain via [`crate::Database::storage_driver()`].
14pub struct StorageDriver {
15    storage_manager: Arc<StorageManager>,
16    catalog: Arc<Mutex<Catalog>>,
17    vfs: Arc<VirtualFileSystemRegistry>,
18}
19
20impl StorageDriver {
21    pub(crate) fn new(
22        storage_manager: Arc<StorageManager>,
23        catalog: Arc<Mutex<Catalog>>,
24        vfs: Arc<VirtualFileSystemRegistry>,
25    ) -> Self {
26        Self {
27            storage_manager,
28            catalog,
29            vfs,
30        }
31    }
32
33    /// Database path as reported by the storage engine.
34    pub fn db_path(&self) -> &Path {
35        self.storage_manager.db_path()
36    }
37
38    /// Aggregate storage info (page count, page size, free pages).
39    pub fn storage_info(&self) -> StorageInfo {
40        self.storage_manager.storage_info()
41    }
42
43    /// Buffer manager memory and pin statistics.
44    pub fn buffer_info(&self) -> BufferInfo {
45        self.storage_manager.buffer_info()
46    }
47
48    /// File-level size statistics (data pages + WAL).
49    pub fn file_info(&self) -> FileInfo {
50        self.storage_manager.file_info()
51    }
52
53    /// Free space manager summary.
54    pub fn fsm_info(&self) -> FsmInfo {
55        self.storage_manager.fsm_info()
56    }
57
58    /// Current WAL size in bytes.
59    pub fn wal_size(&self) -> usize {
60        self.storage_manager.wal_size()
61    }
62
63    /// Access the table catalog for reading table/column metadata.
64    pub fn table_catalog(&self) -> Arc<akar_storage::TableCatalog> {
65        self.storage_manager.table_catalog()
66    }
67
68    /// Access the node/rel catalog.
69    pub fn catalog(&self) -> &Arc<Mutex<Catalog>> {
70        &self.catalog
71    }
72
73    /// Access the virtual file system registry.
74    pub fn vfs(&self) -> &Arc<VirtualFileSystemRegistry> {
75        &self.vfs
76    }
77
78    /// Number of tables (node + rel) in the catalog.
79    pub fn num_tables(&self) -> usize {
80        self.catalog.lock().unwrap().all_entries().count()
81    }
82
83    /// Number of node tables.
84    pub fn num_node_tables(&self) -> usize {
85        self.catalog
86            .lock()
87            .unwrap()
88            .all_entries()
89            .filter(|e| e.is_node_table())
90            .count()
91    }
92
93    /// Number of rel tables.
94    pub fn num_rel_tables(&self) -> usize {
95        self.catalog
96            .lock()
97            .unwrap()
98            .all_entries()
99            .filter(|e| !e.is_node_table())
100            .count()
101    }
102
103    /// Total number of pages allocated across all data files.
104    pub fn total_pages(&self) -> u64 {
105        self.storage_info().total_pages
106    }
107
108    /// Total data file size on disk in bytes.
109    pub fn total_file_size(&self) -> u64 {
110        self.file_info().total_file_size
111    }
112
113    /// Number of pinned buffer frames.
114    pub fn pinned_frames(&self) -> usize {
115        self.buffer_info().num_pinned
116    }
117}