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
//! Async streaming support for RDF parsing and serialization
//!
//! This module provides async/await compatible streaming interfaces for RDF data processing,
//! with support for backpressure, progress reporting, and cancellation.
//!
//! # Examples
//!
//! ## Async Parsing with Progress Reporting
//!
//! ```no_run
//! use oxirs_core::io::{AsyncRdfParser, AsyncStreamingParser, AsyncStreamingConfig};
//! use oxirs_core::parser::RdfFormat;
//! use tokio::fs::File;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let file = File::open("data.nt").await?;
//!     let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
//!     
//!     let config = AsyncStreamingConfig {
//!         chunk_size: 65536,  // 64KB chunks
//!         ..Default::default()
//!     };
//!     
//!     let progress = Box::new(|p: &oxirs_core::io::StreamingProgress| {
//!         println!("Parsed {} quads ({} bytes)", p.items_processed, p.bytes_processed);
//!     });
//!     
//!     let quads = parser.parse_async(file, config, Some(progress), None).await?;
//!     println!("Total: {} quads", quads.len());
//!     Ok(())
//! }
//! ```
//!
//! ## Async Serialization with Cancellation
//!
//! ```no_run
//! use oxirs_core::io::{AsyncRdfSerializer, AsyncStreamingSerializer, AsyncStreamingConfig};
//! use oxirs_core::parser::RdfFormat;
//! use oxirs_core::model::*;
//! use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let serializer = AsyncStreamingSerializer::new(RdfFormat::NTriples);
//!     let cancel_token = Arc::new(AtomicBool::new(false));
//!     
//!     // Create some quads
//!     let quads = vec![
//!         // ... your quads here
//!     ];
//!     
//!     let mut output = Vec::new();
//!     serializer.serialize_quads_async(
//!         &mut output,
//!         quads.into_iter(),
//!         AsyncStreamingConfig::default(),
//!         None,
//!         Some(cancel_token.clone()),
//!     ).await?;
//!     
//!     Ok(())
//! }
//! ```

use crate::{
    model::{Quad, Triple},
    parser::{Parser, ParserConfig, RdfFormat},
    serializer::Serializer,
    OxirsError, Result,
};
use futures::future::BoxFuture;
use std::pin::Pin;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

/// Progress information for async operations
#[derive(Debug, Clone)]
pub struct StreamingProgress {
    /// Total bytes processed
    pub bytes_processed: usize,
    /// Total items (quads/triples) processed
    pub items_processed: usize,
    /// Errors encountered (if error tolerance is enabled)
    pub errors_encountered: usize,
    /// Estimated total bytes (if known)
    pub total_bytes: Option<usize>,
    /// Processing rate (items per second)
    pub items_per_second: Option<f64>,
}

impl StreamingProgress {
    /// Create new progress info
    pub fn new() -> Self {
        StreamingProgress {
            bytes_processed: 0,
            items_processed: 0,
            errors_encountered: 0,
            total_bytes: None,
            items_per_second: None,
        }
    }

    /// Calculate completion percentage if total size is known
    pub fn completion_percentage(&self) -> Option<f64> {
        self.total_bytes.map(|total| {
            if total == 0 {
                100.0
            } else {
                (self.bytes_processed as f64 / total as f64) * 100.0
            }
        })
    }
}

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

/// Callback for progress reporting
pub type ProgressCallback = Box<dyn Fn(&StreamingProgress) + Send + Sync>;

/// Configuration for async streaming operations
#[derive(Clone)]
pub struct AsyncStreamingConfig {
    /// Size of chunks to read/write at a time
    pub chunk_size: usize,
    /// Size of the internal buffer
    pub buffer_size: usize,
    /// Whether to continue on parse errors
    pub ignore_errors: bool,
    /// Maximum number of errors before stopping
    pub max_errors: Option<usize>,
    /// Base IRI for resolving relative IRIs
    pub base_iri: Option<String>,
}

impl Default for AsyncStreamingConfig {
    fn default() -> Self {
        AsyncStreamingConfig {
            chunk_size: 8192,   // 8KB chunks
            buffer_size: 65536, // 64KB buffer
            ignore_errors: false,
            max_errors: None,
            base_iri: None,
        }
    }
}

