Update process()

The process function no longer initializes a HashMap. Instead, it takes the shared handle to the HashMap as an argument. It also needs to lock the HashMap before using it.

1. use tokio::net::TcpStream;
2. use mini_redis::{Connection, Frame};

4. async fn process(socket: TcpStream, db: Db) {
5. use mini_redis::Command::{self, Get, Set};

7. // Connection, provided by `mini-redis`, handles parsing frames from
8. // the socket
9. let mut connection = Connection::new(socket);

11. while let Some(frame) = connection.read_frame().await.unwrap() {
12. let response = match Command::from_frame(frame).unwrap() {
13. Set(cmd) => {
14. let mut db = db.lock().unwrap();
15. db.insert(cmd.key().to_string(), cmd.value().clone());
16. Frame::Simple("OK".to_string())
17. }
18. Get(cmd) => {
19. let db = db.lock().unwrap();
20. if let Some(value) = db.get(cmd.key()) {
21. Frame::Bulk(value.clone())
22. } else {
23. Frame::Null
24. }
25. }
26. cmd => panic!("unimplemented {:?}", cmd),
27. };

29. // Write the response to the client
30. connection.write_frame(&response).await.unwrap();
31. }
32. }