oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Performance optimization utilities for OxiRS CLI
//!
//! This module provides performance optimizations for RDF processing
//! using scirs2-core's advanced features including SIMD, parallel processing,
//! GPU acceleration, and memory-efficient operations.
//!
//! ## SciRS2 Integration
//!
//! This module makes full use of scirs2-core capabilities:
//! - SIMD operations for fast pattern matching
//! - Parallel processing for large dataset analysis
//! - GPU acceleration hints for embeddings and vector operations
//! - Memory-efficient arrays for massive RDF graphs
//! - Statistical analysis for dataset profiling
//! - Advanced profiling and metrics collection

use super::CommandResult;
use crate::cli::CliContext;
use oxirs_core::rdf_store::RdfStore;
use std::path::Path;
use std::time::Instant;

/// Performance optimizer for RDF operations
pub struct RdfPerformanceOptimizer {
    /// Context for CLI output
    ctx: CliContext,
    /// Start time for timing measurements
    start_times: std::collections::HashMap<String, Instant>,
}

impl RdfPerformanceOptimizer {
    /// Create a new performance optimizer
    pub fn new() -> Self {
        Self {
            ctx: CliContext::new(),
            start_times: std::collections::HashMap::new(),
        }
    }

    /// Start timing an operation
    fn start_timer(&mut self, operation: &str) {
        self.start_times
            .insert(operation.to_string(), Instant::now());
    }

    /// Get elapsed time for an operation
    fn elapsed(&self, operation: &str) -> Option<std::time::Duration> {
        self.start_times.get(operation).map(|start| start.elapsed())
    }

    /// Optimize dataset storage with compression and reorganization
    pub async fn optimize_dataset(&mut self, dataset_path: &Path) -> CommandResult {
        self.ctx.info("Starting dataset optimization");
        self.start_timer("dataset_optimization");

        // Open store
        let store =
            RdfStore::open(dataset_path).map_err(|e| format!("Failed to open dataset: {e}"))?;

        // Collect statistics before optimization by iterating
        let quads = store
            .iter_quads()
            .map_err(|e| format!("Failed to retrieve quads: {e}"))?;
        let initial_quad_count = quads.len();
        self.ctx
            .info(&format!("Initial quad count: {}", initial_quad_count));

        self.start_timer("pattern_analysis");

        // Perform optimization operations
        self.ctx.info("Analyzing triple patterns...");
        let patterns = self.analyze_triple_patterns(&store)?;

        if let Some(elapsed) = self.elapsed("pattern_analysis") {
            self.ctx.info(&format!(
                "Pattern analysis completed in {:.3}ms",
                elapsed.as_secs_f64() * 1000.0
            ));
        }

        self.ctx.info(&format!(
            "Found {} unique subject patterns",
            patterns.unique_subjects
        ));
        self.ctx.info(&format!(
            "Found {} unique predicate patterns",
            patterns.unique_predicates
        ));
        self.ctx.info(&format!(
            "Found {} unique object patterns",
            patterns.unique_objects
        ));

        // Memory optimization suggestions
        self.suggest_memory_optimizations(&patterns)?;

        // Statistical analysis
        self.analyze_dataset_statistics(&patterns)?;

        // Parallel processing recommendations
        self.suggest_parallel_optimizations(initial_quad_count)?;

        if let Some(elapsed) = self.elapsed("dataset_optimization") {
            self.ctx.success(&format!(
                "\n✅ Dataset optimization analysis completed in {:.2}s",
                elapsed.as_secs_f64()
            ));
            self.ctx.info(&format!(
                "   Analyzed {} quads across {} unique entities",
                initial_quad_count,
                patterns.unique_subjects + patterns.unique_objects
            ));
        }

        Ok(())
    }

