oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! Advanced SPARQL query optimization system
//!
//! This module provides sophisticated query optimization capabilities including:
//! - Cost-based query optimization with statistics
//! - Join order optimization using dynamic programming
//! - Index-aware query rewriting and plan selection
//! - Parallel query execution with work-stealing
//! - Query plan caching and adaptive optimization
//! - Cardinality estimation and selectivity analysis

use crate::{
    config::PerformanceConfig,
    error::{FusekiError, FusekiResult},
    store::Store,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{RwLock, Semaphore};
use tracing::{debug, info, instrument};

/// Query optimization service with advanced algorithms
#[derive(Clone)]
pub struct QueryOptimizer {
    config: Arc<PerformanceConfig>,
    statistics: Arc<RwLock<DatabaseStatistics>>,
    plan_cache: Arc<RwLock<HashMap<String, OptimizedQueryPlan>>>,
    cost_model: Arc<CostModel>,
    execution_engine: Arc<ParallelExecutionEngine>,
}

/// Database statistics for cost-based optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseStatistics {
    pub total_triples: u64,
    pub total_graphs: u32,
    pub predicate_stats: HashMap<String, PredicateStatistics>,
    pub graph_stats: HashMap<String, GraphStatistics>,
    pub index_stats: HashMap<String, IndexStatistics>,
    pub last_updated: std::time::SystemTime,
}

/// Predicate-specific statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredicateStatistics {
    pub frequency: u64,
    pub selectivity: f64,
    pub distinct_subjects: u64,
    pub distinct_objects: u64,
    pub avg_subject_fanout: f64,
    pub avg_object_fanout: f64,
}

/// Graph-specific statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStatistics {
    pub triple_count: u64,
    pub predicate_count: u32,
    pub subject_count: u64,
    pub object_count: u64,
    pub avg_outdegree: f64,
}

/// Index statistics for optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexStatistics {
    pub index_type: String,
    pub size_bytes: u64,
    pub access_cost: f64,
    pub selectivity: f64,
    pub last_access: std::time::SystemTime,
}

/// Optimized query execution plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizedQueryPlan {
    pub plan_id: String,
    pub original_query: String,
    pub optimized_query: String,
    pub execution_steps: Vec<ExecutionStep>,
    pub estimated_cost: f64,
    pub estimated_cardinality: u64,
    pub optimization_hints: Vec<OptimizationHint>,
    pub parallel_segments: Vec<ParallelSegment>,
    pub created_at: std::time::SystemTime,
    pub hit_count: u64,
}

/// Individual execution step in query plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionStep {
    pub step_id: u32,
    pub operation: String,
    pub estimated_cost: f64,
    pub estimated_rows: u64,
    pub dependencies: Vec<u32>,
    pub can_parallelize: bool,
    pub index_hints: Vec<String>,
}

/// Query optimization hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationHint {
    pub hint_type: String,
    pub description: String,
    pub confidence: f64,
    pub estimated_improvement: f64,
}

/// Parallel execution segment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParallelSegment {
    pub segment_id: u32,
    pub operations: Vec<u32>,
    pub estimated_parallelism: u32,
    pub merge_strategy: String,
}

/// Cost model for query optimization
#[derive(Debug)]
pub struct CostModel {
    pub triple_access_cost: f64,
    pub index_access_cost: f64,
    pub join_cost_factor: f64,
    pub sort_cost_factor: f64,
    pub network_cost_factor: f64,
    pub memory_cost_factor: f64,
}

/// Parallel execution engine
#[derive(Debug)]
pub struct ParallelExecutionEngine {
    pub max_parallelism: usize,
    pub work_queue: Arc<RwLock<Vec<WorkItem>>>,
    pub execution_semaphore: Arc<Semaphore>,
    pub completion_tracker: Arc<RwLock<HashMap<String, ExecutionStatus>>>,
}

/// Work item for parallel execution
#[derive(Debug, Clone)]
pub struct WorkItem {
    pub item_id: String,
    pub operation: String,
    pub priority: u32,
    pub estimated_cost: f64,
    pub dependencies: Vec<String>,
    pub created_at: Instant,
}

