oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
//! Automatic Query Caching Strategies
//!
//! This module provides intelligent, adaptive caching strategies that automatically
//! determine which queries should be cached, with what TTL, and when to invalidate.
//!
//! # Features
//! - Automatic cache decision making based on query patterns
//! - Adaptive TTL based on query update frequency
//! - Multiple caching strategies (LRU, LFU, adaptive, predictive)
//! - Cache warming for frequently accessed queries
//! - Intelligent cache invalidation
//! - Integration with historical cost estimator
//!
//! # Example
//! ```
//! use oxirs_gql::auto_caching_strategies::{AutoCachingManager, CachingStrategy};
//!
//! let mut manager = AutoCachingManager::new();
//! manager.set_strategy(CachingStrategy::Adaptive);
//!
//! // Automatically decides if query should be cached
//! if manager.should_cache("query { user { name } }") {
//!     let ttl = manager.get_optimal_ttl("query { user { name } }");
//!     // Cache with optimal TTL
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use thiserror::Error;

/// Error types for auto caching
#[derive(Debug, Error)]
pub enum CachingError {
    #[error("Lock acquisition failed: {0}")]
    LockError(String),

    #[error("Invalid configuration: {0}")]
    ConfigError(String),

    #[error("Strategy error: {0}")]
    StrategyError(String),
}

/// Caching strategy types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum CachingStrategy {
    /// Least Recently Used - evict least recently accessed items
    LRU,

    /// Least Frequently Used - evict least frequently accessed items
    LFU,

    /// Adaptive - automatically adjust between LRU/LFU based on workload
    #[default]
    Adaptive,

    /// Predictive - use ML to predict which queries to cache
    Predictive,

    /// Time-based - cache based on time-of-day patterns
    TimeBased,

    /// Cost-based - cache expensive queries preferentially
    CostBased,
}

/// Query access pattern
#[derive(Debug, Clone)]
struct AccessPattern {
    /// Total number of accesses
    access_count: u64,

    /// Last access time
    last_accessed: Instant,

    /// Access times (for frequency analysis)
    access_times: VecDeque<Instant>,

    /// Average execution time
    avg_execution_time_ms: f64,

    /// Result size in bytes
    avg_result_size_bytes: u64,

    /// First seen timestamp
    _first_seen: Instant,
}

impl AccessPattern {
    fn new() -> Self {
        let now = Instant::now();
        Self {
            access_count: 0,
            last_accessed: now,
            access_times: VecDeque::new(),
            avg_execution_time_ms: 0.0,
            avg_result_size_bytes: 0,
            _first_seen: now,
        }
    }

    fn record_access(&mut self, execution_time_ms: f64, result_size_bytes: u64) {
        let now = Instant::now();
        self.access_count += 1;
        self.last_accessed = now;

        // Keep last 100 access times for frequency analysis
        self.access_times.push_back(now);
        if self.access_times.len() > 100 {
            self.access_times.pop_front();
        }

        // Update averages
        let alpha = 0.3; // Smoothing factor
        self.avg_execution_time_ms =
            alpha * execution_time_ms + (1.0 - alpha) * self.avg_execution_time_ms;
        self.avg_result_size_bytes = (alpha * result_size_bytes as f64
            + (1.0 - alpha) * self.avg_result_size_bytes as f64)
            as u64;
    }

    /// Calculate access frequency (accesses per minute)
    fn access_frequency(&self) -> f64 {
        if self.access_times.len() < 2 {
            return 0.0;
        }

        let time_span = self
            .last_accessed
            .duration_since(self.access_times[0])
            .as_secs_f64();
        if time_span == 0.0 {
            return 0.0;
        }

        (self.access_times.len() as f64 / time_span) * 60.0
    }

    /// Calculate cache hit benefit score
    fn cache_benefit_score(&self) -> f64 {
        // Higher score = more beneficial to cache
        // Factors: frequency, execution time, result size
        let frequency_score = self.access_frequency();
        let time_score = self.avg_execution_time_ms / 1000.0; // Normalize to seconds
        let size_penalty = (self.avg_result_size_bytes as f64 / 1_000_000.0).min(1.0); // MB, capped at 1

        // Combine: high frequency + slow queries = high benefit
        // Penalize large result sizes
        (frequency_score * time_score) / (1.0 + size_penalty)
    }
}

