Deprecates Triple and Quad methods

It's much simpler to expose the inner fields
pull/41/head
Tpt 4 years ago
parent 71aa5a6c79
commit 2d80960e79
  1. 21
      js/src/model.rs
  2. 95
      lib/src/model/triple.rs
  3. 6
      lib/src/sparql/eval.rs
  4. 22
      lib/src/store/numeric_encoder.rs
  5. 2
      testsuite/src/manifest.rs
  6. 16
      testsuite/src/sparql_evaluator.rs

@ -441,12 +441,11 @@ impl JsQuad {
impl From<Quad> for JsQuad { impl From<Quad> for JsQuad {
fn from(quad: Quad) -> Self { fn from(quad: Quad) -> Self {
let (s, p, o, g) = quad.destruct();
Self { Self {
subject: s.into(), subject: quad.subject.into(),
predicate: p.into(), predicate: quad.predicate.into(),
object: o.into(), object: quad.object.into(),
graph: if let Some(g) = g { graph: if let Some(g) = quad.graph_name {
g.into() g.into()
} else { } else {
JsTerm::DefaultGraph(JsDefaultGraph {}) JsTerm::DefaultGraph(JsDefaultGraph {})
@ -459,11 +458,11 @@ impl TryFrom<JsQuad> for Quad {
type Error = JsValue; type Error = JsValue;
fn try_from(quad: JsQuad) -> Result<Self, JsValue> { fn try_from(quad: JsQuad) -> Result<Self, JsValue> {
Ok(Quad::new( Ok(Quad {
NamedOrBlankNode::try_from(quad.subject)?, subject: NamedOrBlankNode::try_from(quad.subject)?,
NamedNode::try_from(quad.predicate)?, predicate: NamedNode::try_from(quad.predicate)?,
Term::try_from(quad.object)?, object: Term::try_from(quad.object)?,
match quad.graph { graph_name: match quad.graph {
JsTerm::NamedNode(node) => Some(NamedOrBlankNode::from(NamedNode::from(node))), JsTerm::NamedNode(node) => Some(NamedOrBlankNode::from(NamedNode::from(node))),
JsTerm::BlankNode(node) => Some(NamedOrBlankNode::from(BlankNode::from(node))), JsTerm::BlankNode(node) => Some(NamedOrBlankNode::from(BlankNode::from(node))),
JsTerm::Literal(literal) => { JsTerm::Literal(literal) => {
@ -474,7 +473,7 @@ impl TryFrom<JsQuad> for Quad {
} }
JsTerm::DefaultGraph(_) => None, JsTerm::DefaultGraph(_) => None,
}, },
)) })
} }
} }

@ -142,9 +142,14 @@ impl<'a> From<&'a Term> for rio::Term<'a> {
/// A [RDF triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) /// A [RDF triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple)
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Triple { pub struct Triple {
subject: NamedOrBlankNode, /// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple
predicate: NamedNode, pub subject: NamedOrBlankNode,
object: Term,
/// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple
pub predicate: NamedNode,
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple
pub object: Term,
} }
impl Triple { impl Triple {
@ -161,32 +166,32 @@ impl Triple {
} }
} }
/// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple #[deprecated(note = "Use directly the `subject` field")]
pub const fn subject(&self) -> &NamedOrBlankNode { pub const fn subject(&self) -> &NamedOrBlankNode {
&self.subject &self.subject
} }
/// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple #[deprecated(note = "Use directly the `subject` field")]
pub fn subject_owned(self) -> NamedOrBlankNode { pub fn subject_owned(self) -> NamedOrBlankNode {
self.subject self.subject
} }
/// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple #[deprecated(note = "Use directly the `predicate` field")]
pub const fn predicate(&self) -> &NamedNode { pub const fn predicate(&self) -> &NamedNode {
&self.predicate &self.predicate
} }
/// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple #[deprecated(note = "Use directly the `predicate` field")]
pub fn predicate_owned(self) -> NamedNode { pub fn predicate_owned(self) -> NamedNode {
self.predicate self.predicate
} }
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple #[deprecated(note = "Use directly the `object` field")]
pub const fn object(&self) -> &Term { pub const fn object(&self) -> &Term {
&self.object &self.object
} }
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple #[deprecated(note = "Use directly the `object` field")]
pub fn object_owned(self) -> Term { pub fn object_owned(self) -> Term {
self.object self.object
} }
@ -204,16 +209,16 @@ impl Triple {
impl fmt::Display for Triple { impl fmt::Display for Triple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {} {} .", self.subject, self.predicate, self.object) rio::Triple::from(self).fmt(f)
} }
} }
impl<'a> From<&'a Triple> for rio::Triple<'a> { impl<'a> From<&'a Triple> for rio::Triple<'a> {
fn from(node: &'a Triple) -> Self { fn from(node: &'a Triple) -> Self {
rio::Triple { rio::Triple {
subject: node.subject().into(), subject: (&node.subject).into(),
predicate: node.predicate().into(), predicate: (&node.predicate).into(),
object: node.object().into(), object: (&node.object).into(),
} }
} }
} }
@ -221,10 +226,18 @@ impl<'a> From<&'a Triple> for rio::Triple<'a> {
/// A [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) /// A [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset)
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Quad { pub struct Quad {
subject: NamedOrBlankNode, /// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple
predicate: NamedNode, pub subject: NamedOrBlankNode,
object: Term,
graph_name: Option<NamedOrBlankNode>, /// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple
pub predicate: NamedNode,
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple
pub object: Term,
/// 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)
pub graph_name: Option<NamedOrBlankNode>,
} }
impl Quad { impl Quad {
@ -243,52 +256,52 @@ impl Quad {
} }
} }
/// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple #[deprecated(note = "Use directly the `subject` field")]
pub const fn subject(&self) -> &NamedOrBlankNode { pub const fn subject(&self) -> &NamedOrBlankNode {
&self.subject &self.subject
} }
/// The [subject](https://www.w3.org/TR/rdf11-concepts/#dfn-subject) of this triple #[deprecated(note = "Use directly the `subject` field")]
pub fn subject_owned(self) -> NamedOrBlankNode { pub fn subject_owned(self) -> NamedOrBlankNode {
self.subject self.subject
} }
/// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple #[deprecated(note = "Use directly the `predicate` field")]
pub const fn predicate(&self) -> &NamedNode { pub const fn predicate(&self) -> &NamedNode {
&self.predicate &self.predicate
} }
/// The [predicate](https://www.w3.org/TR/rdf11-concepts/#dfn-predicate) of this triple #[deprecated(note = "Use directly the `predicate` field")]
pub fn predicate_owned(self) -> NamedNode { pub fn predicate_owned(self) -> NamedNode {
self.predicate self.predicate
} }
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple #[deprecated(note = "Use directly the `object` field")]
pub const fn object(&self) -> &Term { pub const fn object(&self) -> &Term {
&self.object &self.object
} }
/// The [object](https://www.w3.org/TR/rdf11-concepts/#dfn-object) of this triple #[deprecated(note = "Use directly the `object` field")]
pub fn object_owned(self) -> Term { pub fn object_owned(self) -> Term {
self.object self.object
} }
/// 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) #[deprecated(note = "Use directly the `graph_name` field")]
pub const fn graph_name(&self) -> &Option<NamedOrBlankNode> { pub const fn graph_name(&self) -> &Option<NamedOrBlankNode> {
&self.graph_name &self.graph_name
} }
/// 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) #[deprecated(note = "Use directly the `graph_name` field")]
pub fn graph_name_owned(self) -> Option<NamedOrBlankNode> { pub fn graph_name_owned(self) -> Option<NamedOrBlankNode> {
self.graph_name self.graph_name
} }
/// Returns the underlying [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) #[deprecated(note = "Use `Triple::from` instead")]
pub fn into_triple(self) -> Triple { pub fn into_triple(self) -> Triple {
Triple::new(self.subject, self.predicate, self.object) Triple::new(self.subject, self.predicate, self.object)
} }
/// Extract components from this quad #[deprecated(note = "Use directly the struct fields")]
pub fn destruct(self) -> (NamedOrBlankNode, NamedNode, Term, Option<NamedOrBlankNode>) { pub fn destruct(self) -> (NamedOrBlankNode, NamedNode, Term, Option<NamedOrBlankNode>) {
(self.subject, self.predicate, self.object, self.graph_name) (self.subject, self.predicate, self.object, self.graph_name)
} }
@ -296,13 +309,27 @@ impl Quad {
impl fmt::Display for Quad { impl fmt::Display for Quad {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.graph_name { rio::Quad::from(self).fmt(f)
Some(ref graph_name) => write!( }
f, }
"{} {} {} {} .",
self.subject, self.predicate, self.object, graph_name impl<'a> From<&'a Quad> for rio::Quad<'a> {
), fn from(node: &'a Quad) -> Self {
None => write!(f, "{} {} {} .", self.subject, self.predicate, self.object), rio::Quad {
subject: (&node.subject).into(),
predicate: (&node.predicate).into(),
object: (&node.object).into(),
graph_name: node.graph_name.as_ref().map(|g| g.into()),
}
}
}
impl From<Quad> for Triple {
fn from(quad: Quad) -> Self {
Self {
subject: quad.subject,
predicate: quad.predicate,
object: quad.object,
} }
} }
} }

@ -2210,11 +2210,7 @@ impl<'a, S: ReadableEncodedStore + 'a> Iterator for DescribeIterator<'a, S> {
loop { loop {
if let Some(quad) = self.quads.next() { if let Some(quad) = self.quads.next() {
return Some(match quad { return Some(match quad {
Ok(quad) => self Ok(quad) => self.eval.dataset.decode_quad(&quad).map(|q| q.into()),
.eval
.dataset
.decode_quad(&quad)
.map(|q| q.into_triple()),
Err(error) => Err(error), Err(error) => Err(error),
}); });
} }

@ -543,11 +543,11 @@ impl EncodedQuad {
impl From<&Quad> for EncodedQuad { impl From<&Quad> for EncodedQuad {
fn from(quad: &Quad) -> Self { fn from(quad: &Quad) -> Self {
Self { Self {
subject: quad.subject().into(), subject: (&quad.subject).into(),
predicate: quad.predicate().into(), predicate: (&quad.predicate).into(),
object: quad.object().into(), object: (&quad.object).into(),
graph_name: quad graph_name: quad
.graph_name() .graph_name
.as_ref() .as_ref()
.map_or(ENCODED_DEFAULT_GRAPH, |g| g.into()), .map_or(ENCODED_DEFAULT_GRAPH, |g| g.into()),
} }
@ -932,10 +932,10 @@ pub trait Encoder {
fn encode_quad(&mut self, quad: &Quad) -> Result<EncodedQuad> { fn encode_quad(&mut self, quad: &Quad) -> Result<EncodedQuad> {
Ok(EncodedQuad { Ok(EncodedQuad {
subject: self.encode_named_or_blank_node(quad.subject())?, subject: self.encode_named_or_blank_node(&quad.subject)?,
predicate: self.encode_named_node(quad.predicate())?, predicate: self.encode_named_node(&quad.predicate)?,
object: self.encode_term(quad.object())?, object: self.encode_term(&quad.object)?,
graph_name: match quad.graph_name() { graph_name: match &quad.graph_name {
Some(graph_name) => self.encode_named_or_blank_node(graph_name)?, Some(graph_name) => self.encode_named_or_blank_node(graph_name)?,
None => ENCODED_DEFAULT_GRAPH, None => ENCODED_DEFAULT_GRAPH,
}, },
@ -948,9 +948,9 @@ pub trait Encoder {
graph_name: EncodedTerm, graph_name: EncodedTerm,
) -> Result<EncodedQuad> { ) -> Result<EncodedQuad> {
Ok(EncodedQuad { Ok(EncodedQuad {
subject: self.encode_named_or_blank_node(triple.subject())?, subject: self.encode_named_or_blank_node(&triple.subject)?,
predicate: self.encode_named_node(triple.predicate())?, predicate: self.encode_named_node(&triple.predicate)?,
object: self.encode_term(triple.object())?, object: self.encode_term(&triple.object)?,
graph_name, graph_name,
}) })
} }

@ -270,5 +270,5 @@ fn objects_for_subject_predicate(
) -> impl Iterator<Item = Term> { ) -> impl Iterator<Item = Term> {
store store
.quads_for_pattern(Some(subject), Some(predicate), None, None) .quads_for_pattern(Some(subject), Some(predicate), None, None)
.map(|t| t.object_owned()) .map(|t| t.object)
} }

@ -394,12 +394,12 @@ impl StaticQueryResults {
Some(&rs::RESULT_SET.clone().into()), Some(&rs::RESULT_SET.clone().into()),
None, None,
) )
.map(|q| q.subject_owned()) .map(|q| q.subject)
.next() .next()
{ {
if let Some(bool) = dataset if let Some(bool) = dataset
.quads_for_pattern(Some(&result_set), Some(&rs::BOOLEAN), None, None) .quads_for_pattern(Some(&result_set), Some(&rs::BOOLEAN), None, None)
.map(|q| q.object_owned()) .map(|q| q.object)
.next() .next()
{ {
// Boolean query // Boolean query
@ -409,7 +409,7 @@ impl StaticQueryResults {
let mut variables: Vec<Variable> = dataset let mut variables: Vec<Variable> = dataset
.quads_for_pattern(Some(&result_set), Some(&rs::RESULT_VARIABLE), None, None) .quads_for_pattern(Some(&result_set), Some(&rs::RESULT_VARIABLE), None, None)
.filter_map(|q| { .filter_map(|q| {
if let Term::Literal(l) = q.object_owned() { if let Term::Literal(l) = q.object {
Some(Variable::new(l.value())) Some(Variable::new(l.value()))
} else { } else {
None None
@ -421,12 +421,12 @@ impl StaticQueryResults {
let mut solutions: Vec<_> = dataset let mut solutions: Vec<_> = dataset
.quads_for_pattern(Some(&result_set), Some(&rs::SOLUTION), None, None) .quads_for_pattern(Some(&result_set), Some(&rs::SOLUTION), None, None)
.filter_map(|q| { .filter_map(|q| {
if let Term::BlankNode(solution) = q.object_owned() { if let Term::BlankNode(solution) = q.object {
let solution = solution.into(); let solution = solution.into();
let mut bindings = dataset let mut bindings = dataset
.quads_for_pattern(Some(&solution), Some(&rs::BINDING), None, None) .quads_for_pattern(Some(&solution), Some(&rs::BINDING), None, None)
.filter_map(|q| { .filter_map(|q| {
if let Term::BlankNode(binding) = q.object_owned() { if let Term::BlankNode(binding) = q.object {
let binding = binding.into(); let binding = binding.into();
if let (Some(Term::Literal(variable)), Some(value)) = ( if let (Some(Term::Literal(variable)), Some(value)) = (
dataset dataset
@ -436,7 +436,7 @@ impl StaticQueryResults {
None, None,
None, None,
) )
.map(|q| q.object_owned()) .map(|q| q.object)
.next(), .next(),
dataset dataset
.quads_for_pattern( .quads_for_pattern(
@ -445,7 +445,7 @@ impl StaticQueryResults {
None, None,
None, None,
) )
.map(|q| q.object_owned()) .map(|q| q.object)
.next(), .next(),
) { ) {
Some((Variable::new(variable.value()), value)) Some((Variable::new(variable.value()), value))
@ -461,7 +461,7 @@ impl StaticQueryResults {
let index = dataset let index = dataset
.quads_for_pattern(Some(&solution), Some(&rs::INDEX), None, None) .quads_for_pattern(Some(&solution), Some(&rs::INDEX), None, None)
.filter_map(|q| { .filter_map(|q| {
if let Term::Literal(l) = q.object_owned() { if let Term::Literal(l) = q.object {
u64::from_str(l.value()).ok() u64::from_str(l.value()).ok()
} else { } else {
None None

Loading…
Cancel
Save