oxirs-ttl 0.2.4

Turtle-family RDF parser and serializer for OxiRS - ported from Oxigraph
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
//! Streaming support for memory-efficient parsing of large RDF files
//!
//! This module provides streaming parsers that can process very large RDF files
//! (>1GB) with minimal memory usage by parsing in batches.
//!
//! # Features
//!
//! - **Batch Processing**: Process triples in configurable batch sizes
//! - **Memory Efficient**: Stream files larger than available RAM
//! - **Progress Tracking**: Monitor parsing progress
//! - **Error Recovery**: Optional lenient mode to skip errors
//! - **Prefix Preservation**: Maintains prefix declarations across batches
//!
//! # Example: Basic Streaming
//!
//! ```rust
//! use oxirs_ttl::{StreamingParser, StreamingConfig};
//! use std::io::Cursor;
//!
//! let data = Cursor::new(b"<http://s> <http://p> <http://o> .");
//! let parser = StreamingParser::new(data);
//!
//! let mut total = 0;
//! for batch in parser.batches() {
//!     let triples = batch?;
//!     total += triples.len();
//! }
//! assert_eq!(total, 1);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Example: Custom Batch Size
//!
//! ```rust
//! use oxirs_ttl::{StreamingParser, StreamingConfig};
//! use std::io::Cursor;
//!
//! let config = StreamingConfig::default()
//!     .with_batch_size(5000)  // 5K triples per batch
//!     .with_max_buffer_size(50 * 1024 * 1024);  // 50MB buffer
//!
//! let data = Cursor::new(b"<http://s> <http://p> <http://o> .");
//! let parser = StreamingParser::with_config(data, config);
//!
//! for batch in parser.batches() {
//!     let triples = batch?;
//!     // Process batch
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Example: Processing Large Files
//!
//! ```rust,no_run
//! use oxirs_ttl::{StreamingParser, StreamingConfig};
//! use std::fs::File;
//!
//! let config = StreamingConfig::default()
//!     .with_batch_size(10_000);
//!
//! let file = File::open("large_dataset.ttl")?;
//! let parser = StreamingParser::with_config(file, config);
//!
//! let mut total = 0;
//! for batch in parser.batches() {
//!     let triples = batch?;
//!     total += triples.len();
//!
//!     // Process each batch (e.g., insert into database)
//!     if total % 100_000 == 0 {
//!         println!("Processed {} triples", total);
//!     }
//! }
//! println!("Total: {} triples", total);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::error::{TurtleParseError, TurtleResult};
use oxirs_core::model::Triple;
use std::io::{BufRead, BufReader, Read};

/// Configuration for streaming parser
///
/// Controls batch size, memory limits, and error handling for streaming parsers.
///
/// # Example
///
/// ```rust
/// use oxirs_ttl::StreamingConfig;
///
/// let config = StreamingConfig::default()
///     .with_batch_size(5000)
///     .lenient(true)
///     .with_max_buffer_size(100 * 1024 * 1024);
/// ```
#[derive(Debug, Clone)]
pub struct StreamingConfig {
    /// Number of triples to buffer before yielding
    pub batch_size: usize,
    /// Whether to continue parsing after errors
    pub lenient: bool,
    /// Maximum memory to use for buffering (bytes)
    pub max_buffer_size: usize,
}

impl Default for StreamingConfig {
    fn default() -> Self {
        Self {
            batch_size: 10_000,
            lenient: false,
            max_buffer_size: 100 * 1024 * 1024, // 100 MB
        }
    }
}

impl StreamingConfig {
    /// Create a new streaming configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the batch size
    pub fn with_batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Enable lenient mode (continue on errors)
    pub fn lenient(mut self, lenient: bool) -> Self {
        self.lenient = lenient;
        self
    }

    /// Set maximum buffer size
    pub fn with_max_buffer_size(mut self, size: usize) -> Self {
        self.max_buffer_size = size;
        self
    }
}

