1use std::path::PathBuf;
4
5#[derive(Debug, Clone)]
7pub struct Config {
8 pub path: Option<PathBuf>,
10
11 pub memory_limit: Option<usize>,
13
14 pub spill_path: Option<PathBuf>,
16
17 pub threads: usize,
19
20 pub wal_enabled: bool,
22
23 pub wal_flush_interval_ms: u64,
25
26 pub backward_edges: bool,
28
29 pub query_logging: bool,
31
32 pub adaptive: AdaptiveConfig,
34}
35
36#[derive(Debug, Clone)]
41pub struct AdaptiveConfig {
42 pub enabled: bool,
44
45 pub threshold: f64,
50
51 pub min_rows: u64,
55
56 pub max_reoptimizations: usize,
58}
59
60impl Default for AdaptiveConfig {
61 fn default() -> Self {
62 Self {
63 enabled: true,
64 threshold: 3.0,
65 min_rows: 1000,
66 max_reoptimizations: 3,
67 }
68 }
69}
70
71impl AdaptiveConfig {
72 #[must_use]
74 pub fn disabled() -> Self {
75 Self {
76 enabled: false,
77 ..Default::default()
78 }
79 }
80
81 #[must_use]
83 pub fn with_threshold(mut self, threshold: f64) -> Self {
84 self.threshold = threshold;
85 self
86 }
87
88 #[must_use]
90 pub fn with_min_rows(mut self, min_rows: u64) -> Self {
91 self.min_rows = min_rows;
92 self
93 }
94
95 #[must_use]
97 pub fn with_max_reoptimizations(mut self, max: usize) -> Self {
98 self.max_reoptimizations = max;
99 self
100 }
101}
102
103impl Default for Config {
104 fn default() -> Self {
105 Self {
106 path: None,
107 memory_limit: None,
108 spill_path: None,
109 threads: num_cpus::get(),
110 wal_enabled: true,
111 wal_flush_interval_ms: 100,
112 backward_edges: true,
113 query_logging: false,
114 adaptive: AdaptiveConfig::default(),
115 }
116 }
117}
118
119impl Config {
120 #[must_use]
122 pub fn in_memory() -> Self {
123 Self {
124 path: None,
125 wal_enabled: false,
126 ..Default::default()
127 }
128 }
129
130 #[must_use]
132 pub fn persistent(path: impl Into<PathBuf>) -> Self {
133 Self {
134 path: Some(path.into()),
135 wal_enabled: true,
136 ..Default::default()
137 }
138 }
139
140 #[must_use]
142 pub fn with_memory_limit(mut self, limit: usize) -> Self {
143 self.memory_limit = Some(limit);
144 self
145 }
146
147 #[must_use]
149 pub fn with_threads(mut self, threads: usize) -> Self {
150 self.threads = threads;
151 self
152 }
153
154 #[must_use]
156 pub fn without_backward_edges(mut self) -> Self {
157 self.backward_edges = false;
158 self
159 }
160
161 #[must_use]
163 pub fn with_query_logging(mut self) -> Self {
164 self.query_logging = true;
165 self
166 }
167
168 #[must_use]
170 pub fn with_memory_fraction(mut self, fraction: f64) -> Self {
171 use grafeo_common::memory::buffer::BufferManagerConfig;
172 let system_memory = BufferManagerConfig::detect_system_memory();
173 self.memory_limit = Some((system_memory as f64 * fraction) as usize);
174 self
175 }
176
177 #[must_use]
179 pub fn with_spill_path(mut self, path: impl Into<PathBuf>) -> Self {
180 self.spill_path = Some(path.into());
181 self
182 }
183
184 #[must_use]
186 pub fn with_adaptive(mut self, adaptive: AdaptiveConfig) -> Self {
187 self.adaptive = adaptive;
188 self
189 }
190
191 #[must_use]
193 pub fn without_adaptive(mut self) -> Self {
194 self.adaptive.enabled = false;
195 self
196 }
197}
198
199mod num_cpus {
201 pub fn get() -> usize {
202 std::thread::available_parallelism()
203 .map(|n| n.get())
204 .unwrap_or(4)
205 }
206}