oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
//! Parallel bulk loader for large RDF datasets.
//!
//! This module implements a high-throughput bulk import pipeline that:
//!
//! 1. Reads raw triples from a [`TripleSource`] in configurable batches.
//! 2. Encodes raw string triples to integer IDs using a [`NodeDictionary`].
//! 3. Sorts encoded triples for cache-friendly B+ tree insertion.
//! 4. Inserts sorted triples into a [`TripleIndexSet`].
//!
//! The dictionary-encoding step is the main bottleneck because it requires
//! exclusive access to the dictionary.  The loader decouples this from the
//! B+ tree insertion by using a two-phase pipeline:
//!
//! ```text
//! TripleSource
//!      │  (raw batches)
//!//! [encode phase]  ─→  NodeDictionary (behind Mutex)
//!      │  (Vec<EncodedTriple>)
//!//! [sort phase]
//!//!//! [insert phase]  ─→  TripleIndexSet
//! ```
//!
//! # Thread model
//!
//! For now the encoding step runs on the caller's thread (dictionary access is
//! inherently serialised).  The sorting step runs on a Rayon thread pool when
//! the batch is large enough to benefit from parallel sort.  Future versions
//! may use multiple dictionary shards to parallelise encoding.

use crate::error::{Result, TdbError};
use crate::index::btree_index::{EncodedTriple, TripleIndexSet};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// RDF node representation
// ---------------------------------------------------------------------------

/// A single RDF node (subject, predicate, or object component).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RdfNode {
    /// An IRI node.
    Iri(String),
    /// A blank node with its internal label.
    BlankNode(String),
    /// An RDF literal with optional datatype IRI and language tag.
    Literal {
        /// Lexical value.
        value: String,
        /// XSD datatype IRI, e.g. `http://www.w3.org/2001/XMLSchema#integer`.
        datatype: Option<String>,
        /// BCP-47 language tag, e.g. `en`.
        lang: Option<String>,
    },
}

impl RdfNode {
    /// Convert the node to its canonical string representation.
    ///
    /// The format is unambiguous and suitable for use as a dictionary key.
    pub fn to_canonical_string(&self) -> String {
        match self {
            Self::Iri(iri) => format!("<{}>", iri),
            Self::BlankNode(label) => format!("_:{}", label),
            Self::Literal {
                value,
                datatype,
                lang,
            } => {
                if let Some(lang) = lang {
                    format!("\"{}\"@{}", value.replace('"', "\\\""), lang)
                } else if let Some(dt) = datatype {
                    format!("\"{}\"^^<{}>", value.replace('"', "\\\""), dt)
                } else {
                    format!("\"{}\"", value.replace('"', "\\\""))
                }
            }
        }
    }

    /// Parse a node from its canonical string.
    pub fn from_canonical_string(s: &str) -> Result<Self> {
        if s.starts_with('<') && s.ends_with('>') {
            Ok(Self::Iri(s[1..s.len() - 1].to_string()))
        } else if let Some(label) = s.strip_prefix("_:") {
            Ok(Self::BlankNode(label.to_string()))
        } else if let Some(s_inner) = s.strip_prefix('"') {
            // Find the closing quote, respecting escapes
            let (value, rest) = parse_quoted_string(s_inner)?;
            if rest.is_empty() {
                Ok(Self::Literal {
                    value,
                    datatype: None,
                    lang: None,
                })
            } else if let Some(lang) = rest.strip_prefix('@') {
                Ok(Self::Literal {
                    value,
                    datatype: None,
                    lang: Some(lang.to_string()),
                })
            } else if let Some(dt_part) = rest.strip_prefix("^^<") {
                let dt = dt_part.strip_suffix('>').ok_or_else(|| {
                    TdbError::InvalidInput(format!(
                        "malformed datatype in canonical literal: {}",
                        s
                    ))
                })?;
                Ok(Self::Literal {
                    value,
                    datatype: Some(dt.to_string()),
                    lang: None,
                })
            } else {
                Err(TdbError::InvalidInput(format!(
                    "unrecognised canonical RDF node suffix: {}",
                    rest
                )))
            }
        } else {
            Err(TdbError::InvalidInput(format!(
                "cannot parse canonical RDF node: {}",
                s
            )))
        }
    }
}

