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
//! Advanced Caching Integration for OxiRS ARQ Query Engine
//!
//! This module integrates the shared advanced caching system with the ARQ query processor
//! to provide high-performance caching for query plans, results, and intermediate computations.

use crate::{
    algebra::{Algebra, Solution, Term, Variable},
    query::Query,
    Result,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;

// Import the shared cache from parent engine module
// For now, we'll define basic cache traits locally until the module structure is fixed
pub trait CacheKey: Clone + std::hash::Hash + Eq + Send + Sync {}
pub trait CacheValue: Clone + Send + Sync {}

// Implement CacheKey for String
impl CacheKey for String {}

// Implement CacheValue for StatisticsSnapshot
impl CacheValue for StatisticsSnapshot {}

// Placeholder for AdvancedCache until shared_cache is properly imported
#[derive(Debug)]
pub struct AdvancedCache<K, V> {
    _phantom: std::marker::PhantomData<(K, V)>,
}

impl<K: CacheKey, V: CacheValue> AdvancedCache<K, V> {
    pub fn new(_config: AdvancedCacheConfig) -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn get(&self, _key: &K) -> Option<V> {
        None
    }

    pub fn put(&self, _key: K, _value: V) -> Result<()> {
        Ok(())
    }

    pub fn warm_cache(&self) -> Result<()> {
        Ok(())
    }

    pub fn clear(&self) {
        // No-op for placeholder
    }
}

// Placeholder for AdvancedCacheConfig
#[derive(Debug, Clone)]
pub struct AdvancedCacheConfig {
    pub l1_cache_size: usize,
    pub l2_cache_size: usize,
    pub l3_cache_size: usize,
    pub enable_compression: bool,
}

impl Default for AdvancedCacheConfig {
    fn default() -> Self {
        Self {
            l1_cache_size: 1024,
            l2_cache_size: 4096,
            l3_cache_size: 16384,
            enable_compression: false,
        }
    }
}

/// ARQ-specific cache configuration
#[derive(Debug, Clone)]
pub struct ArqCacheConfig {
    /// Query plan cache configuration
    pub query_plan_cache: AdvancedCacheConfig,
    /// Query result cache configuration
    pub query_result_cache: AdvancedCacheConfig,
    /// BGP evaluation cache configuration
    pub bgp_cache: AdvancedCacheConfig,
    /// Statistics cache configuration
    pub statistics_cache: AdvancedCacheConfig,
    /// Enable cross-query optimization caching
    pub enable_cross_query_caching: bool,
    /// Maximum query result size to cache (bytes)
    pub max_result_size: usize,
    /// Query similarity threshold for result reuse
    pub query_similarity_threshold: f64,
}

impl Default for ArqCacheConfig {
    fn default() -> Self {
        let base_config = AdvancedCacheConfig {
            l1_cache_size: 5000, // Smaller for query plans
            l2_cache_size: 20000,
            l3_cache_size: 100000,
            ..Default::default()
        };

        let result_config = AdvancedCacheConfig {
            l1_cache_size: 1000, // Results can be large
            l2_cache_size: 5000,
            l3_cache_size: 20000,
            enable_compression: true,
        };

        Self {
            query_plan_cache: base_config.clone(),
            query_result_cache: result_config,
            bgp_cache: base_config.clone(),
            statistics_cache: base_config,
            enable_cross_query_caching: true,
            max_result_size: 10 * 1024 * 1024, // 10MB
            query_similarity_threshold: 0.8,
        }
    }
}

/// Cached query plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedQueryPlan {
    /// The optimized algebra
    pub algebra: Algebra,
    /// Estimated execution cost
    pub estimated_cost: f64,
    /// Optimization metadata
    pub optimization_metadata: OptimizationMetadata,
    /// Statistics used for optimization
    pub statistics_snapshot: StatisticsSnapshot,
}

/// Optimization metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationMetadata {
    /// Optimizations applied
    pub optimizations_applied: Vec<String>,
    /// Optimization time
    pub optimization_time_ms: u64,
    /// Selectivity estimates
    pub selectivity_estimates: HashMap<String, f64>,
    /// Join order decisions
    pub join_order: Vec<String>,
}

/// Statistics snapshot for cache validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticsSnapshot {
    /// Dataset size when cached
    pub dataset_size: usize,
    /// Predicate cardinalities
    pub predicate_cardinalities: HashMap<String, usize>,
    /// Snapshot timestamp
    pub timestamp: u64,
    /// Statistics version
    pub version: String,
}

/// Cached query result
#[derive(Debug, Clone)]
pub struct CachedQueryResult {
    /// Result solutions
    pub solutions: Vec<Solution>,
    /// Result metadata
    pub metadata: QueryResultMetadata,
    /// Result size in bytes
    pub size_bytes: usize,
}

