Pre: eliminate some occurrences of Rc, largely through the magic of Into.

This commit is contained in:
Richard Newman 2018-04-24 16:08:26 -07:00
parent a2e13f624c
commit 4a42d2ca2c
15 changed files with 90 additions and 68 deletions

View file

@ -53,12 +53,6 @@ impl From<KnownEntid> for Entid {
}
}
impl From<KnownEntid> for Binding {
fn from(k: KnownEntid) -> Binding {
Binding::Scalar(TypedValue::Ref(k.0))
}
}
impl From<KnownEntid> for TypedValue {
fn from(k: KnownEntid) -> TypedValue {
TypedValue::Ref(k.0)
@ -201,22 +195,9 @@ pub enum Binding {
Map(Rc<StructuredMap>),
}
impl From<TypedValue> for Binding {
fn from(src: TypedValue) -> Self {
Binding::Scalar(src)
}
}
impl<'a> From<&'a str> for Binding {
fn from(value: &'a str) -> Binding {
Binding::Scalar(TypedValue::String(Rc::new(value.to_string())))
}
}
impl From<String> for Binding {
fn from(value: String) -> Binding {
Binding::Scalar(TypedValue::String(Rc::new(value)))
impl<T> From<T> for Binding where T: Into<TypedValue> {
fn from(value: T) -> Binding {
Binding::Scalar(value.into())
}
}
@ -296,14 +277,14 @@ impl TypedValue {
/// values and wrapping them in a new `Rc`. This is expensive, so this might
/// be best limited to tests.
pub fn typed_ns_keyword(ns: &str, name: &str) -> TypedValue {
TypedValue::Keyword(Rc::new(NamespacedKeyword::new(ns, name)))
NamespacedKeyword::new(ns, name).into()
}
/// Construct a new `TypedValue::String` instance by cloning the provided
/// value and wrapping it in a new `Rc`. This is expensive, so this might
/// be best limited to tests.
pub fn typed_string(s: &str) -> TypedValue {
TypedValue::String(Rc::new(s.to_string()))
s.into()
}
pub fn current_instant() -> TypedValue {
@ -367,12 +348,24 @@ impl<'a> From<&'a str> for TypedValue {
}
}
impl From<Rc<String>> for TypedValue {
fn from(value: Rc<String>) -> TypedValue {
TypedValue::String(value.clone())
}
}
impl From<String> for TypedValue {
fn from(value: String) -> TypedValue {
TypedValue::String(Rc::new(value))
}
}
impl From<Rc<NamespacedKeyword>> for TypedValue {
fn from(value: Rc<NamespacedKeyword>) -> TypedValue {
TypedValue::Keyword(value.clone())
}
}
impl From<NamespacedKeyword> for TypedValue {
fn from(value: NamespacedKeyword) -> TypedValue {
TypedValue::Keyword(Rc::new(value))

View file

@ -389,7 +389,7 @@ impl TypedSQLValue for TypedValue {
// share a tag.
(5, rusqlite::types::Value::Integer(x)) => Ok(TypedValue::Long(x)),
(5, rusqlite::types::Value::Real(x)) => Ok(TypedValue::Double(x.into())),
(10, rusqlite::types::Value::Text(x)) => Ok(TypedValue::String(Rc::new(x))),
(10, rusqlite::types::Value::Text(x)) => Ok(x.into()),
(11, rusqlite::types::Value::Blob(x)) => {
let u = Uuid::from_bytes(x.as_slice());
if u.is_err() {
@ -400,7 +400,7 @@ impl TypedSQLValue for TypedValue {
Ok(TypedValue::Uuid(u.unwrap()))
},
(13, rusqlite::types::Value::Text(x)) => {
to_namespaced_keyword(&x).map(|k| TypedValue::Keyword(Rc::new(k)))
to_namespaced_keyword(&x).map(|k| k.into())
},
(_, value) => bail!(ErrorKind::BadSQLValuePair(value, value_type_tag)),
}
@ -420,8 +420,8 @@ impl TypedSQLValue for TypedValue {
&Value::Integer(x) => Some(TypedValue::Long(x)),
&Value::Uuid(x) => Some(TypedValue::Uuid(x)),
&Value::Float(ref x) => Some(TypedValue::Double(x.clone())),
&Value::Text(ref x) => Some(TypedValue::String(Rc::new(x.clone()))),
&Value::NamespacedKeyword(ref x) => Some(TypedValue::Keyword(Rc::new(x.clone()))),
&Value::Text(ref x) => Some(x.clone().into()),
&Value::NamespacedKeyword(ref x) => Some(x.clone().into()),
_ => None
}
}

View file

@ -14,7 +14,6 @@
use std::borrow::Borrow;
use std::io::{Write};
use std::rc::Rc;
use itertools::Itertools;
use rusqlite;
@ -112,7 +111,7 @@ trait ToIdent {
impl ToIdent for TypedValue {
fn map_ident(self, schema: &Schema) -> Self {
if let TypedValue::Ref(e) = self {
schema.get_ident(e).cloned().map(|i| TypedValue::Keyword(Rc::new(i))).unwrap_or(TypedValue::Ref(e))
schema.get_ident(e).cloned().map(|i| i.into()).unwrap_or(TypedValue::Ref(e))
} else {
self
}

View file

@ -8,8 +8,6 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
use std::rc::Rc;
use mentat_core::{
HasSchema,
Schema,
@ -166,7 +164,7 @@ impl ConjoiningClauses {
(true, true) => {
// Ambiguous: this could be a keyword or an ident.
// Default to keyword.
Ok(Val(TypedValue::Keyword(Rc::new(x))))
Ok(Val(x.into()))
},
(true, false) => {
// This can only be an ident. Look it up!
@ -176,7 +174,7 @@ impl ConjoiningClauses {
}
},
(false, true) => {
Ok(Val(TypedValue::Keyword(Rc::new(x))))
Ok(Val(TypedValue::Keyword(x.into())))
},
(false, false) => {
Ok(Impossible(EmptyBecause::TypeMismatch {

View file

@ -260,8 +260,6 @@ impl ConjoiningClauses {
mod testing {
use super::*;
use std::rc::Rc;
use mentat_core::{
Attribute,
Schema,
@ -309,7 +307,7 @@ mod testing {
args: vec![
FnArg::SrcVar(SrcVar::DefaultSrc),
FnArg::IdentOrKeyword(NamespacedKeyword::new("foo", "fts")),
FnArg::Constant(NonIntegerConstant::Text(Rc::new("needle".into()))),
FnArg::Constant("needle".into()),
],
binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")),
@ -331,7 +329,7 @@ mod testing {
assert_eq!(clauses.0[1], ColumnConstraint::Equals(QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Value)),
QueryValue::Column(QualifiedAlias("fulltext_values00".to_string(), Column::Fulltext(FulltextColumn::Rowid)))).into());
assert_eq!(clauses.0[2], ColumnConstraint::Matches(QualifiedAlias("fulltext_values00".to_string(), Column::Fulltext(FulltextColumn::Text)),
QueryValue::TypedValue(TypedValue::String(Rc::new("needle".into())))).into());
QueryValue::TypedValue("needle".into())).into());
let bindings = cc.column_bindings;
assert_eq!(bindings.len(), 3);
@ -367,7 +365,7 @@ mod testing {
args: vec![
FnArg::SrcVar(SrcVar::DefaultSrc),
FnArg::IdentOrKeyword(NamespacedKeyword::new("foo", "bar")),
FnArg::Constant(NonIntegerConstant::Text(Rc::new("needle".into()))),
FnArg::Constant("needle".into()),
],
binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")),

View file

@ -1159,7 +1159,7 @@ fn add_attribute(schema: &mut Schema, e: Entid, a: Attribute) {
#[cfg(test)]
pub(crate) fn ident(ns: &str, name: &str) -> PatternNonValuePlace {
PatternNonValuePlace::Ident(::std::rc::Rc::new(NamespacedKeyword::new(ns, name)))
NamespacedKeyword::new(ns, name).into()
}
#[cfg(test)]

View file

@ -88,7 +88,6 @@ impl ConjoiningClauses {
mod testing {
extern crate mentat_query_parser;
use std::rc::Rc;
use std::collections::BTreeSet;
use super::*;
@ -469,7 +468,9 @@ mod testing {
:where [?x :foo/knows "Bill"]
(not [?x :foo/knows ?y])]"#;
let inputs = QueryInputs::with_value_sequence(vec![(Variable::from_valid_name("?y"),TypedValue::String(Rc::new("John".to_string())))]);
let inputs = QueryInputs::with_value_sequence(vec![
(Variable::from_valid_name("?y"), "John".into())
]);
let cc = alg_with_inputs(&schema, query, inputs);
let vx = Variable::from_valid_name("?x");

View file

@ -646,7 +646,6 @@ mod testing {
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::rc::Rc;
use mentat_core::attribute::Unique;
use mentat_core::{
@ -827,7 +826,7 @@ mod testing {
let v = Variable::from_valid_name("?v");
cc.input_variables.insert(a.clone());
cc.value_bindings.insert(a.clone(), TypedValue::Keyword(Rc::new(NamespacedKeyword::new("foo", "bar"))));
cc.value_bindings.insert(a.clone(), TypedValue::typed_ns_keyword("foo", "bar"));
let known = Known::for_schema(&schema);
cc.apply_parsed_pattern(known, Pattern {
source: None,
@ -931,7 +930,7 @@ mod testing {
source: None,
entity: PatternNonValuePlace::Variable(x.clone()),
attribute: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::Constant(NonIntegerConstant::Text(Rc::new("hello".to_string()))),
value: PatternValuePlace::Constant("hello".into()),
tx: PatternNonValuePlace::Placeholder,
});
@ -954,7 +953,7 @@ mod testing {
// - datoms0.value_type_tag = string
// TODO: implement expand_type_tags.
assert_eq!(cc.wheres, vec![
ColumnConstraint::Equals(d0_v, QueryValue::TypedValue(TypedValue::String(Rc::new("hello".to_string())))),
ColumnConstraint::Equals(d0_v, QueryValue::TypedValue(TypedValue::typed_string("hello"))),
ColumnConstraint::has_unit_type("all_datoms00".to_string(), ValueType::String),
].into());
}
@ -982,7 +981,7 @@ mod testing {
source: None,
entity: PatternNonValuePlace::Variable(x.clone()),
attribute: ident("foo", "roz"),
value: PatternValuePlace::Constant(NonIntegerConstant::Text(Rc::new("idgoeshere".to_string()))),
value: PatternValuePlace::Constant("idgoeshere".into()),
tx: PatternNonValuePlace::Placeholder,
});
cc.apply_parsed_pattern(known, Pattern {

View file

@ -120,7 +120,7 @@ mod tests {
};
fn value_ident(ns: &str, name: &str) -> PatternValuePlace {
PatternValuePlace::IdentOrKeyword(::std::rc::Rc::new(NamespacedKeyword::new(ns, name)))
NamespacedKeyword::new(ns, name).into()
}
/// Tests that the top-level form is a valid `or`, returning the clauses.

View file

@ -626,7 +626,7 @@ mod test {
}
fn ident_kw(kw: edn::NamespacedKeyword) -> PatternNonValuePlace {
PatternNonValuePlace::Ident(Rc::new(kw))
PatternNonValuePlace::Ident(kw.into())
}
fn ident(ns: &str, name: &str) -> PatternNonValuePlace {

View file

@ -16,8 +16,6 @@ extern crate mentat_core;
extern crate mentat_query;
extern crate mentat_query_parser;
use std::rc::Rc;
use edn::{
NamespacedKeyword,
PlainSymbol,
@ -164,7 +162,7 @@ fn can_parse_simple_or_join() {
#[cfg(test)]
fn ident(ns: &str, name: &str) -> PatternNonValuePlace {
PatternNonValuePlace::Ident(::std::rc::Rc::new(NamespacedKeyword::new(ns, name)))
NamespacedKeyword::new(ns, name).into()
}
#[test]
@ -283,7 +281,7 @@ fn can_parse_uuid() {
WhereClause::Pattern(
Pattern::new(None,
PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
PatternNonValuePlace::Ident(Rc::new(NamespacedKeyword::new("foo", "baz"))),
NamespacedKeyword::new("foo", "baz").into(),
PatternValuePlace::Constant(NonIntegerConstant::Uuid(expected)),
PatternNonValuePlace::Placeholder)
.expect("valid pattern")));

View file

@ -761,7 +761,7 @@ mod tests {
let c = Constraint::Infix {
op: Op("MATCHES"),
left: ColumnOrExpression::Column(QualifiedAlias("fulltext01".to_string(), Column::Fulltext(FulltextColumn::Text))),
right: ColumnOrExpression::Value(TypedValue::String(Rc::new("needle".to_string()))),
right: ColumnOrExpression::Value("needle".into()),
};
let q = build_query(&c);
assert_eq!("`fulltext01`.text MATCHES $v0", q.sql);

View file

@ -739,7 +739,7 @@ fn test_ground_scalar() {
// Verify that we accept bound input constants.
let query = r#"[:find ?x . :in ?v :where [(ground ?v) ?x]]"#;
let inputs = QueryInputs::with_value_sequence(vec![(Variable::from_valid_name("?v"), TypedValue::String(Rc::new("aaa".into())))]);
let inputs = QueryInputs::with_value_sequence(vec![(Variable::from_valid_name("?v"), "aaa".into())]);
let constant = translate_with_inputs_to_constant(&schema, query, inputs);
assert_eq!(constant.project_without_rows().unwrap()
.into_scalar().unwrap(),
@ -760,7 +760,7 @@ fn test_ground_tuple() {
// Verify that we accept bound input constants.
let query = r#"[:find [?x ?y] :in ?u ?v :where [(ground [?u ?v]) [?x ?y]]]"#;
let inputs = QueryInputs::with_value_sequence(vec![(Variable::from_valid_name("?u"), TypedValue::Long(2)),
(Variable::from_valid_name("?v"), TypedValue::String(Rc::new("aaa".into()))),]);
(Variable::from_valid_name("?v"), "aaa".into()),]);
let constant = translate_with_inputs_to_constant(&schema, query, inputs);
assert_eq!(constant.project_without_rows().unwrap()

View file

@ -216,13 +216,25 @@ impl NonIntegerConstant {
NonIntegerConstant::BigInteger(_) => unimplemented!(), // TODO: #280.
NonIntegerConstant::Boolean(v) => TypedValue::Boolean(v),
NonIntegerConstant::Float(v) => TypedValue::Double(v),
NonIntegerConstant::Text(v) => TypedValue::String(v),
NonIntegerConstant::Text(v) => v.into(),
NonIntegerConstant::Instant(v) => TypedValue::Instant(v),
NonIntegerConstant::Uuid(v) => TypedValue::Uuid(v),
}
}
}
impl<'a> From<&'a str> for NonIntegerConstant {
fn from(val: &'a str) -> NonIntegerConstant {
NonIntegerConstant::Text(Rc::new(val.to_string()))
}
}
impl From<String> for NonIntegerConstant {
fn from(val: String) -> NonIntegerConstant {
NonIntegerConstant::Text(Rc::new(val))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FnArg {
Variable(Variable),
@ -260,7 +272,7 @@ impl FromValue<FnArg> for FnArg {
Some(FnArg::Constant(NonIntegerConstant::BigInteger(x.clone()))),
Text(ref x) =>
// TODO: intern strings. #398.
Some(FnArg::Constant(NonIntegerConstant::Text(Rc::new(x.clone())))),
Some(FnArg::Constant(x.clone().into())),
Nil |
NamespacedSymbol(_) |
Keyword(_) |
@ -315,6 +327,18 @@ pub enum PatternNonValuePlace {
Ident(Rc<NamespacedKeyword>),
}
impl From<Rc<NamespacedKeyword>> for PatternNonValuePlace {
fn from(value: Rc<NamespacedKeyword>) -> Self {
PatternNonValuePlace::Ident(value.clone())
}
}
impl From<NamespacedKeyword> for PatternNonValuePlace {
fn from(value: NamespacedKeyword) -> Self {
PatternNonValuePlace::Ident(Rc::new(value))
}
}
impl PatternNonValuePlace {
// I think we'll want move variants, so let's leave these here for now.
#[allow(dead_code)]
@ -355,7 +379,7 @@ impl FromValue<PatternNonValuePlace> for PatternNonValuePlace {
}
},
edn::SpannedValue::NamespacedKeyword(ref x) =>
Some(PatternNonValuePlace::Ident(Rc::new(x.clone()))),
Some(x.clone().into()),
_ => None,
}
}
@ -379,6 +403,18 @@ pub enum PatternValuePlace {
Constant(NonIntegerConstant),
}
impl From<Rc<NamespacedKeyword>> for PatternValuePlace {
fn from(value: Rc<NamespacedKeyword>) -> Self {
PatternValuePlace::IdentOrKeyword(value.clone())
}
}
impl From<NamespacedKeyword> for PatternValuePlace {
fn from(value: NamespacedKeyword) -> Self {
PatternValuePlace::IdentOrKeyword(Rc::new(value))
}
}
impl FromValue<PatternValuePlace> for PatternValuePlace {
fn from_value(v: &edn::ValueAndSpan) -> Option<PatternValuePlace> {
match v.inner {
@ -389,7 +425,7 @@ impl FromValue<PatternValuePlace> for PatternValuePlace {
edn::SpannedValue::PlainSymbol(ref x) =>
Variable::from_symbol(x).map(PatternValuePlace::Variable),
edn::SpannedValue::NamespacedKeyword(ref x) =>
Some(PatternValuePlace::IdentOrKeyword(Rc::new(x.clone()))),
Some(x.clone().into()),
edn::SpannedValue::Boolean(x) =>
Some(PatternValuePlace::Constant(NonIntegerConstant::Boolean(x))),
edn::SpannedValue::Float(x) =>
@ -400,7 +436,7 @@ impl FromValue<PatternValuePlace> for PatternValuePlace {
Some(PatternValuePlace::Constant(NonIntegerConstant::Instant(x))),
edn::SpannedValue::Text(ref x) =>
// TODO: intern strings. #398.
Some(PatternValuePlace::Constant(NonIntegerConstant::Text(Rc::new(x.clone())))),
Some(PatternValuePlace::Constant(x.clone().into())),
edn::SpannedValue::Uuid(ref u) =>
Some(PatternValuePlace::Constant(NonIntegerConstant::Uuid(u.clone()))),
@ -754,7 +790,7 @@ impl Pattern {
return Some(Pattern {
source: src,
entity: v_e,
attribute: PatternNonValuePlace::Ident(Rc::new(k.to_reversed())),
attribute: k.to_reversed().into(),
value: e_v,
tx: tx,
});

View file

@ -1321,22 +1321,22 @@ fn test_aggregation_implicit_grouping() {
assert_eq!(vals,
vec![
vec![TypedValue::Ref(ids.get("a").cloned().unwrap()),
TypedValue::String("Alice".to_string().into()),
"Alice".into(),
TypedValue::Long(99),
TypedValue::Long(3),
TypedValue::Double((127f64 / 3f64).into())],
vec![TypedValue::Ref(ids.get("b").cloned().unwrap()),
TypedValue::String("Beli".to_string().into()),
"Beli".into(),
TypedValue::Long(22),
TypedValue::Long(2),
TypedValue::Double((33f64 / 2f64).into())],
vec![TypedValue::Ref(ids.get("c").cloned().unwrap()),
TypedValue::String("Carlos".to_string().into()),
"Carlos".into(),
TypedValue::Long(42),
TypedValue::Long(1),
TypedValue::Double(42f64.into())],
vec![TypedValue::Ref(ids.get("d").cloned().unwrap()),
TypedValue::String("Diana".to_string().into()),
"Diana".into(),
TypedValue::Long(28),
TypedValue::Long(2),
TypedValue::Double((33f64 / 2f64).into())]].into());
@ -1454,6 +1454,6 @@ fn test_tx_data() {
}
};
assert_tx_data(&store, &tx1, TypedValue::String("1".to_string().into()));
assert_tx_data(&store, &tx2, TypedValue::String("2".to_string().into()));
assert_tx_data(&store, &tx1, "1".into());
assert_tx_data(&store, &tx2, "2".into());
}