From f7dcdd5ddd62488d3dc5b0fee816abf96af282d4 Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sat, 29 May 2021 07:03:06 +0300 Subject: [PATCH] make SlabStore return Arc when create new object --- src/service/gateway.rs | 18 ++++++++++++------ src/slabstore.rs | 12 ++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/service/gateway.rs b/src/service/gateway.rs index 825437622..afc8016da 100644 --- a/src/service/gateway.rs +++ b/src/service/gateway.rs @@ -21,7 +21,7 @@ enum GatewayCommand { } pub struct GatewayService { - slabstore: SlabStore, + slabstore: Arc, addr: SocketAddr, publisher: Mutex, } @@ -76,7 +76,8 @@ impl GatewayService { let reply = Reply::from(&request, 0, vec![]); send_queue.send(reply).await?; - // publish to all subscribes self.publisher.lock().await.publish(slab).await?; + // publish to all subscribes + self.publisher.lock().await.publish(slab).await?; info!("received putslab msg"); } @@ -120,14 +121,14 @@ impl GatewayService { pub struct GatewayClient { protocol: ReqProtocol, - slabstore: SlabStore, + pub slabstore: Arc, } impl GatewayClient { - pub fn new(addr: SocketAddr) -> Result { + pub fn new(addr: SocketAddr, path: &str) -> Result { let protocol = ReqProtocol::new(addr); - let slabstore = SlabStore::new(Path::new("slabstore_client.db"))?; + let slabstore = SlabStore::new(Path::new(path))?; Ok(GatewayClient { protocol, @@ -177,7 +178,6 @@ impl GatewayClient { Ok(()) } - pub async fn get_last_index(&mut self) -> Result { let rep = self .protocol @@ -185,6 +185,11 @@ impl GatewayClient { .await?; Ok(deserialize(&rep)?) } + + pub fn get_slabstore(&self) -> Arc { + self.slabstore.clone() + } + } pub async fn fetch_slabs_loop( @@ -201,3 +206,4 @@ pub async fn fetch_slabs_loop( slabs.lock().await.push(slab); } } + diff --git a/src/slabstore.rs b/src/slabstore.rs index e24afc3b3..e20327fa1 100644 --- a/src/slabstore.rs +++ b/src/slabstore.rs @@ -8,20 +8,16 @@ use rocksdb::{IteratorMode, Options, DB}; pub struct SlabStore { db: DB, - opt: Options, - path: Arc, } impl SlabStore { - pub fn new(path: &Path) -> Result { + pub fn new(path: &Path) -> Result> { let mut opt = Options::default(); opt.create_if_missing(true); let db = DB::open(&opt, path)?; - let path = Arc::from(path); - - Ok(SlabStore { db, opt, path }) + Ok(Arc::new(SlabStore { db })) } pub fn get(&self, key: Vec) -> Result>> { @@ -67,8 +63,8 @@ impl SlabStore { } } - pub fn destroy(&self) -> Result<()> { - DB::destroy(&self.opt, self.path.clone())?; + pub fn destroy(path: &Path) -> Result<()> { + DB::destroy(&Options::default(), path)?; Ok(()) } }