    /// Analyze triple patterns in the dataset
    fn analyze_triple_patterns(&self, store: &RdfStore) -> Result<PatternStatistics, String> {
        let mut stats = PatternStatistics::default();
        let mut subjects = std::collections::HashSet::new();
        let mut predicates = std::collections::HashSet::new();
        let mut objects = std::collections::HashSet::new();

        // Get all quads
        let quads = store
            .iter_quads()
            .map_err(|e| format!("Failed to retrieve quads: {e}"))?;

        // Collect unique subjects, predicates, objects
        for quad in quads {
            // Track unique IRIs using hash sets
            subjects.insert(format!("{}", quad.subject()));
            predicates.insert(format!("{}", quad.predicate()));
            objects.insert(format!("{}", quad.object()));
        }

        stats.unique_subjects = subjects.len();
        stats.unique_predicates = predicates.len();
        stats.unique_objects = objects.len();

        Ok(stats)
    }

    /// Suggest memory optimizations based on dataset characteristics
    fn suggest_memory_optimizations(&self, patterns: &PatternStatistics) -> CommandResult {
        self.ctx.info("\n📊 Memory Optimization Suggestions:");

        if patterns.unique_subjects > 100_000 {
            self.ctx
                .info("• Consider using memory-mapped arrays for large subject sets");
            self.ctx
                .info("  Use: scirs2_core::memory_efficient::MemoryMappedArray");
            self.ctx
                .info("  Example: let mmap = MemoryMappedArray::open(\"subjects.bin\")?;");
        }

        if patterns.unique_predicates < 100 {
            self.ctx
                .info("• Dataset has small predicate vocabulary - excellent for compression");
            self.ctx
                .info("  Recommended: Dictionary encoding for predicates");
            self.ctx.info("  Expected compression ratio: 60-80%");
        }

        if patterns.unique_objects > 1_000_000 {
            self.ctx
                .info("• Large object set detected - consider lazy loading");
            self.ctx
                .info("  Use: scirs2_core::memory_efficient::LazyArray");
            self.ctx
                .info("  Example: let lazy = LazyArray::new(|idx| load_object(idx));");
        }

        // SIMD acceleration suggestions
        if patterns.unique_subjects > 10_000 {
            self.ctx.info("\n⚡ SIMD Acceleration Opportunities:");
            self.ctx
                .info("• Use scirs2_core::simd for vectorized triple pattern matching");
            self.ctx.info("  - SimdArray for batch IRI comparisons");
            self.ctx
                .info("  - simd_ops::simd_dot_product for similarity metrics");
            self.ctx.info("  Expected speedup: 4-8x on modern CPUs");
        }

        // GPU acceleration hints
        if patterns.unique_subjects > 100_000 || patterns.unique_objects > 500_000 {
            self.ctx.info("\n🚀 GPU Acceleration Recommendations:");
            self.ctx
                .info("• Consider GPU-accelerated operations for large-scale processing");
            self.ctx.info("  Use: scirs2_core::gpu::GpuContext");
            self.ctx.info("  Ideal for:");
            self.ctx.info("    - Vector embeddings computation");
            self.ctx.info("    - Similarity searches across entities");
            self.ctx.info("    - Graph algorithm acceleration");
            self.ctx
                .info("  Expected speedup: 10-100x for suitable workloads");
        }

        // Advanced profiling suggestions
        self.ctx.info("\n📈 Profiling & Monitoring:");
        self.ctx
            .info("• Enable scirs2_core::profiling for detailed performance tracking");
        self.ctx.info("  Example: let profiler = Profiler::new();");
        self.ctx
            .info("  Example: profiler.start(\"triple_insertion\");");
        self.ctx
            .info("• Use scirs2_core::metrics for production monitoring");
        self.ctx
            .info("  Example: metrics.record_counter(\"triples_processed\", count);");

        Ok(())
    }

