oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
//! # Adaptive Index Advisor
//!
//! This module provides intelligent index recommendations based on query workload analysis.
//! It analyzes query patterns, tracks index usage, and suggests optimal index configurations.
//!
//! ## Features
//!
//! - **Query Pattern Analysis**: Analyzes triple patterns to identify indexing opportunities
//! - **Workload-Based Recommendations**: Learns from query history to suggest beneficial indexes
//! - **Index Benefit Estimation**: Estimates performance improvements for proposed indexes
//! - **Index Consolidation**: Identifies redundant or overlapping indexes
//! - **Cost-Benefit Analysis**: Balances index maintenance cost against query performance gains
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use oxirs_arq::adaptive_index_advisor::{
//!     IndexAdvisor, AdvisorConfig, QueryPattern, IndexRecommendation,
//! };
//!
//! // Create an index advisor
//! let config = AdvisorConfig::default();
//! let mut advisor = IndexAdvisor::new(config);
//!
//! // Record query patterns
//! advisor.record_query("SELECT * WHERE { ?s :knows ?o }");
//!
//! // Get recommendations
//! let recommendations = advisor.get_recommendations();
//! ```

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::time::SystemTime;

/// Index type for RDF data
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IndexType {
    /// Subject-Predicate-Object ordering
    SPO,
    /// Subject-Object-Predicate ordering
    SOP,
    /// Predicate-Subject-Object ordering
    PSO,
    /// Predicate-Object-Subject ordering
    POS,
    /// Object-Subject-Predicate ordering
    OSP,
    /// Object-Predicate-Subject ordering
    OPS,
}

impl fmt::Display for IndexType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SPO => write!(f, "SPO"),
            Self::SOP => write!(f, "SOP"),
            Self::PSO => write!(f, "PSO"),
            Self::POS => write!(f, "POS"),
            Self::OSP => write!(f, "OSP"),
            Self::OPS => write!(f, "OPS"),
        }
    }
}

impl IndexType {
    /// Get all possible index types
    pub fn all() -> Vec<IndexType> {
        vec![
            Self::SPO,
            Self::SOP,
            Self::PSO,
            Self::POS,
            Self::OSP,
            Self::OPS,
        ]
    }

    /// Check if this index covers the given access pattern
    /// An index is useful when its primary component is bound
    pub fn covers_pattern(&self, pattern: &AccessPattern) -> bool {
        match self {
            // SPO/SOP indexes are useful when subject is bound
            Self::SPO | Self::SOP => pattern.has_subject,
            // PSO/POS indexes are useful when predicate is bound
            Self::PSO | Self::POS => pattern.has_predicate,
            // OSP/OPS indexes are useful when object is bound
            Self::OSP | Self::OPS => pattern.has_object,
        }
    }

    /// Get the selectivity order for this index
    pub fn primary_component(&self) -> PatternComponent {
        match self {
            Self::SPO | Self::SOP => PatternComponent::Subject,
            Self::PSO | Self::POS => PatternComponent::Predicate,
            Self::OSP | Self::OPS => PatternComponent::Object,
        }
    }
}

/// Components of a triple pattern
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternComponent {
    Subject,
    Predicate,
    Object,
}

impl fmt::Display for PatternComponent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Subject => write!(f, "subject"),
            Self::Predicate => write!(f, "predicate"),
            Self::Object => write!(f, "object"),
        }
    }
}

/// Access pattern extracted from a query
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AccessPattern {
    /// Whether subject is bound (constant)
    pub has_subject: bool,
    /// Whether predicate is bound (constant)
    pub has_predicate: bool,
    /// Whether object is bound (constant)
    pub has_object: bool,
    /// Optional predicate value if known
    pub predicate_value: Option<String>,
}

impl AccessPattern {
    /// Create a new access pattern
    pub fn new(has_subject: bool, has_predicate: bool, has_object: bool) -> Self {
        Self {
            has_subject,
            has_predicate,
            has_object,
            predicate_value: None,
        }
    }

    /// Create with predicate value
    pub fn with_predicate(mut self, predicate: impl Into<String>) -> Self {
        self.predicate_value = Some(predicate.into());
        self
    }