/// Trait for async RDF parsing with streaming input
pub trait AsyncRdfParser: Send + Sync {
    /// Parse RDF data from an async reader
    fn parse_async<'a, R>(
        &'a self,
        reader: R,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<Vec<Quad>>>
    where
        R: AsyncRead + Unpin + Send + 'a;

    /// Parse RDF data from an async reader with a custom handler
    fn parse_with_handler_async<'a, R, F, Fut>(
        &'a self,
        reader: R,
        handler: F,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        R: AsyncRead + Unpin + Send + 'a,
        F: FnMut(Quad) -> Fut + Send + 'a,
        Fut: std::future::Future<Output = Result<()>> + Send + 'a;
}

/// Trait for async RDF serialization with streaming output
pub trait AsyncRdfSerializer: Send + Sync {
    /// Serialize quads to an async writer
    fn serialize_quads_async<'a, W, I>(
        &'a self,
        writer: W,
        quads: I,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        W: AsyncWrite + Unpin + Send + 'a,
        I: Iterator<Item = Quad> + Send + 'a;

    /// Serialize triples to an async writer
    fn serialize_triples_async<'a, W, I>(
        &'a self,
        writer: W,
        triples: I,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        W: AsyncWrite + Unpin + Send + 'a,
        I: Iterator<Item = Triple> + Send + 'a;
}

/// Async streaming parser implementation
pub struct AsyncStreamingParser {
    format: RdfFormat,
}

impl AsyncStreamingParser {
    /// Create a new async streaming parser
    pub fn new(format: RdfFormat) -> Self {
        AsyncStreamingParser { format }
    }