/// Streaming parser that yields batches of triples
pub struct StreamingParser<R: BufRead> {
    reader: R,
    config: StreamingConfig,
    buffer: String,
    triples_parsed: usize,
    bytes_read: usize,
    /// Accumulated prefix declarations to preserve across batches
    prefix_declarations: String,
}

impl<R: Read> StreamingParser<BufReader<R>> {
    /// Create a new streaming parser from a reader
    pub fn new(reader: R) -> Self {
        Self::with_config(reader, StreamingConfig::default())
    }

    /// Create a streaming parser with custom configuration
    pub fn with_config(reader: R, config: StreamingConfig) -> Self {
        Self {
            reader: BufReader::new(reader),
            config,
            buffer: String::new(),
            triples_parsed: 0,
            bytes_read: 0,
            prefix_declarations: String::new(),
        }
    }
}

impl<R: BufRead> StreamingParser<R> {
    /// Create from an existing BufRead
    pub fn from_buf_reader(reader: R, config: StreamingConfig) -> Self {
        Self {
            reader,
            config,
            buffer: String::new(),
            triples_parsed: 0,
            bytes_read: 0,
            prefix_declarations: String::new(),
        }
    }

    /// Get the number of triples parsed so far
    pub fn triples_parsed(&self) -> usize {
        self.triples_parsed
    }

    /// Get the number of bytes read so far
    pub fn bytes_read(&self) -> usize {
        self.bytes_read
    }

    /// Parse the next batch of triples
    pub fn next_batch(&mut self) -> TurtleResult<Option<Vec<Triple>>> {
        use crate::formats::trig::TriGParser;
        use crate::toolkit::Parser;
        use crate::turtle::TurtleParser;
        use oxirs_core::model::Quad;

        // Read up to batch_size lines or until buffer limit
        // But always complete the current statement (read until we see a '.' or '}')
        self.buffer.clear();
        let mut lines_read = 0;
        let target_lines = self.config.batch_size / 10; // Rough estimate: ~10 triples per line
        let mut in_multiline_string = false;
        let mut last_line_ended_statement = false;

        while lines_read < target_lines && self.buffer.len() < self.config.max_buffer_size {
            let mut line = String::new();
            match self.reader.read_line(&mut line) {
                Ok(0) => break, // EOF
                Ok(n) => {
                    self.bytes_read += n;
                    self.buffer.push_str(&line);
                    lines_read += 1;

                    // Track multiline strings
                    let triple_quotes =
                        line.matches("\"\"\"").count() + line.matches("'''").count();
                    if triple_quotes % 2 == 1 {
                        in_multiline_string = !in_multiline_string;
                    }

                    // Check if this line ends a statement (only if not in multiline string)
                    let trimmed = line.trim();
                    if !in_multiline_string && (trimmed.ends_with('.') || trimmed == "}") {
                        last_line_ended_statement = true;
                        // If we've read enough lines and found a statement boundary, stop here
                        if lines_read >= target_lines {
                            break;
                        }
                    } else {
                        last_line_ended_statement = false;
                    }
                }
                Err(e) => return Err(TurtleParseError::io(e)),
            }
        }

        // Continue reading until we complete the current statement
        // (unless we're at EOF or hit the buffer limit)
        while !last_line_ended_statement
            && self.buffer.len() < self.config.max_buffer_size
            && !in_multiline_string
        {
            let mut line = String::new();
            match self.reader.read_line(&mut line) {
                Ok(0) => break, // EOF
                Ok(n) => {
                    self.bytes_read += n;
                    self.buffer.push_str(&line);

                    // Track multiline strings
                    let triple_quotes =
                        line.matches("\"\"\"").count() + line.matches("'''").count();
                    if triple_quotes % 2 == 1 {
                        in_multiline_string = !in_multiline_string;
                    }

                    let trimmed = line.trim();
                    if !in_multiline_string && (trimmed.ends_with('.') || trimmed == "}") {
                        break;
                    }
                }
                Err(e) => return Err(TurtleParseError::io(e)),
            }
        }

        if self.buffer.is_empty() {
            return Ok(None); // EOF
        }

        // Extract prefix and base declarations from this batch
        for line in self.buffer.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("@prefix") || trimmed.starts_with("@base") {
                // Save prefix/base declarations for future batches
                if !self.prefix_declarations.contains(trimmed) {
                    self.prefix_declarations.push_str(trimmed);
                    self.prefix_declarations.push('\n');
                }
            }
        }

