Adds runtime checks to insert_term_values

Might help to spot logic bugs in the future
pull/171/head
Tpt 3 years ago
parent 4c71327c95
commit 0c9a46fcd1
  1. 143
      lib/src/storage/numeric_encoder.rs

@ -716,62 +716,86 @@ pub fn insert_term_values<E, F: Fn(&StrHash, &str) -> Result<(), E> + Copy>(
encoded: &EncodedTerm, encoded: &EncodedTerm,
insert_str: F, insert_str: F,
) -> Result<(), E> { ) -> Result<(), E> {
match (term, encoded) { match term {
(TermRef::NamedNode(node), EncodedTerm::NamedNode { iri_id }) => { TermRef::NamedNode(node) => {
insert_str(iri_id, node.as_str())?; if let EncodedTerm::NamedNode { iri_id } = encoded {
} insert_str(iri_id, node.as_str())
(TermRef::BlankNode(node), EncodedTerm::BigBlankNode { id_id }) => { } else {
insert_str(id_id, node.as_str())?; unreachable!("Invalid term encoding {:?} for {}", encoded, term)
}
(TermRef::Literal(literal), EncodedTerm::SmallBigLangStringLiteral { language_id, .. }) => {
if let Some(language) = literal.language() {
insert_str(language_id, language)?;
} }
} }
( TermRef::BlankNode(node) => match encoded {
TermRef::Literal(literal), EncodedTerm::BigBlankNode { id_id } => insert_str(id_id, node.as_str()),
EncodedTerm::BigSmallLangStringLiteral { value_id, .. } EncodedTerm::SmallBlankNode(..) | EncodedTerm::NumericalBlankNode { .. } => Ok(()),
| EncodedTerm::BigStringLiteral { value_id }, _ => unreachable!("Invalid term encoding {:?} for {}", encoded, term),
) => { },
insert_str(value_id, literal.value())?; TermRef::Literal(literal) => match encoded {
} EncodedTerm::SmallStringLiteral(..) => Ok(()),
( EncodedTerm::BigStringLiteral { value_id } => insert_str(value_id, literal.value()),
TermRef::Literal(literal), EncodedTerm::SmallSmallLangStringLiteral { .. } => Ok(()),
EncodedTerm::SmallBigLangStringLiteral { language_id, .. } => {
if let Some(language) = literal.language() {
insert_str(language_id, language)
} else {
unreachable!("Invalid term encoding {:?} for {}", encoded, term)
}
}
EncodedTerm::BigSmallLangStringLiteral { value_id, .. } => {
insert_str(value_id, literal.value())
}
EncodedTerm::BigBigLangStringLiteral { EncodedTerm::BigBigLangStringLiteral {
value_id, value_id,
language_id, language_id,
}, } => {
) => { insert_str(value_id, literal.value())?;
insert_str(value_id, literal.value())?; if let Some(language) = literal.language() {
if let Some(language) = literal.language() { insert_str(language_id, language)
insert_str(language_id, language)? } else {
unreachable!("Invalid term encoding {:?} for {}", encoded, term)
}
}
EncodedTerm::SmallTypedLiteral { datatype_id, .. } => {
insert_str(datatype_id, literal.datatype().as_str())
} }
}
(TermRef::Literal(literal), EncodedTerm::SmallTypedLiteral { datatype_id, .. }) => {
insert_str(datatype_id, literal.datatype().as_str())?;
}
(
TermRef::Literal(literal),
EncodedTerm::BigTypedLiteral { EncodedTerm::BigTypedLiteral {
value_id, value_id,
datatype_id, datatype_id,
}, } => {
) => { insert_str(value_id, literal.value())?;
insert_str(value_id, literal.value())?; insert_str(datatype_id, literal.datatype().as_str())
insert_str(datatype_id, literal.datatype().as_str())?; }
} EncodedTerm::BooleanLiteral(..) => Ok(()),
(TermRef::Triple(triple), EncodedTerm::Triple(encoded)) => { EncodedTerm::FloatLiteral(..) => Ok(()),
insert_term_values(triple.subject.as_ref().into(), &encoded.subject, insert_str)?; EncodedTerm::DoubleLiteral(..) => Ok(()),
insert_term_values( EncodedTerm::IntegerLiteral(..) => Ok(()),
triple.predicate.as_ref().into(), EncodedTerm::DecimalLiteral(..) => Ok(()),
&encoded.predicate, EncodedTerm::DateTimeLiteral(..) => Ok(()),
insert_str, EncodedTerm::TimeLiteral(..) => Ok(()),
)?; EncodedTerm::DateLiteral(..) => Ok(()),
insert_term_values(triple.object.as_ref(), &encoded.object, insert_str)?; EncodedTerm::GYearMonthLiteral(..) => Ok(()),
EncodedTerm::GYearLiteral(..) => Ok(()),
EncodedTerm::GMonthDayLiteral(..) => Ok(()),
EncodedTerm::GDayLiteral(..) => Ok(()),
EncodedTerm::GMonthLiteral(..) => Ok(()),
EncodedTerm::DurationLiteral(..) => Ok(()),
EncodedTerm::YearMonthDurationLiteral(..) => Ok(()),
EncodedTerm::DayTimeDurationLiteral(..) => Ok(()),
_ => unreachable!("Invalid term encoding {:?} for {}", encoded, term),
},
TermRef::Triple(triple) => {
if let EncodedTerm::Triple(encoded) = encoded {
insert_term_values(triple.subject.as_ref().into(), &encoded.subject, insert_str)?;
insert_term_values(
triple.predicate.as_ref().into(),
&encoded.predicate,
insert_str,
)?;
insert_term_values(triple.object.as_ref(), &encoded.object, insert_str)
} else {
unreachable!("Invalid term encoding {:?} for {}", encoded, term)
}
} }
_ => (),
} }
Ok(())
} }
pub fn remove_term_values<E, F: Fn(&StrHash) -> Result<(), E> + Copy>( pub fn remove_term_values<E, F: Fn(&StrHash) -> Result<(), E> + Copy>(
@ -779,44 +803,33 @@ pub fn remove_term_values<E, F: Fn(&StrHash) -> Result<(), E> + Copy>(
remove_str: F, remove_str: F,
) -> Result<(), E> { ) -> Result<(), E> {
match encoded { match encoded {
EncodedTerm::NamedNode { iri_id } => { EncodedTerm::NamedNode { iri_id } => remove_str(iri_id),
remove_str(iri_id)?; EncodedTerm::BigBlankNode { id_id } => remove_str(id_id),
}
EncodedTerm::BigBlankNode { id_id } => {
remove_str(id_id)?;
}
EncodedTerm::BigStringLiteral { value_id } EncodedTerm::BigStringLiteral { value_id }
| EncodedTerm::BigSmallLangStringLiteral { value_id, .. } => { | EncodedTerm::BigSmallLangStringLiteral { value_id, .. } => remove_str(value_id),
remove_str(value_id)?; EncodedTerm::SmallBigLangStringLiteral { language_id, .. } => remove_str(language_id),
}
EncodedTerm::SmallBigLangStringLiteral { language_id, .. } => {
remove_str(language_id)?;
}
EncodedTerm::BigBigLangStringLiteral { EncodedTerm::BigBigLangStringLiteral {
value_id, value_id,
language_id, language_id,
} => { } => {
remove_str(value_id)?; remove_str(value_id)?;
remove_str(language_id)?; remove_str(language_id)
}
EncodedTerm::SmallTypedLiteral { datatype_id, .. } => {
remove_str(datatype_id)?;
} }
EncodedTerm::SmallTypedLiteral { datatype_id, .. } => remove_str(datatype_id),
EncodedTerm::BigTypedLiteral { EncodedTerm::BigTypedLiteral {
value_id, value_id,
datatype_id, datatype_id,
} => { } => {
remove_str(value_id)?; remove_str(value_id)?;
remove_str(datatype_id)?; remove_str(datatype_id)
} }
EncodedTerm::Triple(encoded) => { EncodedTerm::Triple(encoded) => {
remove_term_values(&encoded.subject, remove_str)?; remove_term_values(&encoded.subject, remove_str)?;
remove_term_values(&encoded.predicate, remove_str)?; remove_term_values(&encoded.predicate, remove_str)?;
remove_term_values(&encoded.object, remove_str)?; remove_term_values(&encoded.object, remove_str)
} }
_ => (), _ => Ok(()),
} }
Ok(())
} }
pub fn parse_boolean_str(value: &str) -> Option<EncodedTerm> { pub fn parse_boolean_str(value: &str) -> Option<EncodedTerm> {

Loading…
Cancel
Save