diff --git a/cli/src/main.rs b/cli/src/main.rs index 2a2f6562..4ab05892 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -806,9 +806,9 @@ fn dump( ) -> anyhow::Result { 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 { - store.dump_graph(write, format, from_graph_name) + store.dump_graph_to_write(from_graph_name, format, write) } else { - store.dump_dataset(write, format) + store.dump_to_write(format, write) }?) } diff --git a/js/src/store.rs b/js/src/store.rs index f76786a5..bb0af9e7 100644 --- a/js/src/store.rs +++ b/js/src/store.rs @@ -177,10 +177,13 @@ impl JsStore { let format = rdf_format(format)?; let buffer = if let Some(from_graph_name) = FROM_JS.with(|c| c.to_optional_term(from_graph_name))? { - self.store - .dump_graph(Vec::new(), format, &GraphName::try_from(from_graph_name)?) + self.store.dump_graph_to_write( + &GraphName::try_from(from_graph_name)?, + format, + Vec::new(), + ) } else { - self.store.dump_dataset(Vec::new(), format) + self.store.dump_to_write(format, Vec::new()) } .map_err(to_err)?; String::from_utf8(buffer).map_err(to_err) diff --git a/lib/oxrdfio/src/parser.rs b/lib/oxrdfio/src/parser.rs index b9aaf646..1b69f955 100644 --- a/lib/oxrdfio/src/parser.rs +++ b/lib/oxrdfio/src/parser.rs @@ -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. /// /// ``` diff --git a/lib/oxrdfio/src/serializer.rs b/lib/oxrdfio/src/serializer.rs index 487b4539..797bdf90 100644 --- a/lib/oxrdfio/src/serializer.rs +++ b/lib/oxrdfio/src/serializer.rs @@ -60,6 +60,17 @@ impl RdfSerializer { 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. /// ///
Do not forget to run the [`finish`](ToWriteQuadWriter::finish()) method to properly write the last bytes of the file.
diff --git a/lib/src/io/format.rs b/lib/src/io/format.rs index b100d392..e6ff7b3c 100644 --- a/lib/src/io/format.rs +++ b/lib/src/io/format.rs @@ -1,6 +1,6 @@ #![allow(deprecated)] -use oxrdfio::RdfFormat; +use oxrdfio::{RdfFormat, RdfParser, RdfSerializer}; /// [RDF graph](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-graph) serialization formats. /// @@ -108,6 +108,7 @@ impl GraphFormat { } impl From for RdfFormat { + #[inline] fn from(format: GraphFormat) -> Self { match format { GraphFormat::NTriples => Self::NTriples, @@ -117,6 +118,20 @@ impl From for RdfFormat { } } +impl From for RdfParser { + #[inline] + fn from(format: GraphFormat) -> Self { + RdfFormat::from(format).into() + } +} + +impl From 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. /// /// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future. @@ -215,6 +230,7 @@ impl DatasetFormat { } impl From for RdfFormat { + #[inline] fn from(format: DatasetFormat) -> Self { match format { DatasetFormat::NQuads => Self::NQuads, @@ -223,6 +239,20 @@ impl From for RdfFormat { } } +impl From for RdfParser { + #[inline] + fn from(format: DatasetFormat) -> Self { + RdfFormat::from(format).into() + } +} + +impl From for RdfSerializer { + #[inline] + fn from(format: DatasetFormat) -> Self { + RdfFormat::from(format).into() + } +} + impl TryFrom for GraphFormat { type Error = (); diff --git a/lib/src/store.rs b/lib/src/store.rs index e78a137b..4ff084f6 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -639,6 +639,37 @@ impl Store { self.transaction(move |mut t| t.remove(quad)) } + /// Dumps the store into a file. + /// + /// ``` + /// use oxigraph::store::Store; + /// use oxigraph::io::RdfFormat; + /// + /// let file = " .\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( + &self, + serializer: impl Into, + write: W, + ) -> Result { + 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. /// /// Usage example: @@ -653,23 +684,51 @@ impl Store { /// store.load_graph(file, RdfFormat::NTriples, GraphName::DefaultGraph, None)?; /// /// 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()); /// # std::io::Result::Ok(()) /// ``` - pub fn dump_graph<'a, W: Write>( + pub fn dump_graph_to_write<'a, W: Write>( &self, - write: W, - format: impl Into, from_graph_name: impl Into>, + serializer: impl Into, + write: W, ) -> Result { - 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())) { writer.write_triple(quad?.as_ref())?; } 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 = " .\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, + from_graph_name: impl Into>, + ) -> Result { + self.dump_graph_to_write(from_graph_name, format.into(), write) + } + /// Dumps the store into a file. /// /// ``` @@ -679,26 +738,19 @@ impl Store { /// let file = " .\n".as_bytes(); /// /// 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)?; /// assert_eq!(file, buffer.as_slice()); /// # std::io::Result::Ok(()) /// ``` + #[deprecated(note = "use Store.dump_to_write instead", since = "0.4.0")] pub fn dump_dataset( &self, write: W, format: impl Into, ) -> Result { - let format = format.into(); - 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()?) + self.dump_to_write(format.into(), write) } /// 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. /// 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"))] pub fn backup(&self, target_directory: impl AsRef) -> Result<(), StorageError> { self.storage.backup(target_directory.as_ref()) @@ -894,7 +946,7 @@ impl Store { /// /// // quads file insertion /// let file = b" ."; - /// 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 /// let ex = NamedNodeRef::new("http://example.com")?; @@ -1467,7 +1519,7 @@ impl Iterator for GraphNameIter { /// /// // quads file insertion /// let file = b" ."; -/// 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 /// let ex = NamedNodeRef::new("http://example.com")?; @@ -1532,7 +1584,7 @@ impl BulkLoader { /// 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. /// ///
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. diff --git a/lib/tests/store.rs b/lib/tests/store.rs index 070a5351..e7e074d9 100644 --- a/lib/tests/store.rs +++ b/lib/tests/store.rs @@ -202,7 +202,7 @@ fn test_dump_graph() -> Result<(), Box> { } 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!( buffer.into_iter().filter(|c| *c == b'\n').count(), NUMBER_OF_TRIPLES @@ -217,7 +217,7 @@ fn test_dump_dataset() -> Result<(), Box> { store.insert(q)?; } - let buffer = store.dump_dataset(Vec::new(), RdfFormat::NQuads)?; + let buffer = store.dump_to_write(RdfFormat::NQuads, Vec::new())?; assert_eq!( buffer.into_iter().filter(|c| *c == b'\n').count(), NUMBER_OF_TRIPLES diff --git a/python/src/store.rs b/python/src/store.rs index 6068296d..8923b1f6 100644 --- a/python/src/store.rs +++ b/python/src/store.rs @@ -542,9 +542,10 @@ impl PyStore { py.allow_threads(|| { let format = lookup_rdf_format(format, file_path.as_deref())?; 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 { - self.inner.dump_dataset(output, format) + self.inner.dump_to_write(format, output) } .map_err(map_serializer_error) })