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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
//! String interning system for performance optimization
//!
//! This module provides efficient string interning for commonly used strings
//! like IRIs, datatype URIs, and other RDF terms. String interning reduces
//! memory usage and improves comparison performance by ensuring that equal
//! strings are stored only once and can be compared by pointer equality.
//!
//! ## Performance Monitoring
//!
//! The interner integrates SciRS2-core metrics for comprehensive monitoring:
//! - Cache hit/miss rates
//! - Intern operation timing
//! - Memory usage tracking
//! - Deduplication effectiveness

use scirs2_core::metrics::{Counter, Histogram, Timer};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock, Weak};

/// A thread-safe string interner that deduplicates strings with ID mapping
///
/// Integrates SciRS2-core metrics for production-grade performance monitoring:
/// - Automatic cache hit/miss tracking
/// - Intern operation timing
/// - Memory usage histograms
/// - Deduplication effectiveness metrics
pub struct StringInterner {
    /// Map from string content to weak references of interned strings
    strings: RwLock<HashMap<String, Weak<str>>>,
    /// Bidirectional mapping between strings and numeric IDs
    string_to_id: RwLock<HashMap<String, u32>>,
    /// Map from ID back to string
    id_to_string: RwLock<HashMap<u32, Arc<str>>>,
    /// Next available ID
    next_id: AtomicU32,
    /// Statistics for monitoring performance
    stats: RwLock<InternerStats>,
    /// SciRS2 metrics
    cache_hit_counter: Arc<Counter>,
    cache_miss_counter: Arc<Counter>,
    intern_timer: Arc<Timer>,
    string_length_histogram: Arc<Histogram>,
    memory_usage_histogram: Arc<Histogram>,
}

/// Atomic 32-bit unsigned integer type for thread-safe ID generation
use std::sync::atomic::AtomicU32;

/// Statistics for monitoring string interner performance
#[derive(Debug, Clone, Default)]
pub struct InternerStats {
    pub total_requests: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
    pub total_strings_stored: usize,
    pub memory_saved_bytes: usize,
}

/// Memory usage statistics for performance monitoring
#[derive(Debug, Clone)]
pub struct MemoryUsage {
    pub interned_strings: usize,
    pub id_mappings: usize,
    pub estimated_memory_bytes: usize,
    pub memory_saved_bytes: usize,
    pub compression_ratio: f64,
}

/// SciRS2 metrics for interner performance
#[derive(Debug, Clone)]
pub struct InternerMetrics {
    /// Total cache hits
    pub cache_hits: u64,
    /// Total cache misses
    pub cache_misses: u64,
    /// Total intern requests
    pub total_requests: u64,
    /// Cache hit ratio (0.0 to 1.0)
    pub hit_ratio: f64,
    /// Average intern operation time in seconds
    pub avg_intern_time_secs: f64,
    /// Total timing observations recorded
    pub total_intern_observations: u64,
    /// Average string length
    pub avg_string_length: f64,
    /// Total memory tracked in bytes
    pub total_memory_tracked_bytes: u64,
}

impl InternerStats {
    pub fn hit_ratio(&self) -> f64 {
        if self.total_requests == 0 {
            0.0
        } else {
            self.cache_hits as f64 / self.total_requests as f64
        }
    }
}

impl StringInterner {
    /// Create a new string interner with ID mapping and SciRS2 metrics
    ///
    /// Automatically tracks:
    /// - Cache hit/miss rates for intern operations
    /// - Operation timing for performance analysis
    /// - String length distribution
    /// - Memory usage patterns
    pub fn new() -> Self {
        Self::with_capacity(1024) // Default capacity for typical usage
    }

    /// Create a new string interner with specified capacity
    ///
    /// Pre-allocates HashMaps to the given capacity to reduce reallocation overhead.
    /// Initializes all SciRS2 metrics for comprehensive monitoring.
    pub fn with_capacity(capacity: usize) -> Self {
        StringInterner {
            strings: RwLock::new(HashMap::with_capacity(capacity)),
            string_to_id: RwLock::new(HashMap::with_capacity(capacity)),
            id_to_string: RwLock::new(HashMap::with_capacity(capacity)),
            next_id: AtomicU32::new(0),
            stats: RwLock::new(InternerStats::default()),
            cache_hit_counter: Arc::new(Counter::new("interner.cache_hits".to_string())),
            cache_miss_counter: Arc::new(Counter::new("interner.cache_misses".to_string())),
            intern_timer: Arc::new(Timer::new("interner.intern_time".to_string())),
            string_length_histogram: Arc::new(Histogram::new("interner.string_length".to_string())),
            memory_usage_histogram: Arc::new(Histogram::new("interner.memory_usage".to_string())),
        }
    }

