zero copying

main
Ziyang Hu 2 years ago
parent bafd3a3c13
commit 3a6fee6fe0

@ -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<RwLock<SessionHandle>>,
pub txn: TransactionPtr,
pub perm_cf: SharedPtr<ColumnFamilyHandle>,
pub temp_cf: SharedPtr<ColumnFamilyHandle>,
pub params: BTreeMap<String, String>,
pub params: BTreeMap<String, &'b str>,
}
// 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);

@ -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<T: AsRef<[u8]>> where Self: Sized {
pub trait Environment<'t, T: AsRef<[u8]>> where Self: Sized {
fn get_next_storage_id(&mut self, in_root: bool) -> Result<u32>;
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<T: AsRef<[u8]>> where Self: Sized {
}
pub struct MemoryEnv {
pub struct MemoryEnv<'a> {
root: BTreeMap<String, OwnTuple>,
stack: Vec<BTreeMap<String, OwnTuple>>,
params: BTreeMap<String, String>,
params: BTreeMap<String, &'a str>,
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<Vec<u8>> for MemoryEnv {
impl <'t> Environment<'t, Vec<u8>> for MemoryEnv<'t> {
fn get_next_storage_id(&mut self, _in_root: bool) -> Result<u32> {
self.max_storage_id += 1;
Ok(self.max_storage_id)
@ -698,7 +698,7 @@ impl Environment<Vec<u8>> 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<Vec<u8>> for MemoryEnv {
fn resolve_param(&self, name: &str) -> Result<Value> {
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<Vec<u8>> for MemoryEnv {
}
impl<'a> Environment<SlicePtr> for Session<'a> {
impl<'a, 't> Environment<'t, SlicePtr> for Session<'a, 't> {
fn get_next_storage_id(&mut self, in_root: bool) -> Result<u32> {
// TODO: deal with wrapping problem
let mut key_entry = Tuple::with_null_prefix();
@ -805,7 +805,7 @@ impl<'a> Environment<SlicePtr> 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<SlicePtr> for Session<'a> {
fn resolve_param(&self, name: &str) -> Result<Value> {
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<SlicePtr> for Session<'a> {
}
impl<'a> Session<'a> {}
#[cfg(test)]
mod tests {
use super::*;

@ -67,7 +67,7 @@ impl Typing {
}
impl Typing {
pub fn from_pair<T: AsRef<[u8]>, E: Environment<T>>(pair: Pair<Rule>, env: Option<&E>) -> Result<Self> {
pub fn from_pair<'t, T: AsRef<[u8]>, E: Environment<'t, T>>(pair: Pair<Rule>, env: Option<&E>) -> Result<Self> {
Ok(match pair.as_rule() {
Rule::simple_type => match pair.as_str() {
"Any" => Typing::Any,

Loading…
Cancel
Save