/// Query result metadata
#[derive(Debug, Clone)]
pub struct QueryResultMetadata {
    /// Execution time when cached
    pub execution_time: Duration,
    /// Dataset version when executed
    pub dataset_version: String,
    /// Variables in result
    pub variables: Vec<Variable>,
    /// Total solution count
    pub solution_count: usize,
    /// Whether result is complete or partial
    pub is_complete: bool,
}

/// Cache key for query plans
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct QueryPlanCacheKey {
    /// Normalized query hash
    pub query_hash: u64,
    /// Dataset schema hash
    pub schema_hash: u64,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Configuration parameters
    pub config_hash: u64,
}

/// Cache key for query results
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryResultCacheKey {
    /// Query signature
    pub query_signature: QuerySignature,
    /// Dataset version
    pub dataset_version: String,
    /// Parameter bindings (for parameterized queries)
    pub parameter_bindings: HashMap<String, String>,
}

impl std::hash::Hash for QueryResultCacheKey {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.query_signature.hash(state);
        self.dataset_version.hash(state);
        // Hash the parameter bindings in a deterministic way
        let mut sorted_params: Vec<_> = self.parameter_bindings.iter().collect();
        sorted_params.sort_by_key(|(k, _)| *k);
        for (k, v) in sorted_params {
            k.hash(state);
            v.hash(state);
        }
    }
}

/// Query signature for result caching
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct QuerySignature {
    /// Canonical query form
    pub canonical_form: String,
    /// Variable set
    pub variables: Vec<String>,
    /// Operation type
    pub operation_type: QueryOperationType,
    /// Complexity score
    pub complexity_score: u32,
}

/// Query operation types
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum QueryOperationType {
    Select,
    Construct,
    Ask,
    Describe,
    Update,
}

/// Optimization levels
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum OptimizationLevel {
    Basic,
    Standard,
    Aggressive,
    Custom(u32),
}

/// Cache key for BGP evaluations
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct BgpCacheKey {
    /// BGP pattern hash
    pub pattern_hash: u64,
    /// Variable bindings
    pub bindings_hash: u64,
    /// Graph context
    pub graph_context: Option<String>,
}

/// Cached BGP result
#[derive(Debug, Clone)]
pub struct CachedBgpResult {
    /// Solutions found
    pub solutions: Vec<Solution>,
    /// Evaluation metadata
    pub metadata: BgpEvaluationMetadata,
}

/// BGP evaluation metadata
#[derive(Debug, Clone)]
pub struct BgpEvaluationMetadata {
    /// Evaluation time
    pub evaluation_time: Duration,
    /// Solutions count
    pub solution_count: usize,
    /// Index hits
    pub index_hits: usize,
    /// Selectivity achieved
    pub selectivity: f64,
}

/// Advanced cache manager for ARQ
pub struct ArqCacheManager {
    /// Query plan cache
    query_plan_cache: Arc<AdvancedCache<QueryPlanCacheKey, CachedQueryPlan>>,
    /// Query result cache
    query_result_cache: Arc<AdvancedCache<QueryResultCacheKey, CachedQueryResult>>,
    /// BGP evaluation cache
    bgp_cache: Arc<AdvancedCache<BgpCacheKey, CachedBgpResult>>,
    /// Statistics cache
    statistics_cache: Arc<AdvancedCache<String, StatisticsSnapshot>>,
    /// Configuration
    config: ArqCacheConfig,
    /// Cache statistics
    cache_stats: Arc<std::sync::RwLock<ArqCacheStatistics>>,
}

/// ARQ cache statistics
#[derive(Debug, Clone, Default)]
pub struct ArqCacheStatistics {
    /// Query plan cache hits
    pub plan_cache_hits: usize,
    /// Query plan cache misses
    pub plan_cache_misses: usize,
    /// Result cache hits
    pub result_cache_hits: usize,
    /// Result cache misses
    pub result_cache_misses: usize,
    /// BGP cache hits
    pub bgp_cache_hits: usize,
    /// BGP cache misses
    pub bgp_cache_misses: usize,
    /// Total time saved (milliseconds)
    pub time_saved_ms: u64,
    /// Average cache lookup time
    pub avg_lookup_time_us: f64,
    /// Cache efficiency score
    pub efficiency_score: f64,
}

