oxirs-core 0.2.4

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
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
//! High-performance term interning for RDF graphs
//!
//! This module provides efficient term interning specifically designed for RDF data,
//! with separate interners for subjects, predicates, and objects to maximize cache locality.

use crate::model::{BlankNode, Literal, NamedNode, Object, Predicate, Subject};
use anyhow::{Context, Result};
use bimap::BiMap;
use serde::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
use std::sync::{Arc, RwLock};

/// Thread-safe term interner with bidirectional mapping between terms and IDs
#[derive(Debug)]
pub struct TermInterner {
    /// Interner for subject terms
    subjects: Arc<RwLock<BiMap<u32, SubjectTerm>>>,
    /// Interner for predicate terms (always IRIs)
    predicates: Arc<RwLock<BiMap<u32, String>>>,
    /// Interner for object terms
    objects: Arc<RwLock<BiMap<u32, ObjectTerm>>>,
    /// Next available IDs
    next_subject_id: Arc<RwLock<u32>>,
    next_predicate_id: Arc<RwLock<u32>>,
    next_object_id: Arc<RwLock<u32>>,
    /// Statistics
    stats: Arc<RwLock<InternerStats>>,
}

/// Interned subject representation
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SubjectTerm {
    NamedNode(String),
    BlankNode(String),
}

/// Interned object representation
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ObjectTerm {
    NamedNode(String),
    BlankNode(String),
    Literal {
        value: String,
        datatype: Option<String>,
        language: Option<String>,
    },
}

/// Statistics for monitoring interner performance
#[derive(Debug, Clone, Default)]
pub struct InternerStats {
    pub subject_count: usize,
    pub predicate_count: usize,
    pub object_count: usize,
    pub total_lookups: usize,
    pub cache_hits: usize,
    pub memory_bytes: usize,
}

impl InternerStats {
    /// Calculate cache hit ratio
    pub fn hit_ratio(&self) -> f64 {
        if self.total_lookups == 0 {
            0.0
        } else {
            self.cache_hits as f64 / self.total_lookups as f64
        }
    }

    /// Get total number of interned terms
    pub fn total_terms(&self) -> usize {
        self.subject_count + self.predicate_count + self.object_count
    }
}

impl TermInterner {
    /// Create a new term interner
    pub fn new() -> Self {
        TermInterner {
            subjects: Arc::new(RwLock::new(BiMap::new())),
            predicates: Arc::new(RwLock::new(BiMap::new())),
            objects: Arc::new(RwLock::new(BiMap::new())),
            next_subject_id: Arc::new(RwLock::new(0)),
            next_predicate_id: Arc::new(RwLock::new(0)),
            next_object_id: Arc::new(RwLock::new(0)),
            stats: Arc::new(RwLock::new(InternerStats::default())),
        }
    }

    /// Intern a subject and return its ID
    pub fn intern_subject(&self, subject: &Subject) -> u32 {
        let term = match subject {
            Subject::NamedNode(n) => SubjectTerm::NamedNode(n.as_str().to_string()),
            Subject::BlankNode(b) => SubjectTerm::BlankNode(b.id().to_string()),
            Subject::Variable(_) | Subject::QuotedTriple(_) => {
                panic!("Variables and quoted triples cannot be interned in storage")
            }
        };

        // Try to find existing ID
        {
            let subjects = self.subjects.read().expect("subjects lock poisoned");
            if let Some(&id) = subjects.get_by_right(&term) {
                let mut stats = self.stats.write().expect("stats lock poisoned");
                stats.total_lookups += 1;
                stats.cache_hits += 1;
                return id;
            }
        }

        // Create new ID
        let mut subjects = self.subjects.write().expect("subjects lock poisoned");
        // Double-check in case another thread added it
        if let Some(&id) = subjects.get_by_right(&term) {
            let mut stats = self.stats.write().expect("stats lock poisoned");
            stats.total_lookups += 1;
            stats.cache_hits += 1;
            return id;
        }

        let mut next_id = self
            .next_subject_id
            .write()
            .expect("next_subject_id lock poisoned");
        let id = *next_id;
        *next_id += 1;
        subjects.insert(id, term.clone());

        // Update stats
        let mut stats = self.stats.write().expect("stats lock poisoned");
        stats.total_lookups += 1;
        stats.subject_count += 1;
        stats.memory_bytes += estimate_subject_memory(&term);

        id
    }

