Skip to main content

oxirs_arq/executor/
config.rs

1//! Execution Configuration
2//!
3//! This module contains configuration structures for query execution.
4
5use crate::builtin::register_builtin_functions;
6use crate::extensions::ExtensionRegistry;
7use std::sync::{Arc, OnceLock};
8use std::thread;
9use std::time::Duration;
10
11/// Query execution context
12#[derive(Debug, Clone)]
13pub struct ExecutionContext {
14    /// Maximum execution time
15    pub timeout: Option<Duration>,
16    /// Memory limit in bytes
17    pub memory_limit: Option<usize>,
18    /// Enable parallel execution
19    pub parallel: bool,
20    /// Parallel execution configuration
21    pub parallel_config: ParallelConfig,
22    /// Streaming configuration
23    pub streaming: StreamingResultConfig,
24    /// Statistics collection
25    pub collect_stats: bool,
26    /// Query complexity threshold for parallel execution
27    pub parallel_threshold: usize,
28    /// Enable query result caching
29    pub enable_caching: bool,
30    /// Extension registry for functions
31    pub extension_registry: Arc<ExtensionRegistry>,
32}
33
34/// Parallel execution configuration
35#[derive(Debug, Clone)]
36pub struct ParallelConfig {
37    /// Maximum number of threads to use
38    pub max_threads: usize,
39    /// Enable work-stealing
40    pub work_stealing: bool,
41    /// Chunk size for parallel processing
42    pub chunk_size: usize,
43    /// Threshold for enabling parallel execution
44    pub parallel_threshold: usize,
45    /// Thread pool configuration
46    pub thread_pool_config: ThreadPoolConfig,
47    /// Enable NUMA-aware execution
48    pub numa_aware: bool,
49    /// Minimum work size for parallel execution
50    pub min_parallel_work: usize,
51    /// Enable adaptive parallelization
52    pub adaptive: bool,
53}
54
55/// Thread pool configuration
56#[derive(Debug, Clone)]
57pub struct ThreadPoolConfig {
58    /// Thread stack size
59    pub stack_size: Option<usize>,
60    /// Thread priority
61    pub thread_priority: Option<i32>,
62    /// Enable thread affinity
63    pub thread_affinity: bool,
64}
65
66/// Streaming result configuration
67#[derive(Debug, Clone)]
68pub struct StreamingResultConfig {
69    /// Buffer size for streaming results
70    pub buffer_size: usize,
71    /// Batch size for result processing
72    pub batch_size: usize,
73    /// Enable streaming mode
74    pub enabled: bool,
75}
76
77// Global function registry using modern Rust OnceLock
78static FUNCTION_REGISTRY: OnceLock<Arc<ExtensionRegistry>> = OnceLock::new();
79
80fn get_function_registry() -> &'static Arc<ExtensionRegistry> {
81    FUNCTION_REGISTRY.get_or_init(|| {
82        let registry = Arc::<ExtensionRegistry>::new(ExtensionRegistry::new());
83        register_builtin_functions(&registry).expect("Failed to register built-in functions");
84        registry
85    })
86}
87
88impl Default for ExecutionContext {
89    fn default() -> Self {
90        Self {
91            timeout: Some(Duration::from_secs(300)), // 5 minutes default timeout
92            memory_limit: Some(1024 * 1024 * 1024),  // 1GB default limit
93            parallel: true,
94            parallel_config: ParallelConfig::default(),
95            streaming: StreamingResultConfig::default(),
96            collect_stats: false,
97            parallel_threshold: 1000,
98            enable_caching: true,
99            extension_registry: get_function_registry().clone(),
100        }
101    }
102}
103
104impl ExecutionContext {
105    pub fn new() -> Self {
106        Self::default()
107    }
108}
109
110impl Default for ParallelConfig {
111    fn default() -> Self {
112        let num_cpus = thread::available_parallelism()
113            .map(|n| n.get())
114            .unwrap_or(4);
115
116        Self {
117            max_threads: num_cpus,
118            work_stealing: true,
119            chunk_size: 1000,
120            parallel_threshold: 10000,
121            thread_pool_config: ThreadPoolConfig::default(),
122            numa_aware: false,
123            min_parallel_work: 100,
124            adaptive: true,
125        }
126    }
127}
128
129impl Default for ThreadPoolConfig {
130    fn default() -> Self {
131        Self {
132            stack_size: Some(8 * 1024 * 1024), // 8MB stack size
133            thread_priority: None,
134            thread_affinity: false,
135        }
136    }
137}
138
139impl Default for StreamingResultConfig {
140    fn default() -> Self {
141        Self {
142            buffer_size: 10000,
143            batch_size: 1000,
144            enabled: false,
145        }
146    }
147}