diff options
Diffstat (limited to 'src/resp_commands.rs')
| -rw-r--r-- | src/resp_commands.rs | 89 |
1 files changed, 57 insertions, 32 deletions
diff --git a/src/resp_commands.rs b/src/resp_commands.rs index 3c18b07..03ec9dc 100644 --- a/src/resp_commands.rs +++ b/src/resp_commands.rs @@ -1,6 +1,7 @@ +use crate::server::*; use crate::{resp_parser::*, shared_cache::*}; -use crate::{RedisServer, SharedConfig}; use regex::Regex; +use std::sync::{Arc, Mutex}; use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Clone)] @@ -123,13 +124,14 @@ pub enum RedisCommands { } impl RedisCommands { - pub fn execute(self, cache: SharedCache, config: SharedConfig) -> Vec<u8> { + pub fn execute(self, server: Arc<Mutex<RedisServer>>) -> Vec<u8> { use RedisCommands as RC; match self { RC::Ping => resp_bytes!("PONG"), RC::Echo(echo_string) => resp_bytes!(echo_string), RC::Get(key) => { - let mut cache = cache.lock().unwrap(); + let server = server.lock().unwrap(); + let mut cache = server.cache().lock().unwrap(); match cache.get(&key).cloned() { Some(entry) => { @@ -144,7 +146,8 @@ impl RedisCommands { } } RC::Set(command) => { - let mut cache = cache.lock().unwrap(); + let mut server = server.lock().unwrap(); + let mut cache = server.cache().lock().unwrap(); // Check conditions (NX/XX) let key_exists = cache.contains_key(&command.key); @@ -187,6 +190,18 @@ impl RedisCommands { }, ); + // Broadcast SET to replicas after mutating local state + let broadcast_cmd = resp_bytes!(array => [ + resp!(bulk "SET"), + resp!(bulk command.key), + resp!(bulk command.value) + ]); + + // Unlock the mutex so that I can access to broadcast the messaage + drop(cache); + + server.broadcast_command(&broadcast_cmd); + if !command.get_old_value { return resp_bytes!("OK"); } @@ -198,10 +213,8 @@ impl RedisCommands { } RC::ConfigGet(s) => { use RespType as RT; - let config = config.clone(); - if let Some(conf) = config.as_ref() { - let dir = conf.dir.clone().unwrap(); - let dbfilename = conf.dbfilename.clone().unwrap(); + let server = server.lock().unwrap(); + if let (Some(dir), Some(dbfilename)) = (server.dir(), server.dbfilename()) { match s.as_str() { "dir" => RT::Array(vec