oxiland 0.13.0

Embedded RDF datasets, SPARQL, persistence, and streaming I/O for Rust
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
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

use oxigraph::io::{RdfParseError, RdfParser, RdfSyntaxError, ReaderQuadParser, SliceQuadParser};
use oxigraph::model::{GraphName, Quad};

use crate::io::Syntax;
use crate::io::bom::{BomStrippingReader, strip_utf8_bom_bytes, strip_utf8_bom_str};
use crate::io::location::SourceLocation;
use crate::world::{FeatureMap, FeatureValue};
use crate::{Error, Model, Result};

/// Configured RDF parser facade (ADR-007 / ADR-008).
///
/// Parsing always renames blank nodes so independent operations do not collide.
/// The streaming core yields fallible quads with explicit partial progress.
#[derive(Clone, Debug)]
pub struct Parser {
    syntax: Syntax,
    base_iri: Option<String>,
    graph_target: GraphTarget,
    features: FeatureMap,
}

impl Parser {
    /// Creates a parser for an advertised [`Syntax`].
    #[must_use]
    pub fn for_syntax(syntax: Syntax) -> Self {
        Self {
            syntax,
            base_iri: None,
            graph_target: GraphTarget::DefaultGraph,
            features: FeatureMap::new(),
        }
    }

    /// Sets a parser feature (Redland `librdf_parser_set_feature`).
    pub fn set_feature(&self, iri: impl Into<String>, value: FeatureValue) {
        self.features.set(iri, value);
    }

    /// Returns a parser feature when set.
    #[must_use]
    pub fn feature(&self, iri: &str) -> Option<FeatureValue> {
        self.features.get(iri)
    }

    /// Returns the configured syntax.
    #[must_use]
    pub fn syntax(&self) -> Syntax {
        self.syntax
    }

    /// Sets the base IRI used to resolve relative IRIs where the syntax supports it.
    pub fn base_iri(mut self, base_iri: impl Into<String>) -> Result<Self> {
        let base_iri = base_iri.into();
        // Validate early via Oxigraph so configuration fails before I/O.
        let _ = RdfParser::from_format(self.syntax.to_oxigraph())
            .with_base_iri(&base_iri)
            .map_err(|error| Error::InvalidRdf(error.to_string()))?;
        self.base_iri = Some(base_iri);
        Ok(self)
    }

    /// Selects how triples and named graphs are mapped into quads.
    #[must_use]
    pub fn graph_target(mut self, target: GraphTarget) -> Self {
        self.graph_target = target;
        self
    }

    /// Streams quads from any [`Read`] source.
    ///
    /// A leading UTF-8 BOM is skipped when present.
    pub fn parse_reader<R: Read>(&self, reader: R) -> Result<QuadStream<BomStrippingReader<R>>> {
        Ok(QuadStream {
            inner: QuadStreamInner::Reader(
                self.build()?.for_reader(BomStrippingReader::new(reader)),
            ),
            graph_target: self.graph_target.clone(),
        })
    }