    /// Intern a predicate and return its ID
    pub fn intern_predicate(&self, predicate: &Predicate) -> u32 {
        let iri = match predicate {
            Predicate::NamedNode(n) => n.as_str().to_string(),
            Predicate::Variable(_) => {
                panic!("Variables cannot be interned as predicates in storage")
            }
        };

        // Try to find existing ID
        {
            let predicates = self.predicates.read().expect("predicates lock poisoned");
            if let Some(&id) = predicates.get_by_right(&iri) {
                let mut stats = self.stats.write().expect("stats lock poisoned");
                stats.total_lookups += 1;
                stats.cache_hits += 1;
                return id;
            }
        }

        // Create new ID
        let mut predicates = self.predicates.write().expect("predicates lock poisoned");
        // Double-check
        if let Some(&id) = predicates.get_by_right(&iri) {
            let mut stats = self.stats.write().expect("stats lock poisoned");
            stats.total_lookups += 1;
            stats.cache_hits += 1;
            return id;
        }

        let mut next_id = self
            .next_predicate_id
            .write()
            .expect("next_predicate_id lock poisoned");
        let id = *next_id;
        *next_id += 1;
        predicates.insert(id, iri.clone());

        // Update stats
        let mut stats = self.stats.write().expect("stats lock poisoned");
        stats.total_lookups += 1;
        stats.predicate_count += 1;
        stats.memory_bytes += iri.len() + 4; // String + ID

        id
    }

    /// Intern an object and return its ID
    pub fn intern_object(&self, object: &Object) -> u32 {
        let term = match object {
            Object::NamedNode(n) => ObjectTerm::NamedNode(n.as_str().to_string()),
            Object::BlankNode(b) => ObjectTerm::BlankNode(b.id().to_string()),
            Object::Literal(l) => ObjectTerm::Literal {
                value: l.value().to_string(),
                datatype: Some(l.datatype().as_str().to_string()),
                language: l.language().map(|lang| lang.to_string()),
            },
            Object::Variable(_) | Object::QuotedTriple(_) => {
                panic!("Variables and quoted triples cannot be interned in storage")
            }
        };

        // Try to find existing ID
        {
            let objects = self.objects.read().expect("objects lock poisoned");
            if let Some(&id) = objects.get_by_right(&term) {
                let mut stats = self.stats.write().expect("stats lock poisoned");
                stats.total_lookups += 1;
                stats.cache_hits += 1;
                return id;
            }
        }

        // Create new ID
        let mut objects = self.objects.write().expect("objects lock poisoned");
        // Double-check
        if let Some(&id) = objects.get_by_right(&term) {
            let mut stats = self.stats.write().expect("stats lock poisoned");
            stats.total_lookups += 1;
            stats.cache_hits += 1;
            return id;
        }

        let mut next_id = self
            .next_object_id
            .write()
            .expect("next_object_id lock poisoned");
        let id = *next_id;
        *next_id += 1;
        objects.insert(id, term.clone());

        // Update stats
        let mut stats = self.stats.write().expect("stats lock poisoned");
        stats.total_lookups += 1;
        stats.object_count += 1;
        stats.memory_bytes += estimate_object_memory(&term);

        id
    }

    /// Get subject by ID
    pub fn get_subject(&self, id: u32) -> Option<Subject> {
        let subjects = self.subjects.read().expect("subjects lock poisoned");
        subjects.get_by_left(&id).map(|term| match term {
            SubjectTerm::NamedNode(iri) => {
                Subject::NamedNode(NamedNode::new(iri).expect("interned IRI should be valid"))
            }
            SubjectTerm::BlankNode(blank_id) => Subject::BlankNode(
                BlankNode::new(blank_id).expect("interned blank node ID should be valid"),
            ),
        })
    }

    /// Get subject ID without interning (returns None if not found)
    pub fn get_subject_id(&self, subject: &Subject) -> Option<u32> {
        let term = match subject {
            Subject::NamedNode(n) => SubjectTerm::NamedNode(n.as_str().to_string()),
            Subject::BlankNode(b) => SubjectTerm::BlankNode(b.id().to_string()),
            Subject::Variable(_) | Subject::QuotedTriple(_) => {
                panic!("Variables and quoted triples cannot be interned in storage")
            }
        };
        let subjects = self.subjects.read().expect("subjects lock poisoned");
        subjects.get_by_right(&term).copied()
    }

    /// Get predicate by ID
    pub fn get_predicate(&self, id: u32) -> Option<Predicate> {
        let predicates = self.predicates.read().expect("predicates lock poisoned");
        predicates.get_by_left(&id).map(|iri| {
            Predicate::NamedNode(NamedNode::new(iri).expect("interned IRI should be valid"))
        })
    }

