reifydb_core/value/column/pool/
config.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4//! Pool configuration for ftokenizeible initialization
5//!
6//! This module provides configuration options for thread-local pools,
7//! allowing different configurations for test, development, and production.
8
9use super::{Pools, thread_local::set_thread_pools};
10
11/// Configuration for pool initialization
12#[derive(Clone, Debug)]
13pub struct PoolConfig {
14	/// Maximum number of containers per capacity bucket
15	pub max_pool_size: usize,
16
17	/// Enable detailed statistics tracking
18	pub enable_statistics: bool,
19
20	/// Automatically clear pools when this many containers are stored
21	pub auto_clear_threshold: Option<usize>,
22
23	/// Pre-warm pools with containers of these capacities
24	pub prewarm_capacities: Vec<usize>,
25}
26
27impl Default for PoolConfig {
28	fn default() -> Self {
29		Self {
30			max_pool_size: 16,
31			enable_statistics: cfg!(debug_assertions),
32			auto_clear_threshold: None,
33			prewarm_capacities: vec![],
34		}
35	}
36}
37
38impl PoolConfig {
39	/// Create a configuration optimized for testing
40	/// Uses smaller pools to catch memory issues faster
41	pub fn test() -> Self {
42		Self {
43			max_pool_size: 4,
44			enable_statistics: true,
45			auto_clear_threshold: Some(100),
46			prewarm_capacities: vec![],
47		}
48	}
49
50	/// Create a configuration optimized for production
51	/// Uses larger pools and pre-warming for better performance
52	pub fn production() -> Self {
53		Self {
54			max_pool_size: 32,
55			enable_statistics: false,
56			auto_clear_threshold: Some(10000),
57			prewarm_capacities: vec![16, 64, 256, 1024],
58		}
59	}
60
61	/// Create a configuration for development
62	/// Balance between test and production
63	pub fn development() -> Self {
64		Self {
65			max_pool_size: 16,
66			enable_statistics: true,
67			auto_clear_threshold: Some(1000),
68			prewarm_capacities: vec![16, 64],
69		}
70	}
71}
72
73/// Initialize thread-local pools with configuration
74pub fn init_thread_pools(config: PoolConfig) -> Pools {
75	let pools = Pools::new(config.max_pool_size);
76
77	// Pre-warm pools if requested
78	if !config.prewarm_capacities.is_empty() {
79		// TODO: Implement pre-warming logic
80		// This would acquire and immediately release containers of
81		// specified capacities to ensure the pools have containers
82		// ready for common sizes
83	}
84
85	set_thread_pools(pools.clone());
86	pools
87}
88
89/// Initialize with default configuration
90pub fn init_default_thread_pools() -> Pools {
91	init_thread_pools(PoolConfig::default())
92}
93
94/// Initialize with test configuration
95pub fn init_test_pools() -> Pools {
96	init_thread_pools(PoolConfig::test())
97}
98
99/// Initialize with production configuration
100pub fn init_production_pools() -> Pools {
101	init_thread_pools(PoolConfig::production())
102}
103
104/// Initialize with development configuration
105pub fn init_development_pools() -> Pools {
106	init_thread_pools(PoolConfig::development())
107}
108
109#[cfg(test)]
110mod tests {
111	use super::*;
112	use crate::value::column::pool::thread_local::{clear_thread_pools, has_thread_pools};
113
114	#[test]
115	fn test_pool_config_defaults() {
116		let config = PoolConfig::default();
117		assert_eq!(config.max_pool_size, 16);
118		assert_eq!(config.enable_statistics, cfg!(debug_assertions));
119		assert_eq!(config.auto_clear_threshold, None);
120		assert!(config.prewarm_capacities.is_empty());
121	}
122
123	#[test]
124	fn test_pool_config_test() {
125		let config = PoolConfig::test();
126		assert_eq!(config.max_pool_size, 4);
127		assert!(config.enable_statistics);
128		assert_eq!(config.auto_clear_threshold, Some(100));
129		assert!(config.prewarm_capacities.is_empty());
130	}
131
132	#[test]
133	fn test_pool_config_production() {
134		let config = PoolConfig::production();
135		assert_eq!(config.max_pool_size, 32);
136		assert!(!config.enable_statistics);
137		assert_eq!(config.auto_clear_threshold, Some(10000));
138		assert_eq!(config.prewarm_capacities, vec![16, 64, 256, 1024]);
139	}
140
141	#[test]
142	fn test_init_thread_pools() {
143		// Clear any existing pools
144		clear_thread_pools();
145		assert!(!has_thread_pools());
146
147		// Initialize with test config
148		let pools = init_test_pools();
149		assert!(has_thread_pools());
150
151		// Clean up
152		clear_thread_pools();
153	}
154
155	#[test]
156	fn test_init_variations() {
157		// Test each initialization variant
158		clear_thread_pools();
159
160		init_default_thread_pools();
161		assert!(has_thread_pools());
162		clear_thread_pools();
163
164		init_development_pools();
165		assert!(has_thread_pools());
166		clear_thread_pools();
167
168		init_production_pools();
169		assert!(has_thread_pools());
170		clear_thread_pools();
171	}
172}