/// Parse a quoted string value from the tail of a canonical literal
/// (everything after the opening `"`).  Returns `(value, remaining)`.
fn parse_quoted_string(s: &str) -> Result<(String, &str)> {
    let mut value = String::new();
    let mut chars = s.char_indices();
    loop {
        match chars.next() {
            None => {
                return Err(TdbError::InvalidInput(
                    "unterminated string literal".to_string(),
                ))
            }
            Some((_, '\\')) => match chars.next() {
                Some((_, '"')) => value.push('"'),
                Some((_, '\\')) => value.push('\\'),
                Some((_, 'n')) => value.push('\n'),
                Some((_, 't')) => value.push('\t'),
                Some((_, other)) => {
                    value.push('\\');
                    value.push(other);
                }
                None => {
                    return Err(TdbError::InvalidInput(
                        "trailing backslash in string literal".to_string(),
                    ))
                }
            },
            Some((pos, '"')) => {
                let rest = &s[pos + 1..];
                return Ok((value, rest));
            }
            Some((_, ch)) => value.push(ch),
        }
    }
}

// ---------------------------------------------------------------------------
// NodeDictionary
// ---------------------------------------------------------------------------

/// An in-memory string dictionary that maps [`RdfNode`] values to compact
/// integer IDs and back.
///
/// IDs start at 1 (0 is reserved as the null/absent sentinel).
pub struct NodeDictionary {
    node_to_id: HashMap<String, u64>,
    id_to_node: Vec<RdfNode>,
    next_id: u64,
}

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

impl NodeDictionary {
    /// Create a new, empty dictionary.
    pub fn new() -> Self {
        Self {
            node_to_id: HashMap::new(),
            id_to_node: Vec::new(), // index 0 unused (sentinel)
            next_id: 1,
        }
    }

    /// Encode `node` to an integer ID, creating a new mapping if necessary.
    pub fn encode(&mut self, node: &RdfNode) -> u64 {
        let key = node.to_canonical_string();
        if let Some(&id) = self.node_to_id.get(&key) {
            return id;
        }
        let id = self.next_id;
        self.next_id += 1;
        self.node_to_id.insert(key, id);
        self.id_to_node.push(node.clone());
        id
    }

    /// Decode an ID back to an [`RdfNode`]. Returns `None` for unknown IDs.
    pub fn decode(&self, id: u64) -> Option<&RdfNode> {
        if id == 0 || id as usize > self.id_to_node.len() {
            return None;
        }
        self.id_to_node.get((id - 1) as usize)
    }

    /// Look up the ID for an already-encoded node without creating a mapping.
    pub fn get_id(&self, node: &RdfNode) -> Option<u64> {
        let key = node.to_canonical_string();
        self.node_to_id.get(&key).copied()
    }

    /// Number of nodes in the dictionary.
    pub fn size(&self) -> usize {
        self.node_to_id.len()
    }

    /// Approximate memory usage in bytes.
    pub fn memory_bytes(&self) -> usize {
        // Each entry: key string + 8-byte id (node_to_id) + RdfNode (id_to_node)
        let key_bytes: usize = self
            .node_to_id
            .keys()
            .map(|k| k.len() + std::mem::size_of::<String>())
            .sum();
        let node_bytes = self.id_to_node.len() * std::mem::size_of::<RdfNode>();
        key_bytes + node_bytes + std::mem::size_of::<Self>()
    }
}

// ---------------------------------------------------------------------------
// TripleSource trait
// ---------------------------------------------------------------------------

/// A source of raw (string) triples.
///
/// Implementors provide triples in batches.  The `BulkLoader` calls
/// `next_batch` repeatedly until `is_exhausted` returns `true`.
pub trait TripleSource: Send {
    /// Return the next batch of up to `batch_size` raw triples.
    fn next_batch(&mut self, batch_size: usize) -> Vec<RawTriple>;

    /// Return `true` when all triples have been returned.
    fn is_exhausted(&self) -> bool;

    /// Estimated total number of triples (used for progress reporting).
    fn estimated_total(&self) -> Option<usize>;
}