    /// Get pattern signature for grouping
    pub fn signature(&self) -> String {
        format!(
            "{}{}{}",
            if self.has_subject { "S" } else { "?" },
            if self.has_predicate { "P" } else { "?" },
            if self.has_object { "O" } else { "?" }
        )
    }

    /// Count bound components
    pub fn bound_count(&self) -> usize {
        let mut count = 0;
        if self.has_subject {
            count += 1;
        }
        if self.has_predicate {
            count += 1;
        }
        if self.has_object {
            count += 1;
        }
        count
    }

    /// Best index type for this pattern
    pub fn best_index(&self) -> Option<IndexType> {
        match (self.has_subject, self.has_predicate, self.has_object) {
            (true, true, true) => Some(IndexType::SPO),
            (true, true, false) => Some(IndexType::SPO),
            (true, false, true) => Some(IndexType::SOP),
            (true, false, false) => Some(IndexType::SPO),
            (false, true, true) => Some(IndexType::POS),
            (false, true, false) => Some(IndexType::PSO),
            (false, false, true) => Some(IndexType::OSP),
            (false, false, false) => None, // Full scan needed
        }
    }
}

impl fmt::Display for AccessPattern {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.signature())
    }
}

/// Query pattern with associated metadata
#[derive(Debug, Clone)]
pub struct QueryPattern {
    /// The access pattern
    pub access_pattern: AccessPattern,
    /// Number of times this pattern was observed
    pub frequency: usize,
    /// Average selectivity (0.0 - 1.0)
    pub avg_selectivity: f64,
    /// Average execution time in milliseconds
    pub avg_execution_time_ms: f64,
    /// Last observed timestamp
    pub last_seen: SystemTime,
}

impl QueryPattern {
    /// Create a new query pattern
    pub fn new(access_pattern: AccessPattern) -> Self {
        Self {
            access_pattern,
            frequency: 1,
            avg_selectivity: 1.0,
            avg_execution_time_ms: 0.0,
            last_seen: SystemTime::now(),
        }
    }

    /// Update with new observation
    pub fn update(&mut self, selectivity: f64, execution_time_ms: f64) {
        let n = self.frequency as f64;
        self.avg_selectivity = (self.avg_selectivity * n + selectivity) / (n + 1.0);
        self.avg_execution_time_ms =
            (self.avg_execution_time_ms * n + execution_time_ms) / (n + 1.0);
        self.frequency += 1;
        self.last_seen = SystemTime::now();
    }
}

/// Configuration for the index advisor
#[derive(Debug, Clone)]
pub struct AdvisorConfig {
    /// Minimum frequency for a pattern to be considered
    pub min_pattern_frequency: usize,
    /// Maximum number of recommendations to generate
    pub max_recommendations: usize,
    /// Weight for query frequency in benefit calculation
    pub frequency_weight: f64,
    /// Weight for execution time in benefit calculation
    pub execution_time_weight: f64,
    /// Minimum benefit score to recommend an index
    pub min_benefit_score: f64,
    /// Consider index maintenance cost
    pub consider_maintenance_cost: bool,
    /// Decay factor for old patterns (0.0-1.0)
    pub time_decay_factor: f64,
    /// Maximum patterns to track
    pub max_tracked_patterns: usize,
}

impl Default for AdvisorConfig {
    fn default() -> Self {
        Self {
            min_pattern_frequency: 5,
            max_recommendations: 10,
            frequency_weight: 0.4,
            execution_time_weight: 0.6,
            min_benefit_score: 0.1,
            consider_maintenance_cost: true,
            time_decay_factor: 0.95,
            max_tracked_patterns: 1000,
        }
    }
}

impl AdvisorConfig {
    /// Conservative configuration for production
    pub fn conservative() -> Self {
        Self {
            min_pattern_frequency: 10,
            max_recommendations: 5,
            frequency_weight: 0.3,
            execution_time_weight: 0.7,
            min_benefit_score: 0.3,
            consider_maintenance_cost: true,
            time_decay_factor: 0.90,
            max_tracked_patterns: 500,
        }
    }

