oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Hash-keyed Query Result Cache with Dataset Version Tracking
//!
//! Provides:
//! - `QueryResultCache`: LRU-based cache keyed by `(query_hash, dataset_version)`
//! - `DatasetVersionTracker`: monotonic version counter per dataset for invalidation
//!
//! Design goals
//! - Zero `unwrap()` usage
//! - Thread-safe via `Arc<Mutex<...>>`
//! - TTL-aware: expired entries are evicted on access and by explicit sweep
//! - LRU eviction when the capacity ceiling is reached

use crate::error::{FusekiError, FusekiResult};
use lru::LruCache;
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

// ──────────────────────────────────────────────────────────────────────────────
// Public types
// ──────────────────────────────────────────────────────────────────────────────

/// A query result binding row — maps variable name → value string.
pub type Binding = HashMap<String, String>;

/// Composite cache key: combines a query hash (e.g. `DefaultHasher` over the
/// normalized SPARQL string) with the dataset's current version counter.
///
/// When the dataset is mutated via SPARQL UPDATE, `DatasetVersionTracker`
/// increments the version, making all previously stored keys stale.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct QueryCacheKey {
    /// 64-bit hash of the normalized SPARQL query string
    pub query_hash: u64,
    /// Dataset version at the time the query was cached
    pub dataset_version: u64,
}

impl QueryCacheKey {
    /// Convenience constructor
    pub fn new(query_hash: u64, dataset_version: u64) -> Self {
        QueryCacheKey {
            query_hash,
            dataset_version,
        }
    }
}

/// A single cached query result entry.
#[derive(Debug, Clone)]
pub struct CacheEntry {
    /// The result rows returned by the SPARQL query
    pub results: Vec<Binding>,
    /// When this entry was inserted
    pub cached_at: Instant,
    /// Time-to-live in milliseconds
    pub ttl_ms: u64,
    /// Number of times this entry was served from cache
    pub hit_count: u64,
}

impl CacheEntry {
    fn new(results: Vec<Binding>, ttl_ms: u64) -> Self {
        CacheEntry {
            results,
            cached_at: Instant::now(),
            ttl_ms,
            hit_count: 0,
        }
    }

    /// Returns `true` if the TTL for this entry has elapsed.
    pub fn is_expired(&self) -> bool {
        self.cached_at.elapsed().as_millis() as u64 >= self.ttl_ms
    }
}

/// Snapshot statistics for the cache.
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Cumulative cache hits
    pub hits: u64,
    /// Cumulative cache misses
    pub misses: u64,
    /// Cumulative LRU evictions
    pub evictions: u64,
    /// Current number of entries in the cache
    pub size: usize,
}

impl CacheStats {
    /// Hit rate in [0.0, 1.0].
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// QueryResultCache internals
// ──────────────────────────────────────────────────────────────────────────────

struct CacheInner {
    lru: LruCache<QueryCacheKey, CacheEntry>,
    /// Index: dataset_version → set of keys (for bulk invalidation)
    version_index: HashMap<u64, Vec<QueryCacheKey>>,
    evictions: u64,
}

impl CacheInner {
    fn new(capacity: NonZeroUsize) -> Self {
        CacheInner {
            lru: LruCache::new(capacity),
            version_index: HashMap::new(),
            evictions: 0,
        }
    }

    /// Insert an entry, updating the version index and tracking LRU evictions.
    fn insert(&mut self, key: QueryCacheKey, entry: CacheEntry) {
        // Register in version index
        self.version_index
            .entry(key.dataset_version)
            .or_default()
            .push(key);

        // LRU insert; if an old entry is evicted track it
        if let Some((evicted_key, _)) = self.lru.push(key, entry) {
            self.evictions += 1;
            // Clean up the version index for the evicted key
            if let Some(vec) = self.version_index.get_mut(&evicted_key.dataset_version) {
                vec.retain(|k| k != &evicted_key);
            }
        }
    }

