Move TypedValue into mentat_core. r=jsantell,nalexander

This commit is contained in:
Richard Newman 2017-02-08 13:59:56 -08:00
parent afafcd64a0
commit c111d4daff
7 changed files with 53 additions and 36 deletions

View file

@ -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<f64>),
// 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.
///

View file

@ -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.

View file

@ -290,9 +290,16 @@ pub fn ensure_current_version(conn: &mut rusqlite::Connection) -> Result<i32> {
}
}
impl TypedValue {
pub trait TypedSQLValue {
fn from_sql_value_pair(value: rusqlite::types::Value, value_type_tag: i32) -> Result<TypedValue>;
fn to_sql_value_pair<'a>(&'a self) -> (ToSqlOutput<'a>, i32);
fn from_edn_value(value: &Value) -> Option<TypedValue>;
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<TypedValue> {
fn from_sql_value_pair(value: rusqlite::types::Value, value_type_tag: i32) -> Result<TypedValue> {
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<TypedValue> {
fn from_edn_value(value: &Value) -> Option<TypedValue> {
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),

View file

@ -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.

View file

@ -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;

View file

@ -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<f64>),
// 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 {

View file

@ -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;