/// Execution status tracking
#[derive(Debug, Clone)]
pub struct ExecutionStatus {
    pub status: String,
    pub progress: f64,
    pub start_time: Instant,
    pub estimated_completion: Option<Instant>,
}

/// Parameters for query complexity calculation
#[derive(Debug, Clone)]
pub struct QueryComplexityParams<'a> {
    pub triple_patterns: &'a [TriplePattern],
    pub join_count: u32,
    pub filter_count: u32,
    pub has_aggregation: bool,
    pub has_subqueries: bool,
    pub has_optional: bool,
    pub has_union: bool,
}

/// Query analysis result
#[derive(Debug)]
pub struct QueryAnalysis {
    pub query_complexity: f64,
    pub join_count: u32,
    pub filter_count: u32,
    pub triple_patterns: Vec<TriplePattern>,
    pub has_aggregation: bool,
    pub has_subqueries: bool,
    pub has_optional: bool,
    pub has_union: bool,
    pub estimated_cardinality: u64,
}

/// Triple pattern for analysis
#[derive(Debug, Clone)]
pub struct TriplePattern {
    pub subject: String,
    pub predicate: String,
    pub object: String,
    pub graph: Option<String>,
    pub is_bound: (bool, bool, bool),
    pub estimated_selectivity: f64,
}

impl QueryOptimizer {
    /// Create new query optimizer
    pub fn new(config: PerformanceConfig) -> FusekiResult<Self> {
        let statistics = Arc::new(RwLock::new(DatabaseStatistics::default()));
        let plan_cache = Arc::new(RwLock::new(HashMap::new()));

        let cost_model = Arc::new(CostModel {
            triple_access_cost: 1.0,
            index_access_cost: 0.1,
            join_cost_factor: 2.0,
            sort_cost_factor: 1.5,
            network_cost_factor: 10.0,
            memory_cost_factor: 0.5,
        });

        let execution_engine = Arc::new(ParallelExecutionEngine {
            max_parallelism: config.query_optimization.thread_pool_size,
            work_queue: Arc::new(RwLock::new(Vec::new())),
            execution_semaphore: Arc::new(Semaphore::new(
                config.query_optimization.thread_pool_size,
            )),
            completion_tracker: Arc::new(RwLock::new(HashMap::new())),
        });

        Ok(Self {
            config: Arc::new(config),
            statistics,
            plan_cache,
            cost_model,
            execution_engine,
        })
    }

    /// Optimize SPARQL query with advanced techniques
    #[instrument(skip(self, store))]
    pub async fn optimize_query(
        &self,
        query: &str,
        store: &Store,
        dataset: &str,
    ) -> FusekiResult<OptimizedQueryPlan> {
        let plan_id = self.generate_plan_id(query);

        // Check plan cache first
        if let Some(cached_plan) = self.get_cached_plan(&plan_id).await {
            info!("Using cached optimization plan: {}", plan_id);
            return Ok(cached_plan);
        }

        debug!("Starting query optimization for: {}", plan_id);

        // 1. Analyze query structure
        let analysis = self.analyze_query(query).await?;

        // 2. Get fresh database statistics
        self.update_statistics(store, dataset).await?;
        let stats = self.statistics.read().await;

        // 3. Generate multiple candidate plans
        let candidate_plans = self
            .generate_candidate_plans(query, &analysis, &stats)
            .await?;

        // 4. Cost-based plan selection
        let best_plan = self.select_best_plan(candidate_plans, &stats).await?;

        // 5. Apply index-aware optimizations
        let optimized_plan = self.apply_index_optimizations(best_plan, &stats).await?;

        // 6. Generate parallel execution strategy
        let final_plan = self.generate_parallel_strategy(optimized_plan).await?;

        // 7. Cache the optimized plan
        self.cache_plan(plan_id.clone(), final_plan.clone()).await;

        info!("Query optimization completed for: {}", plan_id);
        Ok(final_plan)
    }

