Renames dump_dataset and dump_graph to dump_to_write and dump_graph_to_write

pull/705/head
Tpt 12 months ago committed by Thomas Tanon
parent 2a135283d5
commit 4084acb9b8
  1. 4
      cli/src/main.rs
  2. 9
      js/src/store.rs
  3. 18
      lib/oxrdfio/src/parser.rs
  4. 11
      lib/oxrdfio/src/serializer.rs
  5. 32
      lib/src/io/format.rs
  6. 90
      lib/src/store.rs
  7. 4
      lib/tests/store.rs
  8. 5
      python/src/store.rs

@ -806,9 +806,9 @@ fn dump<W: Write>(
) -> anyhow::Result<W> { ) -> anyhow::Result<W> {
ensure!(format.supports_datasets() || from_graph_name.is_some(), "The --graph option is required when writing a format not supporting datasets like NTriples, Turtle or RDF/XML"); ensure!(format.supports_datasets() || from_graph_name.is_some(), "The --graph option is required when writing a format not supporting datasets like NTriples, Turtle or RDF/XML");
Ok(if let Some(from_graph_name) = from_graph_name { Ok(if let Some(from_graph_name) = from_graph_name {
store.dump_graph(write, format, from_graph_name) store.dump_graph_to_write(from_graph_name, format, write)
} else { } else {
store.dump_dataset(write, format) store.dump_to_write(format, write)
}?) }?)
} }

@ -177,10 +177,13 @@ impl JsStore {
let format = rdf_format(format)?; let format = rdf_format(format)?;
let buffer = let buffer =
if let Some(from_graph_name) = FROM_JS.with(|c| c.to_optional_term(from_graph_name))? { if let Some(from_graph_name) = FROM_JS.with(|c| c.to_optional_term(from_graph_name))? {
self.store self.store.dump_graph_to_write(
.dump_graph(Vec::new(), format, &GraphName::try_from(from_graph_name)?) &GraphName::try_from(from_graph_name)?,
format,
Vec::new(),
)
} else { } else {
self.store.dump_dataset(Vec::new(), format) self.store.dump_to_write(format, Vec::new())
} }
.map_err(to_err)?; .map_err(to_err)?;
String::from_utf8(buffer).map_err(to_err) String::from_utf8(buffer).map_err(to_err)

@ -125,6 +125,24 @@ impl RdfParser {
} }
} }
/// The format the parser uses.
///
/// ```
/// use oxrdfio::{RdfParser, RdfFormat};
///
/// assert_eq!(RdfParser::from_format(RdfFormat::Turtle).format(), RdfFormat::Turtle);
/// ```
pub fn format(&self) -> RdfFormat {
match &self.inner {
RdfParserKind::N3(_) => RdfFormat::N3,
RdfParserKind::NQuads(_) => RdfFormat::NQuads,
RdfParserKind::NTriples(_) => RdfFormat::NTriples,
RdfParserKind::RdfXml(_) => RdfFormat::RdfXml,
RdfParserKind::TriG(_) => RdfFormat::TriG,
RdfParserKind::Turtle(_) => RdfFormat::Turtle,
}
}
/// Provides an IRI that could be used to resolve the file relative IRIs. /// Provides an IRI that could be used to resolve the file relative IRIs.
/// ///
/// ``` /// ```

@ -60,6 +60,17 @@ impl RdfSerializer {
Self { format } Self { format }
} }
/// The format the serializer serializes to.
///
/// ```
/// use oxrdfio::{RdfSerializer, RdfFormat};
///
/// assert_eq!(RdfSerializer::from_format(RdfFormat::Turtle).format(), RdfFormat::Turtle);
/// ```
pub fn format(&self) -> RdfFormat {
self.format
}
/// Writes to a [`Write`] implementation. /// Writes to a [`Write`] implementation.
/// ///
/// <div class="warning">Do not forget to run the [`finish`](ToWriteQuadWriter::finish()) method to properly write the last bytes of the file.</div> /// <div class="warning">Do not forget to run the [`finish`](ToWriteQuadWriter::finish()) method to properly write the last bytes of the file.</div>

