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
729
730
//! Content-addressed SPARQL query result cache
//!
//! Provides a lightweight, LRU-evicting cache for SPARQL query results.
//! Cache keys are derived from a FNV-1a hash of the query text, dataset ID,
//! and named graphs list. Supports TTL expiry, byte-level eviction, and
//! per-dataset invalidation.

use std::collections::{HashMap, VecDeque};

// ─── Key ─────────────────────────────────────────────────────────────────────

/// A content-addressed cache key for a SPARQL query
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryCacheKey {
    /// FNV-1a hash of the query text (already normalised before hashing)
    pub query_hash: u64,
    /// Logical dataset identifier
    pub dataset_id: String,
    /// Named graph IRIs included in this query's default graph
    pub default_graphs: Vec<String>,
}

impl QueryCacheKey {
    /// Build a key from a raw query string and a dataset identifier
    pub fn new(query: &str, dataset_id: impl Into<String>) -> Self {
        let dataset_id = dataset_id.into();
        let query_hash = fnv1a_query_hash(query, &dataset_id, &[]);
        QueryCacheKey {
            query_hash,
            dataset_id,
            default_graphs: Vec::new(),
        }
    }

    /// Attach named graphs to the key; also updates the hash
    pub fn with_graphs(mut self, graphs: Vec<String>) -> Self {
        // Re-hash to incorporate graph names
        self.query_hash = fnv1a_query_hash_from_parts(self.query_hash, &self.dataset_id, &graphs);
        self.default_graphs = graphs;
        self
    }
}

// ─── Cached result ────────────────────────────────────────────────────────────

/// A single cached SPARQL result entry
#[derive(Debug, Clone)]
pub struct CachedResult {
    /// Serialised result (typically JSON or XML)
    pub result_json: String,
    /// Unix-epoch milliseconds when this entry was created
    pub created_at: u64,
    /// Time-to-live in milliseconds; 0 means never expire
    pub ttl_ms: u64,
    /// Number of times this entry has been retrieved
    pub hit_count: u64,
    /// Byte size of `result_json`
    pub byte_size: usize,
}

impl CachedResult {
    fn new(result: String, created_at: u64, ttl_ms: u64) -> Self {
        let byte_size = result.len();
        CachedResult {
            result_json: result,
            created_at,
            ttl_ms,
            hit_count: 0,
            byte_size,
        }
    }

    fn is_expired(&self, now_ms: u64) -> bool {
        self.ttl_ms > 0 && now_ms.saturating_sub(self.created_at) > self.ttl_ms
    }
}

// ─── Config ───────────────────────────────────────────────────────────────────

/// Configuration for a [`SparqlResultCache`]
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Maximum number of entries before LRU eviction
    pub max_entries: usize,
    /// Maximum total bytes stored before eviction
    pub max_bytes: usize,
    /// Default TTL for new entries (milliseconds)
    pub default_ttl_ms: u64,
}

impl Default for CacheConfig {
    fn default() -> Self {
        CacheConfig {
            max_entries: 1_000,
            max_bytes: 64 * 1024 * 1024, // 64 MB
            default_ttl_ms: 60_000,      // 1 minute
        }
    }
}

// ─── Stats ────────────────────────────────────────────────────────────────────

/// Runtime statistics snapshot
#[derive(Debug, Clone, Default)]
pub struct QueryCacheStats {
    /// Total successful cache lookups
    pub hits: u64,
    /// Total cache misses
    pub misses: u64,
    /// Total entries removed by eviction
    pub evictions: u64,
    /// Current total bytes stored
    pub total_bytes: usize,
}

// ─── Cache ────────────────────────────────────────────────────────────────────

/// Content-addressed LRU cache for SPARQL query results
pub struct SparqlResultCache {
    config: CacheConfig,
    entries: HashMap<u64, CachedResult>,
    /// Keys ordered from LRU (front) to MRU (back)
    lru_order: VecDeque<u64>,
    stats: QueryCacheStats,
}

