Skip to main content

cynos_binary/
layout_cache.rs

1//! SchemaLayout cache for avoiding repeated layout computation.
2//!
3//! Full-table layouts are cached by table name.
4//! Projection layouts are created fresh each time (or could be cached by column signature).
5
6use super::SchemaLayout;
7use alloc::string::String;
8use cynos_core::schema::Table;
9use hashbrown::HashMap;
10
11/// Cache for SchemaLayout instances.
12///
13/// - Full-table queries: cached by table name
14/// - Projection queries: created fresh (column combinations are too varied to cache effectively)
15#[derive(Default)]
16pub struct SchemaLayoutCache {
17    /// Cache for full-table layouts, keyed by table name
18    full_table_layouts: HashMap<String, SchemaLayout>,
19}
20
21impl SchemaLayoutCache {
22    /// Create a new empty cache
23    pub fn new() -> Self {
24        Self {
25            full_table_layouts: HashMap::new(),
26        }
27    }
28
29    /// Get or create a SchemaLayout for a full-table query.
30    /// The layout is cached and reused for subsequent queries on the same table.
31    #[inline]
32    pub fn get_or_create_full(&mut self, table_name: &str, schema: &Table) -> &SchemaLayout {
33        self.full_table_layouts
34            .entry(table_name.into())
35            .or_insert_with(|| SchemaLayout::from_schema(schema))
36    }
37
38    /// Create a SchemaLayout for a projection query.
39    /// Projection layouts are not cached since column combinations vary too much.
40    #[inline]
41    pub fn create_projection(schema: &Table, column_names: &[String]) -> SchemaLayout {
42        SchemaLayout::from_projection(schema, column_names)
43    }
44
45    /// Invalidate cache for a specific table (call when schema changes)
46    pub fn invalidate(&mut self, table_name: &str) {
47        self.full_table_layouts.remove(table_name);
48    }
49
50    /// Clear all cached layouts
51    pub fn clear(&mut self) {
52        self.full_table_layouts.clear();
53    }
54}