    /// Intern a string, returning an `Arc<str>` that can be cheaply cloned and compared
    ///
    /// This operation is tracked with SciRS2 metrics:
    /// - Cache hits/misses
    /// - Operation timing
    /// - String length distribution
    pub fn intern(&self, s: &str) -> Arc<str> {
        let _guard = self.intern_timer.start();

        // Track string length
        self.string_length_histogram.observe(s.len() as f64);

        // Fast path: try to get existing string with read lock
        {
            let strings = self.strings.read().expect("strings lock poisoned");
            if let Some(weak_ref) = strings.get(s) {
                if let Some(arc_str) = weak_ref.upgrade() {
                    // Update stats
                    self.cache_hit_counter.inc();
                    {
                        let mut stats = self.stats.write().expect("stats lock poisoned");
                        stats.total_requests += 1;
                        stats.cache_hits += 1;
                    }
                    return arc_str;
                }
            }
        }

        // Slow path: need to create new string with write lock
        let mut strings = self.strings.write().expect("strings lock poisoned");

        // Double-check in case another thread added it while we were waiting
        if let Some(weak_ref) = strings.get(s) {
            if let Some(arc_str) = weak_ref.upgrade() {
                // Update stats
                self.cache_hit_counter.inc();
                drop(strings); // Release write lock early
                {
                    let mut stats = self.stats.write().expect("stats lock poisoned");
                    stats.total_requests += 1;
                    stats.cache_hits += 1;
                }
                return arc_str;
            }
        }

        // Create new interned string
        let arc_str: Arc<str> = Arc::from(s);
        let weak_ref = Arc::downgrade(&arc_str);
        strings.insert(s.to_string(), weak_ref);

        // Update stats
        self.cache_miss_counter.inc();
        drop(strings); // Release write lock early
        {
            let mut stats = self.stats.write().expect("stats lock poisoned");
            stats.total_requests += 1;
            stats.cache_misses += 1;
            stats.total_strings_stored += 1;
            stats.memory_saved_bytes += s.len(); // Approximate memory saved on subsequent hits
        }

        arc_str
    }

    /// Intern a string and return both the `Arc<str>` and its numeric ID
    pub fn intern_with_id(&self, s: &str) -> (Arc<str>, u32) {
        // Fast path: check if we already have this string and its ID
        {
            let string_to_id = self
                .string_to_id
                .read()
                .expect("string_to_id lock poisoned");
            if let Some(&id) = string_to_id.get(s) {
                // We have the ID, now get the Arc<str>
                let id_to_string = self
                    .id_to_string
                    .read()
                    .expect("id_to_string lock poisoned");
                if let Some(arc_str) = id_to_string.get(&id) {
                    // Update stats
                    {
                        let mut stats = self.stats.write().expect("stats lock poisoned");
                        stats.total_requests += 1;
                        stats.cache_hits += 1;
                    }
                    return (arc_str.clone(), id);
                }
            }
        }

        // Slow path: need to create new entry
        let arc_str = self.intern(s); // This will handle the string interning
        let id = self
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Update the ID mappings
        {
            let mut string_to_id = self
                .string_to_id
                .write()
                .expect("string_to_id lock poisoned");
            string_to_id.insert(s.to_string(), id);
        }
        {
            let mut id_to_string = self
                .id_to_string
                .write()
                .expect("id_to_string lock poisoned");
            id_to_string.insert(id, arc_str.clone());
        }

        (arc_str, id)
    }

    /// Get the ID for a string if it's already interned
    pub fn get_id(&self, s: &str) -> Option<u32> {
        let string_to_id = self
            .string_to_id
            .read()
            .expect("string_to_id lock poisoned");
        string_to_id.get(s).copied()
    }

    /// Get the string for an ID if it exists
    pub fn get_string(&self, id: u32) -> Option<Arc<str>> {
        let id_to_string = self
            .id_to_string
            .read()
            .expect("id_to_string lock poisoned");
        id_to_string.get(&id).cloned()
    }

