Adds Repository::load_dataset to load dataset files

pull/10/head
Tpt 5 years ago
parent 9ccc079b4c
commit fd3def2daf
  1. 1
      lib/src/lib.rs
  2. 28
      lib/src/repository.rs
  3. 9
      lib/src/rio.rs
  4. 34
      lib/src/store/mod.rs

@ -47,4 +47,5 @@ pub use crate::store::MemoryRepository;
pub use crate::store::RocksDbRepository;
pub use repository::Repository;
pub use repository::RepositoryConnection;
pub use rio::DatasetSyntax;
pub use rio::GraphSyntax;

@ -1,6 +1,6 @@
use crate::model::*;
use crate::sparql::PreparedQuery;
use crate::{GraphSyntax, Result};
use crate::{DatasetSyntax, GraphSyntax, Result};
use std::io::{BufRead, Read};
/// A `Repository` stores a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset)
@ -139,6 +139,32 @@ pub trait RepositoryConnection: Clone {
base_iri: Option<&str>,
) -> Result<()>;
/// Loads a dataset file (i.e. quads) into the repository
///
/// Usage example:
/// ```
/// use rudf::model::*;
/// use rudf::{Repository, RepositoryConnection, MemoryRepository, Result, DatasetSyntax};
///
/// let repository = MemoryRepository::default();
/// let connection = repository.connection().unwrap();
///
/// // insertion
/// let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> .";
/// connection.load_dataset(file.as_ref(), DatasetSyntax::NQuads, None);
///
/// // quad filter
/// let results: Result<Vec<Quad>> = connection.quads_for_pattern(None, None, None, None).collect();
/// let ex = NamedNode::new("http://example.com");
/// assert_eq!(vec![Quad::new(ex.clone(), ex.clone(), ex.clone(), Some(ex.into()))], results.unwrap());
/// ```
fn load_dataset(
&self,
reader: impl BufRead,
syntax: DatasetSyntax,
base_iri: Option<&str>,
) -> Result<()>;
/// Checks if this repository contains a given quad
fn contains(&self, quad: &Quad) -> Result<bool>;

@ -8,3 +8,12 @@ pub enum GraphSyntax {
/// [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/)
RdfXml,
}
/// [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) serialization formats.
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum DatasetSyntax {
/// [N-Quads](https://www.w3.org/TR/n-quads/)
NQuads,
/// [TriG](https://www.w3.org/TR/trig/)
TriG,
}

@ -12,9 +12,9 @@ pub use crate::store::rocksdb::RocksDbRepository;
use crate::model::*;
use crate::sparql::SimplePreparedQuery;
use crate::store::numeric_encoder::*;
use crate::{GraphSyntax, RepositoryConnection, Result};
use rio_api::parser::TripleParser;
use rio_turtle::{NTriplesParser, TurtleParser};
use crate::{DatasetSyntax, GraphSyntax, RepositoryConnection, Result};
use rio_api::parser::{QuadParser, TripleParser};
use rio_turtle::{NQuadsParser, NTriplesParser, TriGParser, TurtleParser};
use rio_xml::RdfXmlParser;
use std::collections::HashMap;
use std::io::{BufRead, Read};
@ -139,6 +139,19 @@ impl<S: StoreConnection> RepositoryConnection for StoreRepositoryConnection<S> {
}
}
fn load_dataset(
&self,
reader: impl BufRead,
syntax: DatasetSyntax,
base_iri: Option<&str>,
) -> Result<()> {
let base_iri = base_iri.unwrap_or(&"");
match syntax {
DatasetSyntax::NQuads => self.load_from_quad_parser(NQuadsParser::new(reader)?),
DatasetSyntax::TriG => self.load_from_quad_parser(TriGParser::new(reader, base_iri)?),
}
}
fn contains(&self, quad: &Quad) -> Result<bool> {
self.inner
.contains(&self.inner.encoder().encode_quad(quad)?)
@ -181,4 +194,19 @@ impl<S: StoreConnection> StoreRepositoryConnection<S> {
})?;
Ok(())
}
fn load_from_quad_parser<P: QuadParser>(&self, mut parser: P) -> Result<()>
where
P::Error: Send + Sync + 'static,
{
//TODO: handle errors
let mut bnode_map = HashMap::default();
let encoder = self.inner.encoder();
parser.parse_all(&mut move |q| {
self.inner
.insert(&encoder.encode_rio_quad(q, &mut bnode_map).unwrap())
.unwrap()
})?;
Ok(())
}
}

Loading…
Cancel
Save