From ebfcf66e0034b06a4eae72112c91f2d4ac49fdbc Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 22 Nov 2024 13:15:58 +0200 Subject: Get memtables working finally --- log_db/src/common.rs | 98 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 29 deletions(-) (limited to 'log_db/src/common.rs') diff --git a/log_db/src/common.rs b/log_db/src/common.rs index e384a8f..e379364 100644 --- a/log_db/src/common.rs +++ b/log_db/src/common.rs @@ -29,13 +29,6 @@ pub fn metadata_filename(num: u16) -> String { format!("metadata.{}", num) } -#[derive(Debug, Eq, PartialEq)] -pub enum SpecialSequence { - RecordSeparator, - LiteralFieldSeparator, - LiteralEscape, -} - /// LogKey is a packed struct that contains: /// - a log segment number (16 bits) /// - a log index within the segment (48 bits) @@ -191,6 +184,16 @@ impl MetadataHeader { } } +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum ReadConsistency { + /// Reads by client A are guaranteed to see writes by themselves and any writes by other clients B + /// that were done before last index refresh. + Eventual, + /// Reads by client A are guaranteed to see all writes. This is slower: all reads must first + /// refresh indexes. + Strong, +} + #[derive(Debug, Clone, Eq, PartialEq)] pub enum WriteDurability { /// Changes are written to the OS write buffer but not immediately synced to disk. @@ -214,45 +217,47 @@ pub enum IndexableValue { String(String), } +/// A primitive type #[derive(Debug, Clone)] -pub enum RecordFieldType { +pub enum PrimValueType { Int, Float, String, Bytes, } +/// A primitive type + a nullability bit #[derive(Debug, Clone)] -pub struct RecordField { - pub field_type: RecordFieldType, +pub struct ValueType { + pub prim_value_type: PrimValueType, pub nullable: bool, } -impl RecordField { +impl ValueType { pub fn int() -> Self { - RecordField { - field_type: RecordFieldType::Int, + ValueType { + prim_value_type: PrimValueType::Int, nullable: false, } } pub fn float() -> Self { - RecordField { - field_type: RecordFieldType::Float, + ValueType { + prim_value_type: PrimValueType::Float, nullable: false, } } pub fn string() -> Self { - RecordField { - field_type: RecordFieldType::String, + ValueType { + prim_value_type: PrimValueType::String, nullable: false, } } pub fn bytes() -> Self { - RecordField { - field_type: RecordFieldType::Bytes, + ValueType { + prim_value_type: PrimValueType::Bytes, nullable: false, } } @@ -401,7 +406,7 @@ impl Record { &self.0[index] } - pub fn validate(&self, schema: &Vec<(Field, RecordField)>) -> Result<(), io::Error> { + pub fn validate(&self, schema: &Vec<(Field, ValueType)>) -> Result<(), io::Error> { // Validate the record length if self.0.len() != schema.len() { return Err(io::Error::new( @@ -419,29 +424,29 @@ impl Record { match (&self.0[i], field) { ( Value::Null, - RecordField { + ValueType { nullable: true, - field_type: _, + prim_value_type: _, }, ) => {} ( Value::Int(_), - RecordField { - field_type: RecordFieldType::Int, + ValueType { + prim_value_type: PrimValueType::Int, .. }, ) => {} ( Value::String(_), - RecordField { - field_type: RecordFieldType::String, + ValueType { + prim_value_type: PrimValueType::String, .. }, ) => {} ( Value::Bytes(_), - RecordField { - field_type: RecordFieldType::Bytes, + ValueType { + prim_value_type: PrimValueType::Bytes, .. }, ) => {} @@ -450,7 +455,7 @@ impl Record { io::ErrorKind::InvalidInput, format!( "Record field {} has incorrect type: {:?}, expected {:?}", - &i, &self.0[i], &field.field_type + &i, &self.0[i], &field.prim_value_type ), )) } @@ -460,6 +465,41 @@ impl Record { } } +pub fn type_check(value: &Value, value_type: &ValueType) -> bool { + match (value, value_type) { + ( + Value::Int(_), + ValueType { + prim_value_type: PrimValueType::Int, + .. + }, + ) => true, + ( + Value::Float(_), + ValueType { + prim_value_type: PrimValueType::Float, + .. + }, + ) => true, + ( + Value::Bytes(_), + ValueType { + prim_value_type: PrimValueType::Bytes, + .. + }, + ) => true, + ( + Value::String(_), + ValueType { + prim_value_type: PrimValueType::String, + .. + }, + ) => true, + (Value::Null, ValueType { nullable: true, .. }) => true, + _ => false, + } +} + /// A trait that describes how to convert a data structure into a database `Record` and vice versa. pub trait Recordable { /// Convert the data structure implementing the `Recordable` trait into a database `Record`. -- cgit v1.3