    /// Get all ID mappings (useful for serialization/debugging)
    pub fn get_all_mappings(&self) -> Vec<(u32, Arc<str>)> {
        let id_to_string = self
            .id_to_string
            .read()
            .expect("id_to_string lock poisoned");
        id_to_string
            .iter()
            .map(|(&id, s)| (id, s.clone()))
            .collect()
    }

    /// Clean up expired weak references to save memory
    ///
    /// Returns the number of entries cleaned up
    pub fn cleanup(&self) -> usize {
        let mut strings = self.strings.write().expect("strings lock poisoned");
        let before = strings.len();
        strings.retain(|_, weak_ref| weak_ref.strong_count() > 0);
        let after = strings.len();
        before - after
    }

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

    /// Get the number of unique strings currently stored
    pub fn len(&self) -> usize {
        // Return the maximum of both counts to handle mixed usage
        let id_count = self
            .string_to_id
            .read()
            .expect("string_to_id lock poisoned")
            .len();
        let string_count = self.strings.read().expect("strings lock poisoned").len();
        std::cmp::max(id_count, string_count)
    }

    /// Get the number of strings with ID mappings
    pub fn id_mapping_count(&self) -> usize {
        self.string_to_id
            .read()
            .expect("string_to_id lock poisoned")
            .len()
    }

    /// Check if the interner is empty
    pub fn is_empty(&self) -> bool {
        self.strings
            .read()
            .expect("strings lock poisoned")
            .is_empty()
    }

    /// Batch intern multiple strings for improved performance
    /// Returns a Vec of `Arc<str>` in the same order as input
    pub fn intern_batch(&self, strings: &[&str]) -> Vec<Arc<str>> {
        let mut result = Vec::with_capacity(strings.len());
        let mut to_create = Vec::new();

        // First pass: collect existing strings with read lock
        {
            let string_map = self.strings.read().expect("strings lock poisoned");
            for &s in strings {
                if let Some(weak_ref) = string_map.get(s) {
                    if let Some(arc_str) = weak_ref.upgrade() {
                        result.push(arc_str);
                        continue;
                    }
                }
                to_create.push((result.len(), s));
                result.push(Arc::from("")); // Placeholder
            }
        }

        // Second pass: create missing strings with write lock
        if !to_create.is_empty() {
            let mut string_map = self.strings.write().expect("strings lock poisoned");
            let mut stats = self.stats.write().expect("stats lock poisoned");

            for (index, s) in to_create {
                // Double-check in case another thread added it
                if let Some(weak_ref) = string_map.get(s) {
                    if let Some(arc_str) = weak_ref.upgrade() {
                        result[index] = arc_str;
                        stats.cache_hits += 1;
                        continue;
                    }
                }

                // Create new interned string
                let arc_str: Arc<str> = Arc::from(s);
                let weak_ref = Arc::downgrade(&arc_str);
                string_map.insert(s.to_string(), weak_ref);
                result[index] = arc_str;

                stats.cache_misses += 1;
                stats.total_strings_stored += 1;
                stats.memory_saved_bytes += s.len();
            }

            stats.total_requests += strings.len();
        }

        result
    }

    /// Prefetch strings into the interner cache for improved performance
    /// This is useful when you know you'll need certain strings soon
    pub fn prefetch(&self, strings: &[&str]) {
        let _ = self.intern_batch(strings);
    }

    /// Get memory usage statistics for performance monitoring
    pub fn memory_usage(&self) -> MemoryUsage {
        let string_map_size = self.strings.read().expect("strings lock poisoned").len();
        let id_map_size = self
            .string_to_id
            .read()
            .expect("string_to_id lock poisoned")
            .len();
        let stats = self.stats.read().expect("stats lock poisoned");

        MemoryUsage {
            interned_strings: string_map_size,
            id_mappings: id_map_size,
            estimated_memory_bytes: string_map_size * 64 + id_map_size * 8, // Rough estimate
            memory_saved_bytes: stats.memory_saved_bytes,
            compression_ratio: if stats.memory_saved_bytes > 0 {
                stats.memory_saved_bytes as f64
                    / (stats.memory_saved_bytes + string_map_size * 32) as f64
            } else {
                0.0
            },
        }
    }

