Moves Graph and Dataset traits to the model package

pull/10/head
Tpt 6 years ago
parent fbd5d1f6ed
commit 5b1f37fb90
  1. 151
      src/model/dataset.rs
  2. 4
      src/model/mod.rs
  3. 2
      src/model/triple.rs
  4. 22
      src/sparql/algebra.rs
  5. 16
      src/sparql/sparql_grammar.rustpeg
  6. 1
      src/store/isomorphism.rs
  7. 149
      src/store/mod.rs
  8. 3
      src/store/store.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<Item = Result<Triple>>;
type TriplesForSubjectIterator: Iterator<Item = Result<Triple>>;
type ObjectsForSubjectPredicateIterator: Iterator<Item = Result<Term>>;
type PredicatesForSubjectObjectIterator: Iterator<Item = Result<NamedNode>>;
type TriplesForPredicateIterator: Iterator<Item = Result<Triple>>;
type SubjectsForPredicateObjectIterator: Iterator<Item = Result<NamedOrBlankNode>>;
type TriplesForObjectIterator: Iterator<Item = Result<Triple>>;
fn iter(&self) -> Result<Self::TriplesIterator> {
self.triples()
}
fn triples(&self) -> Result<Self::TriplesIterator>;
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::TriplesForSubjectIterator>;
fn objects_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::ObjectsForSubjectPredicateIterator>;
fn object_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Option<Term>> {
//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<Self::PredicatesForSubjectObjectIterator>;
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<Self::TriplesForPredicateIterator>;
fn subjects_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::SubjectsForPredicateObjectIterator>;
fn triples_for_object(&self, object: &Term) -> Result<Self::TriplesForObjectIterator>;
fn contains(&self, triple: &Triple) -> Result<bool>;
fn insert(&self, triple: &Triple) -> Result<()>;
fn remove(&self, triple: &Triple) -> Result<()>;
fn len(&self) -> Result<usize>;
fn is_empty(&self) -> Result<bool>;
}
/// 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<Item = Result<Quad>>;
type QuadsForSubjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForObjectIterator: Iterator<Item = Result<Quad>>;
fn named_graph(&self, name: &NamedOrBlankNode) -> Result<Self::NamedGraph>;
fn default_graph(&self) -> Self::DefaultGraph;
fn union_graph(&self) -> Self::UnionGraph;
fn iter(&self) -> Result<Self::QuadsIterator> {
self.quads()
}
fn quads(&self) -> Result<Self::QuadsIterator>;
fn quads_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::QuadsForSubjectIterator>;
fn quads_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::QuadsForSubjectPredicateIterator>;
fn quads_for_subject_predicate_object(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForSubjectPredicateObjectIterator>;
fn quads_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<Self::QuadsForSubjectObjectIterator>;
fn quads_for_predicate(&self, predicate: &NamedNode)
-> Result<Self::QuadsForPredicateIterator>;
fn quads_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForPredicateObjectIterator>;
fn quads_for_object(&self, object: &Term) -> Result<Self::QuadsForObjectIterator>;
fn contains(&self, quad: &Quad) -> Result<bool>;
fn insert(&self, quad: &Quad) -> Result<()>;
fn remove(&self, quad: &Quad) -> Result<()>;
fn len(&self) -> Result<usize>;
fn is_empty(&self) -> Result<bool>;
}

@ -1,12 +1,16 @@
///! Implements data structures for https://www.w3.org/TR/rdf11-concepts/ ///! Implements data structures for https://www.w3.org/TR/rdf11-concepts/
///! Inspired by [RDFjs](http://rdf.js.org/) ///! Inspired by [RDFjs](http://rdf.js.org/)
mod blank_node; mod blank_node;
mod dataset;
mod literal; mod literal;
mod named_node; mod named_node;
mod triple; mod triple;
pub mod vocab; pub mod vocab;
pub use model::blank_node::BlankNode; 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::literal::Literal;
pub use model::named_node::NamedNode; pub use model::named_node::NamedNode;
pub use model::triple::NamedOrBlankNode; pub use model::triple::NamedOrBlankNode;

@ -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 { 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) /// 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<NamedOrBlankNode>; fn graph_name(&self) -> &Option<NamedOrBlankNode>;