    /// Streams quads from an in-memory byte slice.
    ///
    /// A leading UTF-8 BOM is skipped when present.
    pub fn parse_slice<'a>(
        &self,
        slice: &'a (impl AsRef<[u8]> + ?Sized),
    ) -> Result<SliceStream<'a>> {
        let bytes = strip_utf8_bom_bytes(slice.as_ref());
        Ok(SliceStream {
            inner: self.build()?.for_slice(bytes),
            graph_target: self.graph_target.clone(),
        })
    }

    /// Streams quads from a UTF-8 string.
    ///
    /// A leading Unicode BOM is skipped when present.
    pub fn parse_str<'a>(&self, input: &'a str) -> Result<SliceStream<'a>> {
        self.parse_slice(strip_utf8_bom_str(input).as_bytes())
    }

    /// Streams quads from a filesystem path.
    ///
    /// The path is diagnostic context only; the caller must select [`Syntax`]
    /// explicitly unless using [`Parser::parse_path_with_extension`].
    pub fn parse_path(
        &self,
        path: impl AsRef<Path>,
    ) -> Result<QuadStream<BomStrippingReader<BufReader<File>>>> {
        let path = path.as_ref();
        let file = File::open(path).map_err(|error| io_with_path(error, path))?;
        self.parse_reader(BufReader::new(file))
    }

    /// Parses a path after resolving [`Syntax`] from the file extension.
    ///
    /// Dataset syntaxes (N-Quads, TriG) default to [`GraphTarget::Dataset`] so
    /// typical named-graph files parse successfully. Graph-only syntaxes keep
    /// [`GraphTarget::DefaultGraph`].
    pub fn parse_path_with_extension(
        path: impl AsRef<Path>,
    ) -> Result<(Syntax, QuadStream<BomStrippingReader<BufReader<File>>>)> {
        let path = path.as_ref();
        let extension = path
            .extension()
            .and_then(|ext| ext.to_str())
            .ok_or_else(|| {
                Error::Unsupported(format!(
                    "path '{}' has no file extension for syntax detection",
                    path.display()
                ))
            })?;
        let syntax = Syntax::from_extension(extension)?;
        let mut parser = Parser::for_syntax(syntax);
        if syntax.supports_datasets() {
            parser = parser.graph_target(GraphTarget::Dataset);
        }
        let stream = parser.parse_path(path)?;
        Ok((syntax, stream))
    }

    /// Inserts parsed quads into `model` progressively (ADR-007).
    ///
    /// On parse or mid-stream I/O failure, already-inserted quads remain. The
    /// returned error documents how many quads this call newly inserted before
    /// failing.
    ///
    /// Returns the number of input quads successfully processed (including
    /// duplicates that were already present).
    ///
    /// Persistent (`Model::open`) models sync each successful insert, so a
    /// partial progressive load is durable. Prefer [`Parser::load_transactional`]
    /// when atomic import is required.
    pub fn load_into(&self, model: &Model, reader: impl Read) -> Result<usize> {
        let mut processed = 0usize;
        let mut newly_inserted = 0usize;
        for item in self.parse_reader(reader)? {
            let quad = item.map_err(|error| annotate_partial_load(error, newly_inserted))?;
            if model
                .insert_quad(quad)
                .map_err(|error| annotate_partial_load(error, newly_inserted))?
            {
                newly_inserted += 1;
            }
            processed += 1;
        }
        Ok(processed)
    }

    /// Parses the complete input into memory, then inserts only after the parse
    /// succeeds.
    ///
    /// If a later insert fails, quads newly inserted by this call are removed
    /// best-effort so the model returns to its pre-call contents for this batch.
    /// The name makes the buffering cost explicit. Prefer [`Parser::parse_reader`]
    /// for large streams when partial progress is acceptable.
    pub fn load_collecting(&self, model: &Model, reader: impl Read) -> Result<usize> {
        let quads = self.parse_reader(reader)?.collect::<Result<Vec<_>>>()?;
        let total = quads.len();
        let mut newly_inserted = Vec::new();
        for quad in quads {
            match model.insert_quad(quad.clone()) {
                Ok(true) => newly_inserted.push(quad),
                Ok(false) => {}
                Err(error) => {
                    let mut rollback_error: Option<Error> = None;
                    for inserted in newly_inserted.into_iter().rev() {
                        if let Err(remove_error) = model.remove_quad(&inserted) {
                            if rollback_error.is_none() {
                                rollback_error = Some(remove_error);
                            }
                        }
                    }
                    return Err(match rollback_error {
                        Some(remove_error) => Error::Storage(format!(
                            "insert failed ({error}); rollback also failed ({remove_error})"
                        )),
                        None => error,
                    });
                }
            }
        }
        Ok(total)
    }

    /// Progressive load from a filesystem path.
    pub fn load_path_into(&self, model: &Model, path: impl AsRef<Path>) -> Result<usize> {
        let path = path.as_ref();
        let file = File::open(path).map_err(|error| io_with_path(error, path))?;
        self.load_into(model, BufReader::new(file))
    }

    /// Collecting load from a filesystem path.
    pub fn load_path_collecting(&self, model: &Model, path: impl AsRef<Path>) -> Result<usize> {
        let path = path.as_ref();
        let file = File::open(path).map_err(|error| io_with_path(error, path))?;
        self.load_collecting(model, BufReader::new(file))
    }

    /// Parses the complete input, then inserts inside [`Model::transaction`].
    ///
    /// Mid-parse failure leaves the model unchanged. On Fjall models, durability
    /// is synced only if the transaction commits (ADR-007 / 0.4).
    pub fn load_transactional(&self, model: &Model, reader: impl Read) -> Result<usize> {
        let quads = self.parse_reader(reader)?.collect::<Result<Vec<_>>>()?;
        let total = quads.len();
        model.transaction(|tx| {
            for quad in quads {
                tx.insert_quad(quad)?;
            }
            Ok(total)
        })
    }

    /// Transactional load from a filesystem path.
    pub fn load_path_transactional(&self, model: &Model, path: impl AsRef<Path>) -> Result<usize> {
        let path = path.as_ref();
        let file = File::open(path).map_err(|error| io_with_path(error, path))?;
        self.load_transactional(model, BufReader::new(file))
    }

    fn build(&self) -> Result<RdfParser> {
        let mut parser = RdfParser::from_format(self.syntax.to_oxigraph()).rename_blank_nodes();
        if let Some(base_iri) = &self.base_iri {
            parser = parser
                .with_base_iri(base_iri)
                .map_err(|error| Error::InvalidRdf(error.to_string()))?;
        }
        match &self.graph_target {
            GraphTarget::DefaultGraph => {
                // Reject named-graph input even for TriG/N-Quads so DefaultGraph
                // and Dataset remain distinct (D-02-04).
                parser = parser
                    .with_default_graph(GraphName::DefaultGraph)
                    .without_named_graphs();
            }
            GraphTarget::Named(graph_name) => {
                // Remap the syntax default graph; same-named input quads are
                // allowed; foreign named graphs are rejected in the stream.
                parser = parser.with_default_graph(graph_name.clone());
            }
            GraphTarget::Dataset => {
                if !self.syntax.supports_datasets() {
                    return Err(Error::Unsupported(format!(
                        "syntax '{}' does not support dataset/named-graph input; use GraphTarget::DefaultGraph or Named",
                        self.syntax.name()
                    )));
                }
            }
        }
        Ok(parser)
    }
}