    /// Get comprehensive performance metrics from SciRS2
    ///
    /// Returns detailed statistics including:
    /// - Cache hit/miss counts and ratios
    /// - Operation timing statistics
    /// - String length distribution
    /// - Memory usage patterns
    pub fn get_metrics(&self) -> InternerMetrics {
        let cache_hits = self.cache_hit_counter.get();
        let cache_misses = self.cache_miss_counter.get();
        let total_requests = cache_hits + cache_misses;
        let hit_ratio = if total_requests > 0 {
            cache_hits as f64 / total_requests as f64
        } else {
            0.0
        };

        let timer_stats = self.intern_timer.get_stats();
        let string_length_stats = self.string_length_histogram.get_stats();
        let memory_stats = self.memory_usage_histogram.get_stats();

        InternerMetrics {
            cache_hits,
            cache_misses,
            total_requests,
            hit_ratio,
            avg_intern_time_secs: timer_stats.mean,
            total_intern_observations: timer_stats.count,
            avg_string_length: string_length_stats.mean,
            total_memory_tracked_bytes: memory_stats.sum as u64,
        }
    }

    /// Optimize the interner by cleaning up and compacting data structures
    ///
    /// Performs comprehensive optimization:
    /// - Cleans up expired weak references
    /// - Rehashes HashMaps with optimal capacity
    /// - Updates memory usage statistics
    /// - Tracks optimization impact with SciRS2 metrics
    pub fn optimize(&self) {
        let start = std::time::Instant::now();

        // Clean up expired weak references
        let cleaned_count = self.cleanup();

        // Get current sizes
        let current_size = {
            let strings = self.strings.read().expect("strings lock poisoned");
            strings.len()
        };

        // Rehash with optimal capacity (1.3x current size to reduce future reallocations)
        let optimal_capacity = ((current_size as f64 * 1.3) as usize).max(1024);

        {
            let mut strings = self.strings.write().expect("strings lock poisoned");
            let mut string_to_id = self
                .string_to_id
                .write()
                .expect("string_to_id lock poisoned");
            let mut id_to_string = self
                .id_to_string
                .write()
                .expect("id_to_string lock poisoned");

            // Create new maps with optimal capacity
            let mut new_strings = HashMap::with_capacity(optimal_capacity);
            let mut new_string_to_id = HashMap::with_capacity(optimal_capacity);
            let mut new_id_to_string = HashMap::with_capacity(optimal_capacity);

            // Move data to new maps (rehashing in the process)
            for (key, value) in strings.drain() {
                new_strings.insert(key, value);
            }
            for (key, value) in string_to_id.drain() {
                new_string_to_id.insert(key, value);
            }
            for (key, value) in id_to_string.drain() {
                new_id_to_string.insert(key, value);
            }

            // Replace with optimized maps
            *strings = new_strings;
            *string_to_id = new_string_to_id;
            *id_to_string = new_id_to_string;
        }

        // Calculate and track memory usage
        let mem_usage = self.memory_usage();
        self.memory_usage_histogram
            .observe(mem_usage.estimated_memory_bytes as f64);

        // Update stats
        {
            let mut stats = self.stats.write().expect("stats lock poisoned");
            stats.total_strings_stored = current_size;
        }

        let duration = start.elapsed();
        tracing::debug!(
            "Interner optimized: cleaned {} entries, rehashed to capacity {}, took {:?}",
            cleaned_count,
            optimal_capacity,
            duration
        );
    }
}

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

impl std::fmt::Debug for StringInterner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StringInterner")
            .field(
                "strings_count",
                &self.strings.read().expect("strings lock poisoned").len(),
            )
            .field(
                "id_mappings_count",
                &self
                    .string_to_id
                    .read()
                    .expect("string_to_id lock poisoned")
                    .len(),
            )
            .field(
                "next_id",
                &self.next_id.load(std::sync::atomic::Ordering::Relaxed),
            )
            .field("stats", &self.stats.read().expect("stats lock poisoned"))
            .finish()
    }
}

lazy_static::lazy_static! {
    // Global interner instances for common string types
    /// Global interner for IRI strings
    pub static ref IRI_INTERNER: StringInterner = StringInterner::new();

    /// Global interner for datatype IRIs
    pub static ref DATATYPE_INTERNER: StringInterner = StringInterner::new();

    /// Global interner for language tags
    pub static ref LANGUAGE_INTERNER: StringInterner = StringInterner::new();

    /// Global interner for general strings (JSON-LD processing)
    pub static ref STRING_INTERNER: StringInterner = StringInterner::new();
}

