aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 78b9c24334ccc78e8e87e32af53a9d79cdb98186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#![allow(unused_imports)]
use core::time;
use std::{
    collections::HashMap,
    env,
    fmt::format,
    io::{Read, Write},
    net::{TcpListener, TcpStream},
    sync::{Arc, Mutex},
    thread,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use codecrafters_redis::{
    rdb::{KeyExpiry, ParseError, RDBFile, RedisValue},
    resp_bytes,
    shared_cache::*,
};
use codecrafters_redis::{resp_commands::RedisCommands, Config};
use codecrafters_redis::{
    resp_parser::{parse, RespType},
    SharedConfig,
};

fn spawn_cleanup_thread(cache: SharedCache) {
    let cache_clone = cache.clone();
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(Duration::from_secs(10)); // Check every 10 seconds

            let mut cache = cache_clone.lock().unwrap();
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64;

            // Remove expired keys
            cache.retain(|_, entry| entry.expires_at.map_or(true, |expiry| now <= expiry));
        }
    });
}

use base64::{engine::general_purpose, Engine as _};

fn write_rdb_to_stream<W: Write>(writer: &mut W) -> Result<(), Box<dyn std::error::Error>> {
    let hardcoded_rdb = "UkVESVMwMDEx+glyZWRpcy12ZXIFNy4yLjD6CnJlZGlzLWJpdHPAQPoFY3RpbWXCbQi8ZfoIdXNlZC1tZW3CsMQQAPoIYW9mLWJhc2XAAP/wbjv+wP9aog==";

    let bytes = general_purpose::STANDARD.decode(hardcoded_rdb)?;

    let mut response = format!("${}\r\n", bytes.len()).into_bytes();
    response.extend_from_slice(&bytes);

    // Write the binary RDB data
    writer.write_all(&response)?;

    Ok(())
}

// TODO: This should return a Result to handle the plethora of different errors
fn handle_client(mut stream: TcpStream, cache: SharedCache, config: SharedConfig) {
    let mut buffer = [0; 512];

    loop {
        let _ = match stream.read(&mut buffer) {
            Ok(0) => return, // connection closed
            Ok(n) => n,
            Err(_) => return, // error occurred
        };

        let request = parse(&buffer).unwrap();
        let response =
            RedisCommands::from(request.0.clone()).execute(cache.clone(), config.clone());

        let mut request_command = "".to_string();

        // FIXME: Find a solution for this mess!!
        match &request.0 {
            RespType::Array(arr) => {
                if let RespType::BulkString(s) = arr[0].clone() {
                    request_command = String::from_utf8(s).unwrap();
                }
            }
            _ => {}
        }

        // if this true immediately write and send back rdb file after response
        // HACK: This just feels wrong I feel this shouldn't be handled here and should be handled
        // in the exexute command
        if request_command.starts_with("PSYNC") {
            stream.write(&response).unwrap();
            let _ = write_rdb_to_stream(&mut stream);
        } else {
            // write respose back to the client
            stream.write(&response).unwrap();
        }
    }
}

fn main() -> std::io::Result<()> {
    let cache: SharedCache = Arc::new(Mutex::new(HashMap::new()));
    let mut config: SharedConfig = Arc::new(None);
    let mut port = "6379".to_string();

    match Config::new() {
        Ok(conf) => {
            if let Some(conf) = conf {
                let mut cache = cache.lock().unwrap();
                let dir = conf.dir.clone().unwrap_or("".to_string());
                let dbfilename = conf.dbfilename.clone().unwrap_or("".to_string());
                let redis_server = conf.server.clone();
                port = redis_server.port.clone();
                if let Ok(rdb_file) = RDBFile::read(dir, dbfilename) {
                    if let Some(rdb) = rdb_file {
                        let hash_table = &rdb.databases.get(&0).unwrap().hash_table;

                        for (key, db_entry) in hash_table.iter() {
                            let value = match &db_entry.value {
                                RedisValue::String(data) => {
                                    String::from_utf8(data.clone()).unwrap()
                                }
                                RedisValue::Integer(data) => data.to_string(),
                                _ => {
                                    unreachable!()
                                }
                            };
                            let expires_at = if let Some(key_expiry) = &db_entry.expiry {
                                Some(key_expiry.timestamp)
                            } else {
                                None
                            };
                            let cache_entry = CacheEntry { value, expires_at };
                            cache.insert(String::from_utf8(key.clone()).unwrap(), cache_entry);
                        }
                    }
                }
                config = Arc::new(Some(conf));
            }
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        }
    }

    let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap();

    spawn_cleanup_thread(cache.clone());

    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                let cache_clone = cache.clone();
                let config_clone = Arc::clone(&config);
                thread::spawn(|| {
                    handle_client(stream, cache_clone, config_clone);
                });
            }
            Err(e) => {
                eprintln!("Connection failed: {}", e);
            }
        }
    }
    Ok(())
}