@ -998,7 +998,7 @@ impl ListPattern {
struct SparqlListPattern<'a> { struct SparqlListPattern<'a> {
algebra: &'a ListPattern, algebra: &'a ListPattern,
dataset: &'a Dataset, dataset: &'a DatasetSpec,
} }
impl<'a> fmt::Display for SparqlListPattern<'a> { 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)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash, Default)]
pub struct Dataset { pub struct DatasetSpec {
pub default: Vec<NamedNode>, pub default: Vec<NamedNode>,
pub named: Vec<NamedNode>, pub named: Vec<NamedNode>,
} }
impl Dataset { impl DatasetSpec {
pub fn new_with_default(graph: NamedNode) -> Self { pub fn new_with_default(graph: NamedNode) -> Self {
Self { Self {
default: vec![graph], default: vec![graph],
@ -1319,17 +1319,17 @@ impl Dataset {
} }
} }
impl Add for Dataset { impl Add for DatasetSpec {
type Output = Self; 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.default.extend_from_slice(&rhs.default);
self.named.extend_from_slice(&rhs.named); self.named.extend_from_slice(&rhs.named);
self self
} }
} }
impl fmt::Display for Dataset { impl fmt::Display for DatasetSpec {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for g in &self.default { for g in &self.default {
write!(f, "FROM {} ", g)?; write!(f, "FROM {} ", g)?;
@ -1342,26 +1342,26 @@ impl fmt::Display for Dataset {
} }
lazy_static! { lazy_static! {
static ref EMPTY_DATASET: Dataset = Dataset::default(); static ref EMPTY_DATASET: DatasetSpec = DatasetSpec::default();
} }
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum Query { pub enum Query {
SelectQuery { SelectQuery {
dataset: Dataset, dataset: DatasetSpec,
algebra: ListPattern, algebra: ListPattern,
}, },
ConstructQuery { ConstructQuery {
construct: Vec<TriplePattern>, construct: Vec<TriplePattern>,
dataset: Dataset, dataset: DatasetSpec,
algebra: ListPattern, algebra: ListPattern,
}, },
DescribeQuery { DescribeQuery {
dataset: Dataset, dataset: DatasetSpec,
algebra: ListPattern, algebra: ListPattern,
}, },
AskQuery { AskQuery {
dataset: Dataset, dataset: DatasetSpec,
algebra: ListPattern, algebra: ListPattern,
}, },
} }

@ -121,19 +121,19 @@ AskQuery -> Query = "ASK"i _ d:DatasetClauses w:WhereClause _ g:GroupClause? _ h
} }
//[13] //[13]
DatasetClause -> Dataset = "FROM"i _ d:(DefaultGraphClause / NamedGraphClause) { d } DatasetClause -> DatasetSpec = "FROM"i _ d:(DefaultGraphClause / NamedGraphClause) { d }
DatasetClauses -> Dataset = d:DatasetClauses_item* { DatasetClauses -> DatasetSpec = d:DatasetClauses_item* {
d.into_iter().fold(Dataset::default(), |mut a, b| a + b) d.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b)
} }
DatasetClauses_item -> Dataset = d:DatasetClause _ { d } DatasetClauses_item -> DatasetSpec = d:DatasetClause _ { d }
//[14] //[14]
DefaultGraphClause -> Dataset = s:SourceSelector { DefaultGraphClause -> DatasetSpec = s:SourceSelector {
Dataset::new_with_default(s) DatasetSpec::new_with_default(s)
} }
//[15] //[15]
NamedGraphClause -> Dataset = "NAMED"i _ s:SourceSelector { NamedGraphClause -> DatasetSpec = "NAMED"i _ s:SourceSelector {
Dataset::new_with_named(s) DatasetSpec::new_with_named(s)
} }
//[16] //[16]

@ -6,7 +6,6 @@ use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::Hash; use std::hash::Hash;
use std::hash::Hasher; use std::hash::Hasher;
use store::Graph;
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)] #[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]
struct SubjectPredicate { struct SubjectPredicate {

@ -4,155 +4,6 @@ mod numeric_encoder;
mod rocksdb; mod rocksdb;
mod store; mod store;
use errors::*;
use model::*;
pub use store::memory::MemoryDataset; pub use store::memory::MemoryDataset;
pub use store::memory::MemoryGraph; pub use store::memory::MemoryGraph;
pub use store::rocksdb::RocksDbDataset; pub use store::rocksdb::RocksDbDataset;
pub trait Graph {
type TriplesIterator: Iterator<Item = Result<Triple>>;
type TriplesForSubjectIterator: Iterator<Item = Result<Triple>>;
type ObjectsForSubjectPredicateIterator: Iterator<Item = Result<Term>>;
type PredicatesForSubjectObjectIterator: Iterator<Item = Result<NamedNode>>;
type TriplesForPredicateIterator: Iterator<Item = Result<Triple>>;
type SubjectsForPredicateObjectIterator: Iterator<Item = Result<NamedOrBlankNode>>;
type TriplesForObjectIterator: Iterator<Item = Result<Triple>>;
fn iter(&self) -> Result<Self::TriplesIterator> {
self.triples()
}
fn triples(&self) -> Result<Self::TriplesIterator>;
fn triples_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::TriplesForSubjectIterator>;
fn objects_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::ObjectsForSubjectPredicateIterator>;
fn object_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Option<Term>> {
//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<Self::PredicatesForSubjectObjectIterator>;
fn triples_for_predicate(
&self,
predicate: &NamedNode,
) -> Result<Self::TriplesForPredicateIterator>;
fn subjects_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::SubjectsForPredicateObjectIterator>;
fn triples_for_object(&self, object: &Term) -> Result<Self::TriplesForObjectIterator>;
fn contains(&self, triple: &Triple) -> Result<bool>;
fn insert(&self, triple: &Triple) -> Result<()>;
fn remove(&self, triple: &Triple) -> Result<()>;
fn len(&self) -> Result<usize>;
fn is_empty(&self) -> Result<bool>;
}
pub trait NamedGraph: Graph {
fn name(&self) -> &NamedOrBlankNode;
}
pub trait Dataset {
type NamedGraph: NamedGraph;
type DefaultGraph: Graph;
type UnionGraph: Graph;
type QuadsIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForSubjectObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateIterator: Iterator<Item = Result<Quad>>;
type QuadsForPredicateObjectIterator: Iterator<Item = Result<Quad>>;
type QuadsForObjectIterator: Iterator<Item = Result<Quad>>;
fn named_graph(&self, name: &NamedOrBlankNode) -> Result<Self::NamedGraph>;
fn default_graph(&self) -> Self::DefaultGraph;
fn union_graph(&self) -> Self::UnionGraph;
fn iter(&self) -> Result<Self::QuadsIterator> {
self.quads()
}
fn quads(&self) -> Result<Self::QuadsIterator>;
fn quads_for_subject(
&self,
subject: &NamedOrBlankNode,
) -> Result<Self::QuadsForSubjectIterator>;
fn quads_for_subject_predicate(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
) -> Result<Self::QuadsForSubjectPredicateIterator>;
fn quads_for_subject_predicate_object(
&self,
subject: &NamedOrBlankNode,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForSubjectPredicateObjectIterator>;
fn quads_for_subject_object(
&self,
subject: &NamedOrBlankNode,
object: &Term,
) -> Result<Self::QuadsForSubjectObjectIterator>;
fn quads_for_predicate(&self, predicate: &NamedNode)
-> Result<Self::QuadsForPredicateIterator>;
fn quads_for_predicate_object(
&self,
predicate: &NamedNode,
object: &Term,
) -> Result<Self::QuadsForPredicateObjectIterator>;
fn quads_for_object(&self, object: &Term) -> Result<Self::QuadsForObjectIterator>;
fn contains(&self, quad: &Quad) -> Result<bool>;
fn insert(&self, quad: &Quad) -> Result<()>;
fn remove(&self, quad: &Quad) -> Result<()>;
fn len(&self) -> Result<usize>;
fn is_empty(&self) -> Result<bool>;
}

@ -5,9 +5,6 @@ use std::iter::FromIterator;
use std::iter::Iterator; use std::iter::Iterator;
use std::sync::Arc; use std::sync::Arc;
use store::numeric_encoder::*; 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 /// Defines the Store traits that is used to have efficient binary storage

Loading…
Cancel
Save