use super::{Pools, thread_local::set_thread_pools};
#[derive(Clone, Debug)]
pub struct PoolConfig {
pub max_pool_size: usize,
pub enable_statistics: bool,
pub auto_clear_threshold: Option<usize>,
pub prewarm_capacities: Vec<usize>,
}
impl Default for PoolConfig {
fn default() -> Self {
Self {
max_pool_size: 16,
enable_statistics: cfg!(debug_assertions),
auto_clear_threshold: None,
prewarm_capacities: vec![],
}
}
}
impl PoolConfig {
pub fn test() -> Self {
Self {
max_pool_size: 4,
enable_statistics: true,
auto_clear_threshold: Some(100),
prewarm_capacities: vec![],
}
}
pub fn production() -> Self {
Self {
max_pool_size: 32,
enable_statistics: false,
auto_clear_threshold: Some(10000),
prewarm_capacities: vec![16, 64, 256, 1024],
}
}
pub fn development() -> Self {
Self {
max_pool_size: 16,
enable_statistics: true,
auto_clear_threshold: Some(1000),
prewarm_capacities: vec![16, 64],
}
}
}
pub fn init_thread_pools(config: PoolConfig) -> Pools {
let pools = Pools::new(config.max_pool_size);
if !config.prewarm_capacities.is_empty() {
}
set_thread_pools(pools.clone());
pools
}
pub fn init_default_thread_pools() -> Pools {
init_thread_pools(PoolConfig::default())
}
pub fn init_test_pools() -> Pools {
init_thread_pools(PoolConfig::test())
}
pub fn init_production_pools() -> Pools {
init_thread_pools(PoolConfig::production())
}
pub fn init_development_pools() -> Pools {
init_thread_pools(PoolConfig::development())
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::value::column::pool::thread_local::{clear_thread_pools, has_thread_pools};
#[test]
fn test_pool_defaults() {
let config = PoolConfig::default();
assert_eq!(config.max_pool_size, 16);
assert_eq!(config.enable_statistics, cfg!(debug_assertions));
assert_eq!(config.auto_clear_threshold, None);
assert!(config.prewarm_capacities.is_empty());
}
#[test]
fn test_pool_config_test() {
let config = PoolConfig::test();
assert_eq!(config.max_pool_size, 4);
assert!(config.enable_statistics);
assert_eq!(config.auto_clear_threshold, Some(100));
assert!(config.prewarm_capacities.is_empty());
}
#[test]
fn test_pool_config_production() {
let config = PoolConfig::production();
assert_eq!(config.max_pool_size, 32);
assert!(!config.enable_statistics);
assert_eq!(config.auto_clear_threshold, Some(10000));
assert_eq!(config.prewarm_capacities, vec![16, 64, 256, 1024]);
}
#[test]
fn test_init_thread_pools() {
clear_thread_pools();
assert!(!has_thread_pools());
let pools = init_test_pools();
assert!(has_thread_pools());
clear_thread_pools();
}
#[test]
fn test_init_variations() {
clear_thread_pools();
init_default_thread_pools();
assert!(has_thread_pools());
clear_thread_pools();
init_development_pools();
assert!(has_thread_pools());
clear_thread_pools();
init_production_pools();
assert!(has_thread_pools());
clear_thread_pools();
}
}