This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod get; | |
pub use get::Get; | |
mod publish; | |
pub use publish::Publish; | |
mod set; | |
pub use set::Set; | |
mod subscribe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub mod blocking_client; | |
pub mod client; | |
pub mod cmd; | |
pub use cmd::Command; | |
mod connection; | |
pub use connection::Connection; | |
pub mod frame; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Run the mini-redis server. | |
/// | |
/// Accepts connections from the supplied listener. For each inbound connection, | |
/// a task is spawned to handle that connection. The server runs until the | |
/// `shutdown` future completes, at which point the server shuts down | |
/// gracefully. | |
/// | |
/// `tokio::signal::ctrl_c()` can be used as the `shutdown` argument. This will | |
/// listen for a SIGINT signal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Per-connection handler. Reads requests from `connection` and applies the | |
/// commands to `db`. | |
#[derive(Debug)] | |
struct Handler { | |
/// Shared database handle. | |
/// | |
/// When a command is received from `connection`, it is applied with `db`. | |
/// The implementation of the command is in the `cmd` module. Each command | |
/// will need to interact with `db` in order to complete the work. | |
db: Db, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Server listener state. Created in the `run` call. It includes a `run` method | |
/// which performs the TCP listening and initialization of per-connection state. | |
#[derive(Debug)] | |
struct Listener { | |
/// Shared database handle. | |
/// | |
/// Contains the key / value store as well as the broadcast channels for | |
/// pub/sub. | |
/// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::rc::Rc; | |
use std::cell::RefCell; | |
impl Solution { | |
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { | |
match root { | |
Some(root) => { | |
let root = root.borrow(); | |
return 1 + std::cmp::max( | |
Self::max_depth(root.left.clone()), | |
Self::max_depth(root.right.clone()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definition for a binary tree node. | |
// #[derive(Debug, PartialEq, Eq)] | |
// pub struct TreeNode { | |
// pub val: i32, | |
// pub left: Option<Rc<RefCell<TreeNode>>>, | |
// pub right: Option<Rc<RefCell<TreeNode>>>, | |
// } | |
// | |
// impl TreeNode { | |
// #[inline] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definition for a binary tree node. | |
// #[derive(Debug, PartialEq, Eq)] | |
// pub struct TreeNode { | |
// pub val: i32, | |
// pub left: Option<Rc<RefCell<TreeNode>>>, | |
// pub right: Option<Rc<RefCell<TreeNode>>>, | |
// } | |
// | |
// impl TreeNode { | |
// #[inline] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn handle_request(line: &str, db: &Arc<Database>) -> Response { | |
let request = match Request::parse(line) { | |
Ok(req) => req, | |
Err(e) => return Response::Error { msg: e }, | |
}; | |
let mut db = db.map.lock().unwrap(); | |
match request { | |
Request::Get { key } | |
=> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn Error>> { | |
// Parse the address we're going to run this server on | |
// and set up our TCP listener to accept connections. | |
let addr = env::args() | |
.nth(1) | |
.unwrap_or_else(|| "127.0.0.1:8080".to_string()); | |
let listener = TcpListener::bind(&addr).await?; | |
println!("Listening on: {}", addr); |