    /// Check if cancellation was requested
    fn check_cancelled(cancel_token: &Option<Arc<AtomicBool>>) -> Result<()> {
        if let Some(token) = cancel_token {
            if token.load(Ordering::Relaxed) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Interrupted,
                    "Operation cancelled",
                )
                .into());
            }
        }
        Ok(())
    }

    /// Report progress
    fn report_progress(
        progress_callback: &Option<ProgressCallback>,
        progress_info: &StreamingProgress,
    ) {
        if let Some(callback) = progress_callback {
            callback(progress_info);
        }
    }

    /// Parse line-based formats (N-Triples, N-Quads)
    async fn parse_line_based<R, F, Fut>(
        &self,
        mut reader: R,
        mut handler: F,
        config: AsyncStreamingConfig,
        progress_callback: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> Result<()>
    where
        R: AsyncRead + Unpin + Send,
        F: FnMut(Quad) -> Fut + Send,
        Fut: std::future::Future<Output = Result<()>> + Send,
    {
        let parser_config = ParserConfig {
            base_iri: config.base_iri.clone(),
            ignore_errors: config.ignore_errors,
            max_errors: config.max_errors,
        };
        let parser = Parser::with_config(self.format, parser_config);

        let mut buffer = vec![0u8; config.chunk_size];
        let mut accumulated = Vec::new();
        let mut line_buffer = String::new();
        let mut progress = StreamingProgress::new();
        let start_time = std::time::Instant::now();

        loop {
            Self::check_cancelled(&cancel_token)?;

            let bytes_read = reader.read(&mut buffer).await?;
            if bytes_read == 0 {
                break; // EOF
            }

            progress.bytes_processed += bytes_read;
            accumulated.extend_from_slice(&buffer[..bytes_read]);

            // Process complete lines
            while let Some(newline_pos) = accumulated.iter().position(|&b| b == b'\n') {
                let line_bytes = accumulated.drain(..=newline_pos).collect::<Vec<_>>();

                // Convert to string, handling UTF-8 errors gracefully
                match String::from_utf8(line_bytes) {
                    Ok(mut line) => {
                        // Remove newline
                        if line.ends_with('\n') {
                            line.pop();
                            if line.ends_with('\r') {
                                line.pop();
                            }
                        }

                        line_buffer.push_str(&line);

                        // Try to parse the line
                        match self.parse_single_line(&parser, &line_buffer) {
                            Ok(Some(quad)) => {
                                handler(quad).await?;
                                progress.items_processed += 1;
                                line_buffer.clear();
                            }
                            Ok(None) => {
                                // Empty line or comment
                                line_buffer.clear();
                            }
                            Err(e) => {
                                if config.ignore_errors {
                                    progress.errors_encountered += 1;
                                    if let Some(max_errors) = config.max_errors {
                                        if progress.errors_encountered >= max_errors {
                                            return Err(e);
                                        }
                                    }
                                    tracing::warn!("Parse error: {}", e);
                                    line_buffer.clear();
                                } else {
                                    return Err(e);
                                }
                            }
                        }
                    }
                    Err(e) => {
                        if config.ignore_errors {
                            progress.errors_encountered += 1;
                            tracing::warn!("UTF-8 error: {}", e);
                        } else {
                            return Err(OxirsError::Parse(format!("UTF-8 error: {e}")));
                        }
                    }
                }
            }

            // Calculate processing rate
            let elapsed = start_time.elapsed().as_secs_f64();
            if elapsed > 0.0 {
                progress.items_per_second = Some(progress.items_processed as f64 / elapsed);
            }

            Self::report_progress(&progress_callback, &progress);
        }

        // Process any remaining data
        if !accumulated.is_empty() {
            match String::from_utf8(accumulated) {
                Ok(remaining) => {
                    line_buffer.push_str(&remaining);
                    if !line_buffer.trim().is_empty() {
                        if let Ok(Some(quad)) = self.parse_single_line(&parser, &line_buffer) {
                            handler(quad).await?;
                            progress.items_processed += 1;
                        }
                    }
                }
                Err(e) => {
                    if !config.ignore_errors {
                        return Err(OxirsError::Parse(format!("UTF-8 error: {e}")));
                    }
                }
            }
        }

        Self::report_progress(&progress_callback, &progress);
        Ok(())
    }

    /// Parse a single line for line-based formats
    fn parse_single_line(&self, parser: &Parser, line: &str) -> Result<Option<Quad>> {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            return Ok(None);
        }

        match self.format {
            RdfFormat::NTriples => parser.parse_ntriples_line(line),
            RdfFormat::NQuads => parser.parse_nquads_line(line),
            _ => Err(OxirsError::Parse(
                "Format not supported for line-based parsing".to_string(),
            )),
        }
    }

    /// Parse document-based formats (Turtle, TriG, RDF/XML, JSON-LD)
    async fn parse_document_based<R, F, Fut>(
        &self,
        mut reader: R,
        mut handler: F,
        config: AsyncStreamingConfig,
        progress_callback: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> Result<()>
    where
        R: AsyncRead + Unpin + Send,
        F: FnMut(Quad) -> Fut + Send,
        Fut: std::future::Future<Output = Result<()>> + Send,
    {
        // For document-based formats, we need to read the entire document
        let mut buffer = Vec::new();
        let mut chunk = vec![0u8; config.chunk_size];
        let mut progress = StreamingProgress::new();

        loop {
            Self::check_cancelled(&cancel_token)?;

            let bytes_read = reader.read(&mut chunk).await?;
            if bytes_read == 0 {
                break;
            }

            buffer.extend_from_slice(&chunk[..bytes_read]);
            progress.bytes_processed += bytes_read;

            Self::report_progress(&progress_callback, &progress);
        }

        // Parse the complete document
        let document = String::from_utf8(buffer)
            .map_err(|e| OxirsError::Parse(format!("UTF-8 error: {e}")))?;

        let parser_config = ParserConfig {
            base_iri: config.base_iri,
            ignore_errors: config.ignore_errors,
            max_errors: config.max_errors,
        };
        let parser = Parser::with_config(self.format, parser_config);

        // Parse the document and collect quads
        let quads = parser.parse_str_to_quads(&document)?;

        // Process each quad with the async handler
        for quad in quads {
            Self::check_cancelled(&cancel_token)?;
            handler(quad).await?;
            progress.items_processed += 1;
        }

        Self::report_progress(&progress_callback, &progress);
        Ok(())
    }
}

