This commit is contained in:
2025-08-16 09:50:56 +02:00
parent 0000d82799
commit dbd0635cd9
11 changed files with 139 additions and 142 deletions

View File

@@ -1,4 +1,5 @@
use crate::{error::DBError, protocol::Protocol, server::Server};
use serde::Serialize;
#[derive(Debug, Clone)]
pub enum Cmd {
@@ -444,7 +445,7 @@ impl Cmd {
Cmd::Del(k) => del_cmd(server, k).await,
Cmd::ConfigGet(name) => config_get_cmd(name, server),
Cmd::Keys => keys_cmd(server).await,
Cmd::Info(section) => info_cmd(section),
Cmd::Info(section) => info_cmd(server, section).await,
Cmd::Type(k) => type_cmd(server, k).await,
Cmd::Incr(key) => incr_cmd(server, key).await,
Cmd::Multi => {
@@ -640,19 +641,23 @@ async fn incr_cmd(server: &Server, key: &String) -> Result<Protocol, DBError> {
}
fn config_get_cmd(name: &String, server: &Server) -> Result<Protocol, DBError> {
let mut result = Vec::new();
result.push(Protocol::BulkString(name.clone()));
match name.as_str() {
"dir" => Ok(Protocol::Array(vec![
Protocol::BulkString(name.clone()),
Protocol::BulkString(server.option.dir.clone()),
])),
"dbfilename" => Ok(Protocol::Array(vec![
Protocol::BulkString(name.clone()),
Protocol::BulkString(format!("{}.db", server.selected_db)),
])),
"databases" => Ok(Protocol::Array(vec![
Protocol::BulkString(name.clone()),
Protocol::BulkString(server.option.max_databases.unwrap_or(0).to_string()),
])),
"dir" => {
result.push(Protocol::BulkString(server.option.dir.clone()));
Ok(Protocol::Array(result))
}
"dbfilename" => {
result.push(Protocol::BulkString(format!("{}.db", server.selected_db)));
Ok(Protocol::Array(result))
},
"databases" => {
// This is hardcoded, as the feature was removed
result.push(Protocol::BulkString("16".to_string()));
Ok(Protocol::Array(result))
},
_ => Ok(Protocol::Array(vec![])),
}
}
@@ -664,7 +669,24 @@ async fn keys_cmd(server: &Server) -> Result<Protocol, DBError> {
))
}
fn info_cmd(section: &Option<String>) -> Result<Protocol, DBError> {
#[derive(Serialize)]
struct ServerInfo {
redis_version: String,
encrypted: bool,
}
async fn info_cmd(server: &Server, section: &Option<String>) -> Result<Protocol, DBError> {
let info = ServerInfo {
redis_version: "7.0.0".to_string(),
encrypted: server.current_storage()?.is_encrypted(),
};
let mut info_string = String::new();
info_string.push_str(&format!("# Server\n"));
info_string.push_str(&format!("redis_version:{}\n", info.redis_version));
info_string.push_str(&format!("encrypted:{}\n", if info.encrypted { 1 } else { 0 }));
match section {
Some(s) => match s.as_str() {
"replication" => Ok(Protocol::BulkString(
@@ -672,7 +694,9 @@ fn info_cmd(section: &Option<String>) -> Result<Protocol, DBError> {
)),
_ => Err(DBError(format!("unsupported section {:?}", s))),
},
None => Ok(Protocol::BulkString("# Server\nredis_version:7.0.0\n".to_string())),
None => {
Ok(Protocol::BulkString(info_string))
}
}
}