    /// Get predicate ID without interning (returns None if not found)
    pub fn get_predicate_id(&self, predicate: &Predicate) -> Option<u32> {
        let iri = match predicate {
            Predicate::NamedNode(n) => n.as_str().to_string(),
            Predicate::Variable(_) => {
                panic!("Variables cannot be interned as predicates in storage")
            }
        };
        let predicates = self.predicates.read().expect("predicates lock poisoned");
        predicates.get_by_right(&iri).copied()
    }

    /// Get object by ID
    pub fn get_object(&self, id: u32) -> Option<Object> {
        let objects = self.objects.read().expect("objects lock poisoned");
        objects.get_by_left(&id).map(|term| match term {
            ObjectTerm::NamedNode(iri) => {
                Object::NamedNode(NamedNode::new(iri).expect("interned IRI should be valid"))
            }
            ObjectTerm::BlankNode(id) => Object::BlankNode(
                BlankNode::new(id).expect("interned blank node ID should be valid"),
            ),
            ObjectTerm::Literal {
                value,
                datatype,
                language,
            } => {
                let literal = if let Some(lang) = language {
                    Literal::new_language_tagged_literal(value, lang)
                        .expect("interned language tag should be valid")
                } else if let Some(dt) = datatype {
                    Literal::new_typed(
                        value,
                        NamedNode::new(dt).expect("interned datatype IRI should be valid"),
                    )
                } else {
                    Literal::new(value)
                };
                Object::Literal(literal)
            }
        })
    }

    /// Get object ID without interning (returns None if not found)
    pub fn get_object_id(&self, object: &Object) -> Option<u32> {
        let term = match object {
            Object::NamedNode(n) => ObjectTerm::NamedNode(n.as_str().to_string()),
            Object::BlankNode(b) => ObjectTerm::BlankNode(b.id().to_string()),
            Object::Literal(l) => ObjectTerm::Literal {
                value: l.value().to_string(),
                datatype: Some(l.datatype().as_str().to_string()),
                language: l.language().map(|lang| lang.to_string()),
            },
            Object::Variable(_) | Object::QuotedTriple(_) => {
                panic!("Variables and quoted triples cannot be interned in storage")
            }
        };
        let objects = self.objects.read().expect("objects lock poisoned");
        objects.get_by_right(&term).copied()
    }

    /// Get current statistics
    pub fn stats(&self) -> InternerStats {
        self.stats.read().expect("stats lock poisoned").clone()
    }

    /// Clear all interned terms (useful for memory management)
    pub fn clear(&self) {
        let mut subjects = self.subjects.write().expect("subjects lock poisoned");
        let mut predicates = self.predicates.write().expect("predicates lock poisoned");
        let mut objects = self.objects.write().expect("objects lock poisoned");

        subjects.clear();
        predicates.clear();
        objects.clear();

        *self
            .next_subject_id
            .write()
            .expect("next_subject_id lock poisoned") = 0;
        *self
            .next_predicate_id
            .write()
            .expect("next_predicate_id lock poisoned") = 0;
        *self
            .next_object_id
            .write()
            .expect("next_object_id lock poisoned") = 0;

        let mut stats = self.stats.write().expect("stats lock poisoned");
        *stats = InternerStats::default();
    }

    /// Get memory usage estimate
    pub fn memory_usage(&self) -> usize {
        self.stats.read().expect("stats lock poisoned").memory_bytes
    }

    /// Intern a named node and return its ID (for compatibility with mmap_store)
    pub fn intern_named_node(&self, node: &NamedNode) -> u64 {
        self.intern_subject(&Subject::NamedNode(node.clone())) as u64
    }

    /// Intern a blank node and return its ID (for compatibility with mmap_store)
    pub fn intern_blank_node(&self, node: &BlankNode) -> u64 {
        self.intern_subject(&Subject::BlankNode(node.clone())) as u64
    }

    /// Intern a literal and return its ID (for compatibility with mmap_store)
    pub fn intern_literal(&self, literal: &Literal) -> u64 {
        self.intern_object(&Object::Literal(literal.clone())) as u64
    }

    /// Get named node ID without interning (for compatibility with mmap_store)
    pub fn get_named_node_id(&self, node: &NamedNode) -> Option<u64> {
        self.get_subject_id(&Subject::NamedNode(node.clone()))
            .map(|id| id as u64)
    }

    /// Get blank node ID without interning (for compatibility with mmap_store)
    pub fn get_blank_node_id(&self, node: &BlankNode) -> Option<u64> {
        self.get_subject_id(&Subject::BlankNode(node.clone()))
            .map(|id| id as u64)
    }

    /// Get literal ID without interning (for compatibility with mmap_store)
    pub fn get_literal_id(&self, literal: &Literal) -> Option<u64> {
        self.get_object_id(&Object::Literal(literal.clone()))
            .map(|id| id as u64)
    }