/// Cache decision result
#[derive(Debug, Clone)]
pub struct CacheDecision {
    /// Whether to cache this query
    pub should_cache: bool,

    /// Optimal TTL in seconds
    pub ttl_seconds: u64,

    /// Cache priority (0-100, higher = more important)
    pub priority: u8,

    /// Reason for the decision
    pub reason: String,

    /// Confidence in the decision (0.0-1.0)
    pub confidence: f64,
}

/// Configuration for auto caching manager
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachingConfig {
    /// Minimum access count before considering caching
    pub min_access_count: u64,

    /// Minimum access frequency (per minute) to cache
    pub min_frequency: f64,

    /// Minimum execution time (ms) to consider caching
    pub min_execution_time_ms: f64,

    /// Maximum result size (bytes) to cache
    pub max_cache_size_bytes: u64,

    /// Default TTL in seconds
    pub default_ttl_seconds: u64,

    /// Minimum TTL in seconds
    pub min_ttl_seconds: u64,

    /// Maximum TTL in seconds
    pub max_ttl_seconds: u64,

    /// Enable adaptive TTL adjustment
    pub adaptive_ttl: bool,

    /// Cache benefit threshold (queries below this won't be cached)
    pub min_benefit_score: f64,
}

impl Default for CachingConfig {
    fn default() -> Self {
        Self {
            min_access_count: 3,
            min_frequency: 0.5, // 0.5 accesses per minute
            min_execution_time_ms: 50.0,
            max_cache_size_bytes: 10_000_000, // 10 MB
            default_ttl_seconds: 300,         // 5 minutes
            min_ttl_seconds: 60,              // 1 minute
            max_ttl_seconds: 3600,            // 1 hour
            adaptive_ttl: true,
            min_benefit_score: 1.0,
        }
    }
}

/// Automatic caching manager
pub struct AutoCachingManager {
    /// Access patterns for queries
    access_patterns: Arc<RwLock<HashMap<String, AccessPattern>>>,

    /// Current caching strategy
    strategy: Arc<RwLock<CachingStrategy>>,

    /// Configuration
    config: CachingConfig,

    /// Total queries analyzed
    total_queries: Arc<RwLock<u64>>,

    /// Total cache decisions made
    cache_decisions: Arc<RwLock<u64>>,

    /// Cached queries count
    cached_queries: Arc<RwLock<u64>>,
}

impl AutoCachingManager {
    /// Create a new auto caching manager with default configuration
    pub fn new() -> Self {
        Self::with_config(CachingConfig::default())
    }

    /// Create a new auto caching manager with custom configuration
    pub fn with_config(config: CachingConfig) -> Self {
        Self {
            access_patterns: Arc::new(RwLock::new(HashMap::new())),
            strategy: Arc::new(RwLock::new(CachingStrategy::default())),
            config,
            total_queries: Arc::new(RwLock::new(0)),
            cache_decisions: Arc::new(RwLock::new(0)),
            cached_queries: Arc::new(RwLock::new(0)),
        }
    }

    /// Set the caching strategy
    pub fn set_strategy(&mut self, strategy: CachingStrategy) -> Result<(), CachingError> {
        let mut s = self
            .strategy
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *s = strategy;
        Ok(())
    }

    /// Get the current caching strategy
    pub fn get_strategy(&self) -> Result<CachingStrategy, CachingError> {
        let s = self
            .strategy
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        Ok(*s)
    }

    /// Record a query execution
    pub fn record_query(
        &mut self,
        query: &str,
        execution_time_ms: f64,
        result_size_bytes: u64,
    ) -> Result<(), CachingError> {
        let mut patterns = self
            .access_patterns
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let pattern = patterns
            .entry(query.to_string())
            .or_insert_with(AccessPattern::new);

        pattern.record_access(execution_time_ms, result_size_bytes);

        let mut total = self
            .total_queries
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *total += 1;

        Ok(())
    }

    /// Decide if a query should be cached
    pub fn should_cache(&self, query: &str) -> bool {
        self.get_cache_decision(query)
            .map(|d| d.should_cache)
            .unwrap_or(false)
    }