    /// Aggressive configuration for optimization
    pub fn aggressive() -> Self {
        Self {
            min_pattern_frequency: 3,
            max_recommendations: 15,
            frequency_weight: 0.5,
            execution_time_weight: 0.5,
            min_benefit_score: 0.05,
            consider_maintenance_cost: false,
            time_decay_factor: 0.98,
            max_tracked_patterns: 2000,
        }
    }
}

/// Index recommendation priority
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RecommendationPriority {
    /// Critical - significant performance impact
    Critical,
    /// High - notable improvement expected
    High,
    /// Medium - moderate improvement expected
    Medium,
    /// Low - minor improvement expected
    Low,
}

impl fmt::Display for RecommendationPriority {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Critical => write!(f, "CRITICAL"),
            Self::High => write!(f, "HIGH"),
            Self::Medium => write!(f, "MEDIUM"),
            Self::Low => write!(f, "LOW"),
        }
    }
}

/// A single index recommendation
#[derive(Debug, Clone)]
pub struct IndexRecommendation {
    /// Recommended index type
    pub index_type: IndexType,
    /// Priority level
    pub priority: RecommendationPriority,
    /// Estimated benefit score (0.0 - 1.0)
    pub benefit_score: f64,
    /// Estimated performance improvement percentage
    pub estimated_improvement_percent: f64,
    /// Patterns that would benefit
    pub benefiting_patterns: Vec<AccessPattern>,
    /// Total query frequency affected
    pub total_frequency: usize,
    /// Rationale for the recommendation
    pub rationale: String,
    /// Estimated storage overhead
    pub storage_overhead_percent: f64,
    /// Estimated write performance impact
    pub write_impact_percent: f64,
}

impl IndexRecommendation {
    /// Get a human-readable summary
    pub fn summary(&self) -> String {
        format!(
            "[{}] {} - {:.1}% improvement, affects {} queries",
            self.priority,
            self.index_type,
            self.estimated_improvement_percent,
            self.total_frequency
        )
    }
}

/// Current index configuration
#[derive(Debug, Clone, Default)]
pub struct IndexConfiguration {
    /// Active indexes
    pub active_indexes: HashSet<IndexType>,
    /// Index usage statistics
    pub usage_stats: HashMap<IndexType, IndexUsageStats>,
}

impl IndexConfiguration {
    /// Create a default configuration (SPO only)
    pub fn default_spo() -> Self {
        let mut config = Self::default();
        config.active_indexes.insert(IndexType::SPO);
        config
    }

    /// Check if an index is active
    pub fn has_index(&self, index_type: IndexType) -> bool {
        self.active_indexes.contains(&index_type)
    }

    /// Add an index
    pub fn add_index(&mut self, index_type: IndexType) {
        self.active_indexes.insert(index_type);
    }

    /// Remove an index
    pub fn remove_index(&mut self, index_type: IndexType) -> bool {
        self.active_indexes.remove(&index_type)
    }
}

/// Usage statistics for an index
#[derive(Debug, Clone, Default)]
pub struct IndexUsageStats {
    /// Number of queries that used this index
    pub query_count: usize,
    /// Number of index lookups
    pub lookup_count: usize,
    /// Total rows scanned
    pub rows_scanned: usize,
    /// Total rows returned
    pub rows_returned: usize,
    /// Cache hit ratio
    pub cache_hit_ratio: f64,
}

/// Analysis report for index optimization
#[derive(Debug, Clone)]
pub struct IndexAnalysisReport {
    /// Generated timestamp
    pub generated_at: SystemTime,
    /// Current index configuration
    pub current_config: IndexConfiguration,
    /// Recommendations
    pub recommendations: Vec<IndexRecommendation>,
    /// Unused indexes that could be removed
    pub unused_indexes: Vec<IndexType>,
    /// Overlapping index pairs
    pub overlapping_indexes: Vec<(IndexType, IndexType)>,
    /// Summary statistics
    pub summary: AnalysisSummary,
}