    /// Save the interner to disk
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .context("Failed to create terms file")?;

        let mut writer = BufWriter::new(file);

        // Save subjects
        {
            let subjects = self.subjects.read().expect("subjects lock poisoned");
            let subject_data: Vec<(u32, SubjectTerm)> = subjects
                .iter()
                .map(|(id, term)| (*id, term.clone()))
                .collect();
            oxicode::serde::encode_into_std_write(
                &subject_data,
                &mut writer,
                oxicode::config::standard(),
            )
            .map_err(|e| anyhow::anyhow!("Failed to serialize subjects: {}", e))?;
        }

        // Save predicates
        {
            let predicates = self.predicates.read().expect("predicates lock poisoned");
            let predicate_data: Vec<(u32, String)> = predicates
                .iter()
                .map(|(id, iri)| (*id, iri.clone()))
                .collect();
            oxicode::serde::encode_into_std_write(
                &predicate_data,
                &mut writer,
                oxicode::config::standard(),
            )
            .map_err(|e| anyhow::anyhow!("Failed to serialize predicates: {}", e))?;
        }

        // Save objects
        {
            let objects = self.objects.read().expect("objects lock poisoned");
            let object_data: Vec<(u32, ObjectTerm)> = objects
                .iter()
                .map(|(id, term)| (*id, term.clone()))
                .collect();
            oxicode::serde::encode_into_std_write(
                &object_data,
                &mut writer,
                oxicode::config::standard(),
            )
            .map_err(|e| anyhow::anyhow!("Failed to serialize objects: {}", e))?;
        }

        // Save next IDs
        let next_subject_id = *self
            .next_subject_id
            .read()
            .expect("next_subject_id lock poisoned");
        let next_predicate_id = *self
            .next_predicate_id
            .read()
            .expect("next_predicate_id lock poisoned");
        let next_object_id = *self
            .next_object_id
            .read()
            .expect("next_object_id lock poisoned");

        oxicode::serde::encode_into_std_write(
            &next_subject_id,
            &mut writer,
            oxicode::config::standard(),
        )?;
        oxicode::serde::encode_into_std_write(
            &next_predicate_id,
            &mut writer,
            oxicode::config::standard(),
        )?;
        oxicode::serde::encode_into_std_write(
            &next_object_id,
            &mut writer,
            oxicode::config::standard(),
        )?;

        writer.flush()?;
        Ok(())
    }

    /// Load the interner from disk
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = File::open(path).context("Failed to open terms file")?;
        let mut reader = BufReader::new(file);

        // Load subjects
        let (subject_data, _): (Vec<(u32, SubjectTerm)>, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())
                .map_err(|e| anyhow::anyhow!("Failed to deserialize subjects: {}", e))?;
        let mut subjects = BiMap::new();
        for (id, term) in subject_data {
            subjects.insert(id, term);
        }

        // Load predicates
        let (predicate_data, _): (Vec<(u32, String)>, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())
                .map_err(|e| anyhow::anyhow!("Failed to deserialize predicates: {}", e))?;
        let mut predicates = BiMap::new();
        for (id, iri) in predicate_data {
            predicates.insert(id, iri);
        }

        // Load objects
        let (object_data, _): (Vec<(u32, ObjectTerm)>, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())
                .map_err(|e| anyhow::anyhow!("Failed to deserialize objects: {}", e))?;
        let mut objects = BiMap::new();
        for (id, term) in object_data {
            objects.insert(id, term);
        }

        // Load next IDs
        let (next_subject_id, _): (u32, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())?;
        let (next_predicate_id, _): (u32, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())?;
        let (next_object_id, _): (u32, _) =
            oxicode::serde::decode_from_std_read(&mut reader, oxicode::config::standard())?;

        // Calculate stats
        let stats = InternerStats {
            subject_count: subjects.len(),
            predicate_count: predicates.len(),
            object_count: objects.len(),
            ..Default::default()
        };

        Ok(TermInterner {
            subjects: Arc::new(RwLock::new(subjects)),
            predicates: Arc::new(RwLock::new(predicates)),
            objects: Arc::new(RwLock::new(objects)),
            next_subject_id: Arc::new(RwLock::new(next_subject_id)),
            next_predicate_id: Arc::new(RwLock::new(next_predicate_id)),
            next_object_id: Arc::new(RwLock::new(next_object_id)),
            stats: Arc::new(RwLock::new(stats)),
        })
    }
}

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

