oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Adaptive buffer pool tuning for automatic performance optimization
//!
//! This module provides dynamic tuning of buffer pool parameters based on
//! runtime performance metrics and access patterns.

use super::buffer_pool::BufferPoolStats;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::{Duration, Instant};

/// Adaptive tuning configuration
#[derive(Debug, Clone)]
pub struct AdaptiveTuningConfig {
    /// Minimum buffer pool size
    pub min_pool_size: usize,
    /// Maximum buffer pool size
    pub max_pool_size: usize,
    /// Target hit rate (0.0 to 1.0)
    pub target_hit_rate: f64,
    /// Tuning interval (how often to adjust)
    pub tuning_interval: Duration,
    /// Aggressive tuning mode (faster adjustments)
    pub aggressive: bool,
}

impl Default for AdaptiveTuningConfig {
    fn default() -> Self {
        Self {
            min_pool_size: 100,
            max_pool_size: 10000,
            target_hit_rate: 0.9,
            tuning_interval: Duration::from_secs(60),
            aggressive: false,
        }
    }
}

/// Adaptive buffer pool tuner
pub struct AdaptiveTuner {
    /// Configuration
    config: AdaptiveTuningConfig,
    /// Current recommended pool size
    recommended_size: AtomicUsize,
    /// Last tuning time
    last_tuning: parking_lot::Mutex<Instant>,
    /// Historical hit rates (ring buffer)
    hit_rate_history: parking_lot::Mutex<Vec<f64>>,
    /// Number of size increases
    size_increases: AtomicU64,
    /// Number of size decreases
    size_decreases: AtomicU64,
}

impl AdaptiveTuner {
    /// Create a new adaptive tuner
    pub fn new(config: AdaptiveTuningConfig) -> Self {
        let initial_size = (config.min_pool_size + config.max_pool_size) / 2;

        Self {
            config,
            recommended_size: AtomicUsize::new(initial_size),
            last_tuning: parking_lot::Mutex::new(Instant::now()),
            hit_rate_history: parking_lot::Mutex::new(Vec::with_capacity(10)),
            size_increases: AtomicU64::new(0),
            size_decreases: AtomicU64::new(0),
        }
    }

    /// Analyze current performance and adjust recommendations
    pub fn tune(&self, stats: &BufferPoolStats) -> TuningRecommendation {
        // Check if it's time to tune
        let mut last_tuning = self.last_tuning.lock();
        if last_tuning.elapsed() < self.config.tuning_interval {
            return TuningRecommendation::NoChange;
        }

        let current_hit_rate = stats.hit_rate();
        let current_size = self.recommended_size.load(Ordering::Relaxed);

        // Record hit rate history
        let mut history = self.hit_rate_history.lock();
        history.push(current_hit_rate);
        if history.len() > 10 {
            history.remove(0);
        }

        // Calculate trend
        let trend = self.calculate_trend(&history);

        // Determine action based on hit rate and trend
        let recommendation = if current_hit_rate < self.config.target_hit_rate {
            // Hit rate is below target - consider increasing pool size
            if current_size < self.config.max_pool_size {
                let increase = self.calculate_size_adjustment(current_size, true);
                let new_size = (current_size + increase).min(self.config.max_pool_size);

                self.recommended_size.store(new_size, Ordering::Relaxed);
                self.size_increases.fetch_add(1, Ordering::Relaxed);

                TuningRecommendation::IncreaseSize {
                    old_size: current_size,
                    new_size,
                    reason: TuningReason::LowHitRate {
                        current: current_hit_rate,
                        target: self.config.target_hit_rate,
                    },
                }
            } else {
                TuningRecommendation::AtMaximum {
                    hit_rate: current_hit_rate,
                }
            }
        } else if current_hit_rate > self.config.target_hit_rate + 0.05 && trend > 0.0 {
            // Hit rate is above target with improving trend - consider decreasing
            if current_size > self.config.min_pool_size {
                let decrease = self.calculate_size_adjustment(current_size, false);
                let new_size = current_size
                    .saturating_sub(decrease)
                    .max(self.config.min_pool_size);

                self.recommended_size.store(new_size, Ordering::Relaxed);
                self.size_decreases.fetch_add(1, Ordering::Relaxed);

                TuningRecommendation::DecreaseSize {
                    old_size: current_size,
                    new_size,
                    reason: TuningReason::HighHitRate {
                        current: current_hit_rate,
                        target: self.config.target_hit_rate,
                    },
                }
            } else {
                TuningRecommendation::AtMinimum {
                    hit_rate: current_hit_rate,
                }
            }
        } else {
            TuningRecommendation::NoChange
        };

        *last_tuning = Instant::now();
        recommendation
    }

