oxirs-core 0.2.1

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
# OxiRS Core - Best Practices Guide

This guide provides best practices for using OxiRS Core in production environments, covering performance optimization, error handling, monitoring, and scalability.

## Table of Contents

1. [Performance Best Practices]#performance-best-practices
2. [Error Handling]#error-handling
3. [Monitoring and Observability]#monitoring-and-observability
4. [Security]#security
5. [Scalability Patterns]#scalability-patterns
6. [Data Management]#data-management
7. [Testing Strategies]#testing-strategies

## Performance Best Practices

### 1. Choose the Right Storage Backend

```rust
// For small datasets (<1M triples) - Use in-memory store
let store = RdfStore::new();

// For medium datasets (1M-10M triples) - Use memory-mapped store
let store = MmapStore::open("data.db")?;

// For large datasets (>10M triples) - Use clustered store
let store = ClusteredStore::new(cluster_config)?;
```

### 2. Use Batch Operations

```rust
// ❌ BAD: Insert one-by-one
for triple in triples {
    store.insert_triple(&triple)?;  // Slow!
}

// ✅ GOOD: Use batch insert (50-100x faster)
store.insert_batch(&triples)?;
```

### 3. Enable Query Result Caching

```rust
use oxirs_core::cache::QueryResultCache;
use std::time::Duration;

let cache = QueryResultCache::new()
    .with_max_entries(10_000)
    .with_ttl(Duration::from_secs(300))  // 5 minutes
    .build();

// Cache frequently executed queries
let results = executor.execute_with_cache(query, &cache)?;

// Expected performance:
// - Cache hits: <1ms
// - Cache misses: original query time
// - Hit rate target: >80% for production workloads
```

### 4. Optimize SPARQL Queries

```rust
// ❌ BAD: Unbounded queries
let bad_query = r#"
    SELECT * WHERE { ?s ?p ?o }
"#;

// ✅ GOOD: Use LIMIT and specific patterns
let good_query = r#"
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    SELECT ?name WHERE {
        ?person a foaf:Person .
        ?person foaf:name ?name .
    }
    LIMIT 1000
"#;
```

### 5. Use Proper Indexing

```rust
// Let the query optimizer choose indexes automatically
// But you can hint with specific patterns:

// For subject-focused queries
let results = store.quads_matching(
    Some(&subject),  // Specific subject
    None,           // Any predicate
    None,           // Any object
    None            // Any graph
)?;  // Uses SPO index

// For predicate-focused queries (like property search)
let results = store.quads_matching(
    None,                  // Any subject
    Some(&predicate),      // Specific predicate
    None,                  // Any object
    None                   // Any graph
)?;  // Uses POS index
```

### 6. Parallel Processing for Large Datasets

```rust
use oxirs_core::batch::BatchProcessor;

let processor = BatchProcessor::new()
    .with_batch_size(1000)
    .with_parallelism(num_cpus::get());  // Use all available cores

processor.insert_batch(&store, &large_triple_set)?;

// Performance: 3-8x speedup on bulk operations
```

### 7. Use Zero-Copy Parsing

```rust
use oxirs_core::parser::zero_copy::ZeroCopyParser;

// ❌ BAD: Regular parsing (allocates memory)
let triples = parse_ntriples(data)?;

// ✅ GOOD: Zero-copy parsing (60-80% less memory)
let parser = ZeroCopyParser::new();
let triples = parser.parse_ntriples_zero_copy(data)?;
```

## Error Handling

### 1. Use Result Types Properly

```rust
use oxirs_core::OxirsError;

fn process_rdf_data(path: &str) -> Result<(), OxirsError> {
    let store = RdfStore::new();

    // Use ? operator for error propagation
    let triples = parse_file(path, RdfFormat::Turtle)?;
    store.insert_batch(&triples)?;

    Ok(())
}
```

### 2. Handle Transaction Failures