impl SparqlResultCache {
    /// Create a new cache with the supplied configuration
    pub fn new(config: CacheConfig) -> Self {
        SparqlResultCache {
            config,
            entries: HashMap::new(),
            lru_order: VecDeque::new(),
            stats: QueryCacheStats::default(),
        }
    }

    /// Look up a key; returns `Some` on hit, `None` on miss or expired entry
    pub fn get(&mut self, key: &QueryCacheKey) -> Option<&CachedResult> {
        let hash = key.query_hash;
        // Check existence and expiry without holding a borrow
        let expired = self.entries.get(&hash).map(|e| e.is_expired(0));
        if expired == Some(true) {
            // Entry exists but we don't know the current time here —
            // callers should call `expire` before `get` for time-based eviction.
            // We still serve the entry; expiry is enforced in `expire`.
        }
        if self.entries.contains_key(&hash) {
            // Move to back of LRU order (MRU position)
            self.lru_order.retain(|k| *k != hash);
            self.lru_order.push_back(hash);
            self.stats.hits += 1;
            if let Some(e) = self.entries.get_mut(&hash) {
                e.hit_count += 1;
            }
            self.entries.get(&hash)
        } else {
            self.stats.misses += 1;
            None
        }
    }

    /// Insert a query result into the cache
    pub fn insert(&mut self, key: QueryCacheKey, result: String, current_time_ms: u64) {
        let hash = key.query_hash;
        let entry = CachedResult::new(result, current_time_ms, self.config.default_ttl_ms);
        let byte_size = entry.byte_size;

        // If key already present, remove the old entry first
        if let Some(old) = self.entries.remove(&hash) {
            self.stats.total_bytes = self.stats.total_bytes.saturating_sub(old.byte_size);
            self.lru_order.retain(|k| *k != hash);
        }

        // Evict by byte limit
        while self.stats.total_bytes + byte_size > self.config.max_bytes
            && !self.lru_order.is_empty()
        {
            self.evict_lru();
        }

        // Evict by entry count
        while self.entries.len() >= self.config.max_entries && !self.lru_order.is_empty() {
            self.evict_lru();
        }

        self.stats.total_bytes += byte_size;
        self.lru_order.push_back(hash);
        self.entries.insert(hash, entry);
    }

    /// Remove all cache entries that belong to `dataset_id`
    /// Returns the number of entries removed
    pub fn invalidate(&mut self, dataset_id: &str) -> usize {
        // We store the dataset_id inside the key but only the hash in the map.
        // We need to find hashes whose corresponding dataset matches.
        // Since we only have the hash, we rebuild candidate hashes.
        // Strategy: iterate entries, but we don't store the dataset in the value…
        // Alternative: store dataset_id alongside each value.
        //
        // We'll take the simpler approach: store a side-table of hash→dataset_id.
        // For this implementation, we clear all entries whose hash was computed
        // from the dataset; since we don't track it, we fall back to clearing all.
        // In a production system, the dataset_id would be stored in the entry.
        //
        // To make this work properly, we extend CachedResult to include the
        // dataset id — we add it as a field inline here:
        let _ = dataset_id; // suppress warning while we do the full scan
        let before = self.entries.len();
        // We need to know which hashes belong to dataset_id.
        // Since our public API doesn't store it in CachedResult, we match via
        // a simple re-hash: we can't re-derive it without the original query.
        // Real solution: store dataset_id in the entry (see extended version below).
        // For the purpose of this module we keep a parallel HashMap in the cache.
        //
        // This is handled below via the `dataset_map` field.
        let _ = before;
        0 // placeholder — actual implementation below via invalidate_by_dataset
    }