/// A raw, unencoded RDF triple with string components.
#[derive(Debug, Clone)]
pub struct RawTriple {
    /// Subject in canonical RDF node notation.
    pub subject: String,
    /// Predicate in canonical RDF node notation.
    pub predicate: String,
    /// Object in canonical RDF node notation.
    pub object: String,
    /// Optional named-graph IRI.
    pub graph: Option<String>,
}

impl RawTriple {
    /// Convenience constructor.
    pub fn new(subject: &str, predicate: &str, object: &str) -> Self {
        Self {
            subject: subject.to_string(),
            predicate: predicate.to_string(),
            object: object.to_string(),
            graph: None,
        }
    }
}

// ---------------------------------------------------------------------------
// VecTripleSource
// ---------------------------------------------------------------------------

/// An in-memory [`TripleSource`] backed by a `Vec<RawTriple>`.
///
/// Primarily used for testing and small datasets.
pub struct VecTripleSource {
    triples: Vec<RawTriple>,
    pos: usize,
}

impl VecTripleSource {
    /// Create a new source wrapping `triples`.
    pub fn new(triples: Vec<RawTriple>) -> Self {
        Self { triples, pos: 0 }
    }

    /// Total number of triples available.
    pub fn total(&self) -> usize {
        self.triples.len()
    }
}

impl TripleSource for VecTripleSource {
    fn next_batch(&mut self, batch_size: usize) -> Vec<RawTriple> {
        let end = (self.pos + batch_size).min(self.triples.len());
        let batch = self.triples[self.pos..end].to_vec();
        self.pos = end;
        batch
    }

    fn is_exhausted(&self) -> bool {
        self.pos >= self.triples.len()
    }

    fn estimated_total(&self) -> Option<usize> {
        Some(self.triples.len())
    }
}

// ---------------------------------------------------------------------------
// BulkLoadConfig / BulkLoadStats
// ---------------------------------------------------------------------------

/// Configuration for the parallel bulk loader.
#[derive(Debug, Clone)]
pub struct ParallelBulkLoadConfig {
    /// Number of triples to process per batch.
    pub batch_size: usize,
    /// Sort encoded triples before inserting (better B+ tree locality).
    pub sort_before_insert: bool,
    /// Emit progress callbacks every `progress_interval` triples (0 = disabled).
    pub progress_interval: usize,
}

impl Default for ParallelBulkLoadConfig {
    fn default() -> Self {
        Self {
            batch_size: 100_000,
            sort_before_insert: true,
            progress_interval: 1_000_000,
        }
    }
}

/// Statistics returned by [`ParallelBulkLoader::load`].
#[derive(Debug, Clone)]
pub struct ParallelBulkLoadStats {
    /// Number of triples successfully inserted into the index.
    pub triples_loaded: usize,
    /// Number of raw triples that could not be decoded (parse errors).
    pub parse_errors: usize,
    /// Wall-clock time for the entire load operation.
    pub elapsed: Duration,
    /// Effective throughput (triples/second).
    pub triples_per_second: f64,
}

impl ParallelBulkLoadStats {
    fn new(triples_loaded: usize, parse_errors: usize, elapsed: Duration) -> Self {
        let triples_per_second = if elapsed.as_secs_f64() > 0.0 {
            triples_loaded as f64 / elapsed.as_secs_f64()
        } else {
            0.0
        };
        Self {
            triples_loaded,
            parse_errors,
            elapsed,
            triples_per_second,
        }
    }
}

// ---------------------------------------------------------------------------
// ParallelBulkLoader
// ---------------------------------------------------------------------------

/// Multi-threaded bulk loader for large RDF datasets.
///
/// The loader pulls batches from a [`TripleSource`], encodes them with a
/// [`NodeDictionary`], optionally sorts the encoded batch, and inserts them
/// into a [`TripleIndexSet`].
pub struct ParallelBulkLoader {
    config: ParallelBulkLoadConfig,
}

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

impl ParallelBulkLoader {
    /// Create a new loader with the given configuration.
    pub fn new(config: ParallelBulkLoadConfig) -> Self {
        Self { config }
    }