    /// Get optimal TTL for a query
    pub fn get_optimal_ttl(&self, query: &str) -> Duration {
        self.get_cache_decision(query)
            .map(|d| Duration::from_secs(d.ttl_seconds))
            .unwrap_or_else(|_| Duration::from_secs(self.config.default_ttl_seconds))
    }

    /// Get comprehensive cache decision for a query
    pub fn get_cache_decision(&self, query: &str) -> Result<CacheDecision, CachingError> {
        let patterns = self
            .access_patterns
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let strategy = self
            .strategy
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let mut decisions = self
            .cache_decisions
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *decisions += 1;

        match patterns.get(query) {
            Some(pattern) => self.analyze_pattern(pattern, *strategy),
            None => Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: "No access history".to_string(),
                confidence: 1.0,
            }),
        }
    }

    /// Analyze access pattern and make caching decision
    fn analyze_pattern(
        &self,
        pattern: &AccessPattern,
        strategy: CachingStrategy,
    ) -> Result<CacheDecision, CachingError> {
        // Basic checks
        if pattern.access_count < self.config.min_access_count {
            return Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: format!(
                    "Insufficient access count: {} < {}",
                    pattern.access_count, self.config.min_access_count
                ),
                confidence: 1.0,
            });
        }

        if pattern.avg_execution_time_ms < self.config.min_execution_time_ms {
            return Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: format!(
                    "Execution too fast: {:.2}ms < {}ms",
                    pattern.avg_execution_time_ms, self.config.min_execution_time_ms
                ),
                confidence: 1.0,
            });
        }

        if pattern.avg_result_size_bytes > self.config.max_cache_size_bytes {
            return Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: format!(
                    "Result too large: {} > {} bytes",
                    pattern.avg_result_size_bytes, self.config.max_cache_size_bytes
                ),
                confidence: 1.0,
            });
        }

        let frequency = pattern.access_frequency();
        if frequency < self.config.min_frequency {
            return Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: format!(
                    "Low frequency: {:.2}/min < {}/min",
                    frequency, self.config.min_frequency
                ),
                confidence: 0.8,
            });
        }

        let benefit_score = pattern.cache_benefit_score();
        if benefit_score < self.config.min_benefit_score {
            return Ok(CacheDecision {
                should_cache: false,
                ttl_seconds: self.config.default_ttl_seconds,
                priority: 0,
                reason: format!(
                    "Low benefit score: {:.2} < {}",
                    benefit_score, self.config.min_benefit_score
                ),
                confidence: 0.9,
            });
        }

        // Strategy-specific analysis
        match strategy {
            CachingStrategy::LRU => self.lru_decision(pattern, benefit_score),
            CachingStrategy::LFU => self.lfu_decision(pattern, benefit_score),
            CachingStrategy::Adaptive => self.adaptive_decision(pattern, benefit_score),
            CachingStrategy::Predictive => self.predictive_decision(pattern, benefit_score),
            CachingStrategy::TimeBased => self.time_based_decision(pattern, benefit_score),
            CachingStrategy::CostBased => self.cost_based_decision(pattern, benefit_score),
        }
    }

    /// LRU strategy decision
    fn lru_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        let recency_score = self.calculate_recency_score(pattern);
        let ttl = if self.config.adaptive_ttl {
            self.calculate_adaptive_ttl(pattern)
        } else {
            self.config.default_ttl_seconds
        };

        Ok(CacheDecision {
            should_cache: true,
            ttl_seconds: ttl,
            priority: (recency_score * 100.0).min(100.0) as u8,
            reason: format!(
                "LRU: recency_score={:.2}, benefit={:.2}",
                recency_score, benefit_score
            ),
            confidence: 0.85,
        })
    }

    /// LFU strategy decision
    fn lfu_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        let frequency = pattern.access_frequency();
        let ttl = if self.config.adaptive_ttl {
            self.calculate_adaptive_ttl(pattern)
        } else {
            self.config.default_ttl_seconds
        };

        Ok(CacheDecision {
            should_cache: true,
            ttl_seconds: ttl,
            priority: (frequency * 10.0).min(100.0) as u8,
            reason: format!(
                "LFU: frequency={:.2}/min, benefit={:.2}",
                frequency, benefit_score
            ),
            confidence: 0.85,
        })
    }

    /// Adaptive strategy decision
    fn adaptive_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        let recency_score = self.calculate_recency_score(pattern);
        let frequency = pattern.access_frequency();

        // Adaptively blend LRU and LFU based on access pattern
        let lru_weight = if frequency > 1.0 { 0.3 } else { 0.7 };
        let lfu_weight = 1.0 - lru_weight;

        let combined_score = recency_score * lru_weight + (frequency / 10.0) * lfu_weight;
        let ttl = self.calculate_adaptive_ttl(pattern);

        Ok(CacheDecision {
            should_cache: true,
            ttl_seconds: ttl,
            priority: (combined_score * 100.0).min(100.0) as u8,
            reason: format!(
                "Adaptive: recency={:.2}, freq={:.2}/min, benefit={:.2}",
                recency_score, frequency, benefit_score
            ),
            confidence: 0.9,
        })
    }

    /// Predictive strategy decision
    fn predictive_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        // Predict future access based on historical pattern
        let trend = self.calculate_access_trend(pattern);
        let predicted_benefit = benefit_score * (1.0 + trend);

        let ttl = self.calculate_adaptive_ttl(pattern);

        Ok(CacheDecision {
            should_cache: predicted_benefit > self.config.min_benefit_score,
            ttl_seconds: ttl,
            priority: (predicted_benefit * 20.0).min(100.0) as u8,
            reason: format!(
                "Predictive: trend={:.2}, predicted_benefit={:.2}",
                trend, predicted_benefit
            ),
            confidence: 0.75,
        })
    }

    /// Time-based strategy decision
    fn time_based_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        // Analyze time-of-day access patterns
        let time_score = self.calculate_time_score(pattern);
        let ttl = self.calculate_time_based_ttl(pattern);

        Ok(CacheDecision {
            should_cache: true,
            ttl_seconds: ttl,
            priority: (time_score * 100.0).min(100.0) as u8,
            reason: format!(
                "TimeBased: time_score={:.2}, benefit={:.2}",
                time_score, benefit_score
            ),
            confidence: 0.8,
        })
    }

    /// Cost-based strategy decision
    fn cost_based_decision(
        &self,
        pattern: &AccessPattern,
        benefit_score: f64,
    ) -> Result<CacheDecision, CachingError> {
        let cost_score = pattern.avg_execution_time_ms / 100.0; // Normalize
        let ttl = if self.config.adaptive_ttl {
            // Expensive queries get longer TTL
            let base_ttl = self.calculate_adaptive_ttl(pattern);
            (base_ttl as f64 * (1.0 + cost_score / 10.0)) as u64
        } else {
            self.config.default_ttl_seconds
        };

        let ttl = ttl.clamp(self.config.min_ttl_seconds, self.config.max_ttl_seconds);

        Ok(CacheDecision {
            should_cache: true,
            ttl_seconds: ttl,
            priority: (cost_score * 10.0).min(100.0) as u8,
            reason: format!(
                "CostBased: cost={:.2}ms, benefit={:.2}",
                pattern.avg_execution_time_ms, benefit_score
            ),
            confidence: 0.88,
        })
    }

    /// Calculate recency score (0.0-1.0)
    fn calculate_recency_score(&self, pattern: &AccessPattern) -> f64 {
        let seconds_since_access = pattern.last_accessed.elapsed().as_secs_f64();
        // Exponential decay: recent = high score
        (-seconds_since_access / 300.0).exp() // 5 minute half-life
    }

    /// Calculate access trend (-1.0 to 1.0, positive = increasing)
    fn calculate_access_trend(&self, pattern: &AccessPattern) -> f64 {
        if pattern.access_times.len() < 4 {
            return 0.0;
        }

        // Compare recent vs older access frequency
        let mid = pattern.access_times.len() / 2;
        let times_vec: Vec<Instant> = pattern.access_times.iter().copied().collect();

        let recent = &times_vec[mid..];
        let older = &times_vec[..mid];

        if recent.len() < 2 || older.len() < 2 {
            return 0.0;
        }

        let recent_rate = recent.len() as f64
            / recent[recent.len() - 1]
                .duration_since(recent[0])
                .as_secs_f64()
                .max(1.0);
        let older_rate = older.len() as f64
            / older[older.len() - 1]
                .duration_since(older[0])
                .as_secs_f64()
                .max(1.0);

        ((recent_rate - older_rate) / older_rate.max(0.001)).clamp(-1.0, 1.0)
    }

    /// Calculate time-based score
    fn calculate_time_score(&self, _pattern: &AccessPattern) -> f64 {
        // Simplified: could analyze hour-of-day patterns
        // For now, return moderate score
        0.7
    }

    /// Calculate adaptive TTL based on access pattern
    fn calculate_adaptive_ttl(&self, pattern: &AccessPattern) -> u64 {
        let frequency = pattern.access_frequency();

        // High frequency = shorter TTL (data changes more often)
        // Low frequency = longer TTL (less likely to change)
        let base_ttl = if frequency > 10.0 {
            self.config.min_ttl_seconds
        } else if frequency > 1.0 {
            self.config.default_ttl_seconds
        } else {
            self.config.max_ttl_seconds
        };

        // Adjust based on execution time (expensive queries get longer TTL)
        let time_factor = (pattern.avg_execution_time_ms / 1000.0).min(2.0);
        let adjusted_ttl = (base_ttl as f64 * (1.0 + time_factor * 0.2)) as u64;

        adjusted_ttl.clamp(self.config.min_ttl_seconds, self.config.max_ttl_seconds)
    }

    /// Calculate time-based TTL
    fn calculate_time_based_ttl(&self, pattern: &AccessPattern) -> u64 {
        // For now, use adaptive TTL
        // Could be enhanced with time-of-day analysis
        self.calculate_adaptive_ttl(pattern)
    }

    /// Get caching statistics
    pub fn get_statistics(&self) -> Result<CachingStatistics, CachingError> {
        let patterns = self
            .access_patterns
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let total = *self
            .total_queries
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let decisions = *self
            .cache_decisions
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let cached = *self
            .cached_queries
            .read()
            .map_err(|e| CachingError::LockError(e.to_string()))?;

        let cache_rate = if decisions > 0 {
            (cached as f64 / decisions as f64) * 100.0
        } else {
            0.0
        };

        Ok(CachingStatistics {
            total_queries: total,
            unique_queries: patterns.len(),
            cache_decisions: decisions,
            cached_queries: cached,
            cache_rate_percent: cache_rate,
        })
    }

    /// Clear all access patterns
    pub fn clear(&mut self) -> Result<(), CachingError> {
        let mut patterns = self
            .access_patterns
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        patterns.clear();

        let mut total = self
            .total_queries
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *total = 0;

        let mut decisions = self
            .cache_decisions
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *decisions = 0;

        let mut cached = self
            .cached_queries
            .write()
            .map_err(|e| CachingError::LockError(e.to_string()))?;
        *cached = 0;

        Ok(())
    }
}