@ -1,6 +1,6 @@
#![allow(deprecated)] #![allow(deprecated)]
use oxrdfio::RdfFormat; use oxrdfio::{RdfFormat, RdfParser, RdfSerializer};
/// [RDF graph](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-graph) serialization formats. /// [RDF graph](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-graph) serialization formats.
/// ///
@ -108,6 +108,7 @@ impl GraphFormat {
} }
impl From<GraphFormat> for RdfFormat { impl From<GraphFormat> for RdfFormat {
#[inline]
fn from(format: GraphFormat) -> Self { fn from(format: GraphFormat) -> Self {
match format { match format {
GraphFormat::NTriples => Self::NTriples, GraphFormat::NTriples => Self::NTriples,
@ -117,6 +118,20 @@ impl From<GraphFormat> for RdfFormat {
} }
} }
impl From<GraphFormat> for RdfParser {
#[inline]
fn from(format: GraphFormat) -> Self {
RdfFormat::from(format).into()
}
}
impl From<GraphFormat> for RdfSerializer {
#[inline]
fn from(format: GraphFormat) -> Self {
RdfFormat::from(format).into()
}
}
/// [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) serialization formats. /// [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) serialization formats.
/// ///
/// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future. /// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future.
@ -215,6 +230,7 @@ impl DatasetFormat {
} }
impl From<DatasetFormat> for RdfFormat { impl From<DatasetFormat> for RdfFormat {
#[inline]
fn from(format: DatasetFormat) -> Self { fn from(format: DatasetFormat) -> Self {
match format { match format {
DatasetFormat::NQuads => Self::NQuads, DatasetFormat::NQuads => Self::NQuads,
@ -223,6 +239,20 @@ impl From<DatasetFormat> for RdfFormat {
} }
} }
impl From<DatasetFormat> for RdfParser {
#[inline]
fn from(format: DatasetFormat) -> Self {
RdfFormat::from(format).into()
}
}
impl From<DatasetFormat> for RdfSerializer {
#[inline]
fn from(format: DatasetFormat) -> Self {
RdfFormat::from(format).into()
}
}
impl TryFrom<DatasetFormat> for GraphFormat { impl TryFrom<DatasetFormat> for GraphFormat {
type Error = (); type Error = ();

@ -639,6 +639,37 @@ impl Store {
self.transaction(move |mut t| t.remove(quad)) self.transaction(move |mut t| t.remove(quad))
} }
/// Dumps the store into a file.
///
/// ```
/// use oxigraph::store::Store;
/// use oxigraph::io::RdfFormat;
///
/// let file = "<http://example.com> <http://example.com> <http://example.com> <http://example.com> .\n".as_bytes();
///
/// let store = Store::new()?;
/// store.load_from_read(RdfFormat::NQuads, file)?;
///
/// let buffer = store.dump_to_write(RdfFormat::NQuads, Vec::new())?;
/// assert_eq!(file, buffer.as_slice());
/// # std::io::Result::Ok(())
/// ```
pub fn dump_to_write<W: Write>(
&self,
serializer: impl Into<RdfSerializer>,
write: W,
) -> Result<W, SerializerError> {
let serializer = serializer.into();
if !serializer.format().supports_datasets() {
return Err(SerializerError::DatasetFormatExpected(serializer.format()));
}
let mut writer = serializer.serialize_to_write(write);
for quad in self.iter() {
writer.write_quad(&quad?)?;
}
Ok(writer.finish()?)
}
/// Dumps a store graph into a file. /// Dumps a store graph into a file.
/// ///
/// Usage example: /// Usage example:
@ -653,23 +684,51 @@ impl Store {
/// store.load_graph(file, RdfFormat::NTriples, GraphName::DefaultGraph, None)?; /// store.load_graph(file, RdfFormat::NTriples, GraphName::DefaultGraph, None)?;
/// ///
/// let mut buffer = Vec::new(); /// let mut buffer = Vec::new();
/// store.dump_graph(&mut buffer, RdfFormat::NTriples, GraphNameRef::DefaultGraph)?; /// store.dump_graph_to_write(GraphNameRef::DefaultGraph, RdfFormat::NTriples, &mut buffer)?;
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # std::io::Result::Ok(()) /// # std::io::Result::Ok(())
/// ``` /// ```
pub fn dump_graph<'a, W: Write>( pub fn dump_graph_to_write<'a, W: Write>(
&self, &self,
write: W,
format: impl Into<RdfFormat>,
from_graph_name: impl Into<GraphNameRef<'a>>, from_graph_name: impl Into<GraphNameRef<'a>>,
serializer: impl Into<RdfSerializer>,
write: W,
) -> Result<W, SerializerError> { ) -> Result<W, SerializerError> {
let mut writer = RdfSerializer::from_format(format.into()).serialize_to_write(write); let mut writer = serializer.into().serialize_to_write(write);
for quad in self.quads_for_pattern(None, None, None, Some(from_graph_name.into())) { for quad in self.quads_for_pattern(None, None, None, Some(from_graph_name.into())) {
writer.write_triple(quad?.as_ref())?; writer.write_triple(quad?.as_ref())?;
} }
Ok(writer.finish()?) Ok(writer.finish()?)
} }
/// Dumps a store graph into a file.
///
/// Usage example:
/// ```
/// use oxigraph::store::Store;
/// use oxigraph::io::RdfFormat;
/// use oxigraph::model::*;
///
/// let file = "<http://example.com> <http://example.com> <http://example.com> .\n".as_bytes();
///
/// let store = Store::new()?;
/// store.load_graph(file, RdfFormat::NTriples, GraphName::DefaultGraph, None)?;
///
/// let mut buffer = Vec::new();
/// store.dump_graph(&mut buffer, RdfFormat::NTriples, GraphNameRef::DefaultGraph)?;
/// assert_eq!(file, buffer.as_slice());
/// # std::io::Result::Ok(())
/// ```
#[deprecated(note = "use Store.dump_graph_to_write instead", since = "0.4.0")]
pub fn dump_graph<'a, W: Write>(
&self,
write: W,
format: impl Into<RdfFormat>,
from_graph_name: impl Into<GraphNameRef<'a>>,
) -> Result<W, SerializerError> {
self.dump_graph_to_write(from_graph_name, format.into(), write)
}
/// Dumps the store into a file. /// Dumps the store into a file.
/// ///
/// ``` /// ```
@ -679,26 +738,19 @@ impl Store {
/// let file = "<http://example.com> <http://example.com> <http://example.com> <http://example.com> .\n".as_bytes(); /// let file = "<http://example.com> <http://example.com> <http://example.com> <http://example.com> .\n".as_bytes();
/// ///
/// let store = Store::new()?; /// let store = Store::new()?;
/// store.load_dataset(file, RdfFormat::NQuads, None)?; /// store.load_from_read(RdfFormat::NQuads, file)?;
/// ///
/// let buffer = store.dump_dataset(Vec::new(), RdfFormat::NQuads)?; /// let buffer = store.dump_dataset(Vec::new(), RdfFormat::NQuads)?;
/// assert_eq!(file, buffer.as_slice()); /// assert_eq!(file, buffer.as_slice());
/// # std::io::Result::Ok(()) /// # std::io::Result::Ok(())
/// ``` /// ```
#[deprecated(note = "use Store.dump_to_write instead", since = "0.4.0")]
pub fn dump_dataset<W: Write>( pub fn dump_dataset<W: Write>(
&self, &self,
write: W, write: W,
format: impl Into<RdfFormat>, format: impl Into<RdfFormat>,
) -> Result<W, SerializerError> { ) -> Result<W, SerializerError> {
let format = format.into(); self.dump_to_write(format.into(), write)
if !format.supports_datasets() {
return Err(SerializerError::DatasetFormatExpected(format));
}
let mut writer = RdfSerializer::from_format(format).serialize_to_write(write);
for quad in self.iter() {
writer.write_quad(&quad?)?;
}
Ok(writer.finish()?)
} }
/// Returns all the store named graphs. /// Returns all the store named graphs.
@ -876,7 +928,7 @@ impl Store {
/// but hard links will be used to point to the original database immutable snapshots. /// but hard links will be used to point to the original database immutable snapshots.
/// This allows cheap regular backups. /// This allows cheap regular backups.
/// ///
/// If you want to move your data to another RDF storage system, you should have a look at the [`Store::dump_dataset`] function instead. /// If you want to move your data to another RDF storage system, you should have a look at the [`Store::dump_to_write`] function instead.
#[cfg(not(target_family = "wasm"))] #[cfg(not(target_family = "wasm"))]
pub fn backup(&self, target_directory: impl AsRef<Path>) -> Result<(), StorageError> { pub fn backup(&self, target_directory: impl AsRef<Path>) -> Result<(), StorageError> {
self.storage.backup(target_directory.as_ref()) self.storage.backup(target_directory.as_ref())
@ -894,7 +946,7 @@ impl Store {
/// ///
/// // quads file insertion /// // quads file insertion
/// let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> ."; /// let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> .";
/// store.bulk_loader().load_dataset(file.as_ref(), RdfFormat::NQuads, None)?; /// store.bulk_loader().load_from_read(RdfFormat::NQuads, file.as_ref())?;
/// ///
/// // we inspect the store contents /// // we inspect the store contents
/// let ex = NamedNodeRef::new("http://example.com")?; /// let ex = NamedNodeRef::new("http://example.com")?;
@ -1467,7 +1519,7 @@ impl Iterator for GraphNameIter {
/// ///
/// // quads file insertion /// // quads file insertion
/// let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> ."; /// let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> .";
/// store.bulk_loader().load_dataset(file.as_ref(), RdfFormat::NQuads, None)?; /// store.bulk_loader().load_from_read(RdfFormat::NQuads, file.as_ref())?;
/// ///
/// // we inspect the store contents /// // we inspect the store contents
/// let ex = NamedNodeRef::new("http://example.com")?; /// let ex = NamedNodeRef::new("http://example.com")?;
@ -1532,7 +1584,7 @@ impl BulkLoader {
/// Loads a file using the bulk loader. /// Loads a file using the bulk loader.
/// ///
/// This function is optimized for large dataset loading speed. For small files, [`Store::load_dataset`] might be more convenient. /// This function is optimized for large dataset loading speed. For small files, [`Store::load_from_read`] might be more convenient.
/// ///
/// <div class="warning">This method is not atomic. /// <div class="warning">This method is not atomic.
/// If the parsing fails in the middle of the file, only a part of it may be written to the store. /// If the parsing fails in the middle of the file, only a part of it may be written to the store.

@ -202,7 +202,7 @@ fn test_dump_graph() -> Result<(), Box<dyn Error>> {
} }
let mut buffer = Vec::new(); let mut buffer = Vec::new();
store.dump_graph(&mut buffer, RdfFormat::NTriples, GraphNameRef::DefaultGraph)?; store.dump_graph_to_write(GraphNameRef::DefaultGraph, RdfFormat::NTriples, &mut buffer)?;
assert_eq!( assert_eq!(
buffer.into_iter().filter(|c| *c == b'\n').count(), buffer.into_iter().filter(|c| *c == b'\n').count(),
NUMBER_OF_TRIPLES NUMBER_OF_TRIPLES
@ -217,7 +217,7 @@ fn test_dump_dataset() -> Result<(), Box<dyn Error>> {
store.insert(q)?; store.insert(q)?;
} }
let buffer = store.dump_dataset(Vec::new(), RdfFormat::NQuads)?; let buffer = store.dump_to_write(RdfFormat::NQuads, Vec::new())?;
assert_eq!( assert_eq!(
buffer.into_iter().filter(|c| *c == b'\n').count(), buffer.into_iter().filter(|c| *c == b'\n').count(),
NUMBER_OF_TRIPLES NUMBER_OF_TRIPLES

@ -542,9 +542,10 @@ impl PyStore {
py.allow_threads(|| { py.allow_threads(|| {
let format = lookup_rdf_format(format, file_path.as_deref())?; let format = lookup_rdf_format(format, file_path.as_deref())?;
if let Some(from_graph_name) = &from_graph_name { if let Some(from_graph_name) = &from_graph_name {
self.inner.dump_graph(output, format, from_graph_name) self.inner
.dump_graph_to_write(from_graph_name, format, output)
} else { } else {
self.inner.dump_dataset(output, format) self.inner.dump_to_write(format, output)
} }
.map_err(map_serializer_error) .map_err(map_serializer_error)
}) })

Loading…
Cancel
Save