/// Estimate memory usage for a subject term
fn estimate_subject_memory(term: &SubjectTerm) -> usize {
    match term {
        SubjectTerm::NamedNode(iri) => iri.len() + 4 + 8, // String + ID + enum overhead
        SubjectTerm::BlankNode(id) => id.len() + 4 + 8,
    }
}

/// Estimate memory usage for an object term
fn estimate_object_memory(term: &ObjectTerm) -> usize {
    match term {
        ObjectTerm::NamedNode(iri) => iri.len() + 4 + 8,
        ObjectTerm::BlankNode(id) => id.len() + 4 + 8,
        ObjectTerm::Literal {
            value,
            datatype,
            language,
        } => {
            value.len()
                + datatype.as_ref().map_or(0, |s| s.len())
                + language.as_ref().map_or(0, |s| s.len())
                + 4
                + 24 // ID + Option overhead
        }
    }
}

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

    #[test]
    fn test_subject_interning() {
        let interner = TermInterner::new();

        let subject1 =
            Subject::NamedNode(NamedNode::new("http://example.org/s1").expect("valid IRI"));
        let subject2 = Subject::BlankNode(BlankNode::new("b1").expect("valid blank node id"));
        let subject3 =
            Subject::NamedNode(NamedNode::new("http://example.org/s1").expect("valid IRI"));

        let id1 = interner.intern_subject(&subject1);
        let id2 = interner.intern_subject(&subject2);
        let id3 = interner.intern_subject(&subject3);

        // Same subject should get same ID
        assert_eq!(id1, id3);
        assert_ne!(id1, id2);

        // Check retrieval
        assert_eq!(interner.get_subject(id1), Some(subject1.clone()));
        assert_eq!(interner.get_subject(id2), Some(subject2));

        // Check stats
        let stats = interner.stats();
        assert_eq!(stats.subject_count, 2);
        assert_eq!(stats.cache_hits, 1); // subject3 was a hit
    }

    #[test]
    fn test_predicate_interning() {
        let interner = TermInterner::new();

        let pred1 =
            Predicate::NamedNode(NamedNode::new("http://example.org/p1").expect("valid IRI"));
        let pred2 =
            Predicate::NamedNode(NamedNode::new("http://example.org/p2").expect("valid IRI"));
        let pred3 =
            Predicate::NamedNode(NamedNode::new("http://example.org/p1").expect("valid IRI"));

        let id1 = interner.intern_predicate(&pred1);
        let id2 = interner.intern_predicate(&pred2);
        let id3 = interner.intern_predicate(&pred3);

        assert_eq!(id1, id3);
        assert_ne!(id1, id2);

        assert_eq!(interner.get_predicate(id1), Some(pred1));
        assert_eq!(interner.get_predicate(id2), Some(pred2));
    }

    #[test]
    fn test_object_interning() {
        let interner = TermInterner::new();

        let obj1 = Object::Literal(Literal::new("test"));
        let obj2 = Object::NamedNode(NamedNode::new("http://example.org/o1").expect("valid IRI"));
        let obj3 = Object::Literal(Literal::new("test"));

        let id1 = interner.intern_object(&obj1);
        let id2 = interner.intern_object(&obj2);
        let id3 = interner.intern_object(&obj3);

        assert_eq!(id1, id3);
        assert_ne!(id1, id2);

        assert_eq!(interner.get_object(id1), Some(obj1));
        assert_eq!(interner.get_object(id2), Some(obj2));
    }

    #[test]
    fn test_clear() {
        let interner = TermInterner::new();

        let subject =
            Subject::NamedNode(NamedNode::new("http://example.org/s1").expect("valid IRI"));
        let id = interner.intern_subject(&subject);

        assert!(interner.get_subject(id).is_some());

        interner.clear();

        assert!(interner.get_subject(id).is_none());
        assert_eq!(interner.stats().total_terms(), 0);
    }

    #[test]
    fn test_concurrent_access() {
        use std::sync::Arc;
        use std::thread;

        let interner = Arc::new(TermInterner::new());
        let mut handles = vec![];

        for i in 0..10 {
            let interner_clone = Arc::clone(&interner);
            let handle = thread::spawn(move || {
                let subject = Subject::NamedNode(
                    NamedNode::new(format!("http://example.org/s{}", i % 3))
                        .expect("valid IRI from format"),
                );
                interner_clone.intern_subject(&subject)
            });
            handles.push(handle);
        }

        let ids: Vec<u32> = handles
            .into_iter()
            .map(|h| h.join().expect("thread should not panic"))
            .collect();

        // Should have only 3 unique IDs (s0, s1, s2)
        let unique_ids: std::collections::HashSet<_> = ids.iter().collect();
        assert!(unique_ids.len() <= 3);
    }
}