impl Default for AutoCachingManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Caching statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachingStatistics {
    /// Total queries processed
    pub total_queries: u64,

    /// Number of unique queries
    pub unique_queries: usize,

    /// Total cache decisions made
    pub cache_decisions: u64,

    /// Number of queries cached
    pub cached_queries: u64,

    /// Cache rate percentage
    pub cache_rate_percent: f64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_manager_creation() {
        let manager = AutoCachingManager::new();
        assert_eq!(
            manager.get_strategy().expect("should succeed"),
            CachingStrategy::Adaptive
        );
    }

    #[test]
    fn test_set_strategy() {
        let mut manager = AutoCachingManager::new();
        manager
            .set_strategy(CachingStrategy::LRU)
            .expect("should succeed");
        assert_eq!(
            manager.get_strategy().expect("should succeed"),
            CachingStrategy::LRU
        );
    }

    #[test]
    fn test_record_query() {
        let mut manager = AutoCachingManager::new();
        manager
            .record_query("query { user }", 100.0, 1024)
            .expect("should succeed");

        let stats = manager.get_statistics().expect("should succeed");
        assert_eq!(stats.total_queries, 1);
        assert_eq!(stats.unique_queries, 1);
    }

    #[test]
    fn test_cache_decision_insufficient_access() {
        let mut manager = AutoCachingManager::new();

        // Record only once (below min_access_count)
        manager
            .record_query("query { user }", 100.0, 1024)
            .expect("should succeed");

        let decision = manager
            .get_cache_decision("query { user }")
            .expect("should succeed");
        assert!(!decision.should_cache);
        assert!(decision.reason.contains("Insufficient access count"));
    }