/// An interned string that supports efficient comparison and hashing
#[derive(Debug, Clone)]
pub struct InternedString {
    inner: Arc<str>,
}

impl InternedString {
    /// Create a new interned string using the default IRI interner
    pub fn new(s: &str) -> Self {
        InternedString {
            inner: IRI_INTERNER.intern(s),
        }
    }

    /// Create a new interned string using a specific interner
    pub fn new_with_interner(s: &str, interner: &StringInterner) -> Self {
        InternedString {
            inner: interner.intern(s),
        }
    }

    /// Create an interned datatype string
    pub fn new_datatype(s: &str) -> Self {
        InternedString {
            inner: DATATYPE_INTERNER.intern(s),
        }
    }

    /// Create an interned language tag string
    pub fn new_language(s: &str) -> Self {
        InternedString {
            inner: LANGUAGE_INTERNER.intern(s),
        }
    }

    /// Get the string content
    pub fn as_str(&self) -> &str {
        &self.inner
    }

    /// Get the inner `Arc<str>` for zero-copy operations
    pub fn as_arc_str(&self) -> &Arc<str> {
        &self.inner
    }

    /// Convert into the inner `Arc<str>`
    pub fn into_arc_str(self) -> Arc<str> {
        self.inner
    }
}

impl std::fmt::Display for InternedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.inner)
    }
}

impl std::ops::Deref for InternedString {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl AsRef<str> for InternedString {
    fn as_ref(&self) -> &str {
        &self.inner
    }
}

impl PartialEq for InternedString {
    fn eq(&self, other: &Self) -> bool {
        // Fast pointer comparison first
        Arc::ptr_eq(&self.inner, &other.inner) || self.inner == other.inner
    }
}

impl Eq for InternedString {}

impl Hash for InternedString {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Hash the string content, not the pointer
        self.inner.hash(state);
    }
}

impl PartialOrd for InternedString {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for InternedString {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl From<&str> for InternedString {
    fn from(s: &str) -> Self {
        InternedString::new(s)
    }
}

impl From<String> for InternedString {
    fn from(s: String) -> Self {
        InternedString::new(&s)
    }
}

/// Extension trait for string interning common RDF vocabulary
pub trait RdfVocabulary {
    /// Common XSD namespace
    const XSD_NS: &'static str = "http://www.w3.org/2001/XMLSchema#";
    /// Common RDF namespace
    const RDF_NS: &'static str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    /// Common RDFS namespace
    const RDFS_NS: &'static str = "http://www.w3.org/2000/01/rdf-schema#";
    /// Common OWL namespace  
    const OWL_NS: &'static str = "http://www.w3.org/2002/07/owl#";

    fn xsd_string() -> InternedString {
        InternedString::new_datatype(&format!("{}string", Self::XSD_NS))
    }

    fn xsd_integer() -> InternedString {
        InternedString::new_datatype(&format!("{}integer", Self::XSD_NS))
    }

    fn xsd_decimal() -> InternedString {
        InternedString::new_datatype(&format!("{}decimal", Self::XSD_NS))
    }

    fn xsd_boolean() -> InternedString {
        InternedString::new_datatype(&format!("{}boolean", Self::XSD_NS))
    }

    fn xsd_double() -> InternedString {
        InternedString::new_datatype(&format!("{}double", Self::XSD_NS))
    }

    fn xsd_float() -> InternedString {
        InternedString::new_datatype(&format!("{}float", Self::XSD_NS))
    }

    fn xsd_date_time() -> InternedString {
        InternedString::new_datatype(&format!("{}dateTime", Self::XSD_NS))
    }

    fn rdf_type() -> InternedString {
        InternedString::new(&format!("{}type", Self::RDF_NS))
    }

    fn rdfs_label() -> InternedString {
        InternedString::new(&format!("{}label", Self::RDFS_NS))
    }

    fn rdfs_comment() -> InternedString {
        InternedString::new(&format!("{}comment", Self::RDFS_NS))
    }
}

/// Implement RdfVocabulary for InternedString to provide easy access to common terms
impl RdfVocabulary for InternedString {}

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

