From 3ef1259fa1ad6774bf0cd47b0dd71ca81a8eabeb Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sat, 23 Apr 2022 17:45:21 +0800 Subject: [PATCH] session creation --- cozorocks/bridge/cozorocks.h | 6 ++ cozorocks/src/lib.rs | 4 ++ src/db/engine.rs | 51 ++++++++++++++--- src/db/eval.rs | 106 ++++++++++++++++++++++++++--------- 4 files changed, 131 insertions(+), 36 deletions(-) diff --git a/cozorocks/bridge/cozorocks.h b/cozorocks/bridge/cozorocks.h index 98d87c10..c5fd0e93 100644 --- a/cozorocks/bridge/cozorocks.h +++ b/cozorocks/bridge/cozorocks.h @@ -397,6 +397,9 @@ struct TDBBridge { unique_ptr r_ops, unique_ptr raw_r_ops, unique_ptr txn_options) const { + if (tdb == nullptr) { + return unique_ptr(nullptr); + } auto ret = make_unique(); ret->raw_db = tdb; ret->r_ops = std::move(r_ops); @@ -415,6 +418,9 @@ struct TDBBridge { unique_ptr r_ops, unique_ptr raw_r_ops, unique_ptr txn_options) const { + if (odb == nullptr) { + return unique_ptr(nullptr); + } auto ret = make_unique(); ret->raw_db = odb; ret->r_ops = std::move(r_ops); diff --git a/cozorocks/src/lib.rs b/cozorocks/src/lib.rs index be9c802d..d7fca3f4 100644 --- a/cozorocks/src/lib.rs +++ b/cozorocks/src/lib.rs @@ -444,6 +444,10 @@ impl Deref for TransactionPtr { impl TransactionPtr { + #[inline] + pub fn null() -> Self { + TransactionPtr(UniquePtr::null()) + } #[inline] pub fn set_snapshot(&self) { TransactionBridge::set_snapshot(self) diff --git a/src/db/engine.rs b/src/db/engine.rs index bbe8121d..eb128c0a 100644 --- a/src/db/engine.rs +++ b/src/db/engine.rs @@ -9,7 +9,7 @@ use uuid::Uuid; use uuid::v1::{Context, Timestamp}; use rand::Rng; -struct EngineOptions { +pub struct EngineOptions { cmp: RustComparatorPtr, options: OptionsPtr, t_options: TDBOptions, @@ -18,8 +18,8 @@ struct EngineOptions { } pub struct Engine { - db: DBPtr, - options_store: Box, + pub db: DBPtr, + pub options_store: Box, session_handles: RwLock>>>, } @@ -97,20 +97,51 @@ impl Engine { return Session { engine: self, stack_depth: 0, + txn: TransactionPtr::null(), + perm_cf: SharedPtr::null(), + temp_cf: SharedPtr::null(), handle, }; } } pub struct Session<'a> { - engine: &'a Engine, - stack_depth: i32, - // zero or negative - handle: Arc>, + pub engine: &'a Engine, + pub stack_depth: i32, + pub handle: Arc>, + pub txn: TransactionPtr, + pub perm_cf: SharedPtr, + pub temp_cf: SharedPtr, } // every session has its own column family to play with // metadata are stored in table 0 +impl<'a> Session<'a> { + pub fn start(&mut self) { + self.perm_cf = self.engine.db.default_cf(); + let name = self.handle.read().unwrap().cf_ident.to_string(); + self.temp_cf = self.engine.db.get_cf(name).unwrap(); + let t_options = match self.engine.options_store.t_options { + TDBOptions::Pessimistic(_) => { + TransactOptions::Pessimistic(PTxnOptionsPtr::default()) + } + TDBOptions::Optimistic(_) => { + TransactOptions::Optimistic(OTxnOptionsPtr::new(&self.engine.options_store.cmp)) + } + }; + let r_opts = ReadOptionsPtr::default(); + let rx_opts = ReadOptionsPtr::default(); + let w_opts = WriteOptionsPtr::default(); + let mut wx_opts = WriteOptionsPtr::default(); + wx_opts.set_disable_wal(true); + self.txn = self.engine.db.make_transaction(t_options, r_opts, rx_opts, w_opts, wx_opts); + if self.txn.is_null() { + panic!("Starting session failed as opening transaction failed"); + } + self.handle.write().unwrap().status = SessionStatus::Running; + } +} + #[derive(Clone, Debug)] pub struct SessionHandle { cf_ident: String, @@ -151,7 +182,8 @@ mod tests { let engine2 = Arc::new(Engine::new(p3.to_string(), false).unwrap()); { for _i in 0..10 { - let _sess = engine2.session(); + let mut _sess = engine2.session(); + _sess.start(); } let handles = engine2.session_handles.read().unwrap(); println!("got handles {}", handles.len()); @@ -168,7 +200,8 @@ mod tests { let engine = engine2.clone(); thread_handles.push(thread::spawn(move || { println!("In thread {}", i); - let _sess = engine.session(); + let mut _sess = engine.session(); + _sess.start(); println!("In thread {} end", i); })) } diff --git a/src/db/eval.rs b/src/db/eval.rs index 3145791a..4c3f3761 100644 --- a/src/db/eval.rs +++ b/src/db/eval.rs @@ -1,27 +1,79 @@ -// use crate::relation::table::Table; -// use crate::relation::typing::Typing; -// use crate::relation::value::Value; -// -// pub trait Environment { -// fn push_env(&mut self) { -// -// } -// fn pop_env(&mut self) { -// -// } -// fn define_variable(&mut self, name: &str, val: &Value, in_root: bool) { -// -// } -// fn define_type_alias(&mut self, name: &str, typ: &Typing, in_root: bool) { -// -// } -// fn define_table(&mut self, table: &Table, in_root: bool) { -// -// } -// fn resolve(&mut self, name: &str) { -// -// } -// fn delete_defined(&mut self, name: &str, in_root: bool) { -// -// } -// } +use crate::db::engine::{Engine, Session}; +use crate::relation::table::Table; +use crate::relation::tuple::Tuple; +use crate::relation::typing::Typing; +use crate::relation::value::Value; + +pub trait Environment { + fn push_env(&mut self); + fn pop_env(&mut self); + fn define_variable(&mut self, name: &str, val: &Value, in_root: bool); + fn define_type_alias(&mut self, name: &str, typ: &Typing, in_root: bool); + fn define_table(&mut self, table: &Table, in_root: bool); + fn resolve(&mut self, name: &str); + fn delete_defined(&mut self, name: &str, in_root: bool); +} + + +#[repr(u8)] +enum DefinableTag { + Value = 1, + Typing = 2, + Node = 3, + Edge = 4, + Associate = 5, + Index = 6, +} + + +impl<'a> Session<'a> { + fn encode_definable_key(&self, name: &str, in_root: bool) -> Tuple> { + let depth_code = if in_root { 0 } else { self.stack_depth as i64 }; + let mut tuple = Tuple::with_prefix(0); + tuple.push_str(name); + tuple.push_int(depth_code); + tuple + } +} + + +impl<'a> Environment for Session<'a> { + fn push_env(&mut self) { + self.stack_depth -= 1; + } + + fn pop_env(&mut self) { + if self.stack_depth == 0 { + return; + } + // Remove all stuff starting with the stack depth from the temp session + self.stack_depth += 1; + } + + fn define_variable(&mut self, name: &str, val: &Value, in_root: bool) { + if in_root { + todo!() + } else { + let key = self.encode_definable_key(name, in_root); + let mut data = Tuple::with_prefix(0); + data.push_uint(DefinableTag::Value as u8 as u64); + data.push_value(val); + } + } + + fn define_type_alias(&mut self, name: &str, typ: &Typing, in_root: bool) { + todo!() + } + + fn define_table(&mut self, table: &Table, in_root: bool) { + todo!() + } + + fn resolve(&mut self, name: &str) { + todo!() + } + + fn delete_defined(&mut self, name: &str, in_root: bool) { + todo!() + } +} \ No newline at end of file