    #[test]
    fn test_cache_decision_fast_query() {
        let mut manager = AutoCachingManager::new();
        let query = "query { user }";

        // Record multiple times with fast execution
        for _ in 0..5 {
            manager
                .record_query(query, 10.0, 1024)
                .expect("should succeed"); // 10ms < min_execution_time_ms (50ms)
        }

        let decision = manager.get_cache_decision(query).expect("should succeed");
        assert!(!decision.should_cache);
        assert!(decision.reason.contains("Execution too fast"));
    }

    #[test]
    fn test_cache_decision_large_result() {
        let mut manager = AutoCachingManager::new();
        let query = "query { large_data }";

        // Record with large result size
        for _ in 0..5 {
            manager
                .record_query(query, 200.0, 20_000_000)
                .expect("should succeed"); // 20MB > max (10MB)
        }

        let decision = manager.get_cache_decision(query).expect("should succeed");
        assert!(!decision.should_cache);
        assert!(decision.reason.contains("Result too large"));
    }

    #[test]
    fn test_cache_decision_cacheable() {
        let mut manager = AutoCachingManager::new();
        let query = "query { products }";

        // Record multiple times with good characteristics
        for _ in 0..10 {
            manager
                .record_query(query, 150.0, 5000)
                .expect("should succeed");
            thread::sleep(Duration::from_millis(10)); // Small delay for frequency calculation
        }

        let decision = manager.get_cache_decision(query).expect("should succeed");
        // May or may not cache depending on timing, but should have valid decision
        assert!(decision.ttl_seconds > 0);
    }

