diff --git a/cozo-core/src/fixed_rule/mod.rs b/cozo-core/src/fixed_rule/mod.rs index 01f140f6..feca8c99 100644 --- a/cozo-core/src/fixed_rule/mod.rs +++ b/cozo-core/src/fixed_rule/mod.rs @@ -664,8 +664,8 @@ pub(crate) struct FixedRuleHandle { } lazy_static! { - pub(crate) static ref DEFAULT_FIXED_RULES: Arc>>> = { - Arc::new(BTreeMap::from([ + pub(crate) static ref DEFAULT_FIXED_RULES: BTreeMap>> = { + BTreeMap::from([ #[cfg(feature = "graph-algo")] ( "ClusteringCoefficients".to_string(), @@ -792,7 +792,7 @@ lazy_static! { "Constant".to_string(), Arc::>::new(Box::new(Constant)), ), - ])) + ]) }; } diff --git a/cozo-core/src/lib.rs b/cozo-core/src/lib.rs index f3274c5c..1d9104ea 100644 --- a/cozo-core/src/lib.rs +++ b/cozo-core/src/lib.rs @@ -80,7 +80,6 @@ pub(crate) mod runtime; pub(crate) mod storage; pub(crate) mod utils; -#[derive(Clone)] /// A dispatcher for concrete storage implementations, wrapping [Db]. This is done so that /// client code does not have to deal with generic code constantly. You may prefer to use /// [Db] directly, especially if you provide a custom storage engine. @@ -412,7 +411,7 @@ impl DbInstance { } /// Dispatcher method. See [crate::Db::register_fixed_rule]. pub fn register_fixed_rule( - &mut self, + &self, name: String, rule_impl: Box, ) -> Result<()> { diff --git a/cozo-core/src/query/stored.rs b/cozo-core/src/query/stored.rs index 30c1bb82..c1f36a80 100644 --- a/cozo-core/src/query/stored.rs +++ b/cozo-core/src/query/stored.rs @@ -79,7 +79,7 @@ impl<'a> SessionTx<'a> { } for trigger in &old_handle.replace_triggers { let program = - parse_script(trigger, &Default::default(), &db.algorithms, cur_vld)? + parse_script(trigger, &Default::default(), &db.algorithms.read().unwrap(), cur_vld)? .get_single_program()?; let (_, cleanups) = db @@ -203,7 +203,7 @@ impl<'a> SessionTx<'a> { let mut program = parse_script( trigger, &Default::default(), - &db.algorithms, + &db.algorithms.read().unwrap(), cur_vld, )? .get_single_program()?; @@ -482,7 +482,7 @@ impl<'a> SessionTx<'a> { let mut program = parse_script( trigger, &Default::default(), - &db.algorithms, + &db.algorithms.read().unwrap(), cur_vld, )? .get_single_program()?; diff --git a/cozo-core/src/runtime/db.rs b/cozo-core/src/runtime/db.rs index 7e01face..dc77eeee 100644 --- a/cozo-core/src/runtime/db.rs +++ b/cozo-core/src/runtime/db.rs @@ -82,22 +82,21 @@ pub struct DbManifest { } /// The database object of Cozo. -#[derive(Clone)] pub struct Db { pub(crate) db: S, temp_db: TempStorage, relation_store_id: Arc, - queries_count: Arc, + queries_count: AtomicU64, running_queries: Arc>>, - pub(crate) algorithms: Arc>>>, + pub(crate) algorithms: ShardedLock>>>, #[cfg(not(target_arch = "wasm32"))] - callback_count: Arc, + callback_count: AtomicU32, #[cfg(not(target_arch = "wasm32"))] pub(crate) callback_sender: Sender<(SmartString, CallbackOp, NamedRows, NamedRows)>, #[cfg(not(target_arch = "wasm32"))] pub(crate) event_callbacks: Arc>, - relation_locks: Arc, Arc>>>>, + relation_locks: ShardedLock, Arc>>>, } impl Debug for Db { @@ -189,18 +188,18 @@ impl<'s, S: Storage<'s>> Db { let ret = Self { db: storage, temp_db: Default::default(), - relation_store_id: Arc::new(Default::default()), - queries_count: Arc::new(Default::default()), - running_queries: Arc::new(Mutex::new(Default::default())), - algorithms: DEFAULT_FIXED_RULES.clone(), + relation_store_id: Default::default(), + queries_count: Default::default(), + running_queries: Default::default(), + algorithms: ShardedLock::new(DEFAULT_FIXED_RULES.clone()), #[cfg(not(target_arch = "wasm32"))] - callback_count: Arc::new(Default::default()), + callback_count: Default::default(), #[cfg(not(target_arch = "wasm32"))] callback_sender: sender, // callback_receiver: Arc::new(receiver), #[cfg(not(target_arch = "wasm32"))] - event_callbacks: Arc::new(Default::default()), - relation_locks: Arc::new(Default::default()), + event_callbacks: Default::default(), + relation_locks: Default::default(), }; #[cfg(not(target_arch = "wasm32"))] { @@ -549,16 +548,12 @@ impl<'s, S: Storage<'s>> Db { } } /// Register a custom fixed rule implementation. - /// - /// You must register fixed rules BEFORE you clone the database, - /// otherwise already cloned instances will not get the new fixed rule. pub fn register_fixed_rule( - &mut self, + &self, name: String, rule_impl: Box, ) -> Result<()> { - let new = Arc::make_mut(&mut self.algorithms); - match new.entry(name) { + match self.algorithms.write().unwrap().entry(name) { Entry::Vacant(ent) => { ent.insert(Arc::new(rule_impl)); Ok(()) @@ -713,7 +708,7 @@ impl<'s, S: Storage<'s>> Db { param_pool: &BTreeMap, cur_vld: ValidityTs, ) -> Result { - match parse_script(payload, param_pool, &self.algorithms, cur_vld)? { + match parse_script(payload, param_pool, &self.algorithms.read().unwrap(), cur_vld)? { CozoScript::Single(p) => self.execute_single(cur_vld, p), CozoScript::Imperative(ps) => self.execute_imperative(cur_vld, &ps), CozoScript::Sys(op) => self.run_sys_op(op), diff --git a/cozo-core/src/storage/mem.rs b/cozo-core/src/storage/mem.rs index 44892087..c950e81f 100644 --- a/cozo-core/src/storage/mem.rs +++ b/cozo-core/src/storage/mem.rs @@ -36,7 +36,7 @@ pub fn new_cozo_mem() -> Result> { } /// The non-persistent storage -#[derive(Clone, Default)] +#[derive(Default)] pub struct MemStorage { store: Arc, Vec>>>, } diff --git a/cozo-core/src/storage/rocks.rs b/cozo-core/src/storage/rocks.rs index 198669f2..9a15aa9a 100644 --- a/cozo-core/src/storage/rocks.rs +++ b/cozo-core/src/storage/rocks.rs @@ -111,7 +111,6 @@ pub fn new_cozo_rocksdb(path: impl AsRef) -> Result> { } /// RocksDB storage engine -#[derive(Clone)] pub struct RocksDbStorage { db: RocksDb, } diff --git a/cozo-core/src/storage/sled.rs b/cozo-core/src/storage/sled.rs index c48a837f..5b00c6f2 100644 --- a/cozo-core/src/storage/sled.rs +++ b/cozo-core/src/storage/sled.rs @@ -33,7 +33,6 @@ pub fn new_cozo_sled(path: impl AsRef) -> Result> { } /// Storage engine using Sled -#[derive(Clone)] pub struct SledStorage { db: Db, } diff --git a/cozo-core/src/storage/sqlite.rs b/cozo-core/src/storage/sqlite.rs index c39ae0f1..28e5aed6 100644 --- a/cozo-core/src/storage/sqlite.rs +++ b/cozo-core/src/storage/sqlite.rs @@ -22,11 +22,10 @@ use crate::storage::{Storage, StoreTx}; use crate::utils::swap_option_result; /// The Sqlite storage engine -#[derive(Clone)] pub struct SqliteStorage { lock: Arc>, name: PathBuf, - pool: Arc>>, + pool: Mutex>, } /// Create a sqlite backed database. @@ -50,9 +49,9 @@ pub fn new_cozo_sqlite(path: impl AsRef) -> Result Storage<'s> for TempStorage { diff --git a/cozo-core/src/storage/tikv.rs b/cozo-core/src/storage/tikv.rs index 3b5370ca..ce1971a5 100644 --- a/cozo-core/src/storage/tikv.rs +++ b/cozo-core/src/storage/tikv.rs @@ -52,7 +52,6 @@ lazy_static! { } /// Storage engine based on TiKV -#[derive(Clone)] pub struct TiKvStorage { client: Arc, raw_client: Arc, diff --git a/cozorocks/bridge/db.cpp b/cozorocks/bridge/db.cpp index 4c442a5d..bb05b10d 100644 --- a/cozorocks/bridge/db.cpp +++ b/cozorocks/bridge/db.cpp @@ -53,7 +53,7 @@ ColumnFamilyOptions default_cf_options() { return options; } -shared_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { +unique_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { auto options = default_db_options(); shared_ptr cache = nullptr; @@ -120,7 +120,7 @@ shared_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { } options.create_missing_column_families = true; - shared_ptr db = make_shared(); + unique_ptr db = make_unique(); db->db_path = convert_vec_to_string(opts.db_path); diff --git a/cozorocks/bridge/db.h b/cozorocks/bridge/db.h index fc7e462a..7133d3ce 100644 --- a/cozorocks/bridge/db.h +++ b/cozorocks/bridge/db.h @@ -117,7 +117,7 @@ struct RocksDbBridge { ~RocksDbBridge(); }; -shared_ptr +unique_ptr open_db(const DbOpts &opts, RocksDbStatus &status); #endif //COZOROCKS_DB_H diff --git a/cozorocks/src/bridge/db.rs b/cozorocks/src/bridge/db.rs index f65998c1..19352461 100644 --- a/cozorocks/src/bridge/db.rs +++ b/cozorocks/src/bridge/db.rs @@ -133,9 +133,8 @@ impl DbBuilder { } } -#[derive(Clone)] pub struct RocksDb { - inner: SharedPtr, + inner: UniquePtr, } impl RocksDb { diff --git a/cozorocks/src/bridge/mod.rs b/cozorocks/src/bridge/mod.rs index dabccb4f..576fcbe2 100644 --- a/cozorocks/src/bridge/mod.rs +++ b/cozorocks/src/bridge/mod.rs @@ -125,7 +125,7 @@ pub(crate) mod ffi { fn open_db( builder: &DbOpts, status: &mut RocksDbStatus, - ) -> SharedPtr; + ) -> UniquePtr; fn transact(self: &RocksDbBridge) -> UniquePtr; fn del_range( self: &RocksDbBridge,