        // Prepend accumulated prefix declarations to this batch
        let document = format!("{}{}", self.prefix_declarations, self.buffer);

        // Detect if this is TriG (contains named graphs) or Turtle
        let is_trig = document.contains('{') || document.contains("GRAPH");

        if is_trig {
            // For TriG format with named graphs, read the entire document
            // (proper streaming would require graph-aware state management)
            let mut complete_document = document.clone();
            loop {
                let mut line = String::new();
                match self.reader.read_line(&mut line) {
                    Ok(0) => break, // EOF
                    Ok(n) => {
                        self.bytes_read += n;
                        complete_document.push_str(&line);
                    }
                    Err(e) => return Err(TurtleParseError::io(e)),
                }
            }

            // Use TriG parser for documents with named graphs
            let mut parser = TriGParser::new();
            if self.config.lenient {
                parser.lenient = true;
            }

            match parser.parse(complete_document.as_bytes()) {
                Ok(quads) => {
                    // Extract triples from quads
                    let triples: Vec<Triple> = quads
                        .into_iter()
                        .map(|q: Quad| {
                            Triple::new(
                                q.subject().clone(),
                                q.predicate().clone(),
                                q.object().clone(),
                            )
                        })
                        .collect();
                    self.triples_parsed += triples.len();
                    // Clear buffer to signal EOF on next call
                    self.buffer.clear();
                    Ok(Some(triples))
                }
                Err(_e) if self.config.lenient => {
                    // In lenient mode, return empty batch on error
                    // Clear buffer to signal EOF on next call
                    self.buffer.clear();
                    Ok(Some(Vec::new()))
                }
                Err(e) => Err(e),
            }
        } else {
            // Use Turtle parser for plain triples
            let parser = if self.config.lenient {
                TurtleParser::new_lenient()
            } else {
                TurtleParser::new()
            };

            match parser.parse_document(&document) {
                Ok(triples) => {
                    self.triples_parsed += triples.len();
                    Ok(Some(triples))
                }
                Err(_e) if self.config.lenient => {
                    // In lenient mode, return empty batch on error
                    Ok(Some(Vec::new()))
                }
                Err(e) => Err(e),
            }
        }
    }

    /// Get an iterator over batches
    pub fn batches(self) -> StreamingBatchIterator<R> {
        StreamingBatchIterator { parser: self }
    }

    /// Get an iterator over individual triples
    pub fn triples(self) -> StreamingTripleIterator<R> {
        StreamingTripleIterator {
            parser: self,
            current_batch: Vec::new(),
            batch_index: 0,
        }
    }
}

/// Iterator over batches of triples
pub struct StreamingBatchIterator<R: BufRead> {
    parser: StreamingParser<R>,
}

impl<R: BufRead> Iterator for StreamingBatchIterator<R> {
    type Item = TurtleResult<Vec<Triple>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.parser.next_batch() {
            Ok(Some(batch)) => Some(Ok(batch)),
            Ok(None) => None,
            Err(e) => Some(Err(e)),
        }
    }
}

/// Iterator over individual triples
pub struct StreamingTripleIterator<R: BufRead> {
    parser: StreamingParser<R>,
    current_batch: Vec<Triple>,
    batch_index: usize,
}

impl<R: BufRead> Iterator for StreamingTripleIterator<R> {
    type Item = TurtleResult<Triple>;

    fn next(&mut self) -> Option<Self::Item> {
        // If we have triples in the current batch, return the next one
        if self.batch_index < self.current_batch.len() {
            let triple = self.current_batch[self.batch_index].clone();
            self.batch_index += 1;
            return Some(Ok(triple));
        }

        // Need to load next batch
        match self.parser.next_batch() {
            Ok(Some(batch)) => {
                self.current_batch = batch;
                self.batch_index = 0;
                self.next() // Recursively get first item from new batch
            }
            Ok(None) => None, // EOF
            Err(e) => Some(Err(e)),
        }
    }
}

