oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
//! Core RDF-star serialization methods

use super::super::config::*;
use super::super::parallel::ParallelSerializer;
use super::super::streaming::StreamingSerializer;
use super::StarSerializer;
use crate::model::{StarGraph, StarQuad, StarTerm, StarTriple};
use crate::parser::StarFormat;
use crate::{StarConfig, StarError, StarResult};
use flate2::write::GzEncoder;
use flate2::Compression;
use oxiarc_zstd::ZstdStreamEncoder as ZstdEncoder;
use std::io::{BufWriter, Write};
use tracing::{debug, span, Level};

/// A buffering writer that accumulates data and compresses it with LZ4
/// via oxiarc_lz4 when flushed or dropped.
struct Lz4BufferWriter<W: Write> {
    inner: Option<W>,
    buffer: Vec<u8>,
}

impl<W: Write> Lz4BufferWriter<W> {
    fn new(writer: W) -> Self {
        Self {
            inner: Some(writer),
            buffer: Vec::new(),
        }
    }

    fn flush_compressed(&mut self) -> std::io::Result<()> {
        if self.buffer.is_empty() {
            return Ok(());
        }
        if let Some(ref mut writer) = self.inner {
            let compressed = oxiarc_lz4::compress(&self.buffer)
                .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
            writer.write_all(&compressed)?;
            self.buffer.clear();
        }
        Ok(())
    }
}

impl<W: Write> Write for Lz4BufferWriter<W> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.buffer.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.flush_compressed()
    }
}

impl<W: Write> Drop for Lz4BufferWriter<W> {
    fn drop(&mut self) {
        let _ = self.flush_compressed();
    }
}

impl StarSerializer {
    /// Create a new serializer with default configuration
    pub fn new() -> Self {
        Self {
            config: StarConfig::default(),
            simd_escaper: super::super::simd_escape::SimdEscaper::new(),
        }
    }

    /// Create a new serializer with custom configuration
    pub fn with_config(config: StarConfig) -> Self {
        Self {
            config,
            simd_escaper: super::super::simd_escape::SimdEscaper::new(),
        }
    }

    /// Serialize a StarGraph to a writer in the specified format
    pub fn serialize<W: Write>(
        &self,
        graph: &StarGraph,
        writer: W,
        format: StarFormat,
    ) -> StarResult<()> {
        let span = span!(Level::INFO, "serialize_rdf_star", format = ?format);
        let _enter = span.enter();

        match format {
            StarFormat::TurtleStar => self.serialize_turtle_star(graph, writer),
            StarFormat::NTriplesStar => self.serialize_ntriples_star(graph, writer),
            StarFormat::TrigStar => self.serialize_trig_star(graph, writer),
            StarFormat::NQuadsStar => self.serialize_nquads_star(graph, writer),
            StarFormat::JsonLdStar => self.serialize_jsonld_star(graph, writer),
        }
    }

    /// Serialize to string in the specified format
    pub fn serialize_to_string(&self, graph: &StarGraph, format: StarFormat) -> StarResult<String> {
        let mut buffer = Vec::new();
        self.serialize(graph, &mut buffer, format)?;
        String::from_utf8(buffer).map_err(|e| StarError::serialization_error(e.to_string()))
    }

