diff --git a/herodb/src/cmd.rs b/herodb/src/cmd.rs index 9817f89..176ed2f 100644 --- a/herodb/src/cmd.rs +++ b/herodb/src/cmd.rs @@ -1,5 +1,4 @@ use crate::{error::DBError, protocol::Protocol, server::Server}; -use serde::Serialize; use tokio::time::{timeout, Duration}; use futures::future::select_all; @@ -1093,26 +1092,23 @@ async fn dbsize_cmd(server: &Server) -> Result { } } -#[derive(Serialize)] -struct ServerInfo { - redis_version: String, - encrypted: bool, - selected_db: u64, -} - async fn info_cmd(server: &Server, section: &Option) -> Result { - let info = ServerInfo { - redis_version: "7.0.0".to_string(), - encrypted: server.current_storage()?.is_encrypted(), - selected_db: server.selected_db, - }; + let storage_info = server.current_storage()?.info()?; + let mut info_map: std::collections::HashMap = storage_info.into_iter().collect(); + + info_map.insert("redis_version".to_string(), "7.0.0".to_string()); + info_map.insert("selected_db".to_string(), server.selected_db.to_string()); + info_map.insert("backend".to_string(), format!("{:?}", server.option.backend)); + 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 })); - info_string.push_str(&format!("# Keyspace\n")); - info_string.push_str(&format!("db{}:keys=0,expires=0,avg_ttl=0\n", info.selected_db)); + info_string.push_str("# Server\n"); + info_string.push_str(&format!("redis_version:{}\n", info_map.get("redis_version").unwrap())); + info_string.push_str(&format!("backend:{}\n", info_map.get("backend").unwrap())); + info_string.push_str(&format!("encrypted:{}\n", info_map.get("is_encrypted").unwrap())); + + info_string.push_str("# Keyspace\n"); + info_string.push_str(&format!("db{}:keys={},expires=0,avg_ttl=0\n", info_map.get("selected_db").unwrap(), info_map.get("db_size").unwrap())); match section { Some(s) => { diff --git a/herodb/src/server.rs b/herodb/src/server.rs index 0c128c6..a6e43e2 100644 --- a/herodb/src/server.rs +++ b/herodb/src/server.rs @@ -12,10 +12,12 @@ use crate::error::DBError; use crate::options; use crate::protocol::Protocol; use crate::storage::Storage; +use crate::storage_sled::SledStorage; +use crate::storage_trait::StorageBackend; #[derive(Clone)] pub struct Server { - pub db_cache: std::sync::Arc>>>, + pub db_cache: std::sync::Arc>>>, pub option: options::DBOption, pub client_name: Option, pub selected_db: u64, // Changed from usize to u64 @@ -52,7 +54,7 @@ impl Server { } } - pub fn current_storage(&self) -> Result, DBError> { + pub fn current_storage(&self) -> Result, DBError> { let mut cache = self.db_cache.write().unwrap(); if let Some(storage) = cache.get(&self.selected_db) { @@ -73,11 +75,22 @@ impl Server { println!("Creating new db file: {}", db_file_path.display()); - let storage = Arc::new(Storage::new( - db_file_path, - self.should_encrypt_db(self.selected_db), - self.option.encryption_key.as_deref() - )?); + let storage: Arc = match self.option.backend { + options::BackendType::Redb => { + Arc::new(Storage::new( + db_file_path, + self.should_encrypt_db(self.selected_db), + self.option.encryption_key.as_deref() + )?) + } + options::BackendType::Sled => { + Arc::new(SledStorage::new( + db_file_path, + self.should_encrypt_db(self.selected_db), + self.option.encryption_key.as_deref() + )?) + } + }; cache.insert(self.selected_db, storage.clone()); Ok(storage) diff --git a/herodb/src/storage/mod.rs b/herodb/src/storage/mod.rs index 58dc5bf..abc2cd5 100644 --- a/herodb/src/storage/mod.rs +++ b/herodb/src/storage/mod.rs @@ -277,6 +277,10 @@ impl StorageBackend for Storage { self.is_encrypted() } + fn info(&self) -> Result, DBError> { + self.info() + } + fn clone_arc(&self) -> Arc { unimplemented!("Storage cloning not yet implemented for redb backend") } diff --git a/herodb/src/storage/storage_extra.rs b/herodb/src/storage/storage_extra.rs index 4f2e8f7..d918b58 100644 --- a/herodb/src/storage/storage_extra.rs +++ b/herodb/src/storage/storage_extra.rs @@ -208,6 +208,14 @@ impl Storage { write_txn.commit()?; Ok(applied) } + + pub fn info(&self) -> Result, DBError> { + let dbsize = self.dbsize()?; + Ok(vec![ + ("db_size".to_string(), dbsize.to_string()), + ("is_encrypted".to_string(), self.is_encrypted().to_string()), + ]) + } } // Utility function for glob pattern matching diff --git a/herodb/src/storage_sled/mod.rs b/herodb/src/storage_sled/mod.rs index d6fd862..ec22b88 100644 --- a/herodb/src/storage_sled/mod.rs +++ b/herodb/src/storage_sled/mod.rs @@ -825,8 +825,16 @@ impl StorageBackend for SledStorage { self.crypto.is_some() } + fn info(&self) -> Result, DBError> { + let dbsize = self.dbsize()?; + Ok(vec![ + ("db_size".to_string(), dbsize.to_string()), + ("is_encrypted".to_string(), self.is_encrypted().to_string()), + ]) + } + fn clone_arc(&self) -> Arc { - // Note: This is a simplified clone - in production you might want to + // Note: This is a simplified clone - in production you might want to // handle this differently as sled::Db is already Arc internally Arc::new(SledStorage { db: self.db.clone(), diff --git a/herodb/src/storage_trait.rs b/herodb/src/storage_trait.rs index 5a8f482..13fe11e 100644 --- a/herodb/src/storage_trait.rs +++ b/herodb/src/storage_trait.rs @@ -51,6 +51,7 @@ pub trait StorageBackend: Send + Sync { // Metadata fn is_encrypted(&self) -> bool; + fn info(&self) -> Result, DBError>; // Clone to Arc for sharing fn clone_arc(&self) -> Arc;