    /// Analyze query structure and complexity
    #[instrument(skip(self))]
    async fn analyze_query(&self, query: &str) -> FusekiResult<QueryAnalysis> {
        debug!("Analyzing query structure");

        let query_lower = query.to_lowercase();
        let join_count = query_lower.matches("join").count() as u32;
        let filter_count = query_lower.matches("filter").count() as u32;

        // Extract triple patterns (simplified analysis)
        let triple_patterns = self.extract_triple_patterns(query).await?;

        // Analyze query features
        let has_aggregation = query_lower.contains("group by")
            || query_lower.contains("count(")
            || query_lower.contains("sum(")
            || query_lower.contains("avg(");

        let has_subqueries =
            query_lower.contains("select") && query_lower.matches("select").count() > 1;

        let has_optional = query_lower.contains("optional");
        let has_union = query_lower.contains("union");

        // Calculate complexity score
        let params = QueryComplexityParams {
            triple_patterns: &triple_patterns,
            join_count,
            filter_count,
            has_aggregation,
            has_subqueries,
            has_optional,
            has_union,
        };
        let complexity = self.calculate_query_complexity(&params);

        // Estimate cardinality
        let estimated_cardinality = self.estimate_query_cardinality(&triple_patterns).await;

        Ok(QueryAnalysis {
            query_complexity: complexity,
            join_count,
            filter_count,
            triple_patterns,
            has_aggregation,
            has_subqueries,
            has_optional,
            has_union,
            estimated_cardinality,
        })
    }

    /// Generate multiple candidate execution plans
    #[instrument(skip(self, stats))]
    async fn generate_candidate_plans(
        &self,
        query: &str,
        analysis: &QueryAnalysis,
        stats: &DatabaseStatistics,
    ) -> FusekiResult<Vec<OptimizedQueryPlan>> {
        debug!("Generating candidate plans");

        let mut plans = Vec::new();

        // Plan 1: Left-to-right join order
        let plan1 = self
            .create_left_to_right_plan(query, analysis, stats)
            .await?;
        plans.push(plan1);

        // Plan 2: Optimal join order using dynamic programming
        if analysis.join_count <= 10 {
            // Only for reasonably sized queries
            let plan2 = self
                .create_optimal_join_plan(query, analysis, stats)
                .await?;
            plans.push(plan2);
        }

        // Plan 3: Index-optimized plan
        let plan3 = self
            .create_index_optimized_plan(query, analysis, stats)
            .await?;
        plans.push(plan3);

        // Plan 4: Parallel-first plan
        if analysis.query_complexity > 5.0 {
            let plan4 = self
                .create_parallel_first_plan(query, analysis, stats)
                .await?;
            plans.push(plan4);
        }

        debug!("Generated {} candidate plans", plans.len());
        Ok(plans)
    }

    /// Select best plan using cost-based optimization
    #[instrument(skip(self, plans, stats))]
    async fn select_best_plan(
        &self,
        plans: Vec<OptimizedQueryPlan>,
        stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        debug!("Selecting best plan from {} candidates", plans.len());

        let mut best_plan = None;
        let mut best_cost = f64::INFINITY;

        for plan in plans {
            let cost = self.calculate_plan_cost(&plan, stats).await;
            debug!("Plan {} cost: {:.2}", plan.plan_id, cost);

            if cost < best_cost {
                best_cost = cost;
                best_plan = Some(plan);
            }
        }

        best_plan.ok_or_else(|| FusekiError::internal("No valid execution plan found"))
    }

    /// Apply index-aware optimizations
    #[instrument(skip(self, plan, stats))]
    async fn apply_index_optimizations(
        &self,
        mut plan: OptimizedQueryPlan,
        stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        debug!("Applying index optimizations");

        // Analyze available indexes
        for step in &mut plan.execution_steps {
            if step.operation.contains("triple_pattern") {
                let best_index = self.find_best_index_for_step(step, stats).await;
                if let Some(index) = best_index {
                    step.index_hints.push(index);
                    step.estimated_cost *= 0.1; // Index access is much faster
                }
            }
        }

        // Add index optimization hints
        let index_hint = OptimizationHint {
            hint_type: "INDEX_OPTIMIZATION".to_string(),
            description: "Applied index-aware optimizations".to_string(),
            confidence: 0.9,
            estimated_improvement: 0.8,
        };
        plan.optimization_hints.push(index_hint);

        Ok(plan)
    }