impl ArqCacheManager {
    /// Create new ARQ cache manager
    pub fn new(config: ArqCacheConfig) -> Self {
        Self {
            query_plan_cache: Arc::new(AdvancedCache::new(config.query_plan_cache.clone())),
            query_result_cache: Arc::new(AdvancedCache::new(config.query_result_cache.clone())),
            bgp_cache: Arc::new(AdvancedCache::new(config.bgp_cache.clone())),
            statistics_cache: Arc::new(AdvancedCache::new(config.statistics_cache.clone())),
            config,
            cache_stats: Arc::new(std::sync::RwLock::new(ArqCacheStatistics::default())),
        }
    }

    /// Get cached query plan
    pub fn get_query_plan(&self, key: &QueryPlanCacheKey) -> Option<CachedQueryPlan> {
        let start_time = std::time::Instant::now();
        let result = self.query_plan_cache.get(key);

        {
            let mut stats = self.cache_stats.write().expect("lock poisoned");
            if result.is_some() {
                stats.plan_cache_hits += 1;
            } else {
                stats.plan_cache_misses += 1;
            }
            self.update_avg_lookup_time(&mut stats, start_time.elapsed());
        }

        result
    }

    /// Cache query plan
    pub fn cache_query_plan(&self, key: QueryPlanCacheKey, plan: CachedQueryPlan) -> Result<()> {
        self.query_plan_cache.put(key, plan)?;
        Ok(())
    }

    /// Get cached query result
    pub fn get_query_result(&self, key: &QueryResultCacheKey) -> Option<CachedQueryResult> {
        let start_time = std::time::Instant::now();

        // Validate cache key freshness
        if !self.is_result_cache_valid(key) {
            return None;
        }

        let result = self.query_result_cache.get(key);

        {
            let mut stats = self.cache_stats.write().expect("lock poisoned");
            if let Some(ref cached_result) = result {
                stats.result_cache_hits += 1;
                stats.time_saved_ms += cached_result.metadata.execution_time.as_millis() as u64;
            } else {
                stats.result_cache_misses += 1;
            }
            self.update_avg_lookup_time(&mut stats, start_time.elapsed());
        }

        result
    }

    /// Cache query result
    pub fn cache_query_result(
        &self,
        key: QueryResultCacheKey,
        result: CachedQueryResult,
    ) -> Result<()> {
        // Check size limits
        if result.size_bytes > self.config.max_result_size {
            return Ok(()); // Don't cache oversized results
        }

        self.query_result_cache.put(key, result)?;
        Ok(())
    }

    /// Get cached BGP result
    pub fn get_bgp_result(&self, key: &BgpCacheKey) -> Option<CachedBgpResult> {
        let start_time = std::time::Instant::now();
        let result = self.bgp_cache.get(key);

        {
            let mut stats = self.cache_stats.write().expect("lock poisoned");
            if result.is_some() {
                stats.bgp_cache_hits += 1;
            } else {
                stats.bgp_cache_misses += 1;
            }
            self.update_avg_lookup_time(&mut stats, start_time.elapsed());
        }

        result
    }

    /// Cache BGP result
    pub fn cache_bgp_result(&self, key: BgpCacheKey, result: CachedBgpResult) -> Result<()> {
        self.bgp_cache.put(key, result)?;
        Ok(())
    }

    /// Create query plan cache key
    pub fn create_plan_cache_key(
        &self,
        query: &Query,
        schema_hash: u64,
        optimization_level: OptimizationLevel,
    ) -> QueryPlanCacheKey {
        let query_hash = self.hash_query(query);
        let config_hash = self.hash_config();

        QueryPlanCacheKey {
            query_hash,
            schema_hash,
            optimization_level,
            config_hash,
        }
    }

    /// Create query result cache key
    pub fn create_result_cache_key(
        &self,
        query: &Query,
        dataset_version: String,
        parameter_bindings: HashMap<String, String>,
    ) -> QueryResultCacheKey {
        let query_signature = self.create_query_signature(query);

        QueryResultCacheKey {
            query_signature,
            dataset_version,
            parameter_bindings,
        }
    }

    /// Create BGP cache key
    pub fn create_bgp_cache_key(
        &self,
        pattern_hash: u64,
        bindings: &HashMap<Variable, Term>,
        graph_context: Option<&str>,
    ) -> BgpCacheKey {
        let bindings_hash = self.hash_bindings(bindings);

        BgpCacheKey {
            pattern_hash,
            bindings_hash,
            graph_context: graph_context.map(|s| s.to_string()),
        }
    }

    /// Get cache statistics
    pub fn get_statistics(&self) -> ArqCacheStatistics {
        let stats = self.cache_stats.read().expect("lock poisoned");
        stats.clone()
    }

    /// Warm caches based on query patterns
    pub fn warm_caches(&self) -> Result<()> {
        // Warm query plan cache
        self.query_plan_cache.warm_cache()?;

        // Warm result cache
        self.query_result_cache.warm_cache()?;

        // Warm BGP cache
        self.bgp_cache.warm_cache()?;

        Ok(())
    }

