From 5b1f37fb90b7d4ec8c05644c5691ee335be7db5a Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 17 Sep 2018 15:14:21 +0200 Subject: [PATCH] Moves Graph and Dataset traits to the model package --- src/model/dataset.rs | 151 ++++++++++++++++++++++++++++++ src/model/mod.rs | 4 + src/model/triple.rs | 2 +- src/sparql/algebra.rs | 22 ++--- src/sparql/sparql_grammar.rustpeg | 16 ++-- src/store/isomorphism.rs | 1 - src/store/mod.rs | 149 ----------------------------- src/store/store.rs | 3 - 8 files changed, 175 insertions(+), 173 deletions(-) create mode 100644 src/model/dataset.rs diff --git a/src/model/dataset.rs b/src/model/dataset.rs new file mode 100644 index 00000000..fb2009bb --- /dev/null +++ b/src/model/dataset.rs @@ -0,0 +1,151 @@ +use errors::*; +use model::*; + +/// Trait for [RDF graphs](https://www.w3.org/TR/rdf11-concepts/#dfn-graph) +pub trait Graph { + type TriplesIterator: Iterator>; + type TriplesForSubjectIterator: Iterator>; + type ObjectsForSubjectPredicateIterator: Iterator>; + type PredicatesForSubjectObjectIterator: Iterator>; + type TriplesForPredicateIterator: Iterator>; + type SubjectsForPredicateObjectIterator: Iterator>; + type TriplesForObjectIterator: Iterator>; + + fn iter(&self) -> Result { + self.triples() + } + + fn triples(&self) -> Result; + + fn triples_for_subject( + &self, + subject: &NamedOrBlankNode, + ) -> Result; + + fn objects_for_subject_predicate( + &self, + subject: &NamedOrBlankNode, + predicate: &NamedNode, + ) -> Result; + + fn object_for_subject_predicate( + &self, + subject: &NamedOrBlankNode, + predicate: &NamedNode, + ) -> Result> { + //TODO use transpose when stable + match self + .objects_for_subject_predicate(subject, predicate)? + .nth(0) + { + Some(object) => Ok(Some(object?)), + None => Ok(None), + } + } + + fn predicates_for_subject_object( + &self, + subject: &NamedOrBlankNode, + object: &Term, + ) -> Result; + + fn triples_for_predicate( + &self, + predicate: &NamedNode, + ) -> Result; + + fn subjects_for_predicate_object( + &self, + predicate: &NamedNode, + object: &Term, + ) -> Result; + + fn triples_for_object(&self, object: &Term) -> Result; + + fn contains(&self, triple: &Triple) -> Result; + + fn insert(&self, triple: &Triple) -> Result<()>; + + fn remove(&self, triple: &Triple) -> Result<()>; + + fn len(&self) -> Result; + + fn is_empty(&self) -> Result; +} + +/// Trait for [RDF named graphs](https://www.w3.org/TR/rdf11-concepts/#dfn-named-graph) i.e. RDF graphs identified by an IRI +pub trait NamedGraph: Graph { + fn name(&self) -> &NamedOrBlankNode; +} + +/// Trait for [RDF datasets](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) +pub trait Dataset { + type NamedGraph: NamedGraph; + type DefaultGraph: Graph; + type UnionGraph: Graph; + type QuadsIterator: Iterator>; + type QuadsForSubjectIterator: Iterator>; + type QuadsForSubjectPredicateIterator: Iterator>; + type QuadsForSubjectPredicateObjectIterator: Iterator>; + type QuadsForSubjectObjectIterator: Iterator>; + type QuadsForPredicateIterator: Iterator>; + type QuadsForPredicateObjectIterator: Iterator>; + type QuadsForObjectIterator: Iterator>; + + fn named_graph(&self, name: &NamedOrBlankNode) -> Result; + + fn default_graph(&self) -> Self::DefaultGraph; + + fn union_graph(&self) -> Self::UnionGraph; + + fn iter(&self) -> Result { + self.quads() + } + + fn quads(&self) -> Result; + + fn quads_for_subject( + &self, + subject: &NamedOrBlankNode, + ) -> Result; + + fn quads_for_subject_predicate( + &self, + subject: &NamedOrBlankNode, + predicate: &NamedNode, + ) -> Result; + + fn quads_for_subject_predicate_object( + &self, + subject: &NamedOrBlankNode, + predicate: &NamedNode, + object: &Term, + ) -> Result; + + fn quads_for_subject_object( + &self, + subject: &NamedOrBlankNode, + object: &Term, + ) -> Result; + + fn quads_for_predicate(&self, predicate: &NamedNode) + -> Result; + + fn quads_for_predicate_object( + &self, + predicate: &NamedNode, + object: &Term, + ) -> Result; + + fn quads_for_object(&self, object: &Term) -> Result; + + fn contains(&self, quad: &Quad) -> Result; + + fn insert(&self, quad: &Quad) -> Result<()>; + + fn remove(&self, quad: &Quad) -> Result<()>; + + fn len(&self) -> Result; + + fn is_empty(&self) -> Result; +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 444963f5..88364339 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,12 +1,16 @@ ///! Implements data structures for https://www.w3.org/TR/rdf11-concepts/ ///! Inspired by [RDFjs](http://rdf.js.org/) mod blank_node; +mod dataset; mod literal; mod named_node; mod triple; pub mod vocab; pub use model::blank_node::BlankNode; +pub use model::dataset::Dataset; +pub use model::dataset::Graph; +pub use model::dataset::NamedGraph; pub use model::literal::Literal; pub use model::named_node::NamedNode; pub use model::triple::NamedOrBlankNode; diff --git a/src/model/triple.rs b/src/model/triple.rs index 458eeddd..633d3ba4 100644 --- a/src/model/triple.rs +++ b/src/model/triple.rs @@ -204,7 +204,7 @@ impl TripleLike for Triple { } } -/// The interface of [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) that are in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) +/// The interface of containers that looks like [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) that are in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) pub trait QuadLike: TripleLike { /// The name of the RDF [graph](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-graph) in which the triple is or None if it is in the [default graph](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph) fn graph_name(&self) -> &Option; diff --git a/src/sparql/algebra.rs b/src/sparql/algebra.rs index a46aff3b..e180a9e1 100644 --- a/src/sparql/algebra.rs +++ b/src/sparql/algebra.rs @@ -998,7 +998,7 @@ impl ListPattern { struct SparqlListPattern<'a> { algebra: &'a ListPattern, - dataset: &'a Dataset, + dataset: &'a DatasetSpec, } impl<'a> fmt::Display for SparqlListPattern<'a> { @@ -1298,12 +1298,12 @@ impl<'a> fmt::Display for SparqlOrderComparator<'a> { } #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash, Default)] -pub struct Dataset { +pub struct DatasetSpec { pub default: Vec, pub named: Vec, } -impl Dataset { +impl DatasetSpec { pub fn new_with_default(graph: NamedNode) -> Self { Self { default: vec![graph], @@ -1319,17 +1319,17 @@ impl Dataset { } } -impl Add for Dataset { +impl Add for DatasetSpec { type Output = Self; - fn add(mut self, rhs: Dataset) -> Self { + fn add(mut self, rhs: DatasetSpec) -> Self { self.default.extend_from_slice(&rhs.default); self.named.extend_from_slice(&rhs.named); self } } -impl fmt::Display for Dataset { +impl fmt::Display for DatasetSpec { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for g in &self.default { write!(f, "FROM {} ", g)?; @@ -1342,26 +1342,26 @@ impl fmt::Display for Dataset { } lazy_static! { - static ref EMPTY_DATASET: Dataset = Dataset::default(); + static ref EMPTY_DATASET: DatasetSpec = DatasetSpec::default(); } #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] pub enum Query { SelectQuery { - dataset: Dataset, + dataset: DatasetSpec, algebra: ListPattern, }, ConstructQuery { construct: Vec, - dataset: Dataset, + dataset: DatasetSpec, algebra: ListPattern, }, DescribeQuery { - dataset: Dataset, + dataset: DatasetSpec, algebra: ListPattern, }, AskQuery { - dataset: Dataset, + dataset: DatasetSpec, algebra: ListPattern, }, } diff --git a/src/sparql/sparql_grammar.rustpeg b/src/sparql/sparql_grammar.rustpeg index 1c6091b6..3f6bab11 100644 --- a/src/sparql/sparql_grammar.rustpeg +++ b/src/sparql/sparql_grammar.rustpeg @@ -121,19 +121,19 @@ AskQuery -> Query = "ASK"i _ d:DatasetClauses w:WhereClause _ g:GroupClause? _ h } //[13] -DatasetClause -> Dataset = "FROM"i _ d:(DefaultGraphClause / NamedGraphClause) { d } -DatasetClauses -> Dataset = d:DatasetClauses_item* { - d.into_iter().fold(Dataset::default(), |mut a, b| a + b) +DatasetClause -> DatasetSpec = "FROM"i _ d:(DefaultGraphClause / NamedGraphClause) { d } +DatasetClauses -> DatasetSpec = d:DatasetClauses_item* { + d.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b) } -DatasetClauses_item -> Dataset = d:DatasetClause _ { d } +DatasetClauses_item -> DatasetSpec = d:DatasetClause _ { d } //[14] -DefaultGraphClause -> Dataset = s:SourceSelector { - Dataset::new_with_default(s) +DefaultGraphClause -> DatasetSpec = s:SourceSelector { + DatasetSpec::new_with_default(s) } //[15] -NamedGraphClause -> Dataset = "NAMED"i _ s:SourceSelector { - Dataset::new_with_named(s) +NamedGraphClause -> DatasetSpec = "NAMED"i _ s:SourceSelector { + DatasetSpec::new_with_named(s) } //[16] diff --git a/src/store/isomorphism.rs b/src/store/isomorphism.rs index 4da28db9..3b4d3949 100644 --- a/src/store/isomorphism.rs +++ b/src/store/isomorphism.rs @@ -6,7 +6,6 @@ use std::collections::HashMap; use std::collections::HashSet; use std::hash::Hash; use std::hash::Hasher; -use store::Graph; #[derive(Eq, PartialEq, Hash, Ord, PartialOrd)] struct SubjectPredicate { diff --git a/src/store/mod.rs b/src/store/mod.rs index b8c81d73..ef240169 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -4,155 +4,6 @@ mod numeric_encoder; mod rocksdb; mod store; -use errors::*; -use model::*; - pub use store::memory::MemoryDataset; pub use store::memory::MemoryGraph; pub use store::rocksdb::RocksDbDataset; - -pub trait Graph { - type TriplesIterator: Iterator>; - type TriplesForSubjectIterator: Iterator>; - type ObjectsForSubjectPredicateIterator: Iterator>; - type PredicatesForSubjectObjectIterator: Iterator>; - type TriplesForPredicateIterator: Iterator>; - type SubjectsForPredicateObjectIterator: Iterator>; - type TriplesForObjectIterator: Iterator>; - - fn iter(&self) -> Result { - self.triples() - } - - fn triples(&self) -> Result; - - fn triples_for_subject( - &self, - subject: &NamedOrBlankNode, - ) -> Result; - - fn objects_for_subject_predicate( - &self, - subject: &NamedOrBlankNode, - predicate: &NamedNode, - ) -> Result; - - fn object_for_subject_predicate( - &self, - subject: &NamedOrBlankNode, - predicate: &NamedNode, - ) -> Result> { - //TODO use transpose when stable - match self - .objects_for_subject_predicate(subject, predicate)? - .nth(0) - { - Some(object) => Ok(Some(object?)), - None => Ok(None), - } - } - - fn predicates_for_subject_object( - &self, - subject: &NamedOrBlankNode, - object: &Term, - ) -> Result; - - fn triples_for_predicate( - &self, - predicate: &NamedNode, - ) -> Result; - - fn subjects_for_predicate_object( - &self, - predicate: &NamedNode, - object: &Term, - ) -> Result; - - fn triples_for_object(&self, object: &Term) -> Result; - - fn contains(&self, triple: &Triple) -> Result; - - fn insert(&self, triple: &Triple) -> Result<()>; - - fn remove(&self, triple: &Triple) -> Result<()>; - - fn len(&self) -> Result; - - fn is_empty(&self) -> Result; -} - -pub trait NamedGraph: Graph { - fn name(&self) -> &NamedOrBlankNode; -} - -pub trait Dataset { - type NamedGraph: NamedGraph; - type DefaultGraph: Graph; - type UnionGraph: Graph; - type QuadsIterator: Iterator>; - type QuadsForSubjectIterator: Iterator>; - type QuadsForSubjectPredicateIterator: Iterator>; - type QuadsForSubjectPredicateObjectIterator: Iterator>; - type QuadsForSubjectObjectIterator: Iterator>; - type QuadsForPredicateIterator: Iterator>; - type QuadsForPredicateObjectIterator: Iterator>; - type QuadsForObjectIterator: Iterator>; - - fn named_graph(&self, name: &NamedOrBlankNode) -> Result; - - fn default_graph(&self) -> Self::DefaultGraph; - - fn union_graph(&self) -> Self::UnionGraph; - - fn iter(&self) -> Result { - self.quads() - } - - fn quads(&self) -> Result; - - fn quads_for_subject( - &self, - subject: &NamedOrBlankNode, - ) -> Result; - - fn quads_for_subject_predicate( - &self, - subject: &NamedOrBlankNode, - predicate: &NamedNode, - ) -> Result; - - fn quads_for_subject_predicate_object( - &self, - subject: &NamedOrBlankNode, - predicate: &NamedNode, - object: &Term, - ) -> Result; - - fn quads_for_subject_object( - &self, - subject: &NamedOrBlankNode, - object: &Term, - ) -> Result; - - fn quads_for_predicate(&self, predicate: &NamedNode) - -> Result; - - fn quads_for_predicate_object( - &self, - predicate: &NamedNode, - object: &Term, - ) -> Result; - - fn quads_for_object(&self, object: &Term) -> Result; - - fn contains(&self, quad: &Quad) -> Result; - - fn insert(&self, quad: &Quad) -> Result<()>; - - fn remove(&self, quad: &Quad) -> Result<()>; - - fn len(&self) -> Result; - - fn is_empty(&self) -> Result; -} diff --git a/src/store/store.rs b/src/store/store.rs index b029a11b..63e0eb8f 100644 --- a/src/store/store.rs +++ b/src/store/store.rs @@ -5,9 +5,6 @@ use std::iter::FromIterator; use std::iter::Iterator; use std::sync::Arc; use store::numeric_encoder::*; -use store::Dataset; -use store::Graph; -use store::NamedGraph; /// Defines the Store traits that is used to have efficient binary storage