|
|
|
@ -7,6 +7,7 @@ use oxrdf::Variable; |
|
|
|
|
use oxrdf::*; |
|
|
|
|
use std::collections::BTreeMap; |
|
|
|
|
use std::io::{self, BufRead, Write}; |
|
|
|
|
use std::mem::take; |
|
|
|
|
|
|
|
|
|
/// This limit is set in order to avoid stack overflow error when parsing nested triples due to too many recursive calls.
|
|
|
|
|
/// The actual limit value is a wet finger compromise between not failing to parse valid files and avoiding to trigger stack overflow errors.
|
|
|
|
@ -138,6 +139,8 @@ impl<R: BufRead> JsonQueryResultsReader<R> { |
|
|
|
|
let mut reader = JsonReader::from_reader(source); |
|
|
|
|
let mut buffer = Vec::default(); |
|
|
|
|
let mut variables = None; |
|
|
|
|
let mut buffered_bindings: Option<Vec<_>> = None; |
|
|
|
|
let mut output_iter = None; |
|
|
|
|
|
|
|
|
|
if reader.read_event(&mut buffer)? != JsonEvent::StartObject { |
|
|
|
|
return Err(SyntaxError::msg("SPARQL JSON results should be an object").into()); |
|
|
|
@ -148,7 +151,24 @@ impl<R: BufRead> JsonQueryResultsReader<R> { |
|
|
|
|
match event { |
|
|
|
|
JsonEvent::ObjectKey(key) => match key { |
|
|
|
|
"head" => { |
|
|
|
|
variables = Some(read_head(&mut reader, &mut buffer)?); |
|
|
|
|
let extracted_variables = read_head(&mut reader, &mut buffer)?; |
|
|
|
|
if let Some(buffered_bindings) = buffered_bindings.take() { |
|
|
|
|
let mut mapping = BTreeMap::default(); |
|
|
|
|
for (i, var) in extracted_variables.iter().enumerate() { |
|
|
|
|
mapping.insert(var.as_str().to_string(), i); |
|
|
|
|
} |
|
|
|
|
output_iter = Some(Self::Solutions { |
|
|
|
|
variables: extracted_variables, |
|
|
|
|
solutions: JsonSolutionsReader { |
|
|
|
|
kind: JsonSolutionsReaderKind::Buffered { |
|
|
|
|
bindings: buffered_bindings.into_iter(), |
|
|
|
|
}, |
|
|
|
|
mapping, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
variables = Some(extracted_variables); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
"results" => { |
|
|
|
|
if reader.read_event(&mut buffer)? != JsonEvent::StartObject { |
|
|
|
@ -169,24 +189,45 @@ impl<R: BufRead> JsonQueryResultsReader<R> { |
|
|
|
|
if reader.read_event(&mut buffer)? != JsonEvent::StartArray { |
|
|
|
|
return Err(SyntaxError::msg("'bindings' should be an object").into()); |
|
|
|
|
} |
|
|
|
|
return if let Some(variables) = variables { |
|
|
|
|
if let Some(variables) = variables { |
|
|
|
|
let mut mapping = BTreeMap::default(); |
|
|
|
|
for (i, var) in variables.iter().enumerate() { |
|
|
|
|
mapping.insert(var.as_str().to_string(), i); |
|
|
|
|
} |
|
|
|
|
Ok(Self::Solutions { |
|
|
|
|
return Ok(Self::Solutions { |
|
|
|
|
variables, |
|
|
|
|
solutions: JsonSolutionsReader { |
|
|
|
|
reader, |
|
|
|
|
buffer, |
|
|
|
|
kind: JsonSolutionsReaderKind::Streaming { reader, buffer }, |
|
|
|
|
mapping, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
Err(SyntaxError::msg( |
|
|
|
|
"SPARQL tuple query results should contain a head key", |
|
|
|
|
// We buffer all results before being able to read the header
|
|
|
|
|
let mut bindings = Vec::new(); |
|
|
|
|
let mut variables = Vec::new(); |
|
|
|
|
let mut values = Vec::new(); |
|
|
|
|
loop { |
|
|
|
|
match reader.read_event(&mut buffer)? { |
|
|
|
|
JsonEvent::StartObject => (), |
|
|
|
|
JsonEvent::EndObject => { |
|
|
|
|
bindings.push((take(&mut variables), take(&mut values))); |
|
|
|
|
} |
|
|
|
|
JsonEvent::EndArray | JsonEvent::Eof => { |
|
|
|
|
buffered_bindings = Some(bindings); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
JsonEvent::ObjectKey(key) => { |
|
|
|
|
variables.push(key.to_string()); |
|
|
|
|
values.push(read_value(&mut reader, &mut buffer, 0)?); |
|
|
|
|
} |
|
|
|
|
_ => { |
|
|
|
|
return Err(SyntaxError::msg( |
|
|
|
|
"Invalid result serialization", |
|
|
|
|
) |
|
|
|
|
.into()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
"boolean" => { |
|
|
|
@ -203,18 +244,17 @@ impl<R: BufRead> JsonQueryResultsReader<R> { |
|
|
|
|
.into()); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
JsonEvent::EndObject => { |
|
|
|
|
return Err(SyntaxError::msg( |
|
|
|
|
"SPARQL results should contain a bindings key or a boolean key", |
|
|
|
|
) |
|
|
|
|
.into()) |
|
|
|
|
} |
|
|
|
|
JsonEvent::EndObject => (), |
|
|
|
|
JsonEvent::Eof => { |
|
|
|
|
return Err(SyntaxError::msg( |
|
|
|
|
return if let Some(output_iter) = output_iter { |
|
|
|
|
Ok(output_iter) |
|
|
|
|
} else { |
|
|
|
|
Err(SyntaxError::msg( |
|
|
|
|
"Unexpected end of JSON object without 'results' or 'boolean' key", |
|
|
|
|
) |
|
|
|
|
.into()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
_ => return Err(SyntaxError::msg("Invalid SPARQL results serialization").into()), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -222,16 +262,27 @@ impl<R: BufRead> JsonQueryResultsReader<R> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct JsonSolutionsReader<R: BufRead> { |
|
|
|
|
mapping: BTreeMap<String, usize>, |
|
|
|
|
kind: JsonSolutionsReaderKind<R>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
enum JsonSolutionsReaderKind<R: BufRead> { |
|
|
|
|
Streaming { |
|
|
|
|
reader: JsonReader<R>, |
|
|
|
|
buffer: Vec<u8>, |
|
|
|
|
mapping: BTreeMap<String, usize>, |
|
|
|
|
}, |
|
|
|
|
Buffered { |
|
|
|
|
bindings: std::vec::IntoIter<(Vec<String>, Vec<Term>)>, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
pub fn read_next(&mut self) -> Result<Option<Vec<Option<Term>>>, ParseError> { |
|
|
|
|
match &mut self.kind { |
|
|
|
|
JsonSolutionsReaderKind::Streaming { reader, buffer } => { |
|
|
|
|
let mut new_bindings = vec![None; self.mapping.len()]; |
|
|
|
|
loop { |
|
|
|
|
match self.reader.read_event(&mut self.buffer)? { |
|
|
|
|
match reader.read_event(buffer)? { |
|
|
|
|
JsonEvent::StartObject => (), |
|
|
|
|
JsonEvent::EndObject => return Ok(Some(new_bindings)), |
|
|
|
|
JsonEvent::EndArray | JsonEvent::Eof => return Ok(None), |
|
|
|
@ -241,14 +292,37 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
"The variable {key} has not been defined in the header" |
|
|
|
|
)) |
|
|
|
|
})?; |
|
|
|
|
new_bindings[k] = Some(self.read_value(0)?) |
|
|
|
|
new_bindings[k] = Some(read_value(reader, buffer, 0)?) |
|
|
|
|
} |
|
|
|
|
_ => return Err(SyntaxError::msg("Invalid result serialization").into()), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
JsonSolutionsReaderKind::Buffered { bindings } => { |
|
|
|
|
Ok(if let Some((variables, values)) = bindings.next() { |
|
|
|
|
let mut new_bindings = vec![None; self.mapping.len()]; |
|
|
|
|
for (variable, value) in variables.into_iter().zip(values) { |
|
|
|
|
let k = *self.mapping.get(&variable).ok_or_else(|| { |
|
|
|
|
SyntaxError::msg(format!( |
|
|
|
|
"The variable {variable} has not been defined in the header" |
|
|
|
|
)) |
|
|
|
|
})?; |
|
|
|
|
new_bindings[k] = Some(value) |
|
|
|
|
} |
|
|
|
|
Some(new_bindings) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn read_value(&mut self, number_of_recursive_calls: usize) -> Result<Term, ParseError> { |
|
|
|
|
fn read_value<R: BufRead>( |
|
|
|
|
reader: &mut JsonReader<R>, |
|
|
|
|
buffer: &mut Vec<u8>, |
|
|
|
|
number_of_recursive_calls: usize, |
|
|
|
|
) -> Result<Term, ParseError> { |
|
|
|
|
if number_of_recursive_calls == MAX_NUMBER_OF_NESTED_TRIPLES { |
|
|
|
|
return Err(SyntaxError::msg(format!( |
|
|
|
|
"Too many nested triples ({MAX_NUMBER_OF_NESTED_TRIPLES}). The parser fails here to avoid a stack overflow." |
|
|
|
@ -280,24 +354,28 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
let mut predicate = None; |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
let mut object = None; |
|
|
|
|
if self.reader.read_event(&mut self.buffer)? != JsonEvent::StartObject { |
|
|
|
|
if reader.read_event(buffer)? != JsonEvent::StartObject { |
|
|
|
|
return Err(SyntaxError::msg("Term serializations should be an object").into()); |
|
|
|
|
} |
|
|
|
|
loop { |
|
|
|
|
match self.reader.read_event(&mut self.buffer)? { |
|
|
|
|
match reader.read_event(buffer)? { |
|
|
|
|
JsonEvent::ObjectKey(key) => match key { |
|
|
|
|
"type" => state = Some(State::Type), |
|
|
|
|
"value" => state = Some(State::Value), |
|
|
|
|
"xml:lang" => state = Some(State::Lang), |
|
|
|
|
"datatype" => state = Some(State::Datatype), |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
"subject" => subject = Some(self.read_value(number_of_recursive_calls + 1)?), |
|
|
|
|
"subject" => { |
|
|
|
|
subject = Some(read_value(reader, buffer, number_of_recursive_calls + 1)?) |
|
|
|
|
} |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
"predicate" => { |
|
|
|
|
predicate = Some(self.read_value(number_of_recursive_calls + 1)?) |
|
|
|
|
predicate = Some(read_value(reader, buffer, number_of_recursive_calls + 1)?) |
|
|
|
|
} |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
"object" => object = Some(self.read_value(number_of_recursive_calls + 1)?), |
|
|
|
|
"object" => { |
|
|
|
|
object = Some(read_value(reader, buffer, number_of_recursive_calls + 1)?) |
|
|
|
|
} |
|
|
|
|
_ => { |
|
|
|
|
return Err(SyntaxError::msg(format!( |
|
|
|
|
"Unexpected key in term serialization: '{key}'" |
|
|
|
@ -307,10 +385,9 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
}, |
|
|
|
|
JsonEvent::StartObject => { |
|
|
|
|
if state != Some(State::Value) { |
|
|
|
|
return Err(SyntaxError::msg( |
|
|
|
|
"Unexpected nested object in term serialization", |
|
|
|
|
) |
|
|
|
|
.into()); |
|
|
|
|
return Err( |
|
|
|
|
SyntaxError::msg("Unexpected nested object in term serialization").into(), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
JsonEvent::String(s) => match state { |
|
|
|
@ -322,10 +399,9 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
"triple" => t = Some(Type::Triple), |
|
|
|
|
_ => { |
|
|
|
|
return Err(SyntaxError::msg(format!( |
|
|
|
|
"Unexpected term type: '{s}'" |
|
|
|
|
)) |
|
|
|
|
.into()) |
|
|
|
|
return Err( |
|
|
|
|
SyntaxError::msg(format!("Unexpected term type: '{s}'")).into() |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
state = None; |
|
|
|
@ -339,10 +415,10 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
state = None; |
|
|
|
|
} |
|
|
|
|
Some(State::Datatype) => { |
|
|
|
|
datatype = |
|
|
|
|
Some(NamedNode::new(s).map_err(|e| { |
|
|
|
|
SyntaxError::msg(format!("Invalid datatype IRI: {e}")) |
|
|
|
|
})?); |
|
|
|
|
datatype = Some( |
|
|
|
|
NamedNode::new(s) |
|
|
|
|
.map_err(|e| SyntaxError::msg(format!("Invalid datatype IRI: {e}")))?, |
|
|
|
|
); |
|
|
|
|
state = None; |
|
|
|
|
} |
|
|
|
|
_ => (), // impossible
|
|
|
|
@ -352,10 +428,9 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
if s == State::Value { |
|
|
|
|
state = None; //End of triple
|
|
|
|
|
} else { |
|
|
|
|
return Err(SyntaxError::msg( |
|
|
|
|
"Term description values should be string", |
|
|
|
|
) |
|
|
|
|
.into()); |
|
|
|
|
return Err( |
|
|
|
|
SyntaxError::msg("Term description values should be string").into() |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
return match t { |
|
|
|
@ -375,9 +450,7 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
.into()), |
|
|
|
|
Some(Type::Literal) => { |
|
|
|
|
let value = value.ok_or_else(|| { |
|
|
|
|
SyntaxError::msg( |
|
|
|
|
"literal serialization should have a 'value' key", |
|
|
|
|
) |
|
|
|
|
SyntaxError::msg("literal serialization should have a 'value' key") |
|
|
|
|
})?; |
|
|
|
|
Ok(match lang { |
|
|
|
|
Some(lang) => { |
|
|
|
@ -403,9 +476,7 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
#[cfg(feature = "rdf-star")] |
|
|
|
|
Some(Type::Triple) => Ok(Triple::new( |
|
|
|
|
match subject.ok_or_else(|| { |
|
|
|
|
SyntaxError::msg( |
|
|
|
|
"triple serialization should have a 'subject' key", |
|
|
|
|
) |
|
|
|
|
SyntaxError::msg("triple serialization should have a 'subject' key") |
|
|
|
|
})? { |
|
|
|
|
Term::NamedNode(subject) => subject.into(), |
|
|
|
|
Term::BlankNode(subject) => subject.into(), |
|
|
|
@ -431,9 +502,7 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
object.ok_or_else(|| { |
|
|
|
|
SyntaxError::msg( |
|
|
|
|
"triple serialization should have a 'object' key", |
|
|
|
|
) |
|
|
|
|
SyntaxError::msg("triple serialization should have a 'object' key") |
|
|
|
|
})?, |
|
|
|
|
) |
|
|
|
|
.into()), |
|
|
|
@ -444,7 +513,6 @@ impl<R: BufRead> JsonSolutionsReader<R> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn read_head<R: BufRead>( |
|
|
|
|
reader: &mut JsonReader<R>, |
|
|
|
|