    /// Clear all caches
    pub fn clear_all_caches(&self) {
        self.query_plan_cache.clear();
        self.query_result_cache.clear();
        self.bgp_cache.clear();
        self.statistics_cache.clear();

        // Reset statistics
        {
            let mut stats = self.cache_stats.write().expect("lock poisoned");
            *stats = ArqCacheStatistics::default();
        }
    }

    /// Invalidate caches based on dataset changes
    pub fn invalidate_on_dataset_change(&self, _changed_predicates: &[String]) -> Result<()> {
        // Implementation would invalidate relevant cache entries
        // For now, clear all caches as a conservative approach
        self.clear_all_caches();
        Ok(())
    }

    // Private helper methods
    fn hash_query(&self, query: &Query) -> u64 {
        // Create a canonical hash of the query
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        // This would hash the normalized query structure
        format!("{query:?}").hash(&mut hasher);
        hasher.finish()
    }

    fn hash_config(&self) -> u64 {
        // Hash relevant configuration parameters
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        self.config
            .query_similarity_threshold
            .to_bits()
            .hash(&mut hasher);
        self.config.enable_cross_query_caching.hash(&mut hasher);
        hasher.finish()
    }

    fn hash_bindings(&self, bindings: &HashMap<Variable, Term>) -> u64 {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        // Sort bindings for consistent hashing
        let mut sorted_bindings: Vec<_> = bindings.iter().collect();
        sorted_bindings.sort_by_key(|(var, _)| var.as_str());

        for (var, term) in sorted_bindings {
            var.hash(&mut hasher);
            format!("{term:?}").hash(&mut hasher);
        }

        hasher.finish()
    }

    fn create_query_signature(&self, query: &Query) -> QuerySignature {
        // Extract canonical form and metadata
        QuerySignature {
            canonical_form: format!("{query:?}"), // Simplified
            variables: query
                .select_variables
                .iter()
                .map(|v| v.as_str().to_string())
                .collect(),
            operation_type: self.determine_operation_type(query),
            complexity_score: self.calculate_complexity_score(query),
        }
    }

    fn determine_operation_type(&self, _query: &Query) -> QueryOperationType {
        // Determine query type based on query structure
        QueryOperationType::Select // Simplified
    }

    fn calculate_complexity_score(&self, _query: &Query) -> u32 {
        // Calculate query complexity score
        100 // Simplified
    }

    fn is_result_cache_valid(&self, _key: &QueryResultCacheKey) -> bool {
        // Check if cached result is still valid
        // This would check dataset version, timestamps, etc.
        true // Simplified
    }

    fn update_avg_lookup_time(&self, stats: &mut ArqCacheStatistics, lookup_time: Duration) {
        let total_lookups = stats.plan_cache_hits
            + stats.plan_cache_misses
            + stats.result_cache_hits
            + stats.result_cache_misses
            + stats.bgp_cache_hits
            + stats.bgp_cache_misses;

        let lookup_time_us = lookup_time.as_micros() as f64;

        if total_lookups == 1 {
            stats.avg_lookup_time_us = lookup_time_us;
        } else {
            stats.avg_lookup_time_us = (stats.avg_lookup_time_us * (total_lookups - 1) as f64
                + lookup_time_us)
                / total_lookups as f64;
        }

        // Update efficiency score
        let hit_rate = (stats.plan_cache_hits + stats.result_cache_hits + stats.bgp_cache_hits)
            as f64
            / total_lookups.max(1) as f64;
        stats.efficiency_score =
            hit_rate * 0.7 + (1.0 - stats.avg_lookup_time_us / 1000.0).max(0.0) * 0.3;
    }
}

// Implement cache traits
impl CacheKey for QueryPlanCacheKey {}
impl CacheValue for CachedQueryPlan {}
impl CacheKey for QueryResultCacheKey {}
impl CacheValue for CachedQueryResult {}
impl CacheKey for BgpCacheKey {}
impl CacheValue for CachedBgpResult {}

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

    #[test]
    fn test_arq_cache_manager_creation() {
        let config = ArqCacheConfig::default();
        let cache_manager = ArqCacheManager::new(config);

        let stats = cache_manager.get_statistics();
        assert_eq!(stats.plan_cache_hits, 0);
        assert_eq!(stats.result_cache_hits, 0);
    }

    #[test]
    fn test_cache_key_creation() {
        let config = ArqCacheConfig::default();
        let _cache_manager = ArqCacheManager::new(config);

        // Test would create actual query and test key creation
        // This is a placeholder test structure
    }
}