/// Progress callback for streaming operations
pub trait ProgressCallback: Send {
    /// Called when a batch is parsed
    fn on_batch(&mut self, triples_count: usize, bytes_read: usize);

    /// Called when an error occurs (in lenient mode)
    fn on_error(&mut self, error: &TurtleParseError);
}

/// Simple progress printer
pub struct PrintProgress {
    last_report: usize,
    report_interval: usize,
}

impl PrintProgress {
    /// Create a new progress printer
    pub fn new(report_interval: usize) -> Self {
        Self {
            last_report: 0,
            report_interval,
        }
    }
}

impl Default for PrintProgress {
    fn default() -> Self {
        Self::new(10_000)
    }
}

impl ProgressCallback for PrintProgress {
    fn on_batch(&mut self, triples_count: usize, bytes_read: usize) {
        if triples_count - self.last_report >= self.report_interval {
            eprintln!(
                "Parsed {} triples ({:.2} MB)",
                triples_count,
                bytes_read as f64 / 1_024_000.0
            );
            self.last_report = triples_count;
        }
    }

    fn on_error(&mut self, error: &TurtleParseError) {
        eprintln!("Warning: {}", error);
    }
}

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

    #[test]
    fn test_streaming_parser_basic() {
        let turtle = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" .
            ex:bob ex:name "Bob" .
            ex:charlie ex:name "Charlie" .
        "#;

        let reader = Cursor::new(turtle);
        let mut parser = StreamingParser::new(reader);

        let batch = parser.next_batch().expect("parsing should succeed");
        assert!(batch.is_some());

        let triples = batch.expect("operation should succeed");
        assert_eq!(triples.len(), 3);
    }

    #[test]
    fn test_batch_iterator() {
        let turtle = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" .
            ex:bob ex:name "Bob" .
        "#;

        let reader = Cursor::new(turtle);
        let parser = StreamingParser::new(reader);

        let batches: Vec<_> = parser.batches().collect();
        assert_eq!(batches.len(), 1);
        assert!(batches[0].is_ok());
    }

    #[test]
    fn test_triple_iterator() {
        let turtle = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" .
            ex:bob ex:name "Bob" .
        "#;

        let reader = Cursor::new(turtle);
        let parser = StreamingParser::new(reader);

        let triples: Result<Vec<_>, _> = parser.triples().collect();
        assert!(triples.is_ok());
        assert_eq!(triples.expect("operation should succeed").len(), 2);
    }

    #[test]
    fn test_large_document_streaming() {
        // Generate a large document
        let mut turtle = String::from("@prefix ex: <http://example.org/> .\n");
        for i in 0..1000 {
            turtle.push_str(&format!("ex:subject{} ex:predicate \"object{}\" .\n", i, i));
        }

        let reader = Cursor::new(turtle);
        let config = StreamingConfig::default().with_batch_size(100);
        let mut parser = StreamingParser::with_config(reader, config);

        let mut total_triples = 0;
        while let Some(batch) = parser.next_batch().expect("parsing should succeed") {
            total_triples += batch.len();
        }

        assert_eq!(total_triples, 1000);
    }

    #[test]
    fn test_lenient_mode() {
        let turtle = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" .
            invalid syntax here
            ex:bob ex:name "Bob" .
        "#;

        let reader = Cursor::new(turtle);
        let config = StreamingConfig::default().lenient(true);
        let parser = StreamingParser::with_config(reader, config);

        // Should not panic in lenient mode
        let _triples: Vec<_> = parser.triples().collect();
    }

    #[test]
    fn test_progress_tracking() {
        let turtle = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:name "Alice" .
            ex:bob ex:name "Bob" .
        "#;

        let reader = Cursor::new(turtle);
        let mut parser = StreamingParser::new(reader);

        let _ = parser.next_batch();

        assert!(parser.triples_parsed() > 0);
        assert!(parser.bytes_read() > 0);
    }
}