diff options
| -rw-r--r-- | log_db/benches/utils.rs | 8 | ||||
| -rw-r--r-- | log_db/src/common.rs | 44 | ||||
| -rw-r--r-- | log_db/src/config.rs | 2 | ||||
| -rw-r--r-- | log_db/src/engine.rs | 10 | ||||
| -rw-r--r-- | log_db/src/lib.rs | 14 | ||||
| -rw-r--r-- | log_db/src/record.rs | 22 | ||||
| -rw-r--r-- | log_db/tests/integration.rs | 27 |
7 files changed, 61 insertions, 66 deletions
diff --git a/log_db/benches/utils.rs b/log_db/benches/utils.rs index b9b641e..ed114ea 100644 --- a/log_db/benches/utils.rs +++ b/log_db/benches/utils.rs @@ -18,11 +18,11 @@ pub struct Inst { } impl Recordable for Inst { type Field = Field; - fn schema() -> Vec<(Field, ValueType)> { + fn schema() -> Vec<(Field, Type)> { vec![ - (Field::Id, ValueType::int()), - (Field::Name, ValueType::string()), - (Field::Data, ValueType::bytes()), + (Field::Id, Type::int()), + (Field::Name, Type::string()), + (Field::Data, Type::bytes()), ] } fn primary_key() -> Self::Field { diff --git a/log_db/src/common.rs b/log_db/src/common.rs index 09de2d7..6b7cac8 100644 --- a/log_db/src/common.rs +++ b/log_db/src/common.rs @@ -210,7 +210,7 @@ pub enum IndexableValue { /// A primitive type #[derive(Debug, Clone)] -pub enum PrimValueType { +pub enum PrimitiveType { Int, Decimal, String, @@ -219,36 +219,36 @@ pub enum PrimValueType { /// A primitive type + a nullability bit #[derive(Debug, Clone)] -pub struct ValueType { - pub prim_value_type: PrimValueType, +pub struct Type { + pub primitive: PrimitiveType, pub nullable: bool, } -impl ValueType { +impl Type { pub fn int() -> Self { - ValueType { - prim_value_type: PrimValueType::Int, + Type { + primitive: PrimitiveType::Int, nullable: false, } } pub fn decimal() -> Self { - ValueType { - prim_value_type: PrimValueType::Decimal, + Type { + primitive: PrimitiveType::Decimal, nullable: false, } } pub fn string() -> Self { - ValueType { - prim_value_type: PrimValueType::String, + Type { + primitive: PrimitiveType::String, nullable: false, } } pub fn bytes() -> Self { - ValueType { - prim_value_type: PrimValueType::Bytes, + Type { + primitive: PrimitiveType::Bytes, nullable: false, } } @@ -364,37 +364,37 @@ impl Value { } } -pub fn type_check(value: &Value, value_type: &ValueType) -> bool { +pub fn type_check(value: &Value, value_type: &Type) -> bool { match (value, value_type) { ( Value::Int(_), - ValueType { - prim_value_type: PrimValueType::Int, + Type { + primitive: PrimitiveType::Int, .. }, ) => true, ( Value::Decimal(_), - ValueType { - prim_value_type: PrimValueType::Decimal, + Type { + primitive: PrimitiveType::Decimal, .. }, ) => true, ( Value::Bytes(_), - ValueType { - prim_value_type: PrimValueType::Bytes, + Type { + primitive: PrimitiveType::Bytes, .. }, ) => true, ( Value::String(_), - ValueType { - prim_value_type: PrimValueType::String, + Type { + primitive: PrimitiveType::String, .. }, ) => true, - (Value::Null, ValueType { nullable: true, .. }) => true, + (Value::Null, Type { nullable: true, .. }) => true, _ => false, } } diff --git a/log_db/src/config.rs b/log_db/src/config.rs index daf0983..40e0f3c 100644 --- a/log_db/src/config.rs +++ b/log_db/src/config.rs @@ -74,7 +74,7 @@ impl<R: Recordable> ConfigBuilder<R> { #[derive(Clone)] pub struct Config<R: Recordable> { - pub fields: Vec<(R::Field, ValueType)>, + pub fields: Vec<(R::Field, Type)>, pub primary_key: R::Field, pub secondary_keys: Vec<R::Field>, pub data_dir: String, diff --git a/log_db/src/engine.rs b/log_db/src/engine.rs index ef73db2..2858bab 100644 --- a/log_db/src/engine.rs +++ b/log_db/src/engine.rs @@ -78,8 +78,8 @@ impl<R: Recordable> Engine<R> { DBError::ValidationError("Key must be present in the field schema".to_owned()), )?; - match value_type.prim_value_type { - PrimValueType::Int | PrimValueType::String => {} + match value_type.primitive { + PrimitiveType::Int | PrimitiveType::String => {} _ => return Err(DBError::ValidationError("Key must be indexable".to_owned())), } } @@ -454,9 +454,9 @@ impl<R: Recordable> Engine<R> { ) -> DBResult<Vec<Record>> { fn range_bound_to_indexable( bound: Bound<&Value>, - field_type: &ValueType, + field_type: &Type, ) -> DBResult<Bound<IndexableValue>> { - fn convert(value: &Value, field_type: &ValueType) -> DBResult<IndexableValue> { + fn convert(value: &Value, field_type: &Type) -> DBResult<IndexableValue> { if !type_check(&value, field_type) { return Err(DBError::ValidationError(format!( "Queried value does not match type: {:?}", @@ -737,7 +737,7 @@ impl<R: Recordable> Engine<R> { } #[inline] - fn get_field_type(&self, field: &R::Field) -> Option<&ValueType> { + fn get_field_type(&self, field: &R::Field) -> Option<&Type> { self.config .fields .iter() diff --git a/log_db/src/lib.rs b/log_db/src/lib.rs index 9f2cac9..f7a7e47 100644 --- a/log_db/src/lib.rs +++ b/log_db/src/lib.rs @@ -20,10 +20,11 @@ mod memtable_primary; mod memtable_secondary; mod record; -pub use common::*; +pub use common::{DBError, DBResult, Type, Value}; pub use config::{ReadConsistency, WriteDurability}; pub use record::Recordable; +use common::*; use config::*; use engine::*; use log_reader_forward::*; @@ -198,8 +199,8 @@ mod tests { impl Recordable for TestInst1 { type Field = Field; - fn schema() -> Vec<(Field, ValueType)> { - vec![(Field::Id, ValueType::int())] + fn schema() -> Vec<(Field, Type)> { + vec![(Field::Id, Type::int())] } fn primary_key() -> Self::Field { Field::Id @@ -252,11 +253,8 @@ mod tests { } } - fn schema() -> Vec<(Field, ValueType)> { - vec![ - (Field::Id, ValueType::int()), - (Field::Name, ValueType::string()), - ] + fn schema() -> Vec<(Field, Type)> { + vec![(Field::Id, Type::int()), (Field::Name, Type::string())] } } diff --git a/log_db/src/record.rs b/log_db/src/record.rs index cbb1faa..a464520 100644 --- a/log_db/src/record.rs +++ b/log_db/src/record.rs @@ -49,7 +49,7 @@ impl Record { &self.values[index] } - pub fn validate<Field: Eq>(&self, schema: &Vec<(Field, ValueType)>) -> DBResult<()> { + pub fn validate<Field: Eq>(&self, schema: &Vec<(Field, Type)>) -> DBResult<()> { // Validate the record length if self.values.len() != schema.len() { return Err(DBError::ValidationError(format!( @@ -64,36 +64,36 @@ impl Record { match (&self.values[i], field) { ( Value::Null, - ValueType { + Type { nullable: true, - prim_value_type: _, + primitive: _, }, ) => {} ( Value::Int(_), - ValueType { - prim_value_type: PrimValueType::Int, + Type { + primitive: PrimitiveType::Int, .. }, ) => {} ( Value::String(_), - ValueType { - prim_value_type: PrimValueType::String, + Type { + primitive: PrimitiveType::String, .. }, ) => {} ( Value::Bytes(_), - ValueType { - prim_value_type: PrimValueType::Bytes, + Type { + primitive: PrimitiveType::Bytes, .. }, ) => {} _ => { return Err(DBError::ValidationError(format!( "Record field {} has incorrect type: {:?}, expected {:?}", - &i, &self.values[i], &field.prim_value_type + &i, &self.values[i], &field.primitive ))); } } @@ -107,7 +107,7 @@ pub trait Recordable { /// The field type of the data structure implementing the `Recordable` trait. type Field: Eq + Clone + Debug; /// Define the schema of the instance implementing the `Recordable` trait. - fn schema() -> Vec<(Self::Field, ValueType)>; + fn schema() -> Vec<(Self::Field, Type)>; /// Define the primary key of the instance implementing the `Recordable` trait. fn primary_key() -> Self::Field; /// Define the secondary keys of the instance implementing the `Recordable` trait. diff --git a/log_db/tests/integration.rs b/log_db/tests/integration.rs index 50f4544..5535adf 100644 --- a/log_db/tests/integration.rs +++ b/log_db/tests/integration.rs @@ -44,11 +44,11 @@ struct Inst { impl Recordable for Inst { type Field = Field; - fn schema() -> Vec<(Self::Field, ValueType)> { + fn schema() -> Vec<(Self::Field, Type)> { vec![ - (Field::Id, ValueType::int()), - (Field::Name, ValueType::string().nullable()), - (Field::Data, ValueType::bytes()), + (Field::Id, Type::int()), + (Field::Name, Type::string().nullable()), + (Field::Data, Type::bytes()), ] } fn primary_key() -> Self::Field { @@ -188,8 +188,8 @@ fn test_get_nonexistant() { struct InstTestNullable {} impl Recordable for InstTestNullable { type Field = Field; - fn schema() -> Vec<(Self::Field, ValueType)> { - vec![(Field::Id, ValueType::int())] + fn schema() -> Vec<(Self::Field, Type)> { + vec![(Field::Id, Type::int())] } fn primary_key() -> Self::Field { Field::Id @@ -219,11 +219,8 @@ fn test_upsert_fails_on_null_in_non_nullable_field() { struct InstTestNumValues {} impl Recordable for InstTestNumValues { type Field = Field; - fn schema() -> Vec<(Self::Field, ValueType)> { - vec![ - (Field::Id, ValueType::int()), - (Field::Name, ValueType::string()), - ] + fn schema() -> Vec<(Self::Field, Type)> { + vec![(Field::Id, Type::int()), (Field::Name, Type::string())] } fn primary_key() -> Self::Field { Field::Id @@ -253,8 +250,8 @@ fn test_upsert_fails_on_invalid_number_of_values() { struct InstTestInvalidType {} impl Recordable for InstTestInvalidType { type Field = Field; - fn schema() -> Vec<(Self::Field, ValueType)> { - vec![(Field::Id, ValueType::int())] + fn schema() -> Vec<(Self::Field, Type)> { + vec![(Field::Id, Type::int())] } fn primary_key() -> Self::Field { Field::Id @@ -325,8 +322,8 @@ struct InstSingleId { impl Recordable for InstSingleId { type Field = Field; - fn schema() -> Vec<(Self::Field, ValueType)> { - vec![(Field::Id, ValueType::int())] + fn schema() -> Vec<(Self::Field, Type)> { + vec![(Field::Id, Type::int())] } fn primary_key() -> Self::Field { Field::Id |
