From 91ebe758c152a56515938eaa4a0c505b4ce68b03 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Mon, 16 Jan 2023 16:07:33 +0800 Subject: [PATCH] make DB Clone again --- cozo-core/src/runtime/db.rs | 13 +++++++------ cozo-core/src/storage/mem.rs | 2 +- cozo-core/src/storage/mod.rs | 2 +- cozo-core/src/storage/rocks.rs | 1 + cozo-core/src/storage/sled.rs | 1 + cozo-core/src/storage/sqlite.rs | 5 +++-- cozo-core/src/storage/temp.rs | 2 +- cozo-core/src/storage/tikv.rs | 1 + cozo-lib-c/src/lib.rs | 6 +++--- cozo-lib-java/src/lib.rs | 8 ++++---- cozo-lib-nodejs/src/lib.rs | 4 ++-- cozorocks/bridge/db.cpp | 4 ++-- cozorocks/bridge/db.h | 2 +- cozorocks/src/bridge/db.rs | 3 ++- cozorocks/src/bridge/mod.rs | 2 +- cozoserver/src/main.rs | 17 +++++++---------- cozoserver/src/repl.rs | 3 +-- 17 files changed, 39 insertions(+), 37 deletions(-) diff --git a/cozo-core/src/runtime/db.rs b/cozo-core/src/runtime/db.rs index dcf50cc9..af428157 100644 --- a/cozo-core/src/runtime/db.rs +++ b/cozo-core/src/runtime/db.rs @@ -81,18 +81,19 @@ 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: AtomicU64, + queries_count: Arc, running_queries: Arc>>, - pub(crate) fixed_rules: ShardedLock>>>, + pub(crate) fixed_rules: Arc>>>>, #[cfg(not(target_arch = "wasm32"))] - callback_count: AtomicU32, + callback_count: Arc, #[cfg(not(target_arch = "wasm32"))] - pub(crate) event_callbacks: ShardedLock, - relation_locks: ShardedLock, Arc>>>, + pub(crate) event_callbacks: Arc>, + relation_locks: Arc, Arc>>>>, } impl Debug for Db { @@ -186,7 +187,7 @@ impl<'s, S: Storage<'s>> Db { relation_store_id: Default::default(), queries_count: Default::default(), running_queries: Default::default(), - fixed_rules: ShardedLock::new(DEFAULT_FIXED_RULES.clone()), + fixed_rules: Arc::new(ShardedLock::new(DEFAULT_FIXED_RULES.clone())), #[cfg(not(target_arch = "wasm32"))] callback_count: Default::default(), // callback_receiver: Arc::new(receiver), diff --git a/cozo-core/src/storage/mem.rs b/cozo-core/src/storage/mem.rs index c15e0a6f..27aae4b2 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(Default)] +#[derive(Default, Clone)] pub struct MemStorage { store: Arc, Vec>>>, } diff --git a/cozo-core/src/storage/mod.rs b/cozo-core/src/storage/mod.rs index b8017ac3..bec4544f 100644 --- a/cozo-core/src/storage/mod.rs +++ b/cozo-core/src/storage/mod.rs @@ -26,7 +26,7 @@ pub(crate) mod tikv; // pub(crate) mod re; /// Swappable storage trait for Cozo's storage engine -pub trait Storage<'s>: Sync { +pub trait Storage<'s>: Sync + Clone { /// The associated transaction type used by this engine type Tx: StoreTx<'s>; diff --git a/cozo-core/src/storage/rocks.rs b/cozo-core/src/storage/rocks.rs index 9a15aa9a..198669f2 100644 --- a/cozo-core/src/storage/rocks.rs +++ b/cozo-core/src/storage/rocks.rs @@ -111,6 +111,7 @@ 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 5b00c6f2..c48a837f 100644 --- a/cozo-core/src/storage/sled.rs +++ b/cozo-core/src/storage/sled.rs @@ -33,6 +33,7 @@ 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 28e5aed6..916c915f 100644 --- a/cozo-core/src/storage/sqlite.rs +++ b/cozo-core/src/storage/sqlite.rs @@ -22,10 +22,11 @@ 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: Mutex>, + pool: Arc>>, } /// Create a sqlite backed database. @@ -51,7 +52,7 @@ 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 ce1971a5..3b5370ca 100644 --- a/cozo-core/src/storage/tikv.rs +++ b/cozo-core/src/storage/tikv.rs @@ -52,6 +52,7 @@ lazy_static! { } /// Storage engine based on TiKV +#[derive(Clone)] pub struct TiKvStorage { client: Arc, raw_client: Arc, diff --git a/cozo-lib-c/src/lib.rs b/cozo-lib-c/src/lib.rs index ff8faf5b..2580e161 100644 --- a/cozo-lib-c/src/lib.rs +++ b/cozo-lib-c/src/lib.rs @@ -12,7 +12,7 @@ use std::collections::BTreeMap; use std::ffi::{c_char, CStr, CString}; use std::ptr::null_mut; use std::sync::atomic::{AtomicI32, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use lazy_static::lazy_static; @@ -20,7 +20,7 @@ use cozo::*; struct Handles { current: AtomicI32, - dbs: Mutex>>, + dbs: Mutex>, } lazy_static! { @@ -69,7 +69,7 @@ pub unsafe extern "C" fn cozo_open_db( let id = HANDLES.current.fetch_add(1, Ordering::AcqRel); let mut dbs = HANDLES.dbs.lock().unwrap(); - dbs.insert(id, Arc::new(db)); + dbs.insert(id, db); *db_id = id; null_mut() } diff --git a/cozo-lib-java/src/lib.rs b/cozo-lib-java/src/lib.rs index f7267026..84ec1148 100644 --- a/cozo-lib-java/src/lib.rs +++ b/cozo-lib-java/src/lib.rs @@ -7,7 +7,7 @@ */ use std::collections::BTreeMap; use std::sync::atomic::{AtomicI32, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use jni::objects::{JClass, JString}; use jni::sys::{jboolean, jint, jstring}; @@ -19,7 +19,7 @@ use cozo::*; #[derive(Default)] struct Handles { current: AtomicI32, - dbs: Mutex>>, + dbs: Mutex>, } lazy_static! { @@ -29,7 +29,7 @@ lazy_static! { }; } -fn get_db(id: i32) -> Option> { +fn get_db(id: i32) -> Option { let dbs = HANDLES.dbs.lock().unwrap(); dbs.get(&id).cloned() } @@ -49,7 +49,7 @@ pub extern "system" fn Java_org_cozodb_CozoJavaBridge_openDb( Ok(db) => { let id = HANDLES.current.fetch_add(1, Ordering::AcqRel); let mut dbs = HANDLES.dbs.lock().unwrap(); - dbs.insert(id, Arc::new(db)); + dbs.insert(id, db); id } Err(err) => { diff --git a/cozo-lib-nodejs/src/lib.rs b/cozo-lib-nodejs/src/lib.rs index b67cb4d1..74f4f75b 100644 --- a/cozo-lib-nodejs/src/lib.rs +++ b/cozo-lib-nodejs/src/lib.rs @@ -204,7 +204,7 @@ fn params2js<'a>( #[derive(Default)] struct Handles { current: AtomicU32, - dbs: Mutex>>, + dbs: Mutex>, cb_idx: AtomicU32, current_cbs: Mutex>>>, } @@ -221,7 +221,7 @@ fn open_db(mut cx: FunctionContext) -> JsResult { Ok(db) => { let id = HANDLES.current.fetch_add(1, Ordering::AcqRel); let mut dbs = HANDLES.dbs.lock().unwrap(); - dbs.insert(id, Arc::new(db)); + dbs.insert(id, db); Ok(cx.number(id)) } Err(err) => { diff --git a/cozorocks/bridge/db.cpp b/cozorocks/bridge/db.cpp index bb05b10d..4c442a5d 100644 --- a/cozorocks/bridge/db.cpp +++ b/cozorocks/bridge/db.cpp @@ -53,7 +53,7 @@ ColumnFamilyOptions default_cf_options() { return options; } -unique_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { +shared_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { auto options = default_db_options(); shared_ptr cache = nullptr; @@ -120,7 +120,7 @@ unique_ptr open_db(const DbOpts &opts, RocksDbStatus &status) { } options.create_missing_column_families = true; - unique_ptr db = make_unique(); + shared_ptr db = make_shared(); db->db_path = convert_vec_to_string(opts.db_path); diff --git a/cozorocks/bridge/db.h b/cozorocks/bridge/db.h index 7133d3ce..fc7e462a 100644 --- a/cozorocks/bridge/db.h +++ b/cozorocks/bridge/db.h @@ -117,7 +117,7 @@ struct RocksDbBridge { ~RocksDbBridge(); }; -unique_ptr +shared_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 19352461..f65998c1 100644 --- a/cozorocks/src/bridge/db.rs +++ b/cozorocks/src/bridge/db.rs @@ -133,8 +133,9 @@ impl DbBuilder { } } +#[derive(Clone)] pub struct RocksDb { - inner: UniquePtr, + inner: SharedPtr, } impl RocksDb { diff --git a/cozorocks/src/bridge/mod.rs b/cozorocks/src/bridge/mod.rs index 366afbaf..b41da66a 100644 --- a/cozorocks/src/bridge/mod.rs +++ b/cozorocks/src/bridge/mod.rs @@ -122,7 +122,7 @@ pub(crate) mod ffi { type RocksDbBridge; fn get_db_path(self: &RocksDbBridge) -> &CxxString; - fn open_db(builder: &DbOpts, status: &mut RocksDbStatus) -> UniquePtr; + fn open_db(builder: &DbOpts, status: &mut RocksDbStatus) -> SharedPtr; fn transact(self: &RocksDbBridge) -> UniquePtr; fn del_range(self: &RocksDbBridge, lower: &[u8], upper: &[u8], status: &mut RocksDbStatus); fn put(self: &RocksDbBridge, key: &[u8], val: &[u8], status: &mut RocksDbStatus); diff --git a/cozoserver/src/main.rs b/cozoserver/src/main.rs index 0c6bdbab..c0ec1dae 100644 --- a/cozoserver/src/main.rs +++ b/cozoserver/src/main.rs @@ -12,7 +12,6 @@ use std::fs; use std::net::Ipv6Addr; use std::process::exit; use std::str::FromStr; -use std::sync::Arc; use clap::Parser; use env_logger::Env; @@ -78,14 +77,12 @@ fn main() { eprintln!("{SECURITY_WARNING}"); } - let db = Arc::new( - DbInstance::new( - args.engine.as_str(), - args.path.as_str(), - &args.config.clone(), - ) - .unwrap(), - ); + let db = DbInstance::new( + args.engine.as_str(), + args.path.as_str(), + &args.config.clone(), + ) + .unwrap(); if let Some(restore_path) = &args.restore { db.restore_backup(restore_path).unwrap(); @@ -117,7 +114,7 @@ fn main() { } } -fn server_main(args: Args, db: Arc) { +fn server_main(args: Args, db: DbInstance) { let conf_path = format!("{}.{}.cozo_auth", args.path, args.engine); let auth_guard = match fs::read_to_string(&conf_path) { Ok(s) => s.trim().to_string(), diff --git a/cozoserver/src/repl.rs b/cozoserver/src/repl.rs index f76871aa..6b0a4e5d 100644 --- a/cozoserver/src/repl.rs +++ b/cozoserver/src/repl.rs @@ -12,7 +12,6 @@ use std::collections::BTreeMap; use std::error::Error; use std::fs::File; use std::io::{Read, Write}; -use std::sync::Arc; use miette::{bail, miette, IntoDiagnostic}; use serde_json::{json, Value}; @@ -58,7 +57,7 @@ impl rustyline::validate::Validator for Indented { } } -pub(crate) fn repl_main(db: Arc) -> Result<(), Box> { +pub(crate) fn repl_main(db: DbInstance) -> Result<(), Box> { println!("Welcome to the Cozo REPL."); println!("Type a space followed by newline to enter multiline mode.");