    /// Serialize with advanced options (streaming, compression, parallel processing)
    pub fn serialize_with_options<W: Write + Send + 'static>(
        &self,
        graph: &StarGraph,
        writer: W,
        format: StarFormat,
        options: &SerializationOptions,
    ) -> StarResult<()> {
        let span = span!(Level::INFO, "serialize_with_options", format = ?format, streaming = options.streaming, parallel = options.parallel);
        let _enter = span.enter();

        // Choose serialization strategy based on options and graph size
        let triple_count = graph.total_len();

        if options.parallel && triple_count > options.batch_size {
            debug!("Using parallel serialization for {} triples", triple_count);
            let parallel_serializer =
                ParallelSerializer::new(options.max_threads, options.batch_size);
            parallel_serializer.serialize_parallel(graph, writer, format, options)
        } else if options.streaming && triple_count > 50000 {
            debug!("Using streaming serialization for {} triples", triple_count);
            let streaming_config = StreamingConfig {
                chunk_size: options.batch_size,
                buffer_capacity: options.buffer_size,
                enable_buffering: true,
                memory_threshold: options.buffer_size * 64, // 64x buffer size
                compress_chunks: options.compression != CompressionType::None,
            };
            let mut streaming_serializer = StreamingSerializer::new(writer, streaming_config);
            streaming_serializer
                .serialize_triples_streaming(graph.triples().iter().cloned(), format)
        } else {
            debug!("Using standard serialization for {} triples", triple_count);
            // Apply compression wrapper if requested
            if options.compression != CompressionType::None {
                let compressed_writer =
                    self.create_compressed_writer(writer, options.compression)?;
                self.serialize(graph, compressed_writer, format)
            } else {
                self.serialize(graph, writer, format)
            }
        }
    }

    /// Create a compressed writer based on compression type
    fn create_compressed_writer<W: Write + 'static>(
        &self,
        writer: W,
        compression: CompressionType,
    ) -> StarResult<Box<dyn Write>> {
        match compression {
            CompressionType::None => Ok(Box::new(writer)),
            CompressionType::Gzip => {
                debug!("Creating Gzip compressed writer");
                let encoder = GzEncoder::new(writer, Compression::default());
                Ok(Box::new(encoder))
            }
            CompressionType::Zstd => {
                debug!("Creating Zstd compressed writer");
                let encoder = ZstdEncoder::new(writer, 3);
                Ok(Box::new(encoder))
            }
            CompressionType::Lz4 => {
                debug!("Creating LZ4 compressed writer");
                // oxiarc_lz4 is buffer-based; wrap in a buffering writer that compresses on drop
                Ok(Box::new(Lz4BufferWriter::new(writer)))
            }
        }
    }

    /// Serialize large dataset using streaming approach
    pub fn serialize_streaming<W: Write>(
        &self,
        graph: &StarGraph,
        writer: W,
        format: StarFormat,
        chunk_size: usize,
    ) -> StarResult<()> {
        let span =
            span!(Level::DEBUG, "serialize_streaming", format = ?format, chunk_size = chunk_size);
        let _enter = span.enter();

        let config = StreamingConfig {
            chunk_size,
            ..Default::default()
        };
        let mut streaming_serializer = StreamingSerializer::new(writer, config);
        streaming_serializer
            .serialize_triples_streaming(graph.triples().iter().cloned(), format)?;

        debug!(
            "Streamed {} triples in format {:?}",
            graph.total_len(),
            format
        );
        Ok(())
    }

    /// Serialize using parallel processing for large graphs
    pub fn serialize_parallel<W: Write + Send + 'static>(
        &self,
        graph: &StarGraph,
        writer: W,
        format: StarFormat,
        num_threads: usize,
        batch_size: usize,
    ) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_parallel", format = ?format, num_threads = num_threads, batch_size = batch_size);
        let _enter = span.enter();

        let parallel_serializer = ParallelSerializer::new(num_threads, batch_size);
        let options = SerializationOptions::default();
        parallel_serializer.serialize_parallel(graph, writer, format, &options)?;

        debug!(
            "Parallel serialization completed for {} triples using {} threads",
            graph.total_len(),
            num_threads
        );
        Ok(())
    }

    /// Auto-detect optimal serialization strategy based on graph characteristics
    pub fn serialize_optimized<W: Write + Send + 'static>(
        &self,
        graph: &StarGraph,
        writer: W,
        format: StarFormat,
    ) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_optimized", format = ?format);
        let _enter = span.enter();

        let triple_count = graph.total_len();
        let quoted_count = graph.count_quoted_triples();
        let complexity_score = quoted_count as f64 / triple_count.max(1) as f64;

        debug!(
            "Graph analysis: {} triples, {} quoted (complexity: {:.2})",
            triple_count, quoted_count, complexity_score
        );

        let mut options = SerializationOptions::default();

        // Configure based on graph characteristics
        if triple_count > 1_000_000 {
            // Very large graph - use streaming
            options.streaming = true;
            options.compression = CompressionType::Zstd; // High performance compression
            options.buffer_size = 4 * 1024 * 1024; // 4MB buffer
            debug!("Selected streaming strategy for very large graph");
        } else if triple_count > 100_000 && complexity_score < 0.1 {
            // Large simple graph - use parallel processing
            options.parallel = true;
            options.max_threads = std::cmp::min(8, 4); // Use 4 as default thread count
            options.batch_size = 25000;
            debug!("Selected parallel strategy for large simple graph");
        } else if complexity_score > 0.3 {
            // Complex graph with many quoted triples - use smaller batches
            options.batch_size = 5000;
            options.buffer_size = 512 * 1024; // 512KB buffer
            debug!("Selected conservative strategy for complex graph");
        }
        // else: use default strategy for smaller/simpler graphs

        self.serialize_with_options(graph, writer, format, &options)
    }

    /// Get memory usage estimation for serialization
    pub fn estimate_memory_usage(
        &self,
        graph: &StarGraph,
        format: StarFormat,
        options: &SerializationOptions,
    ) -> usize {
        let base_memory = self.estimate_size(graph, format);

        let memory_multiplier = if options.parallel {
            // Parallel processing uses more memory for batching
            2.5
        } else if options.streaming {
            // Streaming uses less memory
            0.5
        } else {
            1.0
        };

        let buffer_overhead = if options.streaming {
            options.buffer_size * 2 // Double buffering
        } else {
            options.batch_size * 100 // Rough estimate of batch memory
        };

        ((base_memory as f64 * memory_multiplier) as usize) + buffer_overhead
    }

    /// Serialize to Turtle-star format
    pub fn serialize_turtle_star<W: Write>(&self, graph: &StarGraph, writer: W) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_turtle_star");
        let _enter = span.enter();

        let mut buf_writer = BufWriter::new(writer);
        let mut context = SerializationContext::new();

        // Add common prefixes
        self.add_common_prefixes(&mut context);

        // Write prefixes
        self.write_turtle_prefixes(&mut buf_writer, &context)?;

        // Write triples
        for triple in graph.triples() {
            self.write_turtle_triple(&mut buf_writer, triple, &context)?;
        }

        buf_writer
            .flush()
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        debug!("Serialized {} triples in Turtle-star format", graph.len());
        Ok(())
    }

    /// Write Turtle-star prefixes
    fn write_turtle_prefixes<W: Write>(
        &self,
        writer: &mut W,
        context: &SerializationContext,
    ) -> StarResult<()> {
        for (prefix, namespace) in &context.prefixes {
            writeln!(writer, "@prefix {prefix}: <{namespace}> .")
                .map_err(|e| StarError::serialization_error(e.to_string()))?;
        }

        if !context.prefixes.is_empty() {
            writeln!(writer).map_err(|e| StarError::serialization_error(e.to_string()))?;
        }

        Ok(())
    }

    /// Write a single Turtle-star triple
    fn write_turtle_triple<W: Write>(
        &self,
        writer: &mut W,
        triple: &StarTriple,
        context: &SerializationContext,
    ) -> StarResult<()> {
        let subject_str = self.format_term(&triple.subject, context)?;
        let predicate_str = self.format_term(&triple.predicate, context)?;
        let object_str = self.format_term(&triple.object, context)?;

        if context.pretty_print {
            writeln!(
                writer,
                "{}{} {} {} .",
                context.current_indent(),
                subject_str,
                predicate_str,
                object_str
            )
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        } else {
            writeln!(writer, "{subject_str} {predicate_str} {object_str} .")
                .map_err(|e| StarError::serialization_error(e.to_string()))?;
        }

        Ok(())
    }

    /// Serialize to N-Triples-star format
    pub fn serialize_ntriples_star<W: Write>(
        &self,
        graph: &StarGraph,
        writer: W,
    ) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_ntriples_star");
        let _enter = span.enter();

        let mut buf_writer = BufWriter::new(writer);
        let context = SerializationContext::new(); // N-Triples doesn't use prefixes

        for triple in graph.triples() {
            self.write_ntriples_triple(&mut buf_writer, triple, &context)?;
        }

        buf_writer
            .flush()
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        debug!(
            "Serialized {} triples in N-Triples-star format",
            graph.len()
        );
        Ok(())
    }

    /// Write a single N-Triples-star triple
    fn write_ntriples_triple<W: Write>(
        &self,
        writer: &mut W,
        triple: &StarTriple,
        _context: &SerializationContext,
    ) -> StarResult<()> {
        let subject_str = Self::format_term_ntriples(&triple.subject)?;
        let predicate_str = Self::format_term_ntriples(&triple.predicate)?;
        let object_str = Self::format_term_ntriples(&triple.object)?;

        writeln!(writer, "{subject_str} {predicate_str} {object_str} .")
            .map_err(|e| StarError::serialization_error(e.to_string()))?;

        Ok(())
    }

    /// Serialize to TriG-star format (with named graphs)
    pub fn serialize_trig_star<W: Write>(&self, graph: &StarGraph, writer: W) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_trig_star");
        let _enter = span.enter();

        let mut buf_writer = BufWriter::new(writer);
        let mut context = SerializationContext::new();
        context.pretty_print = true;

        // Add common prefixes
        self.add_common_prefixes(&mut context);

        // Write prefixes first
        self.write_turtle_prefixes(&mut buf_writer, &context)?;

        // Serialize default graph if it has triples
        if !graph.triples().is_empty() {
            writeln!(buf_writer, "{{")
                .map_err(|e| StarError::serialization_error(e.to_string()))?;

            context.increase_indent();
            for triple in graph.triples() {
                self.write_turtle_triple(&mut buf_writer, triple, &context)?;
            }
            context.decrease_indent();

            writeln!(buf_writer, "}}")
                .map_err(|e| StarError::serialization_error(e.to_string()))?;
            writeln!(buf_writer).map_err(|e| StarError::serialization_error(e.to_string()))?;
        }

        // Serialize named graphs
        for graph_name in graph.named_graph_names() {
            if let Some(named_triples) = graph.named_graph_triples(graph_name) {
                if !named_triples.is_empty() {
                    // Write graph declaration
                    let graph_term = self.parse_graph_name(graph_name, &context)?;
                    writeln!(buf_writer, "{graph_term} {{")
                        .map_err(|e| StarError::serialization_error(e.to_string()))?;

                    context.increase_indent();
                    for triple in named_triples {
                        self.write_turtle_triple(&mut buf_writer, triple, &context)?;
                    }
                    context.decrease_indent();

                    writeln!(buf_writer, "}}")
                        .map_err(|e| StarError::serialization_error(e.to_string()))?;
                    writeln!(buf_writer)
                        .map_err(|e| StarError::serialization_error(e.to_string()))?;
                }
            }
        }

        buf_writer
            .flush()
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        debug!(
            "Serialized {} quads ({} total triples) in TriG-star format",
            graph.quad_len(),
            graph.total_len()
        );
        Ok(())
    }

    /// Serialize to N-Quads-star format
    pub fn serialize_nquads_star<W: Write>(&self, graph: &StarGraph, writer: W) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_nquads_star");
        let _enter = span.enter();

        let mut buf_writer = BufWriter::new(writer);
        let context = SerializationContext::new(); // N-Quads doesn't use prefixes

        // Serialize all quads from the graph (including both default and named graphs)
        for quad in graph.quads() {
            self.write_nquads_quad_complete(&mut buf_writer, quad, &context)?;
        }

        buf_writer
            .flush()
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        debug!(
            "Serialized {} quads ({} total triples) in N-Quads-star format",
            graph.quad_len(),
            graph.total_len()
        );
        Ok(())
    }

    /// Write a single N-Quads-star quad with proper graph context
    fn write_nquads_quad_complete<W: Write>(
        &self,
        writer: &mut W,
        quad: &StarQuad,
        _context: &SerializationContext,
    ) -> StarResult<()> {
        let subject_str = Self::format_term_ntriples(&quad.subject)?;
        let predicate_str = Self::format_term_ntriples(&quad.predicate)?;
        let object_str = Self::format_term_ntriples(&quad.object)?;

        if let Some(ref graph_term) = quad.graph {
            // Named graph quad
            let graph_str = Self::format_term_ntriples(graph_term)?;
            writeln!(
                writer,
                "{subject_str} {predicate_str} {object_str} {graph_str} ."
            )
            .map_err(|e| StarError::serialization_error(e.to_string()))?;
        } else {
            // Default graph quad (triple)
            writeln!(writer, "{subject_str} {predicate_str} {object_str} .")
                .map_err(|e| StarError::serialization_error(e.to_string()))?;
        }

        Ok(())
    }

    /// Write a single N-Quads-star quad (triple + optional graph) - legacy method
    #[allow(dead_code)]
    fn write_nquads_quad<W: Write>(
        &self,
        writer: &mut W,
        triple: &StarTriple,
        _context: &SerializationContext,
    ) -> StarResult<()> {
        let subject_str = Self::format_term_ntriples(&triple.subject)?;
        let predicate_str = Self::format_term_ntriples(&triple.predicate)?;
        let object_str = Self::format_term_ntriples(&triple.object)?;

        // Default graph (no graph component)
        writeln!(writer, "{subject_str} {predicate_str} {object_str} .")
            .map_err(|e| StarError::serialization_error(e.to_string()))?;

        Ok(())
    }

    /// Serialize to JSON-LD-star format
    pub fn serialize_jsonld_star<W: Write>(&self, graph: &StarGraph, writer: W) -> StarResult<()> {
        let span = span!(Level::DEBUG, "serialize_jsonld_star");
        let _enter = span.enter();

        let mut buf_writer = BufWriter::new(writer);
        let context = SerializationContext::new();

        // Create JSON-LD context
        let mut jsonld_document = serde_json::Map::new();

        // Add JSON-LD context
        let mut context_obj = serde_json::Map::new();
        context_obj.insert(
            "@vocab".to_string(),
            serde_json::Value::String("http://example.org/".to_string()),
        );

        // Add common prefixes
        context_obj.insert(
            "rdf".to_string(),
            serde_json::Value::String("http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string()),
        );
        context_obj.insert(
            "rdfs".to_string(),
            serde_json::Value::String("http://www.w3.org/2000/01/rdf-schema#".to_string()),
        );
        context_obj.insert(
            "xsd".to_string(),
            serde_json::Value::String("http://www.w3.org/2001/XMLSchema#".to_string()),
        );

        jsonld_document.insert(
            "@context".to_string(),
            serde_json::Value::Object(context_obj),
        );

        // Group quads by subject
        let mut subjects = std::collections::HashMap::new();

        // Process all quads
        for quad in graph.quads() {
            self.add_quad_to_jsonld(&mut subjects, quad)?;
        }

        // Convert subjects to JSON-LD array
        let mut graph_array = Vec::new();
        for (subject_str, properties) in subjects {
            let mut subject_obj = serde_json::Map::new();

            // Add @id for non-blank nodes
            if !subject_str.starts_with("_:") {
                subject_obj.insert("@id".to_string(), serde_json::Value::String(subject_str));
            }

            // Add properties
            for (predicate, values) in properties {
                subject_obj.insert(predicate, serde_json::Value::Array(values));
            }

            graph_array.push(serde_json::Value::Object(subject_obj));
        }

        jsonld_document.insert("@graph".to_string(), serde_json::Value::Array(graph_array));

        // Write JSON with pretty printing
        let json_output = if context.pretty_print {
            serde_json::to_string_pretty(&jsonld_document)
        } else {
            serde_json::to_string(&jsonld_document)
        }
        .map_err(|e| StarError::serialization_error(format!("JSON serialization error: {e}")))?;

        buf_writer
            .write_all(json_output.as_bytes())
            .map_err(|e| StarError::serialization_error(e.to_string()))?;

        buf_writer
            .flush()
            .map_err(|e| StarError::serialization_error(e.to_string()))?;

        debug!(
            "Serialized {} quads in JSON-LD-star format",
            graph.quad_len()
        );
        Ok(())
    }

    /// Add a quad to the JSON-LD structure
    fn add_quad_to_jsonld(
        &self,
        subjects: &mut std::collections::HashMap<
            String,
            std::collections::HashMap<String, Vec<serde_json::Value>>,
        >,
        quad: &StarQuad,
    ) -> StarResult<()> {
        let subject_str = self.term_to_jsonld_id(&quad.subject)?;
        let predicate_str = self.term_to_jsonld_predicate(&quad.predicate)?;
        let object_value = self.term_to_jsonld_value(&quad.object)?;

        let subject_props = subjects.entry(subject_str).or_default();
        let prop_values = subject_props.entry(predicate_str).or_default();
        prop_values.push(object_value);

        Ok(())
    }

    /// Convert a StarTerm to JSON-LD @id format
    pub fn term_to_jsonld_id(&self, term: &StarTerm) -> StarResult<String> {
        match term {
            StarTerm::NamedNode(node) => Ok(node.iri.clone()),
            StarTerm::BlankNode(node) => Ok(format!("_:{}", node.id)),
            StarTerm::QuotedTriple(triple) => {
                // For quoted triples as subjects, create a special identifier
                Ok(format!("_:qt_{}", self.hash_triple(triple)))
            }
            _ => Err(StarError::serialization_error(
                "Invalid subject term".to_string(),
            )),
        }
    }

    /// Convert a StarTerm to JSON-LD predicate format
    pub fn term_to_jsonld_predicate(&self, term: &StarTerm) -> StarResult<String> {
        match term {
            StarTerm::NamedNode(node) => Ok(node.iri.clone()),
            _ => Err(StarError::serialization_error(
                "Invalid predicate term".to_string(),
            )),
        }
    }

    /// Convert a StarTerm to JSON-LD value format
    #[allow(clippy::only_used_in_recursion)]
    pub fn term_to_jsonld_value(&self, term: &StarTerm) -> StarResult<serde_json::Value> {
        match term {
            StarTerm::NamedNode(node) => {
                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@id".to_string(),
                    serde_json::Value::String(node.iri.clone()),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::BlankNode(node) => {
                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@id".to_string(),
                    serde_json::Value::String(format!("_:{}", node.id)),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::Literal(literal) => {
                let mut obj = serde_json::Map::new();

                // Add the value
                if let Ok(num) = literal.value.parse::<f64>() {
                    // Numeric literal
                    obj.insert(
                        "@value".to_string(),
                        serde_json::Value::Number(
                            serde_json::Number::from_f64(num)
                                .unwrap_or(serde_json::Number::from(0)),
                        ),
                    );
                } else if literal.value == "true" || literal.value == "false" {
                    // Boolean literal
                    obj.insert(
                        "@value".to_string(),
                        serde_json::Value::Bool(literal.value == "true"),
                    );
                } else {
                    // String literal
                    obj.insert(
                        "@value".to_string(),
                        serde_json::Value::String(literal.value.clone()),
                    );
                }

                // Add datatype if present
                if let Some(ref datatype) = literal.datatype {
                    obj.insert(
                        "@type".to_string(),
                        serde_json::Value::String(datatype.iri.clone()),
                    );
                }

                // Add language if present
                if let Some(ref lang) = literal.language {
                    obj.insert(
                        "@language".to_string(),
                        serde_json::Value::String(lang.clone()),
                    );
                }

                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::QuotedTriple(triple) => {
                // RDF-star extension: represent quoted triple as annotation
                let mut annotation_obj = serde_json::Map::new();
                annotation_obj.insert(
                    "subject".to_string(),
                    self.term_to_jsonld_value(&triple.subject)?,
                );
                annotation_obj.insert(
                    "predicate".to_string(),
                    self.term_to_jsonld_value(&triple.predicate)?,
                );
                annotation_obj.insert(
                    "object".to_string(),
                    self.term_to_jsonld_value(&triple.object)?,
                );

                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@annotation".to_string(),
                    serde_json::Value::Object(annotation_obj),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::Variable(var) => {
                // Variables are typically used in SPARQL queries, not in serialized data
                // For JSON-LD, we'll represent them as special objects
                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@variable".to_string(),
                    serde_json::Value::String(var.name.clone()),
                );
                Ok(serde_json::Value::Object(obj))
            }
        }
    }

    /// Generate a hash for a triple (simple implementation)
    fn hash_triple(&self, triple: &StarTriple) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        format!("{triple:?}").hash(&mut hasher);
        hasher.finish()
    }

    /// Format a StarTerm for Turtle-star (with prefix compression)
    #[allow(clippy::only_used_in_recursion)]
    fn format_term(&self, term: &StarTerm, context: &SerializationContext) -> StarResult<String> {
        match term {
            StarTerm::NamedNode(node) => Ok(context.compress_iri(&node.iri)),
            StarTerm::BlankNode(node) => Ok(format!("_:{}", node.id)),
            StarTerm::Literal(literal) => {
                let mut result = format!("\"{}\"", Self::escape_literal_static(&literal.value));

                if let Some(ref lang) = literal.language {
                    result.push_str(&format!("@{lang}"));
                } else if let Some(ref datatype) = literal.datatype {
                    result.push_str(&format!("^^{}", context.compress_iri(&datatype.iri)));
                }

                Ok(result)
            }
            StarTerm::QuotedTriple(triple) => {
                let subject = self.format_term(&triple.subject, context)?;
                let predicate = self.format_term(&triple.predicate, context)?;
                let object = self.format_term(&triple.object, context)?;
                Ok(format!("<< {subject} {predicate} {object} >>"))
            }
            StarTerm::Variable(var) => Ok(format!("?{}", var.name)),
        }
    }

    /// Format a StarTerm for N-Triples-star (full IRIs, no prefixes)
    fn format_term_ntriples(term: &StarTerm) -> StarResult<String> {
        match term {
            StarTerm::NamedNode(node) => Ok(format!("<{}>", node.iri)),
            StarTerm::BlankNode(node) => Ok(format!("_:{}", node.id)),
            StarTerm::Literal(literal) => {
                let mut result = format!("\"{}\"", Self::escape_literal_static(&literal.value));

                if let Some(ref lang) = literal.language {
                    result.push_str(&format!("@{lang}"));
                } else if let Some(ref datatype) = literal.datatype {
                    result.push_str(&format!("^^<{}>", datatype.iri));
                }

                Ok(result)
            }
            StarTerm::QuotedTriple(triple) => {
                let subject = Self::format_term_ntriples(&triple.subject)?;
                let predicate = Self::format_term_ntriples(&triple.predicate)?;
                let object = Self::format_term_ntriples(&triple.object)?;
                Ok(format!("<< {subject} {predicate} {object} >>"))
            }
            StarTerm::Variable(var) => Ok(format!("?{}", var.name)),
        }
    }

    /// Escape special characters in literals (SIMD-accelerated)
    pub fn escape_literal(&self, value: &str) -> String {
        self.simd_escaper.escape_literal(value)
    }

    /// Static escape function for backward compatibility
    pub fn escape_literal_static(value: &str) -> String {
        // Use a temporary escaper for static calls
        let escaper = super::super::simd_escape::SimdEscaper::new();
        escaper.escape_literal(value)
    }

    /// Add common namespace prefixes
    fn add_common_prefixes(&self, context: &mut SerializationContext) {
        context.add_prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        context.add_prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
        context.add_prefix("owl", "http://www.w3.org/2002/07/owl#");
        context.add_prefix("xsd", "http://www.w3.org/2001/XMLSchema#");
        context.add_prefix("foaf", "http://xmlns.com/foaf/0.1/");
        context.add_prefix("dc", "http://purl.org/dc/terms/");
    }

    /// Parse a graph name string back to a term for TriG serialization
    fn parse_graph_name(
        &self,
        graph_name: &str,
        context: &SerializationContext,
    ) -> StarResult<String> {
        if graph_name.starts_with("_:") {
            // Blank node graph name
            Ok(graph_name.to_string())
        } else {
            // Named node graph name - compress with prefixes if possible
            Ok(context.compress_iri(graph_name))
        }
    }

    /// Static method for converting StarTerm to JSON-LD predicate format
    pub fn term_to_jsonld_predicate_static(term: &StarTerm) -> StarResult<String> {
        match term {
            StarTerm::NamedNode(node) => Ok(node.iri.clone()),
            _ => Err(StarError::serialization_error(
                "Only named nodes can be used as predicates in JSON-LD".to_string(),
            )),
        }
    }

    /// Static method for converting StarTerm to JSON-LD value format  
    pub fn term_to_jsonld_value_static(term: &StarTerm) -> StarResult<serde_json::Value> {
        match term {
            StarTerm::NamedNode(node) => {
                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@id".to_string(),
                    serde_json::Value::String(node.iri.clone()),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::BlankNode(node) => {
                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@id".to_string(),
                    serde_json::Value::String(format!("_:{}", node.id)),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::Literal(literal) => {
                if let Some(ref lang) = literal.language {
                    let mut obj = serde_json::Map::new();
                    obj.insert(
                        "@value".to_string(),
                        serde_json::Value::String(literal.value.clone()),
                    );
                    obj.insert(
                        "@language".to_string(),
                        serde_json::Value::String(lang.clone()),
                    );
                    Ok(serde_json::Value::Object(obj))
                } else if let Some(ref datatype) = literal.datatype {
                    let mut obj = serde_json::Map::new();
                    obj.insert(
                        "@value".to_string(),
                        serde_json::Value::String(literal.value.clone()),
                    );
                    obj.insert(
                        "@type".to_string(),
                        serde_json::Value::String(datatype.iri.clone()),
                    );
                    Ok(serde_json::Value::Object(obj))
                } else {
                    // Plain literal defaults to string
                    Ok(serde_json::Value::String(literal.value.clone()))
                }
            }
            StarTerm::QuotedTriple(triple) => {
                // JSON-LD-star representation of quoted triples as annotations
                let mut annotation_obj = serde_json::Map::new();
                annotation_obj.insert(
                    "subject".to_string(),
                    Self::term_to_jsonld_value_static(&triple.subject)?,
                );
                annotation_obj.insert(
                    "predicate".to_string(),
                    Self::term_to_jsonld_value_static(&triple.predicate)?,
                );
                annotation_obj.insert(
                    "object".to_string(),
                    Self::term_to_jsonld_value_static(&triple.object)?,
                );

                let mut obj = serde_json::Map::new();
                obj.insert(
                    "@annotation".to_string(),
                    serde_json::Value::Object(annotation_obj),
                );
                Ok(serde_json::Value::Object(obj))
            }
            StarTerm::Variable(var) => Err(StarError::serialization_error(format!(
                "Variables cannot be serialized to JSON-LD: ?{}",
                var.name
            ))),
        }
    }
}