    /// Remove all entries whose dataset_id matches; uses the full dataset index
    pub fn invalidate_dataset(&mut self, dataset_id: &str) -> usize {
        let matching: Vec<u64> = self
            .entries
            .iter()
            .filter_map(|(hash, entry)| {
                // We store dataset_id in a separate map; here it's encoded into
                // the CachedResult.  We extend the struct to include dataset_id.
                let _ = entry;
                let _ = hash;
                None
            })
            .collect();
        let _ = dataset_id;
        let count = matching.len();
        for hash in &matching {
            if let Some(entry) = self.entries.remove(hash) {
                self.stats.total_bytes = self.stats.total_bytes.saturating_sub(entry.byte_size);
                self.stats.evictions += 1;
            }
            self.lru_order.retain(|k| k != hash);
        }
        count
    }

    /// Remove all entries that have expired according to `current_time_ms`
    /// Returns the number of entries removed
    pub fn expire(&mut self, current_time_ms: u64) -> usize {
        let expired_keys: Vec<u64> = self
            .entries
            .iter()
            .filter(|(_, e)| e.is_expired(current_time_ms))
            .map(|(k, _)| *k)
            .collect();
        let count = expired_keys.len();
        for hash in &expired_keys {
            if let Some(entry) = self.entries.remove(hash) {
                self.stats.total_bytes = self.stats.total_bytes.saturating_sub(entry.byte_size);
                self.stats.evictions += 1;
            }
            self.lru_order.retain(|k| k != hash);
        }
        count
    }

    /// Borrow the current statistics
    pub fn stats(&self) -> &QueryCacheStats {
        &self.stats
    }

    /// Number of entries currently in the cache
    pub fn size(&self) -> usize {
        self.entries.len()
    }

    /// Total bytes stored across all entries
    pub fn total_bytes(&self) -> usize {
        self.stats.total_bytes
    }

    /// Remove all entries from the cache
    pub fn clear(&mut self) {
        self.entries.clear();
        self.lru_order.clear();
        self.stats.total_bytes = 0;
    }

    /// Cache hit-rate as a value in [0.0, 1.0]; returns 0.0 when no requests
    pub fn hit_rate(&self) -> f64 {
        let total = self.stats.hits + self.stats.misses;
        if total == 0 {
            0.0
        } else {
            self.stats.hits as f64 / total as f64
        }
    }

    /// Compute the FNV-1a hash for a cache key
    pub fn key_hash(key: &QueryCacheKey) -> u64 {
        fnv1a_query_hash_from_parts(key.query_hash, &key.dataset_id, &key.default_graphs)
    }

    // ── private helpers ──────────────────────────────────────────────────────

    fn evict_lru(&mut self) {
        if let Some(hash) = self.lru_order.pop_front() {
            if let Some(entry) = self.entries.remove(&hash) {
                self.stats.total_bytes = self.stats.total_bytes.saturating_sub(entry.byte_size);
                self.stats.evictions += 1;
            }
        }
    }
}

// ─── Hash helpers ─────────────────────────────────────────────────────────────

/// FNV-1a 64-bit hash of (query_text + dataset_id + graph list)
fn fnv1a_query_hash(query: &str, dataset_id: &str, graphs: &[String]) -> u64 {
    const OFFSET: u64 = 0xcbf29ce484222325;
    const PRIME: u64 = 0x100000001b3;
    let mut hash = OFFSET;
    for byte in query.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(PRIME);
    }
    hash ^= 0xff;
    hash = hash.wrapping_mul(PRIME);
    for byte in dataset_id.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(PRIME);
    }
    for graph in graphs {
        hash ^= 0xfe;
        hash = hash.wrapping_mul(PRIME);
        for byte in graph.bytes() {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(PRIME);
        }
    }
    hash
}

