From 337ee3ff806db6249fbb464693e1395c9341bacd Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Fri, 22 Apr 2022 22:10:26 +0800 Subject: [PATCH] start engine --- cozorocks/src/lib.rs | 40 +++++++++++++++++----------------- src/db/engine.rs | 51 +++++++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/cozorocks/src/lib.rs b/cozorocks/src/lib.rs index 885473c4..3a78fa04 100644 --- a/cozorocks/src/lib.rs +++ b/cozorocks/src/lib.rs @@ -227,9 +227,9 @@ impl WriteOptionsPtr { } } -pub struct TransactionOptionsPtr(UniquePtr); +pub struct PTxnOptionsPtr(UniquePtr); -impl Deref for TransactionOptionsPtr { +impl Deref for PTxnOptionsPtr { type Target = UniquePtr; #[inline] @@ -238,14 +238,14 @@ impl Deref for TransactionOptionsPtr { } } -impl DerefMut for TransactionOptionsPtr { +impl DerefMut for PTxnOptionsPtr { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl TransactionOptionsPtr { +impl PTxnOptionsPtr { #[inline] pub fn default() -> Self { Self(new_transaction_options()) @@ -257,9 +257,9 @@ impl TransactionOptionsPtr { } } -pub struct OptimisticTransactionOptionsPtr(UniquePtr); +pub struct OTxnOptionsPtr(UniquePtr); -impl Deref for OptimisticTransactionOptionsPtr { +impl Deref for OTxnOptionsPtr { type Target = UniquePtr; #[inline] @@ -268,23 +268,23 @@ impl Deref for OptimisticTransactionOptionsPtr { } } -impl DerefMut for OptimisticTransactionOptionsPtr { +impl DerefMut for OTxnOptionsPtr { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl OptimisticTransactionOptionsPtr { +impl OTxnOptionsPtr { #[inline] pub fn new(cmp: &RustComparatorPtr) -> Self { Self(new_optimistic_transaction_options(cmp)) } } -pub struct TransactionDBOptionsPtr(UniquePtr); +pub struct PTxnDBOptionsPtr(UniquePtr); -impl Deref for TransactionDBOptionsPtr { +impl Deref for PTxnDBOptionsPtr { type Target = UniquePtr; #[inline] @@ -293,23 +293,23 @@ impl Deref for TransactionDBOptionsPtr { } } -impl DerefMut for TransactionDBOptionsPtr { +impl DerefMut for PTxnDBOptionsPtr { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl TransactionDBOptionsPtr { +impl PTxnDBOptionsPtr { #[inline] pub fn default() -> Self { Self(new_tdb_options()) } } -pub struct OptimisticTransactionDBOptionsPtr(UniquePtr); +pub struct OTxnDBOptionsPtr(UniquePtr); -impl Deref for OptimisticTransactionDBOptionsPtr { +impl Deref for OTxnDBOptionsPtr { type Target = UniquePtr; #[inline] @@ -318,14 +318,14 @@ impl Deref for OptimisticTransactionDBOptionsPtr { } } -impl DerefMut for OptimisticTransactionDBOptionsPtr { +impl DerefMut for OTxnDBOptionsPtr { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl OptimisticTransactionDBOptionsPtr { +impl OTxnDBOptionsPtr { #[inline] pub fn default() -> Self { Self(new_odb_options()) @@ -536,13 +536,13 @@ unsafe impl Send for DBPtr {} unsafe impl Sync for DBPtr {} pub enum TransactOptions { - Pessimistic(TransactionOptionsPtr), - Optimistic(OptimisticTransactionOptionsPtr), + Pessimistic(PTxnOptionsPtr), + Optimistic(OTxnOptionsPtr), } pub enum TDBOptions { - Pessimistic(TransactionDBOptionsPtr), - Optimistic(OptimisticTransactionDBOptionsPtr), + Pessimistic(PTxnDBOptionsPtr), + Optimistic(OTxnDBOptionsPtr), } impl DBPtr { diff --git a/src/db/engine.rs b/src/db/engine.rs index 13df945c..f3bdeead 100644 --- a/src/db/engine.rs +++ b/src/db/engine.rs @@ -31,9 +31,9 @@ unsafe impl Sync for Engine {} impl Engine { pub fn new(path: String, optimistic: bool) -> Result { let t_options = if optimistic { - TDBOptions::Optimistic(OptimisticTransactionDBOptionsPtr::default()) + TDBOptions::Optimistic(OTxnDBOptionsPtr::default()) } else { - TDBOptions::Pessimistic(TransactionDBOptionsPtr::default()) + TDBOptions::Pessimistic(PTxnDBOptionsPtr::default()) }; let mut options = OptionsPtr::default(); let cmp = RustComparatorPtr::new("cozo_cmp_v1", crate::relation::key_order::compare); @@ -110,7 +110,7 @@ pub struct Session<'a> { // every session has its own column family to play with // metadata are stored in table 0 -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct SessionHandle { cf_ident: String, status: SessionStatus, @@ -126,7 +126,7 @@ pub enum SessionStatus { #[cfg(test)] mod tests { - use std::fs; + use std::{fs, thread}; use super::*; #[test] @@ -147,15 +147,40 @@ mod tests { } let engine2 = Engine::new(p2.to_string(), false); assert!(engine2.is_ok()); - let engine2 = Engine::new(p3.to_string(), false).unwrap(); - let _sess = engine2.session(); - let handles = engine2.session_handles.lock().unwrap(); - println!("got handles {}", handles.len()); - let cf_ident = &handles.first().unwrap().read().unwrap().cf_ident; - println!("Opening ok {}", cf_ident); - let cf = engine2.db.get_cf(cf_ident).unwrap(); - assert!(!cf.is_null()); - println!("Getting CF ok"); + let engine2 = Arc::new(Engine::new(p3.to_string(), false).unwrap()); + { + for i in 0..10 { + let _sess = engine2.session(); + } + let handles = engine2.session_handles.lock().unwrap(); + println!("got handles {}", handles.len()); + let cf_ident = &handles.first().unwrap().read().unwrap().cf_ident; + println!("Opening ok {}", cf_ident); + let cf = engine2.db.get_cf(cf_ident).unwrap(); + assert!(!cf.is_null()); + println!("Getting CF ok"); + } + let mut thread_handles = vec![]; + + println!("concurrent"); + for i in 0..10 { + let engine = engine2.clone(); + thread_handles.push(thread::spawn(move || { + // println!("In thread {}", i); + let _sess = engine.session(); + // println!("In thread {} end", i); + })) + } + + + for t in thread_handles { + t.join().unwrap(); + } + println!("All OK"); + { + let handles = engine2.session_handles.lock().unwrap(); + println!("got handles {:#?}", handles.iter().map(|h| h.read().unwrap().cf_ident.to_string()).collect::>()); + } } let _ = fs::remove_dir_all(p1); let _ = fs::remove_dir_all(p2);