From c111d4daff78bfda9b42b32b1de6785965dda34d Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Wed, 8 Feb 2017 13:59:56 -0800 Subject: [PATCH] Move TypedValue into mentat_core. r=jsantell,nalexander --- core/src/lib.rs | 30 ++++++++++++++++++++++++++++++ db/src/bootstrap.rs | 4 +++- db/src/db.rs | 17 ++++++++++++----- db/src/debug.rs | 4 +++- db/src/lib.rs | 1 + db/src/types.rs | 29 +---------------------------- db/tests/value_tests.rs | 4 +++- 7 files changed, 53 insertions(+), 36 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index aea97538..cd792850 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -8,6 +8,9 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +extern crate ordered_float; +use self::ordered_float::OrderedFloat; + /// Core types defining a Mentat knowledge base. /// Represents one entid in the entid space. @@ -30,6 +33,33 @@ pub enum ValueType { Keyword, } +/// Represents a Mentat value in a particular value set. +// TODO: expand to include :db.type/{instant,url,uuid}. +// TODO: BigInt? +#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] +pub enum TypedValue { + Ref(Entid), + Boolean(bool), + Long(i64), + Double(OrderedFloat), + // TODO: &str throughout? + String(String), + Keyword(String), +} + +impl TypedValue { + pub fn value_type(&self) -> ValueType { + match self { + &TypedValue::Ref(_) => ValueType::Ref, + &TypedValue::Boolean(_) => ValueType::Boolean, + &TypedValue::Long(_) => ValueType::Long, + &TypedValue::Double(_) => ValueType::Double, + &TypedValue::String(_) => ValueType::String, + &TypedValue::Keyword(_) => ValueType::Keyword, + } + } +} + /// A Mentat schema attribute has a value type and several other flags determining how assertions /// with the attribute are interpreted. /// diff --git a/db/src/bootstrap.rs b/db/src/bootstrap.rs index 6999a7b6..b92955ce 100644 --- a/db/src/bootstrap.rs +++ b/db/src/bootstrap.rs @@ -15,9 +15,11 @@ use edn; use edn::types::Value; use entids; use errors::*; +use db::TypedSQLValue; use mentat_tx::entities::Entity; use mentat_tx_parser; -use types::{IdentMap, Partition, PartitionMap, Schema, TypedValue}; +use mentat_core::TypedValue; +use types::{IdentMap, Partition, PartitionMap, Schema}; use values; /// The first transaction ID applied to the knowledge base. diff --git a/db/src/db.rs b/db/src/db.rs index db150f65..299bc3e5 100644 --- a/db/src/db.rs +++ b/db/src/db.rs @@ -290,9 +290,16 @@ pub fn ensure_current_version(conn: &mut rusqlite::Connection) -> Result { } } -impl TypedValue { +pub trait TypedSQLValue { + fn from_sql_value_pair(value: rusqlite::types::Value, value_type_tag: i32) -> Result; + fn to_sql_value_pair<'a>(&'a self) -> (ToSqlOutput<'a>, i32); + fn from_edn_value(value: &Value) -> Option; + fn to_edn_value_pair(&self) -> (Value, ValueType); +} + +impl TypedSQLValue for TypedValue { /// Given a SQLite `value` and a `value_type_tag`, return the corresponding `TypedValue`. - pub fn from_sql_value_pair(value: rusqlite::types::Value, value_type_tag: i32) -> Result { + fn from_sql_value_pair(value: rusqlite::types::Value, value_type_tag: i32) -> Result { match (value_type_tag, value) { (0, rusqlite::types::Value::Integer(x)) => Ok(TypedValue::Ref(x)), (1, rusqlite::types::Value::Integer(x)) => Ok(TypedValue::Boolean(0 != x)), @@ -313,7 +320,7 @@ impl TypedValue { /// EDN values which are not Mentat typed values. /// /// This function is deterministic. - pub fn from_edn_value(value: &Value) -> Option { + fn from_edn_value(value: &Value) -> Option { match value { &Value::Boolean(x) => Some(TypedValue::Boolean(x)), &Value::Integer(x) => Some(TypedValue::Long(x)), @@ -325,7 +332,7 @@ impl TypedValue { } /// Return the corresponding SQLite `value` and `value_type_tag` pair. - pub fn to_sql_value_pair<'a>(&'a self) -> (ToSqlOutput<'a>, i32) { + fn to_sql_value_pair<'a>(&'a self) -> (ToSqlOutput<'a>, i32) { match self { &TypedValue::Ref(x) => (rusqlite::types::Value::Integer(x).into(), 0), &TypedValue::Boolean(x) => (rusqlite::types::Value::Integer(if x { 1 } else { 0 }).into(), 1), @@ -338,7 +345,7 @@ impl TypedValue { } /// Return the corresponding EDN `value` and `value_type` pair. - pub fn to_edn_value_pair(&self) -> (Value, ValueType) { + fn to_edn_value_pair(&self) -> (Value, ValueType) { match self { &TypedValue::Ref(x) => (Value::Integer(x), ValueType::Ref), &TypedValue::Boolean(x) => (Value::Boolean(x), ValueType::Boolean), diff --git a/db/src/debug.rs b/db/src/debug.rs index 8fe480e6..b4f64527 100644 --- a/db/src/debug.rs +++ b/db/src/debug.rs @@ -25,8 +25,10 @@ use bootstrap; use edn; use edn::symbols; use entids; +use mentat_core::TypedValue; use mentat_tx::entities::{Entid}; -use types::{DB, TypedValue}; +use db::TypedSQLValue; +use types::DB; use errors::Result; /// Represents a *datom* (assertion) in the store. diff --git a/db/src/lib.rs b/db/src/lib.rs index c022abc6..fcadcb18 100644 --- a/db/src/lib.rs +++ b/db/src/lib.rs @@ -19,6 +19,7 @@ extern crate time; extern crate tabwriter; extern crate edn; +extern crate mentat_core; extern crate mentat_tx; extern crate mentat_tx_parser; diff --git a/db/src/types.rs b/db/src/types.rs index d24141fc..8e6b3c09 100644 --- a/db/src/types.rs +++ b/db/src/types.rs @@ -11,43 +11,16 @@ #![allow(dead_code)] use std::collections::{BTreeMap}; -use edn::OrderedFloat; extern crate mentat_core; pub use self::mentat_core::{ Entid, ValueType, + TypedValue, Attribute, }; -/// Represents a Mentat value in a particular value set. -// TODO: expand to include :db.type/{instant,url,uuid}. -// TODO: BigInt? -#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] -pub enum TypedValue { - Ref(Entid), - Boolean(bool), - Long(i64), - Double(OrderedFloat), - // TODO: &str throughout? - String(String), - Keyword(String), -} - -impl TypedValue { - pub fn value_type(&self) -> ValueType { - match self { - &TypedValue::Ref(_) => ValueType::Ref, - &TypedValue::Boolean(_) => ValueType::Boolean, - &TypedValue::Long(_) => ValueType::Long, - &TypedValue::Double(_) => ValueType::Double, - &TypedValue::String(_) => ValueType::String, - &TypedValue::Keyword(_) => ValueType::Keyword, - } - } -} - /// Represents one partition of the entid space. #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] pub struct Partition { diff --git a/db/tests/value_tests.rs b/db/tests/value_tests.rs index 755e340a..fb4e3174 100644 --- a/db/tests/value_tests.rs +++ b/db/tests/value_tests.rs @@ -9,11 +9,13 @@ // specific language governing permissions and limitations under the License. extern crate edn; +extern crate mentat_core; extern crate mentat_db; extern crate ordered_float; extern crate rusqlite; -use mentat_db::{TypedValue, ValueType}; +use mentat_core::{TypedValue, ValueType}; +use mentat_db::db::TypedSQLValue; use ordered_float::OrderedFloat; use edn::symbols;