    #[test]
    fn test_string_interner_basic() {
        let interner = StringInterner::new();

        let s1 = interner.intern("http://example.org/test");
        let s2 = interner.intern("http://example.org/test");
        let s3 = interner.intern("http://example.org/different");

        // Same string should return same Arc (pointer equality)
        assert!(Arc::ptr_eq(&s1, &s2));
        assert!(!Arc::ptr_eq(&s1, &s3));

        // Content should be equal
        assert_eq!(s1.as_ref(), "http://example.org/test");
        assert_eq!(s2.as_ref(), "http://example.org/test");
        assert_eq!(s3.as_ref(), "http://example.org/different");
    }

    #[test]
    fn test_string_interner_stats() {
        let interner = StringInterner::new();

        // First request - cache miss
        let _s1 = interner.intern("test");
        let stats = interner.stats();
        assert_eq!(stats.total_requests, 1);
        assert_eq!(stats.cache_misses, 1);
        assert_eq!(stats.cache_hits, 0);

        // Second request for same string - cache hit
        let _s2 = interner.intern("test");
        let stats = interner.stats();
        assert_eq!(stats.total_requests, 2);
        assert_eq!(stats.cache_misses, 1);
        assert_eq!(stats.cache_hits, 1);
        assert_eq!(stats.hit_ratio(), 0.5);
    }

    #[test]
    fn test_string_interner_cleanup() {
        let interner = StringInterner::new();

        {
            let _s1 = interner.intern("temporary");
            assert_eq!(interner.len(), 1);
        } // s1 goes out of scope

        interner.cleanup();
        assert_eq!(interner.len(), 0);
    }

    #[test]
    fn test_interned_string_creation() {
        let s1 = InternedString::new("http://example.org/test");
        let s2 = InternedString::new("http://example.org/test");
        let s3 = InternedString::new("http://example.org/different");

        assert_eq!(s1, s2);
        assert_ne!(s1, s3);
        assert_eq!(s1.as_str(), "http://example.org/test");
    }

    #[test]
    fn test_interned_string_ordering() {
        let s1 = InternedString::new("apple");
        let s2 = InternedString::new("banana");
        let s3 = InternedString::new("apple");

        assert!(s1 < s2);
        assert!(s2 > s1);
        assert_eq!(s1, s3);

        // Test that ordering is consistent
        let mut strings = vec![s2.clone(), s1.clone(), s3.clone()];
        strings.sort();
        assert_eq!(strings, vec![s1, s3, s2]);
    }