impl AsyncRdfParser for AsyncStreamingParser {
    fn parse_async<'a, R>(
        &'a self,
        reader: R,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<Vec<Quad>>>
    where
        R: AsyncRead + Unpin + Send + 'a,
    {
        Box::pin(async move {
            let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();

            // Spawn a task to collect quads
            let collector = tokio::spawn(async move {
                let mut quads = Vec::new();
                while let Some(quad) = rx.recv().await {
                    quads.push(quad);
                }
                quads
            });

            // Parse with handler that sends quads to the channel
            let parse_result = self
                .parse_with_handler_async(
                    reader,
                    |quad| {
                        let tx = tx.clone();
                        async move {
                            tx.send(quad)
                                .map_err(|_| OxirsError::Parse("Channel send error".to_string()))?;
                            Ok(())
                        }
                    },
                    config,
                    progress,
                    cancel_token,
                )
                .await;

            // Close the channel
            drop(tx);

            // Check for parse errors
            parse_result?;

            // Collect the quads
            let quads = collector
                .await
                .map_err(|_| OxirsError::Parse("Failed to collect quads".to_string()))?;
            Ok(quads)
        })
    }

    fn parse_with_handler_async<'a, R, F, Fut>(
        &'a self,
        reader: R,
        handler: F,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        R: AsyncRead + Unpin + Send + 'a,
        F: FnMut(Quad) -> Fut + Send + 'a,
        Fut: std::future::Future<Output = Result<()>> + Send + 'a,
    {
        Box::pin(async move {
            match self.format {
                RdfFormat::NTriples | RdfFormat::NQuads => {
                    self.parse_line_based(reader, handler, config, progress, cancel_token)
                        .await
                }
                _ => {
                    self.parse_document_based(reader, handler, config, progress, cancel_token)
                        .await
                }
            }
        })
    }
}

/// Async streaming serializer implementation
pub struct AsyncStreamingSerializer {
    format: RdfFormat,
}

impl AsyncStreamingSerializer {
    /// Create a new async streaming serializer
    pub fn new(format: RdfFormat) -> Self {
        AsyncStreamingSerializer { format }
    }

    /// Check if cancellation was requested
    fn check_cancelled(cancel_token: &Option<Arc<AtomicBool>>) -> Result<()> {
        if let Some(token) = cancel_token {
            if token.load(Ordering::Relaxed) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Interrupted,
                    "Operation cancelled",
                )
                .into());
            }
        }
        Ok(())
    }

    /// Serialize a single quad to a string
    fn serialize_quad(&self, quad: &Quad) -> Result<String> {
        let serializer = Serializer::new(self.format);
        match self.format {
            RdfFormat::NTriples => {
                // For N-Triples, we only serialize if it's in the default graph
                if quad.is_default_graph() {
                    let mut graph = crate::model::Graph::new();
                    graph.insert(quad.to_triple());
                    serializer.serialize_graph(&graph)
                } else {
                    Ok(String::new())
                }
            }
            RdfFormat::NQuads => serializer.serialize_quad_to_nquads(quad),
            _ => Err(OxirsError::Serialize(
                "Format not supported for streaming serialization".to_string(),
            )),
        }
    }

    /// Serialize a single triple to a string
    #[allow(dead_code)]
    fn serialize_triple(&self, triple: &Triple) -> Result<String> {
        let quad = Quad::from_triple(triple.clone());
        self.serialize_quad(&quad)
    }
}