```rust
use oxirs_core::transaction::TransactionError;

fn safe_transaction(store: &RdfStore, data: &[Triple]) -> Result<(), OxirsError> {
    let mut tx = store.begin_transaction(IsolationLevel::Snapshot)?;

    // Use match for specific error handling
    match tx.insert_batch(data) {
        Ok(_) => tx.commit(),
        Err(TransactionError::Conflict) => {
            // Retry on conflicts
            tx.rollback()?;
            // Implement retry logic here
            Err(OxirsError::Transaction("Conflict detected".into()))
        }
        Err(e) => {
            tx.rollback()?;
            Err(e.into())
        }
    }
}
```

### 3. Implement Retry Logic

```rust
use std::time::Duration;
use std::thread;

fn execute_with_retry<F, T>(
    mut f: F,
    max_retries: u32,
    backoff_ms: u64
) -> Result<T, OxirsError>
where
    F: FnMut() -> Result<T, OxirsError>,
{
    let mut attempts = 0;

    loop {
        match f() {
            Ok(result) => return Ok(result),
            Err(e) if attempts < max_retries && e.is_retryable() => {
                attempts += 1;
                thread::sleep(Duration::from_millis(backoff_ms * attempts as u64));
            }
            Err(e) => return Err(e),
        }
    }
}

// Usage
execute_with_retry(
    || store.insert_triple(&triple),
    max_retries: 3,
    backoff_ms: 100
)?;
```

## Monitoring and Observability

### 1. Enable Metrics Collection

```rust
use oxirs_core::metrics::MetricsCollector;

let metrics = MetricsCollector::new();

// Track query performance
metrics.record_query_execution(query_id, duration);

// Track cache performance
metrics.record_cache_hit();
metrics.record_cache_miss();

// Track storage metrics
metrics.record_storage_size(bytes);
metrics.record_triple_count(count);

// Export metrics (Prometheus format)
let prometheus_output = metrics.export_prometheus();
```

### 2. Query Profiling in Production

```rust
use oxirs_core::profiling::QueryProfiler;

let profiler = QueryProfiler::new()
    .with_sampling_rate(0.1)  // Profile 10% of queries
    .with_slow_query_threshold_ms(1000)  // Log queries >1s
    .build();

// Only slow queries and sampled queries are profiled
let results = executor.execute_with_profiling(query, &profiler)?;

// Get aggregated statistics
let stats = profiler.get_aggregated_stats()?;
println!("Avg query time: {}ms", stats.avg_execution_time_ms);
println!("P95 query time: {}ms", stats.p95_execution_time_ms);
println!("Slow queries: {}", stats.slow_query_count);
```

### 3. Health Checks

```rust
use oxirs_core::health::HealthChecker;

let health = HealthChecker::new();

// Register health checks
health.register_check("store_size", || {
    let size = store.size()?;
    if size < 1_000_000_000 {  // < 1GB
        Ok(HealthStatus::Healthy)
    } else {
        Ok(HealthStatus::Warning("Store size approaching limit".into()))
    }
});

health.register_check("query_latency", || {
    let latency = metrics.avg_query_latency_ms();
    if latency < 100.0 {
        Ok(HealthStatus::Healthy)
    } else {
        Ok(HealthStatus::Degraded("High query latency".into()))
    }
});

// Check overall health
let status = health.check_all()?;
```

### 4. Logging Best Practices

```rust
use log::{debug, info, warn, error};

// ✅ GOOD: Structured logging with context
info!(
    "Query executed successfully";
    "query_id" => query_id,
    "execution_time_ms" => duration.as_millis(),
    "triples_matched" => count
);

// ❌ BAD: Unstructured logging
println!("Query done in {}ms", duration.as_millis());
```

## Security

### 1. Input Validation

```rust
use oxirs_core::validation::validate_sparql;

fn execute_user_query(query: &str) -> Result<QueryResults, OxirsError> {
    // Validate query syntax and check for injection attacks
    validate_sparql(query)?;

    // Limit query complexity
    let complexity = analyze_query_complexity(query)?;
    if complexity.depth > 10 || complexity.joins > 100 {
        return Err(OxirsError::Query("Query too complex".into()));
    }

    executor.execute(query)
}
```

