Просмотр исходного кода

make SlabStore return Arc<Self> when create new object

ghassmo 5 лет назад
Родитель
Сommit
f7dcdd5ddd
2 измененных файлов с 16 добавлено и 14 удалено
  1. 12 6
      src/service/gateway.rs
  2. 4 8
      src/slabstore.rs

+ 12 - 6
src/service/gateway.rs

@@ -21,7 +21,7 @@ enum GatewayCommand {
 }
 
 pub struct GatewayService {
-    slabstore: SlabStore,
+    slabstore: Arc<SlabStore>,
     addr: SocketAddr,
     publisher: Mutex<Publisher>,
 }
@@ -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<SlabStore>,
 }
 
 impl GatewayClient {
-    pub fn new(addr: SocketAddr) -> Result<GatewayClient> {
+    pub fn new(addr: SocketAddr, path: &str) -> Result<Self> {
         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<u64> {
         let rep = self
             .protocol
@@ -185,6 +185,11 @@ impl GatewayClient {
             .await?;
         Ok(deserialize(&rep)?)
     }
+
+    pub fn get_slabstore(&self) -> Arc<SlabStore> {
+        self.slabstore.clone()
+    }
+
 }
 
 pub async fn fetch_slabs_loop(
@@ -201,3 +206,4 @@ pub async fn fetch_slabs_loop(
         slabs.lock().await.push(slab);
     }
 }
+

+ 4 - 8
src/slabstore.rs

@@ -8,20 +8,16 @@ use rocksdb::{IteratorMode, Options, DB};
 
 pub struct SlabStore {
     db: DB,
-    opt: Options,
-    path: Arc<Path>,
 }
 
 impl SlabStore {
-    pub fn new(path: &Path) -> Result<Self> {
+    pub fn new(path: &Path) -> Result<Arc<Self>> {
         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<u8>) -> Result<Option<Vec<u8>>> {
@@ -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(())
     }
 }