impl IndexAnalysisReport {
    /// Get high priority recommendations
    pub fn high_priority(&self) -> Vec<&IndexRecommendation> {
        self.recommendations
            .iter()
            .filter(|r| r.priority <= RecommendationPriority::High)
            .collect()
    }

    /// Check if any changes are recommended
    pub fn has_recommendations(&self) -> bool {
        !self.recommendations.is_empty() || !self.unused_indexes.is_empty()
    }

    /// Generate a text summary
    pub fn text_summary(&self) -> String {
        let mut text = String::from("Index Analysis Report\n");
        text.push_str(&format!("Generated: {:?}\n\n", self.generated_at));

        text.push_str(&format!(
            "Current Indexes: {:?}\n",
            self.current_config.active_indexes
        ));
        text.push_str(&format!(
            "Patterns Analyzed: {}\n",
            self.summary.total_patterns
        ));
        text.push_str(&format!(
            "Total Queries: {}\n\n",
            self.summary.total_queries
        ));

        if !self.recommendations.is_empty() {
            text.push_str("Recommendations:\n");
            for rec in &self.recommendations {
                text.push_str(&format!("  - {}\n", rec.summary()));
            }
            text.push('\n');
        }

        if !self.unused_indexes.is_empty() {
            text.push_str(&format!(
                "Unused Indexes (consider removing): {:?}\n",
                self.unused_indexes
            ));
        }

        text
    }
}

/// Summary statistics for analysis
#[derive(Debug, Clone, Default)]
pub struct AnalysisSummary {
    /// Total patterns analyzed
    pub total_patterns: usize,
    /// Total queries analyzed
    pub total_queries: usize,
    /// Average selectivity
    pub avg_selectivity: f64,
    /// Most frequent pattern signature
    pub most_frequent_pattern: Option<String>,
    /// Estimated overall improvement if all recommendations adopted
    pub potential_improvement_percent: f64,
}

/// Main index advisor
#[derive(Debug)]
pub struct IndexAdvisor {
    /// Configuration
    config: AdvisorConfig,
    /// Current index configuration
    current_indexes: IndexConfiguration,
    /// Tracked query patterns
    patterns: HashMap<String, QueryPattern>,
    /// Statistics
    stats: AdvisorStatistics,
}

/// Statistics for the advisor
#[derive(Debug, Clone, Default)]
pub struct AdvisorStatistics {
    /// Total queries recorded
    pub total_queries: usize,
    /// Total patterns discovered
    pub total_patterns: usize,
    /// Analysis count
    pub analyses_performed: usize,
    /// Recommendations generated
    pub recommendations_generated: usize,
}

impl IndexAdvisor {
    /// Create a new index advisor
    pub fn new(config: AdvisorConfig) -> Self {
        Self {
            config,
            current_indexes: IndexConfiguration::default_spo(),
            patterns: HashMap::new(),
            stats: AdvisorStatistics::default(),
        }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(AdvisorConfig::default())
    }

    /// Set the current index configuration
    pub fn set_indexes(&mut self, indexes: IndexConfiguration) {
        self.current_indexes = indexes;
    }

    /// Record a query pattern
    pub fn record_pattern(
        &mut self,
        access_pattern: AccessPattern,
        selectivity: f64,
        execution_time_ms: f64,
    ) {
        let signature = access_pattern.signature();
        self.stats.total_queries += 1;

        if let Some(pattern) = self.patterns.get_mut(&signature) {
            pattern.update(selectivity, execution_time_ms);
        } else {
            let mut pattern = QueryPattern::new(access_pattern);
            pattern.avg_selectivity = selectivity;
            pattern.avg_execution_time_ms = execution_time_ms;
            self.patterns.insert(signature, pattern);
            self.stats.total_patterns += 1;
        }

        // Evict old patterns if needed
        if self.patterns.len() > self.config.max_tracked_patterns {
            self.evict_old_patterns();
        }
    }

    /// Record a simple query (parses pattern from SPARQL-like syntax)
    pub fn record_query(&mut self, query: &str) {
        // Simple pattern extraction - look for triple patterns
        let patterns = self.extract_patterns(query);
        for pattern in patterns {
            self.record_pattern(pattern, 1.0, 0.0);
        }
    }