    #[test]
    fn test_interned_string_hashing() {
        use std::collections::HashMap;

        let s1 = InternedString::new("test");
        let s2 = InternedString::new("test");
        let s3 = InternedString::new("different");

        let mut map = HashMap::new();
        map.insert(s1.clone(), "value1");
        map.insert(s3.clone(), "value2");

        // s2 should map to the same value as s1 since they're equal
        assert_eq!(map.get(&s2), Some(&"value1"));
        assert_eq!(map.get(&s3), Some(&"value2"));
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn test_global_interners() {
        let iri1 = InternedString::new("http://example.org/test");
        let iri2 = InternedString::new("http://example.org/test");

        let datatype1 = InternedString::new_datatype("http://www.w3.org/2001/XMLSchema#string");
        let datatype2 = InternedString::new_datatype("http://www.w3.org/2001/XMLSchema#string");

        let lang1 = InternedString::new_language("en");
        let lang2 = InternedString::new_language("en");

        // Verify that equal strings are interned
        assert_eq!(iri1, iri2);
        assert_eq!(datatype1, datatype2);
        assert_eq!(lang1, lang2);
    }

    #[test]
    fn test_rdf_vocabulary() {
        let string_type = InternedString::xsd_string();
        let integer_type = InternedString::xsd_integer();
        let rdf_type = InternedString::rdf_type();

        assert_eq!(
            string_type.as_str(),
            "http://www.w3.org/2001/XMLSchema#string"
        );
        assert_eq!(
            integer_type.as_str(),
            "http://www.w3.org/2001/XMLSchema#integer"
        );
        assert_eq!(
            rdf_type.as_str(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
        );

        // Test that repeated calls return interned strings
        let string_type2 = InternedString::xsd_string();
        assert_eq!(string_type, string_type2);
    }

    #[test]
    fn test_interned_string_display() {
        let s = InternedString::new("http://example.org/test");
        assert_eq!(format!("{s}"), "http://example.org/test");
    }

    #[test]
    fn test_interned_string_deref() {
        let s = InternedString::new("test");
        assert_eq!(&*s, "test");
        assert_eq!(s.len(), 4);
        assert!(s.starts_with("te"));
    }

    #[test]
    fn test_interned_string_conversions() {
        let s1 = InternedString::from("test");
        let s2 = InternedString::from("test".to_string());

        assert_eq!(s1, s2);
        assert_eq!(s1.as_str(), "test");
    }

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

        let interner = Arc::new(StringInterner::new());
        let handles: Vec<_> = (0..10)
            .map(|i| {
                let interner = Arc::clone(&interner);
                thread::spawn(move || {
                    let s = format!("http://example.org/test{}", i % 3);
                    (0..100).map(|_| interner.intern(&s)).collect::<Vec<_>>()
                })
            })
            .collect();

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

        // Verify that all equal strings are the same Arc
        for result_set in &results {
            for (i, s1) in result_set.iter().enumerate() {
                for s2 in &result_set[i + 1..] {
                    if s1.as_ref() == s2.as_ref() {
                        assert!(Arc::ptr_eq(s1, s2));
                    }
                }
            }
        }

        // Should have at most 3 unique strings (test0, test1, test2)
        assert!(interner.len() <= 3);
    }

    #[test]
    fn test_term_id_mapping() {
        let interner = StringInterner::new();

        // Test interning with ID
        let (arc1, id1) = interner.intern_with_id("test_string");
        let (arc2, id2) = interner.intern_with_id("test_string");

        // Same string should get same ID
        assert_eq!(id1, id2);
        assert!(Arc::ptr_eq(&arc1, &arc2));

        // Different strings should get different IDs
        let (arc3, id3) = interner.intern_with_id("different_string");
        assert_ne!(id1, id3);
        assert!(!Arc::ptr_eq(&arc1, &arc3));

        // Test ID lookup
        assert_eq!(interner.get_id("test_string"), Some(id1));
        assert_eq!(interner.get_id("different_string"), Some(id3));
        assert_eq!(interner.get_id("nonexistent"), None);

        // Test string lookup
        assert_eq!(
            interner
                .get_string(id1)
                .expect("operation should succeed")
                .as_ref(),
            "test_string"
        );
        assert_eq!(
            interner
                .get_string(id3)
                .expect("operation should succeed")
                .as_ref(),
            "different_string"
        );
        assert_eq!(interner.get_string(999), None);
    }

    #[test]
    fn test_id_mapping_stats() {
        let interner = StringInterner::new();

        assert_eq!(interner.id_mapping_count(), 0);

        interner.intern_with_id("string1");
        assert_eq!(interner.id_mapping_count(), 1);

        interner.intern_with_id("string2");
        assert_eq!(interner.id_mapping_count(), 2);

        // Interning same string again shouldn't increase count
        interner.intern_with_id("string1");
        assert_eq!(interner.id_mapping_count(), 2);
    }

    #[test]
    fn test_get_all_mappings() {
        let interner = StringInterner::new();

        let (_, id1) = interner.intern_with_id("first");
        let (_, id2) = interner.intern_with_id("second");
        let (_, id3) = interner.intern_with_id("third");

        let mappings = interner.get_all_mappings();
        assert_eq!(mappings.len(), 3);

        // Verify all mappings are present
        let mut found_ids = [false; 3];
        for (id, string) in mappings {
            match string.as_ref() {
                "first" => {
                    assert_eq!(id, id1);
                    found_ids[0] = true;
                }
                "second" => {
                    assert_eq!(id, id2);
                    found_ids[1] = true;
                }
                "third" => {
                    assert_eq!(id, id3);
                    found_ids[2] = true;
                }
                _ => panic!("Unexpected string in mappings"),
            }
        }
        assert!(found_ids.iter().all(|&found| found));
    }

    #[test]
    fn test_mixed_interning_modes() {
        let interner = StringInterner::new();

        // Mix regular interning and ID interning
        let arc1 = interner.intern("regular");
        let (_arc2, id2) = interner.intern_with_id("with_id");
        let arc3 = interner.intern("regular"); // Same as first

        // Regular interning should still work
        assert!(Arc::ptr_eq(&arc1, &arc3));

        // ID interning should work independently
        assert_eq!(
            interner
                .get_string(id2)
                .expect("operation should succeed")
                .as_ref(),
            "with_id"
        );

        // Mixed mode length reporting should work
        assert!(interner.len() >= 2);
    }
}