diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-01-06 18:52:22 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-01-06 18:52:22 +0200 |
| commit | 21d0b647d6916c4dbbde8a310a0760872d44f7a2 (patch) | |
| tree | 0a22a2d94fe061ea778f03000d74d4bf1d4749f4 /log_db/src/common.rs | |
| parent | 563703446eaec2dbed721dbe2c6565cee0d419be (diff) | |
Rework DB interface to use Recordable instances
Diffstat (limited to 'log_db/src/common.rs')
| -rw-r--r-- | log_db/src/common.rs | 126 |
1 files changed, 10 insertions, 116 deletions
diff --git a/log_db/src/common.rs b/log_db/src/common.rs index a4f253c..a3ff5c2 100644 --- a/log_db/src/common.rs +++ b/log_db/src/common.rs @@ -400,114 +400,6 @@ impl Value { } } -#[derive(Debug, Clone)] -pub struct Record { - values: Vec<Value>, - tombstone: bool, -} - -impl Record { - pub fn serialize(&self) -> Vec<u8> { - let mut bytes = Vec::new(); - - if self.tombstone { - bytes.extend(&[B_TOMBSTONE]); - } else { - bytes.extend(&[B_LIVE]); - } - - for value in &self.values { - bytes.extend(value.serialize()); - } - bytes - } - - pub fn deserialize(bytes: &[u8]) -> Record { - let mut values = Vec::new(); - - let tombstone = bytes[0] == B_TOMBSTONE; - - let mut start = 1; - while start < bytes.len() { - let (rv, consumed) = Value::deserialize(&bytes[start..]); - values.push(rv); - start += consumed; - } - Record { values, tombstone } - } - - pub fn from(values: &[Value]) -> Record { - Record { - values: values.to_vec(), - tombstone: false, - } - } - - pub fn values(&self) -> &[Value] { - &self.values - } - - pub fn at(&self, index: usize) -> &Value { - &self.values[index] - } - - pub fn is_tombstone(&self) -> bool { - self.tombstone - } - - pub fn validate<Field: Eq>(&self, schema: &Vec<(Field, ValueType)>) -> Result<(), DBError> { - // Validate the record length - if self.values.len() != schema.len() { - return Err(DBError::ValidationError(format!( - "Record has an incorrect number of fields: {}, expected {}", - self.values.len(), - schema.len() - ))); - } - - // Validate that record fields match schema types - for (i, (_, field)) in schema.iter().enumerate() { - match (&self.values[i], field) { - ( - Value::Null, - ValueType { - nullable: true, - prim_value_type: _, - }, - ) => {} - ( - Value::Int(_), - ValueType { - prim_value_type: PrimValueType::Int, - .. - }, - ) => {} - ( - Value::String(_), - ValueType { - prim_value_type: PrimValueType::String, - .. - }, - ) => {} - ( - Value::Bytes(_), - ValueType { - prim_value_type: PrimValueType::Bytes, - .. - }, - ) => {} - _ => { - return Err(DBError::ValidationError(format!( - "Record field {} has incorrect type: {:?}, expected {:?}", - &i, &self.values[i], &field.prim_value_type - ))); - } - } - } - Ok(()) - } -} - pub fn type_check(value: &Value, value_type: &ValueType) -> bool { match (value, value_type) { ( @@ -543,14 +435,6 @@ pub fn type_check(value: &Value, value_type: &ValueType) -> bool { } } -/// 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`. - fn to_record(&self) -> Record; - /// Convert a database `Record` into the data structure implementing the `Recordable` trait. - fn from_record(record: &Record) -> Self; -} - pub fn get_secondary_memtable_index_by_field<Field: Eq>( sks: &Vec<Field>, field: &Field, @@ -898,3 +782,13 @@ pub fn request_exclusive_lock(data_dir: &Path, file: &mut fs::File) -> Result<() Ok(()) } + +macro_rules! dbg_trace { + ($($args: expr),*) => { + print!("TRACE: file: {}, line: {}", file!(), line!()); + $( + print!(", {}: {:?}", stringify!($args), $args); + )* + println!(""); // to get a new line at the end + } +} |