    #[test]
    fn test_adaptive_ttl() {
        let mut manager = AutoCachingManager::with_config(CachingConfig {
            adaptive_ttl: true,
            min_access_count: 2,
            ..Default::default()
        });

        let query = "query { user }";

        // High frequency accesses
        for _ in 0..10 {
            manager
                .record_query(query, 100.0, 1024)
                .expect("should succeed");
        }

        let ttl1 = manager.get_optimal_ttl(query);

        // Low frequency will get longer TTL in theory, but we need separate query
        let query2 = "query { settings }";
        for _ in 0..3 {
            manager
                .record_query(query2, 100.0, 1024)
                .expect("should succeed");
            thread::sleep(Duration::from_millis(200));
        }

        let ttl2 = manager.get_optimal_ttl(query2);

        // Both should be within valid range
        assert!(ttl1.as_secs() >= manager.config.min_ttl_seconds);
        assert!(ttl2.as_secs() >= manager.config.min_ttl_seconds);
    }

    #[test]
    fn test_statistics() {
        let mut manager = AutoCachingManager::new();

        manager
            .record_query("query { user }", 100.0, 1024)
            .expect("should succeed");
        manager
            .record_query("query { user }", 100.0, 1024)
            .expect("should succeed");
        manager
            .record_query("query { posts }", 150.0, 2048)
            .expect("should succeed");

        let stats = manager.get_statistics().expect("should succeed");
        assert_eq!(stats.total_queries, 3);
        assert_eq!(stats.unique_queries, 2);
    }

    #[test]
    fn test_clear() {
        let mut manager = AutoCachingManager::new();

        manager
            .record_query("query { user }", 100.0, 1024)
            .expect("should succeed");

        let stats1 = manager.get_statistics().expect("should succeed");
        assert_eq!(stats1.total_queries, 1);

        manager.clear().expect("should succeed");

        let stats2 = manager.get_statistics().expect("should succeed");
        assert_eq!(stats2.total_queries, 0);
        assert_eq!(stats2.unique_queries, 0);
    }

    #[test]
    fn test_different_strategies() {
        let strategies = vec![
            CachingStrategy::LRU,
            CachingStrategy::LFU,
            CachingStrategy::Adaptive,
            CachingStrategy::Predictive,
            CachingStrategy::TimeBased,
            CachingStrategy::CostBased,
        ];

        for strategy in strategies {
            let mut manager = AutoCachingManager::new();
            manager.set_strategy(strategy).expect("should succeed");

            let query = "query { user }";
            for _ in 0..5 {
                manager
                    .record_query(query, 100.0, 1024)
                    .expect("should succeed");
            }

            let decision = manager.get_cache_decision(query);
            assert!(decision.is_ok(), "Strategy {:?} failed", strategy);
        }
    }
}