Repository: Migorithm/duva
Pull request #63 (2024-11-28 04:46:43)

decoder -> endecoder, hirarchy change
`EnDecoder` is simple unit struct that wraps behaviours, namely decoding and encoding. 


Patch:
diff --git a/src/adapters/persistence/decoder/mod.rs b/src/adapters/persistence/decoder/mod.rs
index 3d154a9e..9fe58c58 100644
--- a/src/adapters/persistence/decoder/mod.rs
+++ b/src/adapters/persistence/decoder/mod.rs
@@ -6,18 +6,3 @@ use crate::services::statefuls::routers::interfaces::TDecodeData;
 mod builder;
 pub mod byte_decoder;
 pub mod states;
-
-#[derive(Default, Debug, Clone)]
-pub struct Decoder;
-
-impl TDecodeData for Decoder {
-    fn decode_data(
-        &self,
-        bytes: Vec<u8>,
-    ) -> anyhow::Result<crate::services::statefuls::persistence_models::RdbFile> {
-        let decoder: BytesDecoder<DecoderInit> = bytes.as_slice().into();
-        let database = decoder.load_header()?.load_metadata()?.load_database()?;
-        print!("database: {:?}", database);
-        Ok(database)
-    }
-}
diff --git a/src/adapters/persistence/mod.rs b/src/adapters/persistence/mod.rs
index 2c9593de..a198d100 100644
--- a/src/adapters/persistence/mod.rs
+++ b/src/adapters/persistence/mod.rs
@@ -78,8 +78,12 @@ use std::ops::RangeInclusive;
 use std::time::SystemTime;
 pub mod decoder;
 pub mod encoder;
+use decoder::byte_decoder::BytesDecoder;
+use decoder::states::DecoderInit;
 pub use encoder::byte_encoder;
 
+use crate::services::statefuls::routers::interfaces::TDecodeData;
+
 const HEADER_MAGIC_STRING: &str = "REDIS";
 const METADATA_SECTION_INDICATOR: u8 = 0xFA;
 const DATABASE_SECTION_INDICATOR: u8 = 0xFE;
@@ -111,3 +115,18 @@ impl StoredDuration {
         }
     }
 }
+
+#[derive(Default, Debug, Clone)]
+pub struct EnDecoder;
+
+impl TDecodeData for EnDecoder {
+    fn decode_data(
+        &self,
+        bytes: Vec<u8>,
+    ) -> anyhow::Result<crate::services::statefuls::persistence_models::RdbFile> {
+        let decoder: BytesDecoder<DecoderInit> = bytes.as_slice().into();
+        let database = decoder.load_header()?.load_metadata()?.load_database()?;
+        print!("database: {:?}", database);
+        Ok(database)
+    }
+}
diff --git a/src/main.rs b/src/main.rs
index a5d157a7..b07dc8c8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,12 @@
 use std::sync::OnceLock;
 
-use redis_starter_rust::{adapters::persistence::decoder::Decoder, config::Config, start_up};
+use redis_starter_rust::{adapters::persistence::EnDecoder, config::Config, start_up};
 
 const NUM_OF_PERSISTENCE: usize = 10;
 
 #[tokio::main]
 async fn main() -> anyhow::Result<()> {
-    start_up(config(), NUM_OF_PERSISTENCE, Decoder).await
+    start_up(config(), NUM_OF_PERSISTENCE, EnDecoder).await
 }
 
 static CONFIG: OnceLock<Config> = OnceLock::new();


Test patch:
diff --git a/src/test.rs b/src/test.rs
index ac2b9368..734aeb6d 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -1,6 +1,6 @@
 use std::sync::OnceLock;
 
-use crate::adapters::persistence::decoder::Decoder;
+use crate::adapters::persistence::EnDecoder;
 use crate::config::Config;
 use crate::services::query_manager::interface::TRead;
 use crate::services::query_manager::interface::TWriteBuf;
@@ -36,7 +36,7 @@ impl TWriteBuf for FakeStream {
     }
 }
 
-async fn get_key(key: &str, persistence_router: &CacheManager<Decoder>) -> QueryIO {
+async fn get_key(key: &str, persistence_router: &CacheManager<EnDecoder>) -> QueryIO {
     persistence_router.route_get(key.to_string()).await.unwrap()
 }
 
@@ -45,7 +45,7 @@ async fn set_key_with_no_expiry(
     value: &str,
 
     ttl_sender: TtlSchedulerInbox,
-    persistence_router: &CacheManager<Decoder>,
+    persistence_router: &CacheManager<EnDecoder>,
 ) -> QueryIO {
     persistence_router
         .route_set(
@@ -65,7 +65,7 @@ async fn set_key_with_no_expiry(
 /// OUTPUT(when get method is invoked on the key) : "value"
 #[tokio::test]
 async fn test_set() {
-    let (persistence_handlers, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (persistence_handlers, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     let stream = FakeStream {
         written: "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n"
@@ -98,7 +98,7 @@ async fn test_set_with_expiry() {
             .to_vec(),
     };
 
-    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     let mut controller = QueryManager::new(stream, test_config());
 
@@ -128,7 +128,7 @@ async fn test_set_with_expire_should_expire_within_100ms() {
             .as_bytes()
             .to_vec(),
     };
-    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     let mut controller = QueryManager::new(stream, test_config());
 
@@ -163,7 +163,7 @@ async fn test_config_get_dir() {
             .as_bytes()
             .to_vec(),
     };
-    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     let mut controller = QueryManager::new(stream, test_config());
 
@@ -182,7 +182,7 @@ async fn test_config_get_dir() {
 #[tokio::test]
 async fn test_keys() {
     //GIVEN
-    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     set_key_with_no_expiry("key", "value", ttl_inbox.clone(), &cache_dispatcher).await;
 
@@ -218,7 +218,7 @@ async fn test_keys() {
 #[tokio::test]
 async fn test_replication_info() {
     //GIVEN
-    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, Decoder);
+    let (cache_dispatcher, ttl_inbox) = CacheManager::run_cache_actors(3, EnDecoder);
 
     let stream = FakeStream {
         written: "*2\r\n$4\r\nINFO\r\n$11\r\nreplication\r\n"
diff --git a/tests/test_set_get.rs b/tests/test_set_get.rs
index e7925de6..177cc8ec 100644
--- a/tests/test_set_get.rs
+++ b/tests/test_set_get.rs
@@ -1,6 +1,6 @@
 mod common;
 use common::TestStreamHandler;
-use redis_starter_rust::{adapters::persistence::decoder::Decoder, config::Config, start_up};
+use redis_starter_rust::{adapters::persistence::EnDecoder, config::Config, start_up};
 use std::sync::OnceLock;
 use tokio::net::TcpStream;
 
@@ -14,7 +14,7 @@ async fn test_get_set() {
     // GIVEN
     let config = integration_test_config(11111);
 
-    tokio::spawn(start_up(config, 3, Decoder));
+    tokio::spawn(start_up(config, 3, EnDecoder));
     //warm up time
     tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
     let mut stream = TcpStream::connect(config.bind_addr()).await.unwrap();