vibesql_storage/database/
config.rs

1// ============================================================================
2// Database Configuration
3// ============================================================================
4
5#![allow(clippy::identity_op)]
6
7/// Default columnar cache budget (256MB)
8pub const DEFAULT_COLUMNAR_CACHE_BUDGET: usize = 256 * 1024 * 1024;
9
10/// Configuration for database resource budgets
11#[derive(Debug, Clone)]
12pub struct DatabaseConfig {
13    /// Maximum memory for indexes and buffer pools (bytes)
14    pub memory_budget: usize,
15
16    /// Maximum disk space for database files (bytes)
17    pub disk_budget: usize,
18
19    /// Policy for handling memory budget violations
20    pub spill_policy: SpillPolicy,
21
22    /// SQL dialect compatibility mode (MySQL, SQLite, etc.)
23    pub sql_mode: vibesql_types::SqlMode,
24
25    /// Maximum memory for columnar cache (bytes)
26    /// Set to 0 to disable the columnar cache
27    pub columnar_cache_budget: usize,
28}
29
30/// Policy for what to do when memory budget is exceeded
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum SpillPolicy {
33    /// Reject CREATE INDEX if it would exceed budget
34    Reject,
35
36    /// Automatically spill cold indexes from memory to disk
37    SpillToDisk,
38
39    /// Best effort - try to allocate, graceful degradation
40    BestEffort,
41}
42
43impl DatabaseConfig {
44    /// Default configuration for browser/WASM environments
45    /// - 512MB memory budget (conservative for browsers)
46    /// - 2GB disk budget (typical OPFS quota)
47    /// - SpillToDisk policy (automatic eviction)
48    /// - MySQL mode (default)
49    /// - 64MB columnar cache (modest for browser memory constraints)
50    pub fn browser_default() -> Self {
51        DatabaseConfig {
52            memory_budget: 512 * 1024 * 1024,    // 512MB
53            disk_budget: 2 * 1024 * 1024 * 1024, // 2GB
54            spill_policy: SpillPolicy::SpillToDisk,
55            sql_mode: vibesql_types::SqlMode::default(),
56            columnar_cache_budget: 64 * 1024 * 1024, // 64MB
57        }
58    }
59
60    /// Default configuration for server environments
61    /// - 16GB memory budget (abundant server RAM)
62    /// - 1TB disk budget (generous server storage)
63    /// - BestEffort policy (prefer memory, fall back to disk)
64    /// - MySQL mode (default)
65    /// - 256MB columnar cache (can cache all TPC-H tables at SF 1.0)
66    pub fn server_default() -> Self {
67        DatabaseConfig {
68            memory_budget: (16u64 * 1024 * 1024 * 1024) as usize, // 16GB
69            disk_budget: (1024u64 * 1024 * 1024 * 1024) as usize, // 1TB
70            spill_policy: SpillPolicy::BestEffort,
71            sql_mode: vibesql_types::SqlMode::default(),
72            columnar_cache_budget: 256 * 1024 * 1024, // 256MB
73        }
74    }
75
76    /// Minimal configuration for testing
77    /// - 10MB memory budget (force eviction quickly)
78    /// - 100MB disk budget
79    /// - SpillToDisk policy
80    /// - MySQL mode (default)
81    /// - 1MB columnar cache (tiny for testing eviction)
82    pub fn test_default() -> Self {
83        DatabaseConfig {
84            memory_budget: 10 * 1024 * 1024, // 10MB
85            disk_budget: 100 * 1024 * 1024,  // 100MB
86            spill_policy: SpillPolicy::SpillToDisk,
87            sql_mode: vibesql_types::SqlMode::default(),
88            columnar_cache_budget: 1 * 1024 * 1024, // 1MB
89        }
90    }
91}
92
93impl Default for DatabaseConfig {
94    fn default() -> Self {
95        // Default to server configuration (most permissive)
96        Self::server_default()
97    }
98}