### 2. Resource Limits

```rust
use oxirs_core::limits::ResourceLimits;

let limits = ResourceLimits::new()
    .with_max_query_time(Duration::from_secs(30))
    .with_max_memory_per_query(100_000_000)  // 100MB
    .with_max_results(10_000)
    .build();

let executor = QueryExecutor::new(store)
    .with_limits(limits);
```

### 3. Access Control

```rust
use oxirs_core::security::{AccessControl, Permission};

let acl = AccessControl::new();

// Define permissions
acl.grant_permission(user_id, Permission::Read)?;
acl.grant_permission(admin_id, Permission::Write)?;
acl.grant_permission(admin_id, Permission::Admin)?;

// Check permissions before operations
fn insert_triple(
    store: &RdfStore,
    triple: &Triple,
    user_id: &str,
    acl: &AccessControl
) -> Result<(), OxirsError> {
    if !acl.has_permission(user_id, Permission::Write)? {
        return Err(OxirsError::Unauthorized);
    }

    store.insert_triple(triple)
}
```

## Scalability Patterns

### 1. Horizontal Scaling with Clustering

```rust
use oxirs_core::cluster::{ClusterConfig, ClusterStore};

let config = ClusterConfig {
    nodes: vec![
        "node1.example.com:5000",
        "node2.example.com:5000",
        "node3.example.com:5000",
    ],
    replication_factor: 3,
    consistency_level: ConsistencyLevel::Quorum,
};

let store = ClusterStore::new(config)?;

// Data automatically distributed across nodes
// Queries automatically parallelized
```

### 2. Read Replicas

```rust
use oxirs_core::replication::{PrimaryStore, ReplicaStore};

// Primary store (for writes)
let primary = PrimaryStore::new("primary.db")?;

// Read replicas (for read scaling)
let replica1 = ReplicaStore::connect(&primary)?;
let replica2 = ReplicaStore::connect(&primary)?;

// Route reads to replicas, writes to primary
fn handle_query(query: &str, is_write: bool) -> Result<QueryResults, OxirsError> {
    if is_write {
        primary.execute(query)
    } else {
        // Load balance across replicas
        let replica = select_replica(&[replica1, replica2]);
        replica.execute(query)
    }
}
```

### 3. Caching Strategies

```rust
// Multi-level caching
use oxirs_core::cache::{L1Cache, L2Cache, CacheHierarchy};

let cache = CacheHierarchy::new()
    .with_l1(L1Cache::new(1000))      // In-memory, hot queries
    .with_l2(L2Cache::new(10_000))    // Larger, warm queries
    .with_ttl(Duration::from_secs(300))
    .build();

// Cache automatically manages eviction and promotion
```

## Data Management

### 1. Regular Backups

```rust
use oxirs_core::backup::{BackupManager, BackupType};

let backup_mgr = BackupManager::new();

// Full backup
backup_mgr.create_backup(
    &store,
    "backup/full_20251225.db",
    BackupType::Full
)?;

// Incremental backup (only changes since last backup)
backup_mgr.create_backup(
    &store,
    "backup/incr_20251225_1200.db",
    BackupType::Incremental
)?;

// Restore from backup chain
backup_mgr.restore(
    &store,
    &["backup/full_20251220.db", "backup/incr_20251225_1200.db"]
)?;
```

### 2. Data Compaction

```rust
// Compact store to remove deleted triples and reclaim space
store.compact()?;

// Schedule regular compaction
use std::time::Duration;

let compaction_interval = Duration::from_secs(3600);  // Every hour
schedule_periodic_task(compaction_interval, || {
    if store.deleted_ratio() > 0.2 {  // >20% deleted
        store.compact()?;
    }
    Ok(())
});
```

### 3. Data Versioning

