File tree 4 files changed +29
-19
lines changed
4 files changed +29
-19
lines changed Original file line number Diff line number Diff line change 2
2
name = " sider"
3
3
description = " A Multithreaded Redis clone written from scratch in Rust."
4
4
license = " MIT"
5
- version = " 0.1.3 "
5
+ version = " 0.1.4 "
6
6
edition = " 2021"
7
7
8
8
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change @@ -398,20 +398,21 @@ pub fn handle_subscribe(
398
398
stream : TcpStream ,
399
399
) -> String {
400
400
let mut bus = bus. write ( ) . unwrap ( ) ;
401
- let value = bus . get_mut ( & req. key ) ;
402
- let topic_name = req . key . clone ( ) ;
403
- let topic_name_len = topic_name . len ( ) ;
404
-
405
- let response = format ! ( "*3 \r \n $9 \r \n subscribe \r \n ${topic_name_len} \r \n {topic_name} \r \n :1 \r \n " ) ;
406
- // let stream: Arc<&mut TcpStream> = Arc::clone(&stream );
407
- match value {
408
- Some ( streams ) => {
409
- streams . push ( stream ) ;
410
- response
411
- }
412
- None => {
413
- bus. insert ( topic_name , vec ! [ stream] ) ;
414
- response
415
- }
401
+ let topics = & req. value ;
402
+ let mut response = String :: new ( ) ;
403
+ for topic in topics {
404
+ let channel = bus . get_mut ( topic ) ;
405
+ let stream = stream . try_clone ( ) . unwrap ( ) ;
406
+ let topic_name_len = topic . len ( ) ;
407
+ response += & format ! ( "*3 \r \n $9 \r \n subscribe \r \n ${topic_name_len} \r \n {topic} \r \n :1 \r \n " ) ;
408
+ match channel {
409
+ Some ( streams ) => {
410
+ streams . push ( stream ) ;
411
+ }
412
+ None => {
413
+ bus. insert ( topic . to_string ( ) , vec ! [ stream] ) ;
414
+ }
415
+ } ;
416
416
}
417
+ response
417
418
}
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
1
3
use crate :: { Command , Request } ;
2
4
3
5
pub fn parse_resp ( s : & String ) -> Request {
@@ -38,9 +40,16 @@ pub fn parse_resp(s: &String) -> Request {
38
40
}
39
41
}
40
42
match command {
41
- Some ( c) => {
43
+ Some ( command) => {
44
+ if let Command :: SUBSCRIBE = command {
45
+ value. insert ( 0 , key) ;
46
+ // Remove duplicates as it's the official Redis behavior
47
+ let set: HashSet < _ > = value. drain ( ..) . collect ( ) ;
48
+ value. extend ( set) ;
49
+ key = String :: new ( ) ;
50
+ }
42
51
let req = Request {
43
- command : c ,
52
+ command,
44
53
key,
45
54
value,
46
55
} ;
You can’t perform that action at this time.
0 commit comments