    /// Calculate size adjustment amount
    fn calculate_size_adjustment(&self, current_size: usize, increase: bool) -> usize {
        if self.config.aggressive {
            // Aggressive: 20% adjustment
            current_size / 5
        } else {
            // Conservative: 10% adjustment
            current_size / 10
        }
        .max(1)
    }

    /// Calculate trend from history (positive = improving, negative = declining)
    fn calculate_trend(&self, history: &[f64]) -> f64 {
        if history.len() < 2 {
            return 0.0;
        }

        // Simple linear regression slope
        let n = history.len() as f64;
        let sum_x: f64 = (0..history.len()).map(|i| i as f64).sum();
        let sum_y: f64 = history.iter().sum();
        let sum_xy: f64 = history.iter().enumerate().map(|(i, &y)| i as f64 * y).sum();
        let sum_x_sq: f64 = (0..history.len()).map(|i| (i as f64).powi(2)).sum();

        (n * sum_xy - sum_x * sum_y) / (n * sum_x_sq - sum_x.powi(2))
    }

    /// Get current recommended size
    pub fn recommended_size(&self) -> usize {
        self.recommended_size.load(Ordering::Relaxed)
    }

    /// Get tuning statistics
    pub fn stats(&self) -> AdaptiveTuningStats {
        AdaptiveTuningStats {
            current_size: self.recommended_size.load(Ordering::Relaxed),
            size_increases: self.size_increases.load(Ordering::Relaxed),
            size_decreases: self.size_decreases.load(Ordering::Relaxed),
            hit_rate_history: self.hit_rate_history.lock().clone(),
        }
    }
}

/// Tuning recommendation
#[derive(Debug, Clone)]
pub enum TuningRecommendation {
    /// No change needed
    NoChange,
    /// Increase buffer pool size
    IncreaseSize {
        /// Old size
        old_size: usize,
        /// New recommended size
        new_size: usize,
        /// Reason for increase
        reason: TuningReason,
    },
    /// Decrease buffer pool size
    DecreaseSize {
        /// Old size
        old_size: usize,
        /// New recommended size
        new_size: usize,
        /// Reason for decrease
        reason: TuningReason,
    },
    /// Already at maximum size
    AtMaximum {
        /// Current hit rate
        hit_rate: f64,
    },
    /// Already at minimum size
    AtMinimum {
        /// Current hit rate
        hit_rate: f64,
    },
}

/// Reason for tuning adjustment
#[derive(Debug, Clone)]
pub enum TuningReason {
    /// Hit rate is below target
    LowHitRate {
        /// Current hit rate
        current: f64,
        /// Target hit rate
        target: f64,
    },
    /// Hit rate is above target
    HighHitRate {
        /// Current hit rate
        current: f64,
        /// Target hit rate
        target: f64,
    },
}

/// Adaptive tuning statistics
#[derive(Debug, Clone)]
pub struct AdaptiveTuningStats {
    /// Current recommended size
    pub current_size: usize,
    /// Number of times size was increased
    pub size_increases: u64,
    /// Number of times size was decreased
    pub size_decreases: u64,
    /// Historical hit rates
    pub hit_rate_history: Vec<f64>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicU64;

    fn create_test_stats(hit_rate: f64) -> BufferPoolStats {
        let stats = BufferPoolStats::default();
        let total = 1000;
        let hits = (total as f64 * hit_rate) as u64;

        stats.total_fetches.store(total, Ordering::Relaxed);
        stats.cache_hits.store(hits, Ordering::Relaxed);
        stats.cache_misses.store(total - hits, Ordering::Relaxed);

        stats
    }

    #[test]
    fn test_adaptive_tuner_creation() {
        let config = AdaptiveTuningConfig::default();
        let tuner = AdaptiveTuner::new(config);

        let initial_size = tuner.recommended_size();
        assert!(initial_size >= 100);
        assert!(initial_size <= 10000);
    }