    /// Analyze workload and generate recommendations
    pub fn analyze(&mut self) -> IndexAnalysisReport {
        self.stats.analyses_performed += 1;

        let mut recommendations = Vec::new();
        let mut index_benefits: HashMap<IndexType, IndexBenefitAccumulator> = HashMap::new();

        // Analyze each pattern
        for pattern in self.patterns.values() {
            if pattern.frequency < self.config.min_pattern_frequency {
                continue;
            }

            // Find best index for this pattern
            if let Some(best_index) = pattern.access_pattern.best_index() {
                // Check if we already have this index
                if !self.current_indexes.has_index(best_index) {
                    let entry = index_benefits.entry(best_index).or_default();
                    entry.add_pattern(pattern);
                }
            }
        }

        // Convert benefits to recommendations
        for (index_type, benefits) in index_benefits {
            let benefit_score = self.calculate_benefit_score(&benefits);
            if benefit_score >= self.config.min_benefit_score {
                let priority = self.determine_priority(benefit_score);
                let estimated_improvement = self.estimate_improvement(&benefits);

                recommendations.push(IndexRecommendation {
                    index_type,
                    priority,
                    benefit_score,
                    estimated_improvement_percent: estimated_improvement,
                    benefiting_patterns: benefits.patterns.clone(),
                    total_frequency: benefits.total_frequency,
                    rationale: self.generate_rationale(&benefits, index_type),
                    storage_overhead_percent: self.estimate_storage_overhead(index_type),
                    write_impact_percent: self.estimate_write_impact(index_type),
                });
            }
        }

        // Sort by priority and benefit
        recommendations.sort_by(|a, b| match a.priority.cmp(&b.priority) {
            std::cmp::Ordering::Equal => b
                .benefit_score
                .partial_cmp(&a.benefit_score)
                .unwrap_or(std::cmp::Ordering::Equal),
            other => other,
        });

        // Limit recommendations
        recommendations.truncate(self.config.max_recommendations);
        self.stats.recommendations_generated += recommendations.len();

        // Find unused indexes
        let unused_indexes = self.find_unused_indexes();

        // Find overlapping indexes
        let overlapping_indexes = self.find_overlapping_indexes();

        // Generate summary
        let summary = self.generate_summary(&recommendations);

        IndexAnalysisReport {
            generated_at: SystemTime::now(),
            current_config: self.current_indexes.clone(),
            recommendations,
            unused_indexes,
            overlapping_indexes,
            summary,
        }
    }

    /// Get current statistics
    pub fn statistics(&self) -> &AdvisorStatistics {
        &self.stats
    }

    /// Get current configuration
    pub fn config(&self) -> &AdvisorConfig {
        &self.config
    }

    /// Clear all tracked patterns
    pub fn clear_patterns(&mut self) {
        self.patterns.clear();
        self.stats.total_patterns = 0;
    }

    /// Export tracked patterns for persistence
    pub fn export_patterns(&self) -> Vec<(String, QueryPattern)> {
        self.patterns
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }

    /// Import patterns
    pub fn import_patterns(&mut self, patterns: Vec<(String, QueryPattern)>) {
        for (sig, pattern) in patterns {
            self.patterns.insert(sig, pattern);
            self.stats.total_patterns += 1;
        }
    }

    // Private methods

    fn extract_patterns(&self, query: &str) -> Vec<AccessPattern> {
        let mut patterns = Vec::new();
        let query_lower = query.to_lowercase();

        // Look for triple pattern-like structures: ?var :pred ?var or :subj :pred ?var etc.
        // Simple heuristic parsing - not full SPARQL parsing
        for part in query_lower.split('{') {
            for segment in part.split('.') {
                let segment = segment.trim();
                if segment.is_empty() || segment.starts_with('}') {
                    continue;
                }

                let tokens: Vec<&str> = segment.split_whitespace().collect();
                if tokens.len() >= 3 {
                    let has_subject = !tokens[0].starts_with('?');
                    let has_predicate = !tokens[1].starts_with('?');
                    let has_object = !tokens[2].starts_with('?');

                    let mut pattern = AccessPattern::new(has_subject, has_predicate, has_object);
                    if has_predicate {
                        pattern.predicate_value = Some(tokens[1].to_string());
                    }
                    patterns.push(pattern);
                }
            }
        }

        if patterns.is_empty() {
            // Default to full scan pattern if no patterns found
            patterns.push(AccessPattern::new(false, false, false));
        }

        patterns
    }

