From 3a6fee6fe08afc0551d3325a4ef71ef5626befe6 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Thu, 28 Apr 2022 16:54:46 +0800 Subject: [PATCH] zero copying --- src/db/engine.rs | 8 ++++---- src/db/eval.rs | 24 +++++++++++------------- src/relation/typing.rs | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/db/engine.rs b/src/db/engine.rs index ef4c6707..de01004f 100644 --- a/src/db/engine.rs +++ b/src/db/engine.rs @@ -116,19 +116,19 @@ impl Engine { } } -pub struct Session<'a> { +pub struct Session<'a, 'b> { pub engine: &'a Engine, pub stack_depth: i32, pub handle: Arc>, pub txn: TransactionPtr, pub perm_cf: SharedPtr, pub temp_cf: SharedPtr, - pub params: BTreeMap, + pub params: BTreeMap, } // every session has its own column family to play with // metadata are stored in table 0 -impl<'a> Session<'a> { +impl<'a, 'b> Session<'a, 'b> { pub fn start(&mut self) -> Result<()> { self.perm_cf = self.engine.db.default_cf(); assert!(!self.perm_cf.is_null()); @@ -173,7 +173,7 @@ impl<'a> Session<'a> { } } -impl<'a> Drop for Session<'a> { +impl<'a, 't> Drop for Session<'a, 't> { fn drop(&mut self) { if let Err(e) = self.finish_work() { eprintln!("Dropping session failed {:?}", e); diff --git a/src/db/eval.rs b/src/db/eval.rs index 98022753..2e13374c 100644 --- a/src/db/eval.rs +++ b/src/db/eval.rs @@ -16,12 +16,12 @@ use crate::parser::{Parser, Rule}; use crate::parser::text_identifier::build_name_in_def; use crate::relation::value; -pub trait Environment> where Self: Sized { +pub trait Environment<'t, T: AsRef<[u8]>> where Self: Sized { fn get_next_storage_id(&mut self, in_root: bool) -> Result; fn get_stack_depth(&self) -> i32; fn push_env(&mut self); fn pop_env(&mut self) -> Result<()>; - fn set_param(&mut self, name: &str, val: String); + fn set_param(&mut self, name: &str, val: &'t str); fn define_variable(&mut self, name: &str, val: &Value, in_root: bool) -> Result<()> { let mut data = Tuple::with_data_prefix(DataKind::Value); data.push_value(val); @@ -664,20 +664,20 @@ pub trait Environment> where Self: Sized { } -pub struct MemoryEnv { +pub struct MemoryEnv<'a> { root: BTreeMap, stack: Vec>, - params: BTreeMap, + params: BTreeMap, max_storage_id: u32, } -impl Default for MemoryEnv { +impl <'a> Default for MemoryEnv<'a> { fn default() -> Self { MemoryEnv { root: BTreeMap::default(), stack: vec![BTreeMap::default()], max_storage_id: 0, params: BTreeMap::default() } } } -impl Environment> for MemoryEnv { +impl <'t> Environment<'t, Vec> for MemoryEnv<'t> { fn get_next_storage_id(&mut self, _in_root: bool) -> Result { self.max_storage_id += 1; Ok(self.max_storage_id) @@ -698,7 +698,7 @@ impl Environment> for MemoryEnv { Ok(()) } - fn set_param(&mut self, name: &str, val: String) { + fn set_param(&mut self, name: &str, val: &'t str) { self.params.insert(name.to_string(), val); } @@ -714,7 +714,7 @@ impl Environment> for MemoryEnv { fn resolve_param(&self, name: &str) -> Result { let text = self.params.get(name).ok_or_else(|| CozoError::UndefinedParam(name.to_string()))?; let pair = Parser::parse(Rule::expr, text)?.next().unwrap(); - Ok(Value::from_pair(pair)?) + Value::from_pair(pair) } fn delete_defined(&mut self, name: &str, in_root: bool) -> Result<()> { @@ -742,7 +742,7 @@ impl Environment> for MemoryEnv { } -impl<'a> Environment for Session<'a> { +impl<'a, 't> Environment<'t, SlicePtr> for Session<'a, 't> { fn get_next_storage_id(&mut self, in_root: bool) -> Result { // TODO: deal with wrapping problem let mut key_entry = Tuple::with_null_prefix(); @@ -805,7 +805,7 @@ impl<'a> Environment for Session<'a> { Ok(()) } - fn set_param(&mut self, name: &str, val: String) { + fn set_param(&mut self, name: &str, val: &'t str) { self.params.insert(name.to_string(), val); } @@ -828,7 +828,7 @@ impl<'a> Environment for Session<'a> { fn resolve_param(&self, name: &str) -> Result { let text = self.params.get(name).ok_or_else(|| CozoError::UndefinedParam(name.to_string()))?; let pair = Parser::parse(Rule::expr, text)?.next().unwrap(); - Ok(Value::from_pair(pair)?) + Value::from_pair(pair) } fn delete_defined(&mut self, name: &str, in_root: bool) -> Result<()> { @@ -870,8 +870,6 @@ impl<'a> Environment for Session<'a> { } -impl<'a> Session<'a> {} - #[cfg(test)] mod tests { use super::*; diff --git a/src/relation/typing.rs b/src/relation/typing.rs index 9e5d1118..b82f4e63 100644 --- a/src/relation/typing.rs +++ b/src/relation/typing.rs @@ -67,7 +67,7 @@ impl Typing { } impl Typing { - pub fn from_pair, E: Environment>(pair: Pair, env: Option<&E>) -> Result { + pub fn from_pair<'t, T: AsRef<[u8]>, E: Environment<'t, T>>(pair: Pair, env: Option<&E>) -> Result { Ok(match pair.as_rule() { Rule::simple_type => match pair.as_str() { "Any" => Typing::Any,