    /// Generate parallel execution strategy
    #[instrument(skip(self, plan))]
    async fn generate_parallel_strategy(
        &self,
        mut plan: OptimizedQueryPlan,
    ) -> FusekiResult<OptimizedQueryPlan> {
        debug!("Generating parallel execution strategy");

        // Identify parallelizable operations
        let parallelizable_ops: Vec<u32> = plan
            .execution_steps
            .iter()
            .filter(|step| step.can_parallelize)
            .map(|step| step.step_id)
            .collect();

        if parallelizable_ops.len() > 1 {
            // Create parallel segments
            let chunk_size =
                (parallelizable_ops.len() / self.execution_engine.max_parallelism).max(1);

            for (segment_id, chunk) in parallelizable_ops.chunks(chunk_size).enumerate() {
                let segment = ParallelSegment {
                    segment_id: segment_id as u32,
                    operations: chunk.to_vec(),
                    estimated_parallelism: chunk.len().min(self.execution_engine.max_parallelism)
                        as u32,
                    merge_strategy: "UNION_ALL".to_string(),
                };
                plan.parallel_segments.push(segment);
            }

            // Add parallelization hint
            let parallel_hint = OptimizationHint {
                hint_type: "PARALLELIZATION".to_string(),
                description: format!("Created {} parallel segments", plan.parallel_segments.len()),
                confidence: 0.8,
                estimated_improvement: 0.6,
            };
            plan.optimization_hints.push(parallel_hint);
        }

        Ok(plan)
    }

    /// Update database statistics for cost estimation
    #[instrument(skip(self, _store))]
    async fn update_statistics(&self, _store: &Store, dataset: &str) -> FusekiResult<()> {
        debug!("Updating database statistics for dataset: {}", dataset);

        let mut stats = self.statistics.write().await;

        // Mock statistics update - in real implementation would query the store
        stats.total_triples = 1000000; // Example value
        stats.total_graphs = 10;
        stats.last_updated = std::time::SystemTime::now();

        // Update predicate statistics
        stats.predicate_stats.insert(
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string(),
            PredicateStatistics {
                frequency: 100000,
                selectivity: 0.1,
                distinct_subjects: 50000,
                distinct_objects: 1000,
                avg_subject_fanout: 2.0,
                avg_object_fanout: 100.0,
            },
        );

        Ok(())
    }

    /// Calculate query complexity score
    fn calculate_query_complexity(&self, params: &QueryComplexityParams) -> f64 {
        let mut complexity = params.triple_patterns.len() as f64;
        complexity += params.join_count as f64 * 2.0;
        complexity += params.filter_count as f64 * 0.5;

        if params.has_aggregation {
            complexity += 3.0;
        }
        if params.has_subqueries {
            complexity += 5.0;
        }
        if params.has_optional {
            complexity += 2.0;
        }
        if params.has_union {
            complexity += 1.5;
        }

        complexity
    }

    /// Extract triple patterns from query (simplified)
    async fn extract_triple_patterns(&self, _query: &str) -> FusekiResult<Vec<TriplePattern>> {
        // Simplified pattern extraction - in real implementation would use SPARQL parser
        let patterns = vec![TriplePattern {
            subject: "?s".to_string(),
            predicate: "?p".to_string(),
            object: "?o".to_string(),
            graph: None,
            is_bound: (false, false, false),
            estimated_selectivity: 0.1,
        }];
        Ok(patterns)
    }

    /// Estimate query cardinality
    async fn estimate_query_cardinality(&self, patterns: &[TriplePattern]) -> u64 {
        // Simplified cardinality estimation
        let base_cardinality = 1000u64;
        patterns
            .iter()
            .map(|p| (base_cardinality as f64 * p.estimated_selectivity) as u64)
            .sum()
    }

