session creation

main
Ziyang Hu 2 years ago
parent 9f63ac3a05
commit 3ef1259fa1

@ -397,6 +397,9 @@ struct TDBBridge {
unique_ptr<ReadOptions> r_ops,
unique_ptr<ReadOptions> raw_r_ops,
unique_ptr<TransactionOptions> txn_options) const {
if (tdb == nullptr) {
return unique_ptr<TransactionBridge>(nullptr);
}
auto ret = make_unique<TransactionBridge>();
ret->raw_db = tdb;
ret->r_ops = std::move(r_ops);
@ -415,6 +418,9 @@ struct TDBBridge {
unique_ptr<ReadOptions> r_ops,
unique_ptr<ReadOptions> raw_r_ops,
unique_ptr<OptimisticTransactionOptions> txn_options) const {
if (odb == nullptr) {
return unique_ptr<TransactionBridge>(nullptr);
}
auto ret = make_unique<TransactionBridge>();
ret->raw_db = odb;
ret->r_ops = std::move(r_ops);

@ -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)

@ -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<EngineOptions>,
pub db: DBPtr,
pub options_store: Box<EngineOptions>,
session_handles: RwLock<Vec<Arc<RwLock<SessionHandle>>>>,
}
@ -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<RwLock<SessionHandle>>,
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>,
}
// 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);
}))
}

@ -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<Vec<u8>> {
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!()
}
}
Loading…
Cancel
Save