impl AsyncRdfSerializer for AsyncStreamingSerializer {
    fn serialize_quads_async<'a, W, I>(
        &'a self,
        mut writer: W,
        quads: I,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        W: AsyncWrite + Unpin + Send + 'a,
        I: Iterator<Item = Quad> + Send + 'a,
    {
        Box::pin(async move {
            let mut buffer = String::with_capacity(config.buffer_size);
            let mut progress_info = StreamingProgress::new();
            let start_time = std::time::Instant::now();

            for quad in quads {
                Self::check_cancelled(&cancel_token)?;

                // Serialize the quad
                let serialized = self.serialize_quad(&quad)?;
                buffer.push_str(&serialized);
                progress_info.items_processed += 1;

                // Write buffer if it's getting full
                if buffer.len() >= config.chunk_size {
                    writer.write_all(buffer.as_bytes()).await?;
                    progress_info.bytes_processed += buffer.len();
                    buffer.clear();
                }

                // Update progress
                let elapsed = start_time.elapsed().as_secs_f64();
                if elapsed > 0.0 {
                    progress_info.items_per_second =
                        Some(progress_info.items_processed as f64 / elapsed);
                }

                if let Some(ref callback) = progress {
                    callback(&progress_info);
                }
            }

            // Write any remaining data
            if !buffer.is_empty() {
                writer.write_all(buffer.as_bytes()).await?;
                progress_info.bytes_processed += buffer.len();

                // Report final progress
                if let Some(ref callback) = progress {
                    callback(&progress_info);
                }
            }

            // Flush the writer
            writer.flush().await?;

            Ok(())
        })
    }

    fn serialize_triples_async<'a, W, I>(
        &'a self,
        writer: W,
        triples: I,
        config: AsyncStreamingConfig,
        progress: Option<ProgressCallback>,
        cancel_token: Option<Arc<AtomicBool>>,
    ) -> BoxFuture<'a, Result<()>>
    where
        W: AsyncWrite + Unpin + Send + 'a,
        I: Iterator<Item = Triple> + Send + 'a,
    {
        let quads = triples.map(Quad::from_triple);
        self.serialize_quads_async(writer, quads, config, progress, cancel_token)
    }
}

/// Buffered async reader with backpressure support
pub struct BackpressureReader<R> {
    inner: R,
    buffer: Vec<u8>,
    capacity: usize,
    read_pos: usize,
    write_pos: usize,
}

impl<R: AsyncRead + Unpin> BackpressureReader<R> {
    /// Create a new backpressure reader with specified buffer capacity
    pub fn new(inner: R, capacity: usize) -> Self {
        BackpressureReader {
            inner,
            buffer: vec![0; capacity],
            capacity,
            read_pos: 0,
            write_pos: 0,
        }
    }

    /// Get the number of bytes available in the buffer
    pub fn available(&self) -> usize {
        self.write_pos - self.read_pos
    }

    /// Check if the buffer is full
    pub fn is_full(&self) -> bool {
        self.available() == self.capacity
    }
}