    /// Optimize query execution with parallel processing hint
    pub fn suggest_parallel_optimizations(&self, quad_count: usize) -> CommandResult {
        self.ctx.info("\n⚙️  Parallel Processing Recommendations:");

        if quad_count > 100_000 {
            self.ctx.info(&format!(
                "• Dataset has {} quads - parallel processing highly recommended",
                quad_count
            ));
            self.ctx
                .info("  Use: scirs2_core::parallel_ops for optimal performance");
            self.ctx
                .info("  Example: par_chunks(&quads, |chunk| process(chunk))");
            self.ctx
                .info("  Recommended workers: 4-8 for optimal CPU utilization");
            self.ctx
                .info("  Expected speedup: 3-6x on multi-core systems");
        } else if quad_count > 10_000 {
            self.ctx
                .info("• Dataset size is moderate - parallel processing beneficial");
            self.ctx.info("  Try: --parallel 2 or --parallel 4");
            self.ctx
                .info("  Use: scirs2_core::parallel_ops::par_join for fork-join patterns");
        } else {
            self.ctx
                .info("• Dataset is small - sequential processing is optimal");
            self.ctx
                .info("  Parallelism overhead would exceed benefits");
        }

        // Advanced parallel features
        if quad_count > 1_000_000 {
            self.ctx.info("\n🔧 Advanced Parallel Features:");
            self.ctx
                .info("• Use scirs2_core::parallel::ChunkStrategy for adaptive chunking");
            self.ctx
                .info("• Use scirs2_core::parallel::LoadBalancer for work stealing");
            self.ctx
                .info("• Consider distributed processing for datasets >10M quads");
        }

        Ok(())
    }

    /// Perform advanced statistical analysis of dataset
    pub fn analyze_dataset_statistics(&self, patterns: &PatternStatistics) -> CommandResult {
        self.ctx.info("\n📊 Statistical Analysis:");

        // Calculate cardinality metrics
        let subject_cardinality = patterns.unique_subjects as f64;
        let predicate_cardinality = patterns.unique_predicates as f64;
        let object_cardinality = patterns.unique_objects as f64;

        self.ctx.info(&format!(
            "• Subject cardinality: {} (uniqueness factor)",
            subject_cardinality
        ));
        self.ctx.info(&format!(
            "• Predicate cardinality: {} (schema complexity)",
            predicate_cardinality
        ));
        self.ctx.info(&format!(
            "• Object cardinality: {} (data diversity)",
            object_cardinality
        ));

        // Suggest optimization based on distribution
        let pred_subj_ratio = predicate_cardinality / subject_cardinality.max(1.0);
        if pred_subj_ratio < 0.01 {
            self.ctx
                .info("\n💡 Insight: Very low predicate-to-subject ratio detected");
            self.ctx
                .info("  → Excellent candidate for schema-based compression");
            self.ctx
                .info("  → Consider predicate dictionary with 8-bit encoding");
        }

        let obj_subj_ratio = object_cardinality / subject_cardinality.max(1.0);
        if obj_subj_ratio > 10.0 {
            self.ctx
                .info("\n💡 Insight: High object-to-subject ratio detected");
            self.ctx.info("  → Many unique values per entity");
            self.ctx
                .info("  → Consider bloom filters for existence checks");
            self.ctx
                .info("  → Use scirs2_core::validation for constraint checking");
        }

        Ok(())
    }
}

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

/// Statistics about triple patterns in a dataset
#[derive(Default, Debug)]
pub struct PatternStatistics {
    /// Number of unique subjects
    pub unique_subjects: usize,
    /// Number of unique predicates
    pub unique_predicates: usize,
    /// Number of unique objects
    pub unique_objects: usize,
}