    // Helper methods for creating different plan types
    async fn create_left_to_right_plan(
        &self,
        query: &str,
        analysis: &QueryAnalysis,
        _stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        Ok(OptimizedQueryPlan {
            plan_id: format!("left_to_right_{}", self.generate_plan_id(query)),
            original_query: query.to_string(),
            optimized_query: query.to_string(),
            execution_steps: vec![],
            estimated_cost: analysis.query_complexity * 10.0,
            estimated_cardinality: analysis.estimated_cardinality,
            optimization_hints: vec![],
            parallel_segments: vec![],
            created_at: std::time::SystemTime::now(),
            hit_count: 0,
        })
    }

    async fn create_optimal_join_plan(
        &self,
        query: &str,
        analysis: &QueryAnalysis,
        _stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        Ok(OptimizedQueryPlan {
            plan_id: format!("optimal_join_{}", self.generate_plan_id(query)),
            original_query: query.to_string(),
            optimized_query: query.to_string(),
            execution_steps: vec![],
            estimated_cost: analysis.query_complexity * 5.0, // Better than left-to-right
            estimated_cardinality: analysis.estimated_cardinality / 2,
            optimization_hints: vec![],
            parallel_segments: vec![],
            created_at: std::time::SystemTime::now(),
            hit_count: 0,
        })
    }

    async fn create_index_optimized_plan(
        &self,
        query: &str,
        analysis: &QueryAnalysis,
        _stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        Ok(OptimizedQueryPlan {
            plan_id: format!("index_optimized_{}", self.generate_plan_id(query)),
            original_query: query.to_string(),
            optimized_query: query.to_string(),
            execution_steps: vec![],
            estimated_cost: analysis.query_complexity * 2.0, // Much better with indexes
            estimated_cardinality: analysis.estimated_cardinality,
            optimization_hints: vec![],
            parallel_segments: vec![],
            created_at: std::time::SystemTime::now(),
            hit_count: 0,
        })
    }

    async fn create_parallel_first_plan(
        &self,
        query: &str,
        analysis: &QueryAnalysis,
        _stats: &DatabaseStatistics,
    ) -> FusekiResult<OptimizedQueryPlan> {
        Ok(OptimizedQueryPlan {
            plan_id: format!("parallel_first_{}", self.generate_plan_id(query)),
            original_query: query.to_string(),
            optimized_query: query.to_string(),
            execution_steps: vec![],
            estimated_cost: analysis.query_complexity * 3.0,
            estimated_cardinality: analysis.estimated_cardinality,
            optimization_hints: vec![],
            parallel_segments: vec![],
            created_at: std::time::SystemTime::now(),
            hit_count: 0,
        })
    }

    async fn calculate_plan_cost(
        &self,
        plan: &OptimizedQueryPlan,
        _stats: &DatabaseStatistics,
    ) -> f64 {
        plan.estimated_cost
    }

    async fn find_best_index_for_step(
        &self,
        _step: &ExecutionStep,
        _stats: &DatabaseStatistics,
    ) -> Option<String> {
        Some("SPO_INDEX".to_string())
    }

    /// Cache management methods
    async fn get_cached_plan(&self, plan_id: &str) -> Option<OptimizedQueryPlan> {
        let cache = self.plan_cache.read().await;
        cache.get(plan_id).cloned()
    }

    async fn cache_plan(&self, plan_id: String, plan: OptimizedQueryPlan) {
        let mut cache = self.plan_cache.write().await;
        cache.insert(plan_id, plan);
    }