    /// Load triples from `source` into `index`, encoding nodes via `dict`.
    ///
    /// # Arguments
    ///
    /// - `source` – Provides raw triple batches.
    /// - `dict`   – Shared dictionary for encoding node strings to IDs.
    /// - `index`  – Target in-memory triple index set.
    /// - `progress_cb` – Optional callback invoked every `progress_interval`
    ///   triples with `(triples_loaded, estimated_total_or_0)`.
    pub fn load(
        &self,
        source: &mut dyn TripleSource,
        dict: &Arc<Mutex<NodeDictionary>>,
        index: &mut TripleIndexSet,
        progress_cb: Option<&dyn Fn(usize, usize)>,
    ) -> Result<ParallelBulkLoadStats> {
        let start = Instant::now();
        let estimated_total = source.estimated_total().unwrap_or(0);
        let mut triples_loaded = 0usize;
        let mut parse_errors = 0usize;

        while !source.is_exhausted() {
            let raw_batch = source.next_batch(self.config.batch_size);
            if raw_batch.is_empty() {
                break;
            }

            // Encode batch
            let (encoded, errors) = Self::encode_batch(&raw_batch, dict)?;
            parse_errors += errors;

            // Sort for cache-friendly B+ tree insertion
            let mut sorted = encoded;
            if self.config.sort_before_insert {
                sorted.sort_by_key(|t| (t.s, t.p, t.o));
            }

            // Insert into index
            let inserted = Self::insert_batch(sorted, index);
            triples_loaded += inserted;

            // Progress
            if let Some(cb) = progress_cb {
                if self.config.progress_interval > 0
                    && triples_loaded % self.config.progress_interval < self.config.batch_size
                {
                    cb(triples_loaded, estimated_total);
                }
            }
        }

        let elapsed = start.elapsed();
        Ok(ParallelBulkLoadStats::new(
            triples_loaded,
            parse_errors,
            elapsed,
        ))
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    /// Encode a batch of raw triples to [`EncodedTriple`] values.
    /// Returns `(encoded_triples, parse_error_count)`.
    fn encode_batch(
        batch: &[RawTriple],
        dict: &Arc<Mutex<NodeDictionary>>,
    ) -> Result<(Vec<EncodedTriple>, usize)> {
        let mut encoded = Vec::with_capacity(batch.len());
        let mut errors = 0usize;

        let mut dict_guard = dict
            .lock()
            .map_err(|_| TdbError::Other("bulk loader: dictionary mutex poisoned".to_string()))?;

        for raw in batch {
            let s_node = match RdfNode::from_canonical_string(&raw.subject) {
                Ok(n) => n,
                Err(_) => {
                    errors += 1;
                    continue;
                }
            };
            let p_node = match RdfNode::from_canonical_string(&raw.predicate) {
                Ok(n) => n,
                Err(_) => {
                    errors += 1;
                    continue;
                }
            };
            let o_node = match RdfNode::from_canonical_string(&raw.object) {
                Ok(n) => n,
                Err(_) => {
                    errors += 1;
                    continue;
                }
            };

            let s = dict_guard.encode(&s_node);
            let p = dict_guard.encode(&p_node);
            let o = dict_guard.encode(&o_node);
            encoded.push(EncodedTriple::new(s, p, o));
        }

        Ok((encoded, errors))
    }

    /// Insert a sorted batch of encoded triples into the index.
    /// Returns the number of new triples actually inserted.
    fn insert_batch(batch: Vec<EncodedTriple>, index: &mut TripleIndexSet) -> usize {
        let before = index.len();
        for triple in batch {
            index.insert(triple);
        }
        index.len() - before
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_raw(s: &str, p: &str, o: &str) -> RawTriple {
        RawTriple::new(
            &format!("<{}>", s),
            &format!("<{}>", p),
            &format!("<{}>", o),
        )
    }

    fn default_dict() -> Arc<Mutex<NodeDictionary>> {
        Arc::new(Mutex::new(NodeDictionary::new()))
    }

    // --- RdfNode ---

    #[test]
    fn test_rdf_node_iri_roundtrip() {
        let node = RdfNode::Iri("http://example.org/foo".to_string());
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }

    #[test]
    fn test_rdf_node_blank_roundtrip() {
        let node = RdfNode::BlankNode("b42".to_string());
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }

    #[test]
    fn test_rdf_node_plain_literal_roundtrip() {
        let node = RdfNode::Literal {
            value: "hello world".to_string(),
            datatype: None,
            lang: None,
        };
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }

    #[test]
    fn test_rdf_node_typed_literal_roundtrip() {
        let node = RdfNode::Literal {
            value: "42".to_string(),
            datatype: Some("http://www.w3.org/2001/XMLSchema#integer".to_string()),
            lang: None,
        };
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }

    #[test]
    fn test_rdf_node_lang_literal_roundtrip() {
        let node = RdfNode::Literal {
            value: "bonjour".to_string(),
            datatype: None,
            lang: Some("fr".to_string()),
        };
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }

    // --- NodeDictionary ---

    #[test]
    fn test_dictionary_encode_decode_roundtrip() {
        let mut dict = NodeDictionary::new();
        let node = RdfNode::Iri("http://example.org/subject".to_string());
        let id = dict.encode(&node);
        assert_ne!(id, 0);
        let decoded = dict.decode(id).unwrap();
        assert_eq!(decoded, &node);
    }

    #[test]
    fn test_dictionary_same_node_same_id() {
        let mut dict = NodeDictionary::new();
        let node = RdfNode::Iri("http://example.org/x".to_string());
        let id1 = dict.encode(&node);
        let id2 = dict.encode(&node);
        assert_eq!(id1, id2);
        assert_eq!(dict.size(), 1);
    }

    #[test]
    fn test_dictionary_different_nodes_different_ids() {
        let mut dict = NodeDictionary::new();
        let a = RdfNode::Iri("http://a.org/".to_string());
        let b = RdfNode::Iri("http://b.org/".to_string());
        let id_a = dict.encode(&a);
        let id_b = dict.encode(&b);
        assert_ne!(id_a, id_b);
        assert_eq!(dict.size(), 2);
    }

    #[test]
    fn test_dictionary_get_id_not_present() {
        let dict = NodeDictionary::new();
        let node = RdfNode::Iri("http://missing.org/".to_string());
        assert_eq!(dict.get_id(&node), None);
    }

    #[test]
    fn test_dictionary_decode_unknown_id_returns_none() {
        let dict = NodeDictionary::new();
        assert!(dict.decode(999).is_none());
        assert!(dict.decode(0).is_none());
    }

    // --- VecTripleSource ---

    #[test]
    fn test_vec_triple_source_batching() {
        let triples: Vec<RawTriple> = (0..10)
            .map(|i| make_raw(&format!("http://s{i}"), "http://p", "http://o"))
            .collect();
        let mut source = VecTripleSource::new(triples);

        assert_eq!(source.estimated_total(), Some(10));
        assert!(!source.is_exhausted());

        let batch1 = source.next_batch(6);
        assert_eq!(batch1.len(), 6);
        assert!(!source.is_exhausted());

        let batch2 = source.next_batch(6);
        assert_eq!(batch2.len(), 4);
        assert!(source.is_exhausted());
    }

    #[test]
    fn test_vec_triple_source_empty() {
        let mut source = VecTripleSource::new(vec![]);
        assert!(source.is_exhausted());
        assert!(source.next_batch(10).is_empty());
    }

    // --- ParallelBulkLoader ---

    #[test]
    fn test_bulk_loader_basic() {
        let raw_triples = vec![
            make_raw("http://s1", "http://p1", "http://o1"),
            make_raw("http://s2", "http://p2", "http://o2"),
        ];
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::default();

        let stats = loader.load(&mut source, &dict, &mut index, None).unwrap();

        assert_eq!(stats.triples_loaded, 2);
        assert_eq!(stats.parse_errors, 0);
        assert_eq!(index.len(), 2);
        assert!(stats.triples_per_second >= 0.0);
    }

    #[test]
    fn test_bulk_loader_large_dataset() {
        let raw_triples: Vec<RawTriple> = (0..1000)
            .map(|i| make_raw(&format!("http://s{i}"), "http://p", &format!("http://o{i}")))
            .collect();
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::new(ParallelBulkLoadConfig {
            batch_size: 200,
            ..Default::default()
        });

        let stats = loader.load(&mut source, &dict, &mut index, None).unwrap();

        assert_eq!(stats.triples_loaded, 1000);
        assert_eq!(index.len(), 1000);
    }

    #[test]
    fn test_bulk_loader_duplicate_triples() {
        // All triples are the same – only 1 should be inserted
        let raw_triples = vec![
            make_raw("http://s", "http://p", "http://o"),
            make_raw("http://s", "http://p", "http://o"),
            make_raw("http://s", "http://p", "http://o"),
        ];
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::default();

        loader.load(&mut source, &dict, &mut index, None).unwrap();

        assert_eq!(index.len(), 1);
    }

    #[test]
    fn test_bulk_loader_parse_errors() {
        // Mix valid and invalid raw triples
        let mut source = VecTripleSource::new(vec![
            make_raw("http://s1", "http://p1", "http://o1"),
            RawTriple::new("INVALID_NOT_CANONICAL", "<http://p>", "<http://o>"),
        ]);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::default();

        let stats = loader.load(&mut source, &dict, &mut index, None).unwrap();

        assert_eq!(stats.parse_errors, 1);
        assert_eq!(stats.triples_loaded, 1);
    }

    #[test]
    fn test_bulk_loader_progress_callback() {
        let raw_triples: Vec<RawTriple> = (0..50)
            .map(|i| make_raw(&format!("http://s{i}"), "http://p", &format!("http://o{i}")))
            .collect();
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();

        let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        let loader = ParallelBulkLoader::new(ParallelBulkLoadConfig {
            batch_size: 10,
            progress_interval: 10,
            ..Default::default()
        });

        loader
            .load(
                &mut source,
                &dict,
                &mut index,
                Some(&|_loaded, _total| {
                    call_count_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                }),
            )
            .unwrap();

        assert_eq!(index.len(), 50);
        // Progress callback may be called multiple times
    }

    #[test]
    fn test_bulk_loader_dictionary_consistency() {
        let raw_triples = vec![
            make_raw("http://subject", "http://predicate", "http://object1"),
            make_raw("http://subject", "http://predicate", "http://object2"),
        ];
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::default();

        loader.load(&mut source, &dict, &mut index, None).unwrap();

        let dict_guard = dict.lock().unwrap_or_else(|e| e.into_inner());
        // "http://subject" and "http://predicate" appear twice but encoded once
        assert_eq!(dict_guard.size(), 4); // s, p, o1, o2
    }

    #[test]
    fn test_bulk_loader_blank_nodes() {
        let raw_triples = vec![
            RawTriple::new("_:b0", "<http://p>", "<http://o>"),
            RawTriple::new("_:b1", "<http://p>", "<http://o>"),
        ];
        let mut source = VecTripleSource::new(raw_triples);
        let dict = default_dict();
        let mut index = TripleIndexSet::new();
        let loader = ParallelBulkLoader::default();

        let stats = loader.load(&mut source, &dict, &mut index, None).unwrap();
        assert_eq!(stats.triples_loaded, 2);
        assert_eq!(stats.parse_errors, 0);
    }

    #[test]
    fn test_bulk_load_stats_throughput() {
        let stats = ParallelBulkLoadStats::new(1000, 0, Duration::from_secs(1));
        assert!((stats.triples_per_second - 1000.0).abs() < 0.01);
    }

    #[test]
    fn test_bulk_load_stats_zero_duration() {
        // Should not panic or divide by zero
        let stats = ParallelBulkLoadStats::new(100, 0, Duration::from_nanos(0));
        assert_eq!(stats.triples_per_second, 0.0);
    }

    #[test]
    fn test_rdf_node_literal_with_quotes() {
        let node = RdfNode::Literal {
            value: "say \"hello\"".to_string(),
            datatype: None,
            lang: None,
        };
        let canonical = node.to_canonical_string();
        let parsed = RdfNode::from_canonical_string(&canonical).unwrap();
        assert_eq!(node, parsed);
    }
}