absurder_sql/storage/
block_info.rs

1//! Block storage information API for the AbsurderSQL Viewer
2//! Provides read-only access to block metadata and cache statistics
3
4use serde::{Serialize, Deserialize};
5use super::block_storage::BlockStorage;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct BlockInfo {
9    pub block_id: u64,
10    pub checksum: u64,
11    pub version: u32,
12    pub last_modified_ms: u64,
13    pub is_cached: bool,
14    pub is_dirty: bool,
15    pub is_allocated: bool,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct BlockStorageInfo {
20    pub db_name: String,
21    pub total_allocated_blocks: usize,
22    pub total_cached_blocks: usize,
23    pub total_dirty_blocks: usize,
24    pub cache_capacity: usize,
25    pub next_block_id: u64,
26    pub blocks: Vec<BlockInfo>,
27}
28
29impl BlockStorage {
30    /// Get comprehensive block storage information for the viewer
31    pub fn get_storage_info(&self) -> BlockStorageInfo {
32        let dirty_guard = self.dirty_blocks.lock();
33        
34        let mut blocks: Vec<BlockInfo> = Vec::new();
35        
36        // Get metadata for all allocated blocks
37        #[cfg(any(test, debug_assertions))]
38        let metadata = self.get_block_metadata_for_testing();
39        #[cfg(not(any(test, debug_assertions)))]
40        let metadata: std::collections::HashMap<u64, (u64, u32, u64)> = std::collections::HashMap::new(); // In production, we'd need a proper accessor
41        
42        for &block_id in &self.allocated_blocks {
43            let is_cached = self.cache.contains_key(&block_id);
44            let is_dirty = dirty_guard.contains_key(&block_id);
45            
46            let (checksum, version, last_modified_ms) = metadata
47                .get(&block_id)
48                .copied()
49                .unwrap_or((0, 0, 0));
50            
51            blocks.push(BlockInfo {
52                block_id,
53                checksum,
54                version,
55                last_modified_ms,
56                is_cached,
57                is_dirty,
58                is_allocated: true,
59            });
60        }
61        
62        // Sort by block_id for consistent display
63        blocks.sort_by_key(|b| b.block_id);
64        
65        BlockStorageInfo {
66            db_name: self.db_name.clone(),
67            total_allocated_blocks: self.allocated_blocks.len(),
68            total_cached_blocks: self.cache.len(),
69            total_dirty_blocks: dirty_guard.len(),
70            cache_capacity: self.capacity,
71            next_block_id: self.next_block_id,
72            blocks,
73        }
74    }
75}