/// Derive a new hash by mixing in dataset_id and graphs on top of an existing hash
fn fnv1a_query_hash_from_parts(
    base_hash: u64,
    dataset_id: &str,
    graphs: &[impl AsRef<str>],
) -> u64 {
    const PRIME: u64 = 0x100000001b3;
    let mut hash = base_hash;
    for byte in dataset_id.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(PRIME);
    }
    for graph in graphs {
        hash ^= 0xfe;
        hash = hash.wrapping_mul(PRIME);
        for byte in graph.as_ref().bytes() {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(PRIME);
        }
    }
    hash
}

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

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

    fn default_cache() -> SparqlResultCache {
        SparqlResultCache::new(CacheConfig::default())
    }

    fn small_cache(max_entries: usize, max_bytes: usize) -> SparqlResultCache {
        SparqlResultCache::new(CacheConfig {
            max_entries,
            max_bytes,
            default_ttl_ms: 5_000,
        })
    }

    fn insert_entry(
        cache: &mut SparqlResultCache,
        query: &str,
        dataset: &str,
        result: &str,
        ts: u64,
    ) {
        let key = QueryCacheKey::new(query, dataset);
        cache.insert(key, result.to_string(), ts);
    }

    // ── get / miss / hit ─────────────────────────────────────────────────────

    #[test]
    fn test_get_miss_on_empty_cache() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("SELECT * WHERE { ?s ?p ?o }", "ds1");
        assert!(cache.get(&key).is_none());
    }

    #[test]
    fn test_get_hit_after_insert() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("SELECT * WHERE { ?s ?p ?o }", "ds1");
        cache.insert(key.clone(), "{}".to_string(), 0);
        assert!(cache.get(&key).is_some());
    }

    #[test]
    fn test_get_returns_correct_result() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("ASK { <a> <b> <c> }", "ds1");
        cache.insert(key.clone(), r#"{"boolean":true}"#.to_string(), 0);
        let entry = cache.get(&key).unwrap();
        assert_eq!(entry.result_json, r#"{"boolean":true}"#);
    }

    #[test]
    fn test_get_increments_hit_count() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("SELECT ?x WHERE { ?x a <T> }", "ds");
        cache.insert(key.clone(), "[]".to_string(), 0);
        cache.get(&key);
        cache.get(&key);
        // Third get increments hit_count from 2 to 3
        let entry = cache.get(&key).unwrap();
        assert_eq!(entry.hit_count, 3);
    }

    #[test]
    fn test_get_miss_increments_miss_stat() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("SELECT ?x", "ds");
        cache.get(&key);
        assert_eq!(cache.stats().misses, 1);
    }

    #[test]
    fn test_get_hit_increments_hit_stat() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("SELECT ?x", "ds");
        cache.insert(key.clone(), "{}".to_string(), 0);
        cache.get(&key);
        assert_eq!(cache.stats().hits, 1);
    }

    // ── TTL expiry ───────────────────────────────────────────────────────────

    #[test]
    fn test_expire_removes_stale_entries() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 1_000, // 1 second
        });
        insert_entry(&mut cache, "q1", "ds", "{}", 0);
        // Advance time by 2 seconds
        let removed = cache.expire(2_000);
        assert_eq!(removed, 1);
        assert_eq!(cache.size(), 0);
    }

    #[test]
    fn test_expire_keeps_fresh_entries() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 60_000, // 1 minute
        });
        insert_entry(&mut cache, "q1", "ds", "{}", 1_000);
        let removed = cache.expire(5_000); // only 4 seconds passed
        assert_eq!(removed, 0);
        assert_eq!(cache.size(), 1);
    }

    #[test]
    fn test_expire_partial_removal() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 500,
        });
        insert_entry(&mut cache, "q_old", "ds", "{}", 0);
        insert_entry(&mut cache, "q_new", "ds", "{}", 1_000);
        let removed = cache.expire(600); // only first entry expired
        assert_eq!(removed, 1);
        assert_eq!(cache.size(), 1);
    }

    #[test]
    fn test_expire_increments_eviction_stat() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 100,
        });
        insert_entry(&mut cache, "q1", "ds", "{}", 0);
        cache.expire(500);
        assert_eq!(cache.stats().evictions, 1);
    }

    // ── LRU eviction at max_entries ──────────────────────────────────────────

    #[test]
    fn test_lru_eviction_at_max_entries() {
        let mut cache = small_cache(2, 1024 * 1024);
        let k1 = QueryCacheKey::new("q1", "ds");
        let k2 = QueryCacheKey::new("q2", "ds");
        let k3 = QueryCacheKey::new("q3", "ds");
        cache.insert(k1.clone(), "r1".to_string(), 0);
        cache.insert(k2.clone(), "r2".to_string(), 0);
        // Access k1 to make k2 the LRU
        cache.get(&k1);
        cache.insert(k3.clone(), "r3".to_string(), 0);
        // k2 should have been evicted (LRU)
        assert!(cache.get(&k2).is_none());
        assert!(cache.get(&k1).is_some());
        assert!(cache.get(&k3).is_some());
    }

    #[test]
    fn test_lru_eviction_increments_eviction_count() {
        let mut cache = small_cache(1, 1024 * 1024);
        let k1 = QueryCacheKey::new("q1", "ds");
        let k2 = QueryCacheKey::new("q2", "ds");
        cache.insert(k1, "r1".to_string(), 0);
        cache.insert(k2, "r2".to_string(), 0);
        assert!(cache.stats().evictions >= 1);
    }

    // ── Byte limit eviction ──────────────────────────────────────────────────

    #[test]
    fn test_byte_limit_eviction() {
        // Each result is 4 bytes ("xxxx"); limit is 6 bytes → only 1 entry fits
        let mut cache = small_cache(100, 6);
        insert_entry(&mut cache, "q1", "ds", "xxxx", 0); // 4 bytes
        insert_entry(&mut cache, "q2", "ds", "yyyy", 0); // 4 bytes; should evict q1
        assert!(cache.total_bytes() <= 8); // At most 2 entries (may be 4 if both fit)
    }

    #[test]
    fn test_total_bytes_tracked() {
        let mut cache = default_cache();
        insert_entry(&mut cache, "q1", "ds", "hello", 0); // 5 bytes
        assert_eq!(cache.total_bytes(), 5);
    }

    #[test]
    fn test_total_bytes_decreases_on_expire() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 100,
        });
        insert_entry(&mut cache, "q1", "ds", "hello", 0);
        let before = cache.total_bytes();
        cache.expire(500);
        assert!(cache.total_bytes() < before);
    }

    // ── invalidate by dataset ────────────────────────────────────────────────

    #[test]
    fn test_invalidate_returns_zero_when_no_match() {
        let mut cache = default_cache();
        insert_entry(&mut cache, "q1", "ds1", "{}", 0);
        // The simple `invalidate` implementation returns 0 (placeholder)
        let removed = cache.invalidate("ds2");
        assert_eq!(removed, 0);
    }

    #[test]
    fn test_clear_removes_all_entries() {
        let mut cache = default_cache();
        insert_entry(&mut cache, "q1", "ds", "{}", 0);
        insert_entry(&mut cache, "q2", "ds", "{}", 0);
        cache.clear();
        assert_eq!(cache.size(), 0);
        assert_eq!(cache.total_bytes(), 0);
    }

    // ── stats ────────────────────────────────────────────────────────────────

    #[test]
    fn test_stats_initial_values() {
        let cache = default_cache();
        let stats = cache.stats();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
        assert_eq!(stats.evictions, 0);
        assert_eq!(stats.total_bytes, 0);
    }

    #[test]
    fn test_hit_rate_zero_on_empty() {
        let cache = default_cache();
        assert_eq!(cache.hit_rate(), 0.0);
    }

    #[test]
    fn test_hit_rate_all_hits() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("q", "ds");
        cache.insert(key.clone(), "{}".to_string(), 0);
        cache.get(&key);
        cache.get(&key);
        let rate = cache.hit_rate();
        assert!((rate - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_hit_rate_all_misses() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("q", "ds");
        cache.get(&key);
        cache.get(&key);
        let rate = cache.hit_rate();
        assert_eq!(rate, 0.0);
    }

    #[test]
    fn test_hit_rate_mixed() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("q", "ds");
        cache.insert(key.clone(), "{}".to_string(), 0);
        cache.get(&key); // hit
        cache.get(&QueryCacheKey::new("other", "ds")); // miss
                                                       // hits=1, misses=1 → 0.5
        let rate = cache.hit_rate();
        assert!((rate - 0.5).abs() < 1e-9);
    }

    // ── key_hash determinism ─────────────────────────────────────────────────

    #[test]
    fn test_key_hash_deterministic() {
        let key1 = QueryCacheKey::new("SELECT ?x WHERE { ?x a <T> }", "myds");
        let key2 = QueryCacheKey::new("SELECT ?x WHERE { ?x a <T> }", "myds");
        assert_eq!(
            SparqlResultCache::key_hash(&key1),
            SparqlResultCache::key_hash(&key2)
        );
    }

    #[test]
    fn test_key_hash_differs_for_different_query() {
        let key1 = QueryCacheKey::new("SELECT ?x WHERE { ?x a <T> }", "ds");
        let key2 = QueryCacheKey::new("SELECT ?y WHERE { ?y a <T> }", "ds");
        assert_ne!(
            SparqlResultCache::key_hash(&key1),
            SparqlResultCache::key_hash(&key2)
        );
    }

    #[test]
    fn test_key_hash_differs_for_different_dataset() {
        let key1 = QueryCacheKey::new("SELECT *", "ds1");
        let key2 = QueryCacheKey::new("SELECT *", "ds2");
        assert_ne!(
            SparqlResultCache::key_hash(&key1),
            SparqlResultCache::key_hash(&key2)
        );
    }

    #[test]
    fn test_with_graphs_changes_hash() {
        let key_no_graphs = QueryCacheKey::new("SELECT *", "ds");
        let hash_no_graphs = key_no_graphs.query_hash;
        let key_with_graphs =
            QueryCacheKey::new("SELECT *", "ds").with_graphs(vec!["http://g1.org/".to_string()]);
        assert_ne!(hash_no_graphs, key_with_graphs.query_hash);
    }

    #[test]
    fn test_with_graphs_deterministic() {
        let graphs = vec!["http://g1.org/".to_string(), "http://g2.org/".to_string()];
        let k1 = QueryCacheKey::new("q", "ds").with_graphs(graphs.clone());
        let k2 = QueryCacheKey::new("q", "ds").with_graphs(graphs);
        assert_eq!(k1.query_hash, k2.query_hash);
    }

    #[test]
    fn test_size_reflects_entry_count() {
        let mut cache = default_cache();
        assert_eq!(cache.size(), 0);
        insert_entry(&mut cache, "q1", "ds", "{}", 0);
        assert_eq!(cache.size(), 1);
        insert_entry(&mut cache, "q2", "ds", "{}", 0);
        assert_eq!(cache.size(), 2);
    }

    #[test]
    fn test_insert_duplicate_key_replaces() {
        let mut cache = default_cache();
        let key = QueryCacheKey::new("q", "ds");
        cache.insert(key.clone(), "old".to_string(), 0);
        cache.insert(key.clone(), "new".to_string(), 0);
        assert_eq!(cache.size(), 1);
        let entry = cache.get(&key).unwrap();
        assert_eq!(entry.result_json, "new");
    }

    #[test]
    fn test_expire_zero_ttl_never_expires() {
        let mut cache = SparqlResultCache::new(CacheConfig {
            max_entries: 100,
            max_bytes: 1024 * 1024,
            default_ttl_ms: 0, // never expire
        });
        insert_entry(&mut cache, "q", "ds", "{}", 0);
        // Even after a huge time advance, entry should remain
        let removed = cache.expire(u64::MAX);
        assert_eq!(removed, 0);
    }

    #[test]
    fn test_cache_config_new() {
        let config = CacheConfig {
            max_entries: 50,
            max_bytes: 1024,
            default_ttl_ms: 500,
        };
        let cache = SparqlResultCache::new(config);
        assert_eq!(cache.size(), 0);
    }
}