    /// Remove a single entry and clean up the version index.
    fn remove(&mut self, key: &QueryCacheKey) {
        self.lru.pop(key);
        if let Some(vec) = self.version_index.get_mut(&key.dataset_version) {
            vec.retain(|k| k != key);
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// QueryResultCache
// ──────────────────────────────────────────────────────────────────────────────

/// LRU-based cache for SPARQL query results, keyed by `(query_hash, dataset_version)`.
///
/// Thread-safe: all public methods take `&self`.
pub struct QueryResultCache {
    inner: Arc<Mutex<CacheInner>>,
    default_ttl_ms: u64,
    hits: Arc<AtomicU64>,
    misses: Arc<AtomicU64>,
}

impl QueryResultCache {
    /// Create a new cache.
    ///
    /// * `capacity`        — maximum number of entries (LRU eviction beyond this)
    /// * `default_ttl_ms`  — default TTL in milliseconds used by `put`
    pub fn new(capacity: usize, default_ttl_ms: u64) -> FusekiResult<Self> {
        let cap = NonZeroUsize::new(capacity).ok_or_else(|| FusekiError::Configuration {
            message: "QueryResultCache capacity must be > 0".to_string(),
        })?;
        Ok(QueryResultCache {
            inner: Arc::new(Mutex::new(CacheInner::new(cap))),
            default_ttl_ms,
            hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
        })
    }

    /// Look up a cache entry.
    ///
    /// Returns `None` on miss or if the entry has expired (and removes it).
    pub fn get(&self, key: &QueryCacheKey) -> Option<Vec<Binding>> {
        let mut inner = self
            .inner
            .lock()
            .map_err(|e| {
                tracing::error!("QueryResultCache lock poisoned on get: {}", e);
                e
            })
            .ok()?;

        // Peek first to check expiry without promoting in LRU
        if let Some(entry) = inner.lru.peek(key) {
            if entry.is_expired() {
                let key_clone = *key;
                inner.remove(&key_clone);
                self.misses.fetch_add(1, Ordering::Relaxed);
                return None;
            }
        }

        // Promote (update LRU order) and return a clone
        if let Some(entry) = inner.lru.get_mut(key) {
            entry.hit_count += 1;
            let results = entry.results.clone();
            self.hits.fetch_add(1, Ordering::Relaxed);
            Some(results)
        } else {
            self.misses.fetch_add(1, Ordering::Relaxed);
            None
        }
    }

    /// Store a query result.
    ///
    /// * `key`     — composite cache key
    /// * `results` — the binding rows to cache
    /// * `ttl_ms`  — time-to-live in milliseconds for this entry;
    ///   use `put_default_ttl` to apply the cache's default TTL
    pub fn put(&self, key: QueryCacheKey, results: Vec<Binding>, ttl_ms: u64) {
        let entry = CacheEntry::new(results, ttl_ms);
        match self.inner.lock() {
            Ok(mut inner) => inner.insert(key, entry),
            Err(e) => tracing::error!("QueryResultCache lock poisoned on put: {}", e),
        }
    }

    /// Convenience: `put` using the default TTL configured at construction.
    pub fn put_default_ttl(&self, key: QueryCacheKey, results: Vec<Binding>) {
        let ttl = self.default_ttl_ms;
        self.put(key, results, ttl);
    }

    /// Invalidate all entries cached under a specific `dataset_version`.
    ///
    /// Call this immediately after a SPARQL UPDATE has incremented the dataset
    /// version, so that stale results from the old version are evicted.
    pub fn invalidate_dataset(&self, dataset_version: u64) {
        let mut inner = match self.inner.lock() {
            Ok(g) => g,
            Err(e) => {
                tracing::error!("QueryResultCache lock poisoned on invalidate: {}", e);
                return;
            }
        };

        let keys: Vec<QueryCacheKey> = inner
            .version_index
            .remove(&dataset_version)
            .unwrap_or_default();

        for key in &keys {
            inner.lru.pop(key);
        }
    }

    /// Remove all entries whose TTL has elapsed.
    ///
    /// This is an O(n) sweep — call periodically from a background task.
    pub fn evict_expired(&self) {
        let mut inner = match self.inner.lock() {
            Ok(g) => g,
            Err(e) => {
                tracing::error!("QueryResultCache lock poisoned on evict_expired: {}", e);
                return;
            }
        };

        // Collect expired keys without modifying the LruCache while iterating
        let expired: Vec<QueryCacheKey> = inner
            .lru
            .iter()
            .filter(|(_, entry)| entry.is_expired())
            .map(|(key, _)| *key)
            .collect();

        for key in &expired {
            inner.remove(key);
        }
    }

    /// Snapshot statistics.
    pub fn stats(&self) -> CacheStats {
        let size = self.inner.lock().map(|inner| inner.lru.len()).unwrap_or(0);

        CacheStats {
            hits: self.hits.load(Ordering::Relaxed),
            misses: self.misses.load(Ordering::Relaxed),
            evictions: self.inner.lock().map(|inner| inner.evictions).unwrap_or(0),
            size,
        }
    }

    /// Hit rate in [0.0, 1.0].
    pub fn hit_rate(&self) -> f64 {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// DatasetVersionTracker
// ──────────────────────────────────────────────────────────────────────────────

/// Tracks a monotonically-increasing version counter per named dataset.
///
/// Each SPARQL UPDATE call should call `increment_version(dataset_id)` to
/// bump the counter.  The new version is then used as the `dataset_version`
/// component of `QueryCacheKey`s for subsequent queries, automatically
/// invalidating all prior cache entries for that dataset.
pub struct DatasetVersionTracker {
    versions: Arc<Mutex<HashMap<String, u64>>>,
}

impl DatasetVersionTracker {
    /// Create a new tracker with no datasets registered.
    pub fn new() -> Self {
        DatasetVersionTracker {
            versions: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Return the current version for `dataset_id`, or `0` if never tracked.
    pub fn get_version(&self, dataset_id: &str) -> u64 {
        self.versions
            .lock()
            .map(|map| *map.get(dataset_id).unwrap_or(&0))
            .unwrap_or(0)
    }

    /// Atomically increment the version for `dataset_id` and return the new value.
    ///
    /// The first increment on a previously-unseen dataset goes from 0 → 1.
    pub fn increment_version(&self, dataset_id: &str) -> u64 {
        match self.versions.lock() {
            Ok(mut map) => {
                let v = map.entry(dataset_id.to_string()).or_insert(0);
                *v = v.saturating_add(1);
                *v
            }
            Err(e) => {
                tracing::error!("DatasetVersionTracker lock poisoned on increment: {}", e);
                0
            }
        }
    }

    /// Reset the version counter for `dataset_id` to 0.
    ///
    /// Use this when a dataset is dropped or re-created from scratch.
    pub fn reset(&self, dataset_id: &str) {
        match self.versions.lock() {
            Ok(mut map) => {
                map.insert(dataset_id.to_string(), 0);
            }
            Err(e) => {
                tracing::error!("DatasetVersionTracker lock poisoned on reset: {}", e);
            }
        }
    }

    /// List all dataset IDs currently tracked.
    pub fn dataset_ids(&self) -> Vec<String> {
        self.versions
            .lock()
            .map(|map| map.keys().cloned().collect())
            .unwrap_or_default()
    }
}

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

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

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

    // ── Helpers ──────────────────────────────────────────────────────────────

    fn make_cache(capacity: usize, ttl_ms: u64) -> QueryResultCache {
        QueryResultCache::new(capacity, ttl_ms).unwrap()
    }

    fn make_key(hash: u64, version: u64) -> QueryCacheKey {
        QueryCacheKey::new(hash, version)
    }

    fn make_bindings(rows: usize) -> Vec<Binding> {
        (0..rows)
            .map(|i| {
                let mut m = Binding::new();
                m.insert("s".to_string(), format!("http://ex.org/s{}", i));
                m
            })
            .collect()
    }

    // ── QueryCacheKey ─────────────────────────────────────────────────────────

    #[test]
    fn test_key_equality() {
        let k1 = make_key(42, 1);
        let k2 = make_key(42, 1);
        assert_eq!(k1, k2);
    }

    #[test]
    fn test_key_inequality_hash() {
        let k1 = make_key(42, 1);
        let k2 = make_key(99, 1);
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_key_inequality_version() {
        let k1 = make_key(42, 1);
        let k2 = make_key(42, 2);
        assert_ne!(k1, k2);
    }

    // ── CacheEntry expiry ─────────────────────────────────────────────────────

    #[test]
    fn test_entry_not_expired_immediately() {
        let entry = CacheEntry::new(vec![], 60_000);
        assert!(!entry.is_expired(), "Should not be expired immediately");
    }

    #[test]
    fn test_entry_expires_after_ttl() {
        let entry = CacheEntry::new(vec![], 1); // 1 ms TTL
        std::thread::sleep(Duration::from_millis(10));
        assert!(entry.is_expired(), "Should be expired after TTL elapsed");
    }

    // ── QueryResultCache: basic hit/miss ──────────────────────────────────────

    #[test]
    fn test_cache_miss_on_empty() {
        let cache = make_cache(100, 60_000);
        let key = make_key(1, 0);
        assert!(cache.get(&key).is_none(), "Empty cache should miss");
    }

    #[test]
    fn test_cache_put_and_hit() {
        let cache = make_cache(100, 60_000);
        let key = make_key(1, 0);
        let bindings = make_bindings(3);

        cache.put(key, bindings.clone(), 60_000);

        let result = cache.get(&make_key(1, 0));
        assert!(result.is_some(), "Should hit after put");
        assert_eq!(result.unwrap().len(), 3);
    }

    #[test]
    fn test_cache_miss_after_ttl_expiry() {
        let cache = make_cache(100, 1); // 1 ms TTL
        let key = make_key(1, 0);
        cache.put(key, make_bindings(1), 1);

        std::thread::sleep(Duration::from_millis(10));

        assert!(
            cache.get(&make_key(1, 0)).is_none(),
            "Should miss after TTL expires"
        );
    }

    #[test]
    fn test_put_default_ttl() {
        let cache = make_cache(100, 60_000);
        let key = make_key(7, 3);
        cache.put_default_ttl(key, make_bindings(2));
        assert!(cache.get(&make_key(7, 3)).is_some());
    }

    // ── QueryResultCache: LRU eviction ────────────────────────────────────────

    #[test]
    fn test_lru_eviction_at_capacity() {
        let cache = make_cache(2, 60_000);

        cache.put(make_key(1, 0), make_bindings(1), 60_000);
        cache.put(make_key(2, 0), make_bindings(1), 60_000);
        // Access key 1 to make key 2 LRU
        let _ = cache.get(&make_key(1, 0));
        // Insert key 3 → should evict key 2 (LRU)
        cache.put(make_key(3, 0), make_bindings(1), 60_000);

        assert!(cache.get(&make_key(1, 0)).is_some(), "key 1 should survive");
        assert!(cache.get(&make_key(3, 0)).is_some(), "key 3 should survive");

        let stats = cache.stats();
        assert!(
            stats.evictions >= 1,
            "Should have at least one LRU eviction"
        );
    }

    // ── QueryResultCache: invalidate_dataset ─────────────────────────────────

    #[test]
    fn test_invalidate_dataset_removes_matching_version() {
        let cache = make_cache(100, 60_000);

        cache.put(make_key(1, 1), make_bindings(1), 60_000);
        cache.put(make_key(2, 1), make_bindings(1), 60_000);
        cache.put(make_key(3, 2), make_bindings(1), 60_000);

        cache.invalidate_dataset(1);

        assert!(
            cache.get(&make_key(1, 1)).is_none(),
            "Version 1, hash 1 should be gone"
        );
        assert!(
            cache.get(&make_key(2, 1)).is_none(),
            "Version 1, hash 2 should be gone"
        );
        assert!(
            cache.get(&make_key(3, 2)).is_some(),
            "Version 2 should survive"
        );
    }

    #[test]
    fn test_invalidate_nonexistent_version_is_noop() {
        let cache = make_cache(100, 60_000);
        cache.put(make_key(1, 0), make_bindings(1), 60_000);
        // Invalidate a version that was never used
        cache.invalidate_dataset(999);
        assert!(cache.get(&make_key(1, 0)).is_some(), "Entry should survive");
    }

    // ── QueryResultCache: evict_expired ──────────────────────────────────────

    #[test]
    fn test_evict_expired_removes_expired_entries() {
        let cache = make_cache(100, 60_000);

        cache.put(make_key(1, 0), make_bindings(1), 1); // expires fast
        cache.put(make_key(2, 0), make_bindings(1), 60_000); // long TTL

        std::thread::sleep(Duration::from_millis(10));

        cache.evict_expired();

        assert!(
            cache.get(&make_key(1, 0)).is_none(),
            "Expired entry should be gone after evict_expired"
        );
        assert!(
            cache.get(&make_key(2, 0)).is_some(),
            "Non-expired entry should remain"
        );
    }

    // ── QueryResultCache: stats ───────────────────────────────────────────────

    #[test]
    fn test_stats_hit_and_miss_counts() {
        let cache = make_cache(100, 60_000);
        let key = make_key(1, 0);
        cache.put(key, make_bindings(1), 60_000);

        cache.get(&make_key(1, 0)); // hit
        cache.get(&make_key(99, 0)); // miss

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.size, 1);
    }

    #[test]
    fn test_stats_size_matches_entries() {
        let cache = make_cache(100, 60_000);
        assert_eq!(cache.stats().size, 0);

        cache.put(make_key(1, 0), make_bindings(1), 60_000);
        assert_eq!(cache.stats().size, 1);

        cache.put(make_key(2, 0), make_bindings(1), 60_000);
        assert_eq!(cache.stats().size, 2);
    }

    // ── QueryResultCache: hit_rate ────────────────────────────────────────────

    #[test]
    fn test_hit_rate_zero_on_empty() {
        let cache = make_cache(100, 60_000);
        assert!((cache.hit_rate() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_hit_rate_one_hit_one_miss() {
        let cache = make_cache(100, 60_000);
        cache.put(make_key(1, 0), make_bindings(1), 60_000);

        cache.get(&make_key(1, 0)); // hit
        cache.get(&make_key(2, 0)); // miss

        assert!(
            (cache.hit_rate() - 0.5).abs() < f64::EPSILON,
            "Expected 0.5, got {}",
            cache.hit_rate()
        );
    }

    #[test]
    fn test_hit_rate_all_misses() {
        let cache = make_cache(100, 60_000);
        cache.get(&make_key(1, 0));
        cache.get(&make_key(2, 0));
        assert!((cache.hit_rate() - 0.0).abs() < f64::EPSILON);
    }

    // ── QueryResultCache: capacity=0 should fail ─────────────────────────────

    #[test]
    fn test_zero_capacity_returns_error() {
        let result = QueryResultCache::new(0, 60_000);
        assert!(result.is_err(), "Zero capacity should fail");
    }

    // ── DatasetVersionTracker ────────────────────────────────────────────────

    #[test]
    fn test_version_tracker_initial_zero() {
        let tracker = DatasetVersionTracker::new();
        assert_eq!(tracker.get_version("ds1"), 0);
    }

    #[test]
    fn test_version_tracker_increment() {
        let tracker = DatasetVersionTracker::new();
        let v1 = tracker.increment_version("ds1");
        assert_eq!(v1, 1, "First increment should return 1");

        let v2 = tracker.increment_version("ds1");
        assert_eq!(v2, 2, "Second increment should return 2");
    }

    #[test]
    fn test_version_tracker_reset() {
        let tracker = DatasetVersionTracker::new();
        tracker.increment_version("ds1");
        tracker.increment_version("ds1");
        tracker.reset("ds1");
        assert_eq!(tracker.get_version("ds1"), 0, "After reset, version is 0");
    }

    #[test]
    fn test_version_tracker_multiple_datasets() {
        let tracker = DatasetVersionTracker::new();
        tracker.increment_version("ds1");
        tracker.increment_version("ds1");
        tracker.increment_version("ds2");

        assert_eq!(tracker.get_version("ds1"), 2);
        assert_eq!(tracker.get_version("ds2"), 1);
        assert_eq!(tracker.get_version("ds3"), 0, "Unknown dataset returns 0");
    }

    #[test]
    fn test_version_tracker_get_after_increment() {
        let tracker = DatasetVersionTracker::new();
        let v = tracker.increment_version("myds");
        assert_eq!(tracker.get_version("myds"), v);
    }

    #[test]
    fn test_version_tracker_dataset_ids() {
        let tracker = DatasetVersionTracker::new();
        tracker.increment_version("a");
        tracker.increment_version("b");
        tracker.increment_version("c");

        let mut ids = tracker.dataset_ids();
        ids.sort();
        assert_eq!(ids, vec!["a", "b", "c"]);
    }

    // ── CacheStats helpers ────────────────────────────────────────────────────

    #[test]
    fn test_cache_stats_hit_rate_method() {
        let stats = CacheStats {
            hits: 3,
            misses: 1,
            evictions: 0,
            size: 3,
        };
        assert!((stats.hit_rate() - 0.75).abs() < f64::EPSILON);
    }

    #[test]
    fn test_cache_stats_hit_rate_zero_total() {
        let stats = CacheStats {
            hits: 0,
            misses: 0,
            evictions: 0,
            size: 0,
        };
        assert!((stats.hit_rate() - 0.0).abs() < f64::EPSILON);
    }
}