impl<R: AsyncRead + Unpin> AsyncRead for BackpressureReader<R> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        let me = self.get_mut();

        // If we have data in the buffer, return it
        if me.available() > 0 {
            let to_read = std::cmp::min(buf.remaining(), me.available());
            buf.put_slice(&me.buffer[me.read_pos..me.read_pos + to_read]);
            me.read_pos += to_read;

            // Reset positions if buffer is empty
            if me.read_pos == me.write_pos {
                me.read_pos = 0;
                me.write_pos = 0;
            }

            return Poll::Ready(Ok(()));
        }

        // Otherwise, try to fill the buffer
        let write_pos = me.write_pos;
        let mut read_buf = tokio::io::ReadBuf::new(&mut me.buffer[write_pos..]);
        match Pin::new(&mut me.inner).poll_read(cx, &mut read_buf) {
            Poll::Ready(Ok(())) => {
                let bytes_read = read_buf.filled().len();
                if bytes_read == 0 {
                    // EOF
                    return Poll::Ready(Ok(()));
                }

                me.write_pos += bytes_read;

                // Now serve from buffer
                let to_read = std::cmp::min(buf.remaining(), me.available());
                buf.put_slice(&me.buffer[me.read_pos..me.read_pos + to_read]);
                me.read_pos += to_read;

                Poll::Ready(Ok(()))
            }
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Pending => Poll::Pending,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::*;
    use std::sync::atomic::AtomicUsize;

    #[tokio::test]
    async fn test_async_ntriples_parsing() {
        let ntriples_data = r#"<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
"#;

        let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
        let reader = std::io::Cursor::new(ntriples_data.as_bytes());

        let quads = parser
            .parse_async(reader, AsyncStreamingConfig::default(), None, None)
            .await
            .expect("operation should succeed");

        assert_eq!(quads.len(), 2);
        assert!(quads[0].is_default_graph());
        assert!(quads[1].is_default_graph());
    }

    #[tokio::test]
    async fn test_async_parsing_with_progress() {
        let ntriples_data = r#"<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
<http://example.org/charlie> <http://xmlns.com/foaf/0.1/name> "Charlie" .
"#;

        let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
        let reader = std::io::Cursor::new(ntriples_data.as_bytes());

        let progress_count = Arc::new(AtomicUsize::new(0));
        let progress_count_clone = progress_count.clone();

        let progress_callback = Box::new(move |progress: &StreamingProgress| {
            progress_count_clone.fetch_add(1, Ordering::Relaxed);
            assert!(progress.bytes_processed > 0);
            assert!(progress.items_processed <= 3);
        });

        let quads = parser
            .parse_async(
                reader,
                AsyncStreamingConfig::default(),
                Some(progress_callback),
                None,
            )
            .await
            .expect("operation should succeed");

        assert_eq!(quads.len(), 3);
        assert!(progress_count.load(Ordering::Relaxed) > 0);
    }

    #[tokio::test]
    async fn test_async_parsing_with_cancellation() {
        let ntriples_data = r#"<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
<http://example.org/charlie> <http://xmlns.com/foaf/0.1/name> "Charlie" .
"#;

        let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
        let reader = std::io::Cursor::new(ntriples_data.as_bytes());

        let cancel_token = Arc::new(AtomicBool::new(false));
        let cancel_token_clone = cancel_token.clone();

        // Set up handler that cancels after first quad
        let count = Arc::new(AtomicUsize::new(0));
        let count_clone = count.clone();
        let result = parser
            .parse_with_handler_async(
                reader,
                |_quad| {
                    let token = cancel_token_clone.clone();
                    let count = count_clone.clone();
                    async move {
                        let current = count.fetch_add(1, Ordering::Relaxed);
                        if current == 0 {
                            token.store(true, Ordering::Relaxed);
                        }
                        Ok(())
                    }
                },
                AsyncStreamingConfig::default(),
                None,
                Some(cancel_token),
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("cancelled"));
    }

    #[tokio::test]
    async fn test_async_ntriples_serialization() {
        let mut quads = Vec::new();

        let alice = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let name_pred = NamedNode::new("http://xmlns.com/foaf/0.1/name").expect("valid IRI");
        let alice_name = Literal::new("Alice");
        let triple1 = Triple::new(alice.clone(), name_pred.clone(), alice_name);
        quads.push(Quad::from_triple(triple1));

        let bob = NamedNode::new("http://example.org/bob").expect("valid IRI");
        let bob_name = Literal::new("Bob");
        let triple2 = Triple::new(bob, name_pred, bob_name);
        quads.push(Quad::from_triple(triple2));

        let serializer = AsyncStreamingSerializer::new(RdfFormat::NTriples);
        let mut output = Vec::new();

        serializer
            .serialize_quads_async(
                &mut output,
                quads.into_iter(),
                AsyncStreamingConfig::default(),
                None,
                None,
            )
            .await
            .expect("operation should succeed");

        let result = String::from_utf8(output).expect("bytes should be valid UTF-8");
        assert!(result.contains("http://example.org/alice"));
        assert!(result.contains("http://example.org/bob"));
        assert!(result.contains("\"Alice\""));
        assert!(result.contains("\"Bob\""));
    }

    #[tokio::test]
    async fn test_async_serialization_with_progress() {
        let mut triples = Vec::new();

        for i in 0..10 {
            let subject = NamedNode::new(format!("http://example.org/item{i}"))
                .expect("valid IRI from format");
            let pred = NamedNode::new("http://example.org/value").expect("valid IRI");
            let obj = Literal::new(i.to_string());
            triples.push(Triple::new(subject, pred, obj));
        }

        let serializer = AsyncStreamingSerializer::new(RdfFormat::NTriples);
        let mut output = Vec::new();

        let progress_count = Arc::new(AtomicUsize::new(0));
        let progress_count_clone = progress_count.clone();
        let last_bytes = Arc::new(AtomicUsize::new(0));
        let last_bytes_clone = last_bytes.clone();

        let progress_callback = Box::new(move |progress: &StreamingProgress| {
            progress_count_clone.fetch_add(1, Ordering::Relaxed);
            assert!(progress.items_processed <= 10);
            // bytes should be monotonically increasing or stay the same
            let prev_bytes = last_bytes_clone.load(Ordering::Relaxed);
            assert!(progress.bytes_processed >= prev_bytes);
            last_bytes_clone.store(progress.bytes_processed, Ordering::Relaxed);
        });

        serializer
            .serialize_triples_async(
                &mut output,
                triples.into_iter(),
                AsyncStreamingConfig::default(),
                Some(progress_callback),
                None,
            )
            .await
            .expect("operation should succeed");

        assert!(progress_count.load(Ordering::Relaxed) > 0);
    }

    #[tokio::test]
    async fn test_async_error_tolerance() {
        let invalid_ntriples = r#"<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
INVALID LINE HERE
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
"#;

        let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
        let reader = std::io::Cursor::new(invalid_ntriples.as_bytes());

        let config = AsyncStreamingConfig {
            ignore_errors: true,
            ..Default::default()
        };

        let quads = parser
            .parse_async(reader, config, None, None)
            .await
            .expect("operation should succeed");

        // Should parse the two valid lines
        assert_eq!(quads.len(), 2);
    }

    #[tokio::test]
    async fn test_backpressure_reader() {
        let data = b"Hello, World!";
        let cursor = std::io::Cursor::new(data);
        let mut reader = BackpressureReader::new(cursor, 16); // Buffer size larger than data

        let mut output = Vec::new();
        reader
            .read_to_end(&mut output)
            .await
            .expect("async operation should succeed");

        assert_eq!(output, data);
    }

    #[tokio::test]
    async fn test_async_nquads_parsing() {
        let nquads_data = r#"<http://example.org/alice> <http://xmlns.com/foaf/0.1/name> "Alice" <http://example.org/graph1> .
<http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" <http://example.org/graph2> .
"#;

        let parser = AsyncStreamingParser::new(RdfFormat::NQuads);
        let reader = std::io::Cursor::new(nquads_data.as_bytes());

        let quads = parser
            .parse_async(reader, AsyncStreamingConfig::default(), None, None)
            .await
            .expect("operation should succeed");

        assert_eq!(quads.len(), 2);
        assert!(!quads[0].is_default_graph());
        assert!(!quads[1].is_default_graph());
    }

    #[tokio::test]
    async fn test_large_chunk_parsing() {
        // Create a large dataset
        let mut ntriples_data = String::new();
        for i in 0..1000 {
            ntriples_data.push_str(&format!(
                "<http://example.org/item{}> <http://example.org/value> \"{}\" .\n",
                i, i
            ));
        }

        let parser = AsyncStreamingParser::new(RdfFormat::NTriples);
        let reader = std::io::Cursor::new(ntriples_data.as_bytes());

        let config = AsyncStreamingConfig {
            chunk_size: 1024, // Small chunks to test buffering
            ..Default::default()
        };

        let quads = parser
            .parse_async(reader, config, None, None)
            .await
            .expect("operation should succeed");

        assert_eq!(quads.len(), 1000);
    }

    #[tokio::test]
    async fn test_custom_base_iri() {
        let turtle_data = r#"@prefix ex: <http://example.org/> .
ex:alice ex:knows ex:bob ."#;

        let parser = AsyncStreamingParser::new(RdfFormat::Turtle);
        let reader = std::io::Cursor::new(turtle_data.as_bytes());

        let config = AsyncStreamingConfig {
            base_iri: Some("http://example.org/".to_string()),
            ..Default::default()
        };

        let quads = parser
            .parse_async(reader, config, None, None)
            .await
            .expect("operation should succeed");

        assert_eq!(quads.len(), 1);
        let triple = quads[0].to_triple();

        // Should contain the example.org namespace
        if let Subject::NamedNode(subj) = triple.subject() {
            assert!(subj.as_str().contains("example.org"));
        }
    }
}