    fn calculate_benefit_score(&self, benefits: &IndexBenefitAccumulator) -> f64 {
        let freq_score = (benefits.total_frequency as f64).ln().max(0.0) / 10.0;
        let time_score = benefits.total_execution_time_ms / 1000.0;

        let raw_score = self.config.frequency_weight * freq_score
            + self.config.execution_time_weight * time_score;

        // Normalize to 0.0 - 1.0
        (raw_score / 2.0).min(1.0)
    }

    fn determine_priority(&self, benefit_score: f64) -> RecommendationPriority {
        if benefit_score >= 0.8 {
            RecommendationPriority::Critical
        } else if benefit_score >= 0.5 {
            RecommendationPriority::High
        } else if benefit_score >= 0.3 {
            RecommendationPriority::Medium
        } else {
            RecommendationPriority::Low
        }
    }

    fn estimate_improvement(&self, benefits: &IndexBenefitAccumulator) -> f64 {
        // Estimate based on selectivity improvement
        let avg_selectivity = if benefits.patterns.is_empty() {
            1.0
        } else {
            benefits.patterns.len() as f64 / self.patterns.len() as f64
        };

        // Indexes typically improve performance by 10-90% depending on selectivity
        let base_improvement = 50.0 * (1.0 - avg_selectivity);
        base_improvement.clamp(10.0, 90.0)
    }

    fn generate_rationale(
        &self,
        benefits: &IndexBenefitAccumulator,
        index_type: IndexType,
    ) -> String {
        format!(
            "Index {} would benefit {} pattern(s) with total frequency {} queries. \
             Primary optimization for {} lookups.",
            index_type,
            benefits.patterns.len(),
            benefits.total_frequency,
            index_type.primary_component()
        )
    }

    fn estimate_storage_overhead(&self, _index_type: IndexType) -> f64 {
        // Each index typically adds ~100% storage overhead for the indexed data
        // This is a simplified estimate
        100.0
    }

    fn estimate_write_impact(&self, _index_type: IndexType) -> f64 {
        // Each additional index typically adds ~20% write overhead
        20.0
    }

    fn find_unused_indexes(&self) -> Vec<IndexType> {
        let mut unused = Vec::new();
        for &index_type in &self.current_indexes.active_indexes {
            let is_used = self.patterns.values().any(|p| {
                p.frequency >= self.config.min_pattern_frequency
                    && index_type.covers_pattern(&p.access_pattern)
            });
            if !is_used && index_type != IndexType::SPO {
                // Never recommend removing SPO as it's the default
                unused.push(index_type);
            }
        }
        unused
    }

    fn find_overlapping_indexes(&self) -> Vec<(IndexType, IndexType)> {
        let mut overlaps = Vec::new();
        let indexes: Vec<_> = self.current_indexes.active_indexes.iter().collect();

        for i in 0..indexes.len() {
            for j in (i + 1)..indexes.len() {
                if indexes[i].primary_component() == indexes[j].primary_component() {
                    overlaps.push((*indexes[i], *indexes[j]));
                }
            }
        }
        overlaps
    }

    fn generate_summary(&self, recommendations: &[IndexRecommendation]) -> AnalysisSummary {
        let total_patterns = self.patterns.len();
        let total_queries: usize = self.patterns.values().map(|p| p.frequency).sum();

        let avg_selectivity = if total_patterns > 0 {
            self.patterns
                .values()
                .map(|p| p.avg_selectivity)
                .sum::<f64>()
                / total_patterns as f64
        } else {
            1.0
        };

        let most_frequent_pattern = self
            .patterns
            .iter()
            .max_by_key(|(_, p)| p.frequency)
            .map(|(sig, _)| sig.clone());

        let potential_improvement = recommendations
            .iter()
            .map(|r| r.estimated_improvement_percent)
            .sum::<f64>()
            .min(95.0); // Cap at 95%

        AnalysisSummary {
            total_patterns,
            total_queries,
            avg_selectivity,
            most_frequent_pattern,
            potential_improvement_percent: potential_improvement,
        }
    }