/// Destination policy for parsed triples and quads (D-02-04).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GraphTarget {
    /// Emit default-graph quads and reject named-graph input.
    DefaultGraph,
    /// Remap the syntax default graph into this named graph/context and reject
    /// input quads that name a different graph.
    Named(GraphName),
    /// Preserve named graphs from dataset syntaxes (TriG / N-Quads).
    Dataset,
}

/// Streaming quads from a [`Read`] source.
#[must_use]
pub struct QuadStream<R: Read> {
    inner: QuadStreamInner<R>,
    graph_target: GraphTarget,
}

enum QuadStreamInner<R: Read> {
    Reader(ReaderQuadParser<R>),
}

impl<R: Read> Iterator for QuadStream<R> {
    type Item = Result<Quad>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.inner {
            QuadStreamInner::Reader(parser) => parser.next().map(|item| {
                map_parse_result(item)
                    .and_then(|quad| enforce_graph_target(quad, &self.graph_target))
            }),
        }
    }
}

/// Streaming quads from an in-memory slice.
#[must_use]
pub struct SliceStream<'a> {
    inner: SliceQuadParser<'a>,
    graph_target: GraphTarget,
}

impl Iterator for SliceStream<'_> {
    type Item = Result<Quad>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|item| {
            map_syntax_result(item).and_then(|quad| enforce_graph_target(quad, &self.graph_target))
        })
    }
}

fn enforce_graph_target(quad: Quad, target: &GraphTarget) -> Result<Quad> {
    match target {
        GraphTarget::DefaultGraph | GraphTarget::Dataset => Ok(quad),
        GraphTarget::Named(expected) => {
            if quad.graph_name == *expected {
                Ok(quad)
            } else {
                Err(Error::parse(
                    format!(
                        "named graph '{}' is not allowed for GraphTarget::Named('{expected}')",
                        quad.graph_name
                    ),
                    None,
                ))
            }
        }
    }
}

fn map_parse_result(result: std::result::Result<Quad, RdfParseError>) -> Result<Quad> {
    result.map_err(map_rdf_parse_error)
}

pub(crate) fn map_rdf_parse_error(error: RdfParseError) -> Error {
    match error {
        RdfParseError::Io(error) => Error::Io(error),
        RdfParseError::Syntax(error) => map_syntax_error(error),
    }
}

fn map_syntax_result(result: std::result::Result<Quad, RdfSyntaxError>) -> Result<Quad> {
    result.map_err(map_syntax_error)
}

fn map_syntax_error(error: RdfSyntaxError) -> Error {
    let location = error.location().map(SourceLocation::from_range);
    Error::parse(strip_embedded_location(error.to_string()), location)
}

/// Oxigraph often embeds "at line N column M" in the Display text. Strip that
/// when we already expose a structured [`SourceLocation`].
fn strip_embedded_location(message: String) -> String {
    const MARKERS: &[&str] = &[
        "Parser error at line ",
        "parser error at line ",
        "Error at line ",
        "error at line ",
    ];
    for marker in MARKERS {
        if let Some(rest) = message.strip_prefix(marker) {
            if let Some((_, semantic)) = rest.split_once(": ") {
                return semantic.to_owned();
            }
        }
    }
    // Also handle messages that start with other text then " at line ".
    if let Some(idx) = message.find(" at line ") {
        if let Some((_, semantic)) = message[idx..].split_once(": ") {
            return semantic.to_owned();
        }
    }
    message
}

fn annotate_partial_load(error: Error, newly_inserted: usize) -> Error {
    if newly_inserted == 0 {
        return error;
    }
    let note = format!(
        "partial load newly inserted {newly_inserted} statement(s) that remain in the model (ADR-007 progressive load)"
    );
    match error {
        Error::Parse(mut parse) => {
            parse.message = format!("{}; {note}", parse.message);
            Error::Parse(parse)
        }
        Error::Io(io_error) => Error::Io(std::io::Error::new(
            io_error.kind(),
            format!("{io_error}; {note}"),
        )),
        Error::Storage(message) => Error::Storage(format!("{message}; {note}")),
        Error::InvalidRdf(message) => Error::InvalidRdf(format!("{message}; {note}")),
        Error::Serialize(message) => Error::Serialize(format!("{message}; {note}")),
        Error::Unsupported(message) => Error::Unsupported(format!("{message}; {note}")),
        Error::SparqlParse(message) => Error::SparqlParse(format!("{message}; {note}")),
        Error::SparqlEvaluation(message) => Error::SparqlEvaluation(format!("{message}; {note}")),
        Error::OpenStore { path, message } => Error::OpenStore {
            path,
            message: format!("{message}; {note}"),
        },
    }
}

fn io_with_path(error: std::io::Error, path: &Path) -> Error {
    Error::Io(std::io::Error::new(
        error.kind(),
        format!("{}: {}", path.display(), error),
    ))
}