use crate::builtin::register_builtin_functions;
use crate::extensions::ExtensionRegistry;
use std::sync::{Arc, OnceLock};
use std::thread;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ExecutionContext {
pub timeout: Option<Duration>,
pub memory_limit: Option<usize>,
pub parallel: bool,
pub parallel_config: ParallelConfig,
pub streaming: StreamingResultConfig,
pub collect_stats: bool,
pub parallel_threshold: usize,
pub enable_caching: bool,
pub extension_registry: Arc<ExtensionRegistry>,
}
#[derive(Debug, Clone)]
pub struct ParallelConfig {
pub max_threads: usize,
pub work_stealing: bool,
pub chunk_size: usize,
pub parallel_threshold: usize,
pub thread_pool_config: ThreadPoolConfig,
pub numa_aware: bool,
pub min_parallel_work: usize,
pub adaptive: bool,
}
#[derive(Debug, Clone)]
pub struct ThreadPoolConfig {
pub stack_size: Option<usize>,
pub thread_priority: Option<i32>,
pub thread_affinity: bool,
}
#[derive(Debug, Clone)]
pub struct StreamingResultConfig {
pub buffer_size: usize,
pub batch_size: usize,
pub enabled: bool,
}
static FUNCTION_REGISTRY: OnceLock<Arc<ExtensionRegistry>> = OnceLock::new();
fn get_function_registry() -> &'static Arc<ExtensionRegistry> {
FUNCTION_REGISTRY.get_or_init(|| {
let registry = Arc::<ExtensionRegistry>::new(ExtensionRegistry::new());
register_builtin_functions(®istry).expect("Failed to register built-in functions");
registry
})
}
impl Default for ExecutionContext {
fn default() -> Self {
Self {
timeout: Some(Duration::from_secs(300)), memory_limit: Some(1024 * 1024 * 1024), parallel: true,
parallel_config: ParallelConfig::default(),
streaming: StreamingResultConfig::default(),
collect_stats: false,
parallel_threshold: 1000,
enable_caching: true,
extension_registry: get_function_registry().clone(),
}
}
}
impl ExecutionContext {
pub fn new() -> Self {
Self::default()
}
}
impl Default for ParallelConfig {
fn default() -> Self {
let num_cpus = thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4);
Self {
max_threads: num_cpus,
work_stealing: true,
chunk_size: 1000,
parallel_threshold: 10000,
thread_pool_config: ThreadPoolConfig::default(),
numa_aware: false,
min_parallel_work: 100,
adaptive: true,
}
}
}
impl Default for ThreadPoolConfig {
fn default() -> Self {
Self {
stack_size: Some(8 * 1024 * 1024), thread_priority: None,
thread_affinity: false,
}
}
}
impl Default for StreamingResultConfig {
fn default() -> Self {
Self {
buffer_size: 10000,
batch_size: 1000,
enabled: false,
}
}
}