    fn evict_old_patterns(&mut self) {
        // Sort by last seen and keep most recent
        let mut sorted: Vec<_> = self.patterns.iter().collect();
        sorted.sort_by_key(|b| std::cmp::Reverse(b.1.last_seen));

        let to_keep: HashSet<_> = sorted
            .iter()
            .take(self.config.max_tracked_patterns / 2)
            .map(|(k, _)| (*k).clone())
            .collect();

        self.patterns.retain(|k, _| to_keep.contains(k));
        self.stats.total_patterns = self.patterns.len();
    }
}

/// Accumulator for index benefit calculation
#[derive(Debug, Default)]
struct IndexBenefitAccumulator {
    patterns: Vec<AccessPattern>,
    total_frequency: usize,
    total_execution_time_ms: f64,
}

impl IndexBenefitAccumulator {
    fn add_pattern(&mut self, pattern: &QueryPattern) {
        self.patterns.push(pattern.access_pattern.clone());
        self.total_frequency += pattern.frequency;
        self.total_execution_time_ms += pattern.avg_execution_time_ms * pattern.frequency as f64;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_access_pattern_signature() {
        let pattern = AccessPattern::new(true, true, false);
        assert_eq!(pattern.signature(), "SP?");

        let pattern = AccessPattern::new(false, true, true);
        assert_eq!(pattern.signature(), "?PO");

        let pattern = AccessPattern::new(false, false, false);
        assert_eq!(pattern.signature(), "???");
    }

    #[test]
    fn test_access_pattern_best_index() {
        assert_eq!(
            AccessPattern::new(true, true, false).best_index(),
            Some(IndexType::SPO)
        );
        assert_eq!(
            AccessPattern::new(true, false, true).best_index(),
            Some(IndexType::SOP)
        );
        assert_eq!(
            AccessPattern::new(false, true, true).best_index(),
            Some(IndexType::POS)
        );
        assert_eq!(
            AccessPattern::new(false, false, true).best_index(),
            Some(IndexType::OSP)
        );
        assert_eq!(AccessPattern::new(false, false, false).best_index(), None);
    }

    #[test]
    fn test_index_type_covers_pattern() {
        let pattern = AccessPattern::new(true, true, false);
        assert!(IndexType::SPO.covers_pattern(&pattern));
        assert!(IndexType::PSO.covers_pattern(&pattern));
    }

    #[test]
    fn test_query_pattern_update() {
        let mut pattern = QueryPattern::new(AccessPattern::new(true, false, false));
        assert_eq!(pattern.frequency, 1);

        pattern.update(0.5, 10.0);
        assert_eq!(pattern.frequency, 2);

        pattern.update(0.3, 20.0);
        assert_eq!(pattern.frequency, 3);
    }

    #[test]
    fn test_index_advisor_creation() {
        let advisor = IndexAdvisor::with_defaults();
        assert!(advisor.current_indexes.has_index(IndexType::SPO));
    }

    #[test]
    fn test_record_pattern() {
        let mut advisor = IndexAdvisor::with_defaults();

        advisor.record_pattern(AccessPattern::new(true, true, false), 0.1, 5.0);
        advisor.record_pattern(AccessPattern::new(true, true, false), 0.2, 6.0);

        assert_eq!(advisor.stats.total_queries, 2);
        assert_eq!(advisor.stats.total_patterns, 1);
    }

    #[test]
    fn test_record_query() {
        let mut advisor = IndexAdvisor::with_defaults();

        advisor.record_query("SELECT * WHERE { ?s :knows ?o }");
        assert!(advisor.stats.total_queries > 0);
    }

    #[test]
    fn test_analyze_with_patterns() {
        let config = AdvisorConfig {
            min_pattern_frequency: 2,
            min_benefit_score: 0.01, // Lower threshold for test
            ..Default::default()
        };
        let mut advisor = IndexAdvisor::new(config);

        // Record patterns that would benefit from POS index (not covered by SPO)
        for _ in 0..50 {
            advisor.record_pattern(AccessPattern::new(false, true, true), 0.1, 100.0);
        }

        let report = advisor.analyze();
        // May or may not have recommendations depending on benefit calculation
        // Just verify analysis completes successfully
        assert!(report.summary.total_patterns > 0);
        assert!(report.summary.total_queries > 0);
    }

    #[test]
    fn test_analyze_empty() {
        let mut advisor = IndexAdvisor::with_defaults();
        let report = advisor.analyze();
        assert!(report.recommendations.is_empty());
    }

    #[test]
    fn test_recommendation_priority() {
        let config = AdvisorConfig {
            min_pattern_frequency: 1,
            ..Default::default()
        };
        let mut advisor = IndexAdvisor::new(config);

        // High frequency pattern
        for _ in 0..100 {
            advisor.record_pattern(AccessPattern::new(false, false, true), 0.01, 100.0);
        }

        let report = advisor.analyze();
        if !report.recommendations.is_empty() {
            // Should have high priority due to frequency
            assert!(report.recommendations[0].priority <= RecommendationPriority::High);
        }
    }

    #[test]
    fn test_index_configuration() {
        let mut config = IndexConfiguration::default_spo();
        assert!(config.has_index(IndexType::SPO));
        assert!(!config.has_index(IndexType::POS));

        config.add_index(IndexType::POS);
        assert!(config.has_index(IndexType::POS));

        config.remove_index(IndexType::POS);
        assert!(!config.has_index(IndexType::POS));
    }

    #[test]
    fn test_unused_index_detection() {
        let mut advisor = IndexAdvisor::with_defaults();
        advisor.current_indexes.add_index(IndexType::OSP);

        // Only record SPO-compatible patterns
        for _ in 0..10 {
            advisor.record_pattern(AccessPattern::new(true, true, false), 0.1, 5.0);
        }

        let report = advisor.analyze();
        assert!(report.unused_indexes.contains(&IndexType::OSP));
    }

    #[test]
    fn test_export_import_patterns() {
        let mut advisor = IndexAdvisor::with_defaults();
        advisor.record_pattern(AccessPattern::new(true, false, false), 0.5, 10.0);
        advisor.record_pattern(AccessPattern::new(false, true, false), 0.3, 15.0);

        let exported = advisor.export_patterns();
        assert_eq!(exported.len(), 2);

        let mut new_advisor = IndexAdvisor::with_defaults();
        new_advisor.import_patterns(exported);
        assert_eq!(new_advisor.stats.total_patterns, 2);
    }

    #[test]
    fn test_config_presets() {
        let conservative = AdvisorConfig::conservative();
        assert_eq!(conservative.min_pattern_frequency, 10);

        let aggressive = AdvisorConfig::aggressive();
        assert_eq!(aggressive.min_pattern_frequency, 3);
    }

    #[test]
    fn test_report_text_summary() {
        let config = AdvisorConfig {
            min_pattern_frequency: 1,
            ..Default::default()
        };
        let mut advisor = IndexAdvisor::new(config);

        for _ in 0..5 {
            advisor.record_pattern(AccessPattern::new(false, true, true), 0.1, 10.0);
        }

        let report = advisor.analyze();
        let summary = report.text_summary();

        assert!(summary.contains("Index Analysis Report"));
        assert!(summary.contains("Current Indexes"));
    }

    #[test]
    fn test_pattern_bound_count() {
        assert_eq!(AccessPattern::new(true, true, true).bound_count(), 3);
        assert_eq!(AccessPattern::new(true, false, false).bound_count(), 1);
        assert_eq!(AccessPattern::new(false, false, false).bound_count(), 0);
    }

    #[test]
    fn test_index_type_display() {
        assert_eq!(format!("{}", IndexType::SPO), "SPO");
        assert_eq!(format!("{}", IndexType::POS), "POS");
    }
}