    #[test]
    fn test_low_hit_rate_increases_size() {
        let config = AdaptiveTuningConfig {
            tuning_interval: Duration::from_millis(1),
            ..Default::default()
        };
        let tuner = AdaptiveTuner::new(config);

        std::thread::sleep(Duration::from_millis(10));

        let stats = create_test_stats(0.5); // 50% hit rate (below 90% target)
        let recommendation = tuner.tune(&stats);

        match recommendation {
            TuningRecommendation::IncreaseSize {
                old_size, new_size, ..
            } => {
                assert!(new_size > old_size);
            }
            _ => panic!("Expected IncreaseSize recommendation"),
        }
    }

    #[test]
    fn test_high_hit_rate_decreases_size() {
        let config = AdaptiveTuningConfig {
            tuning_interval: Duration::from_millis(1),
            ..Default::default()
        };
        let tuner = AdaptiveTuner::new(config);

        // Build up history with improving hit rates
        std::thread::sleep(Duration::from_millis(10));
        for hit_rate in &[0.92, 0.93, 0.94, 0.95, 0.96] {
            let stats = create_test_stats(*hit_rate);
            tuner.tune(&stats);
            std::thread::sleep(Duration::from_millis(10));
        }

        let stats = create_test_stats(0.97); // Very high hit rate
        let recommendation = tuner.tune(&stats);

        match recommendation {
            TuningRecommendation::DecreaseSize {
                old_size, new_size, ..
            } => {
                assert!(new_size < old_size);
            }
            TuningRecommendation::NoChange => {
                // Also acceptable if trend isn't strong enough
            }
            _ => {}
        }
    }

    #[test]
    fn test_tuning_interval_respected() {
        let config = AdaptiveTuningConfig {
            tuning_interval: Duration::from_millis(100),
            ..Default::default()
        };
        let tuner = AdaptiveTuner::new(config);

        let stats = create_test_stats(0.5);

        // Wait for tuning interval to elapse
        std::thread::sleep(Duration::from_millis(150));
        // First call should trigger tuning
        let rec1 = tuner.tune(&stats);
        assert!(!matches!(rec1, TuningRecommendation::NoChange));

        // Immediate second call should not trigger tuning
        let rec2 = tuner.tune(&stats);
        assert!(matches!(rec2, TuningRecommendation::NoChange));
    }

    #[test]
    fn test_size_bounds_respected() {
        let config = AdaptiveTuningConfig {
            min_pool_size: 100,
            max_pool_size: 200,
            target_hit_rate: 0.9,
            tuning_interval: Duration::from_millis(1),
            aggressive: true,
        };
        let tuner = AdaptiveTuner::new(config);

        // Try to push size to maximum
        for _ in 0..10 {
            std::thread::sleep(Duration::from_millis(10));
            let stats = create_test_stats(0.5); // Low hit rate
            tuner.tune(&stats);
        }

        let final_size = tuner.recommended_size();
        assert!(final_size <= 200);
        assert!(final_size >= 100);
    }

    #[test]
    fn test_aggressive_vs_conservative() {
        let conservative = AdaptiveTuningConfig {
            aggressive: false,
            tuning_interval: Duration::from_millis(1),
            ..Default::default()
        };
        let aggressive = AdaptiveTuningConfig {
            aggressive: true,
            tuning_interval: Duration::from_millis(1),
            ..Default::default()
        };

        let tuner_cons = AdaptiveTuner::new(conservative);
        let tuner_aggr = AdaptiveTuner::new(aggressive);

        std::thread::sleep(Duration::from_millis(10));

        let stats = create_test_stats(0.5);

        let rec_cons = tuner_cons.tune(&stats);
        let rec_aggr = tuner_aggr.tune(&stats);

        // Aggressive should make larger adjustments
        if let (
            TuningRecommendation::IncreaseSize {
                old_size: old1,
                new_size: new1,
                ..
            },
            TuningRecommendation::IncreaseSize {
                old_size: old2,
                new_size: new2,
                ..
            },
        ) = (rec_cons, rec_aggr)
        {
            let increase_cons = new1 - old1;
            let increase_aggr = new2 - old2;
            assert!(increase_aggr >= increase_cons);
        }
    }

    #[test]
    fn test_tuning_stats() {
        let config = AdaptiveTuningConfig {
            tuning_interval: Duration::from_millis(1),
            ..Default::default()
        };
        let tuner = AdaptiveTuner::new(config);

        std::thread::sleep(Duration::from_millis(10));
        let stats = create_test_stats(0.5);
        tuner.tune(&stats);

        let tuning_stats = tuner.stats();
        assert!(tuning_stats.size_increases > 0 || tuning_stats.size_decreases > 0);
    }
}