From bea37e2834a5265035b960277ff27e5f8d74d079 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Fri, 21 Feb 2025 14:16:24 +0200 Subject: Start working in new schema logic to core --- log_db/src/lib.rs | 59 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'log_db/src/lib.rs') diff --git a/log_db/src/lib.rs b/log_db/src/lib.rs index 753e41c..fddb344 100644 --- a/log_db/src/lib.rs +++ b/log_db/src/lib.rs @@ -37,17 +37,17 @@ use memtable_primary::PrimaryMemtable; use memtable_secondary::SecondaryMemtable; use record::*; -pub struct DB { - engine: Engine, +pub struct DB { + engine: Engine, } -impl DB { +impl DB { /// Create a new database configuration builder. - pub fn configure() -> ConfigBuilder { + pub fn configure() -> ConfigBuilder { ConfigBuilder::new() } - fn initialize(config: Config) -> DBResult> { + fn initialize(config: Config) -> DBResult> { let engine = Engine::initialize(config)?; Ok(DB { engine }) } @@ -85,9 +85,13 @@ impl DB { } /// Get a collection of records based on an indexed field value. - pub fn find_by(&mut self, field: &F, value: &Value) -> DBResult> { + pub fn find_by(&mut self, field: impl AsRef, value: &Value) -> DBResult> { let recs = self.engine.with_shared_lock(|engine| { - engine.batch_find_by_records(field, std::iter::once(value), &DEFAULT_QUERY_PARAMS) + engine.batch_find_by_records( + field.as_ref(), + std::iter::once(value), + &DEFAULT_QUERY_PARAMS, + ) })?; Ok(recs @@ -99,12 +103,12 @@ impl DB { /// Get a collection of records based on an indexed field value, with additional parameters. pub fn find_by_with_params( &mut self, - field: &F, + field: impl AsRef, value: &Value, params: &QueryParams, ) -> DBResult> { let recs = self.engine.with_shared_lock(|engine| { - engine.batch_find_by_records(field, std::iter::once(value), params) + engine.batch_find_by_records(field.as_ref(), std::iter::once(value), params) })?; Ok(recs @@ -116,9 +120,13 @@ impl DB { /// Get a collection of records based on a sequence of indexed field values. /// Returns a vector of pairs where the first value is an index into the given sequence of values, /// and the second value is the record. - pub fn batch_find_by(&mut self, field: &F, values: &[Value]) -> DBResult> { + pub fn batch_find_by( + &mut self, + field: impl Into, + values: &[Value], + ) -> DBResult> { let recs = self.engine.with_shared_lock(|engine| { - engine.batch_find_by_records(field, values.iter(), &DEFAULT_QUERY_PARAMS) + engine.batch_find_by_records(&field.into(), values.iter(), &DEFAULT_QUERY_PARAMS) })?; Ok(recs @@ -132,12 +140,12 @@ impl DB { /// and the second value is the record. pub fn batch_find_by_with_params( &mut self, - field: &F, + field: impl AsRef, values: &[Value], params: &QueryParams, ) -> DBResult> { let recs = self.engine.with_shared_lock(|engine| { - engine.batch_find_by_records(field, values.iter(), params) + engine.batch_find_by_records(field.as_ref(), values.iter(), params) })?; Ok(recs @@ -149,9 +157,13 @@ impl DB { /// Get a collection of records based on a range of indexed field values. /// This method can be used to run comparison-like queries, e.g. `field >= 10` /// could be expressed as `db.range_by(Field::Id, 10..)`. - pub fn range_by>(&mut self, field: &F, range: B) -> DBResult> { + pub fn range_by>( + &mut self, + field: impl AsRef, + range: B, + ) -> DBResult> { let recs = self.engine.with_shared_lock(|engine| { - engine.range_by_records(field, range, &DEFAULT_QUERY_PARAMS) + engine.range_by_records(field.as_ref(), range, &DEFAULT_QUERY_PARAMS) })?; Ok(recs @@ -165,13 +177,13 @@ impl DB { /// could be expressed as `db.range_by(Field::Id, 10..)`. pub fn range_by_with_params>( &mut self, - field: &F, + field: impl AsRef, range: B, params: &QueryParams, ) -> DBResult> { let recs = self .engine - .with_shared_lock(|engine| engine.range_by_records(field, range, params))?; + .with_shared_lock(|engine| engine.range_by_records(field.as_ref(), range, params))?; Ok(recs .into_iter() @@ -185,10 +197,10 @@ impl DB { /// /// Deletion is done by marking the record as a tombstone. The record will still be present in the log file, /// but will be ignored by reads. Upon compaction, tombstoned records will be removed. - pub fn delete_by(&mut self, field: &F, value: &Value) -> DBResult> { + pub fn delete_by(&mut self, field: impl AsRef, value: &Value) -> DBResult> { let recs = self .engine - .with_exclusive_lock(|engine| engine.delete_by_field(field, value))?; + .with_exclusive_lock(|engine| engine.delete_by_field(field.as_ref(), value))?; Ok(recs .into_iter() @@ -295,6 +307,15 @@ mod tests { Name, } + impl Into for Field { + fn into(self) -> String { + match self { + Field::Id => "id".to_string(), + Field::Name => "name".to_string(), + } + } + } + struct TestInst1 { id: i64, } -- cgit v1.3