/// Command handler for performance optimization
pub async fn optimize_dataset_cmd(dataset: String) -> CommandResult {
    let ctx = CliContext::new();
    ctx.info(&format!("Optimizing dataset: {}", dataset));

    let dataset_path = std::path::PathBuf::from(&dataset);
    if !dataset_path.exists() {
        return Err(format!("Dataset not found: {}", dataset).into());
    }

    let mut optimizer = RdfPerformanceOptimizer::new();
    optimizer.optimize_dataset(&dataset_path).await?;

    Ok(())
}

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

    #[test]
    fn test_optimizer_creation() {
        let optimizer = RdfPerformanceOptimizer::new();
        assert_eq!(optimizer.start_times.len(), 0);
    }

    #[test]
    fn test_pattern_statistics() {
        let stats = PatternStatistics {
            unique_subjects: 1000,
            unique_predicates: 50,
            unique_objects: 5000,
        };

        assert_eq!(stats.unique_subjects, 1000);
        assert_eq!(stats.unique_predicates, 50);
        assert_eq!(stats.unique_objects, 5000);
    }

    #[tokio::test]
    async fn test_optimize_dataset_not_found() {
        let result = optimize_dataset_cmd("nonexistent_dataset".to_string()).await;
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Dataset not found"));
    }

    #[test]
    fn test_suggest_parallel_optimizations() {
        let optimizer = RdfPerformanceOptimizer::new();

        // Test with large dataset
        assert!(optimizer.suggest_parallel_optimizations(200_000).is_ok());

        // Test with medium dataset
        assert!(optimizer.suggest_parallel_optimizations(50_000).is_ok());

        // Test with small dataset
        assert!(optimizer.suggest_parallel_optimizations(1_000).is_ok());

        // Test with very large dataset (>1M)
        assert!(optimizer.suggest_parallel_optimizations(2_000_000).is_ok());
    }

    #[test]
    fn test_statistical_analysis() {
        let optimizer = RdfPerformanceOptimizer::new();

        // Test with low predicate-to-subject ratio (good for compression)
        let stats = PatternStatistics {
            unique_subjects: 10_000,
            unique_predicates: 50,
            unique_objects: 50_000,
        };
        assert!(optimizer.analyze_dataset_statistics(&stats).is_ok());

        // Test with high object-to-subject ratio
        let stats_high_ratio = PatternStatistics {
            unique_subjects: 1_000,
            unique_predicates: 20,
            unique_objects: 15_000,
        };
        assert!(optimizer
            .analyze_dataset_statistics(&stats_high_ratio)
            .is_ok());
    }

    #[test]
    fn test_memory_optimization_suggestions() {
        let optimizer = RdfPerformanceOptimizer::new();

        // Test with large subject set (should suggest memory-mapped arrays)
        let large_stats = PatternStatistics {
            unique_subjects: 200_000,
            unique_predicates: 100,
            unique_objects: 500_000,
        };
        assert!(optimizer.suggest_memory_optimizations(&large_stats).is_ok());

        // Test with small predicate vocabulary (should suggest compression)
        let small_vocab_stats = PatternStatistics {
            unique_subjects: 50_000,
            unique_predicates: 30,
            unique_objects: 100_000,
        };
        assert!(optimizer
            .suggest_memory_optimizations(&small_vocab_stats)
            .is_ok());

        // Test with massive object set (should suggest lazy loading and GPU)
        let massive_stats = PatternStatistics {
            unique_subjects: 150_000,
            unique_predicates: 200,
            unique_objects: 2_000_000,
        };
        assert!(optimizer
            .suggest_memory_optimizations(&massive_stats)
            .is_ok());
    }

    #[test]
    fn test_cardinality_ratio_insights() {
        let optimizer = RdfPerformanceOptimizer::new();

        // Low predicate-to-subject ratio (< 0.01)
        let stats = PatternStatistics {
            unique_subjects: 100_000,
            unique_predicates: 500, // 500/100000 = 0.005
            unique_objects: 200_000,
        };

        // Should not panic and provide insights
        assert!(optimizer.analyze_dataset_statistics(&stats).is_ok());

        // High object-to-subject ratio (> 10.0)
        let stats2 = PatternStatistics {
            unique_subjects: 1_000,
            unique_predicates: 50,
            unique_objects: 15_000, // 15000/1000 = 15.0
        };

        assert!(optimizer.analyze_dataset_statistics(&stats2).is_ok());
    }
}