```rust
use oxirs_core::versioning::VersionedStore;

let store = VersionedStore::new();

// Create snapshots
let snapshot_id = store.create_snapshot("v1.0")?;

// Make changes
store.insert_triple(&triple)?;

// Revert to snapshot if needed
store.restore_snapshot(snapshot_id)?;
```

## Testing Strategies

### 1. Unit Testing

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

    #[test]
    fn test_triple_insertion() {
        let store = RdfStore::new();
        let triple = create_test_triple();

        assert!(store.insert_triple(&triple).is_ok());
        assert_eq!(store.len(), 1);
    }

    #[test]
    fn test_query_execution() {
        let store = setup_test_store();
        let executor = QueryExecutor::new(store);

        let results = executor.execute("SELECT * WHERE { ?s ?p ?o } LIMIT 10")
            .expect("Query should succeed");

        assert!(results.len() <= 10);
    }
}
```

### 2. Integration Testing

```rust
#[cfg(test)]
mod integration_tests {
    #[test]
    fn test_full_workflow() {
        // Setup
        let store = RdfStore::new();
        let executor = QueryExecutor::new(store.clone());

        // Load data
        let triples = load_test_data("test_data.ttl")?;
        store.insert_batch(&triples)?;

        // Execute query
        let results = executor.execute(TEST_QUERY)?;

        // Verify results
        assert_eq!(results.len(), EXPECTED_COUNT);
    }
}
```

### 3. Performance Testing

```rust
#[cfg(test)]
mod benchmarks {
    use criterion::{black_box, criterion_group, criterion_main, Criterion};

    fn benchmark_insert(c: &mut Criterion) {
        let store = RdfStore::new();
        let triples = generate_test_triples(1000);

        c.bench_function("insert_1000_triples", |b| {
            b.iter(|| {
                store.insert_batch(black_box(&triples))
            });
        });
    }

    criterion_group!(benches, benchmark_insert);
    criterion_main!(benches);
}
```

### 4. Property-Based Testing

```rust
#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn test_triple_roundtrip(
            subject in any::<String>(),
            predicate in any::<String>(),
            object in any::<String>()
        ) {
            let store = RdfStore::new();
            let triple = create_triple(&subject, &predicate, &object);

            store.insert_triple(&triple)?;
            let retrieved = store.get_triple(&triple)?;

            prop_assert_eq!(triple, retrieved);
        }
    }
}
```

## Production Checklist

Before deploying to production, ensure:

- [ ] **Performance**
  - [ ] Query result caching enabled
  - [ ] Batch operations used for bulk data
  - [ ] Appropriate storage backend selected
  - [ ] Query profiling configured

- [ ] **Reliability**
  - [ ] Transactions enabled with appropriate isolation
  - [ ] WAL configured for durability
  - [ ] Backup strategy implemented
  - [ ] Error handling and retry logic in place

- [ ] **Monitoring**
  - [ ] Metrics collection enabled
  - [ ] Health checks configured
  - [ ] Logging properly structured
  - [ ] Alerts set up for critical issues

- [ ] **Security**
  - [ ] Input validation implemented
  - [ ] Resource limits configured
  - [ ] Access control in place
  - [ ] Query complexity limits set

- [ ] **Scalability**
  - [ ] Capacity planning completed
  - [ ] Horizontal scaling strategy defined
  - [ ] Caching strategy implemented
  - [ ] Read replicas configured if needed

- [ ] **Testing**
  - [ ] Unit tests passing
  - [ ] Integration tests passing
  - [ ] Performance benchmarks completed
  - [ ] Load testing performed

---

Following these best practices will help ensure your OxiRS Core deployment is performant, reliable, and scalable in production environments.

For more information, see:
- [Tutorial]TUTORIAL.md - Getting started guide
- [Architecture]ARCHITECTURE.md - Deep dive into internals
- [Performance Guide]PERFORMANCE_GUIDE.md - Detailed optimization strategies
- [Deployment Handbook]DEPLOYMENT.md - Production deployment guide