    fn generate_plan_id(&self, query: &str) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        query.hash(&mut hasher);
        format!("plan_{:x}", hasher.finish())
    }

    /// Get optimization statistics
    pub async fn get_optimization_stats(&self) -> HashMap<String, serde_json::Value> {
        let mut stats = HashMap::new();

        let plan_cache = self.plan_cache.read().await;
        stats.insert(
            "cached_plans".to_string(),
            serde_json::json!(plan_cache.len()),
        );

        let db_stats = self.statistics.read().await;
        stats.insert(
            "total_triples".to_string(),
            serde_json::json!(db_stats.total_triples),
        );
        stats.insert(
            "indexed_predicates".to_string(),
            serde_json::json!(db_stats.predicate_stats.len()),
        );

        stats
    }

    /// Get all cached query plans
    pub async fn get_cached_plans(&self) -> Vec<OptimizedQueryPlan> {
        let plan_cache = self.plan_cache.read().await;
        plan_cache.values().cloned().collect()
    }

    /// Clear the optimization plan cache
    pub async fn clear_plan_cache(&self) -> usize {
        let mut plan_cache = self.plan_cache.write().await;
        let count = plan_cache.len();
        plan_cache.clear();
        info!("Cleared {} cached query plans", count);
        count
    }

    /// Get detailed database statistics
    pub async fn get_database_statistics(&self) -> DatabaseStatistics {
        self.statistics.read().await.clone()
    }
}

impl Default for DatabaseStatistics {
    fn default() -> Self {
        Self {
            total_triples: 0,
            total_graphs: 0,
            predicate_stats: HashMap::new(),
            graph_stats: HashMap::new(),
            index_stats: HashMap::new(),
            last_updated: std::time::SystemTime::now(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{CacheConfig, ConnectionPoolConfig, QueryOptimizationConfig};

    fn create_test_optimizer() -> QueryOptimizer {
        let config = PerformanceConfig {
            caching: CacheConfig {
                enabled: true,
                max_size: 100,
                ttl_secs: 300,
                query_cache_enabled: true,
                result_cache_enabled: true,
                plan_cache_enabled: true,
            },
            query_optimization: QueryOptimizationConfig {
                enabled: true,
                max_query_time_secs: 300,
                max_result_size: 1000000,
                parallel_execution: true,
                thread_pool_size: 4,
            },
            connection_pool: ConnectionPoolConfig {
                min_connections: 1,
                max_connections: 5,
                connection_timeout_secs: 30,
                idle_timeout_secs: 300,
                max_lifetime_secs: 3600,
            },
            rate_limiting: None,
        };

        QueryOptimizer::new(config).unwrap()
    }

    #[tokio::test]
    async fn test_query_analysis() {
        let optimizer = create_test_optimizer();
        let query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . ?s rdf:type ?type }";

        let analysis = optimizer.analyze_query(query).await.unwrap();
        assert!(analysis.query_complexity > 0.0);
        assert!(!analysis.has_aggregation);
    }

    #[tokio::test]
    async fn test_plan_caching() {
        let optimizer = create_test_optimizer();
        let query = "SELECT * WHERE { ?s ?p ?o }";
        let plan_id = optimizer.generate_plan_id(query);

        // Should not be cached initially
        assert!(optimizer.get_cached_plan(&plan_id).await.is_none());

        // Create and cache a plan
        let plan = OptimizedQueryPlan {
            plan_id: plan_id.clone(),
            original_query: query.to_string(),
            optimized_query: query.to_string(),
            execution_steps: vec![],
            estimated_cost: 10.0,
            estimated_cardinality: 100,
            optimization_hints: vec![],
            parallel_segments: vec![],
            created_at: std::time::SystemTime::now(),
            hit_count: 0,
        };

        optimizer.cache_plan(plan_id.clone(), plan).await;

        // Should be cached now
        assert!(optimizer.get_cached_plan(&plan_id).await.is_some());
    }

    #[test]
    fn test_complexity_calculation() {
        let optimizer = create_test_optimizer();
        let patterns = vec![];

        let params = QueryComplexityParams {
            triple_patterns: &patterns,
            join_count: 2,
            filter_count: 1,
            has_aggregation: true,
            has_subqueries: false,
            has_optional: true,
            has_union: false,
        };
        let complexity = optimizer.calculate_query_complexity(&params);

        assert!(complexity > 0.0);
    }
}