From e84f7b7e3b6f57cbdb628ea1deeafe4ddf1dda4b Mon Sep 17 00:00:00 2001 From: Maxime Van Hees Date: Tue, 9 Sep 2025 17:37:18 +0200 Subject: [PATCH] working created DB over RPC --- herodb/src/main.rs | 2 +- herodb/src/rpc.rs | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/herodb/src/main.rs b/herodb/src/main.rs index 8aca68a..3a3be12 100644 --- a/herodb/src/main.rs +++ b/herodb/src/main.rs @@ -73,7 +73,7 @@ async fn main() { // Start RPC server if enabled let rpc_handle = if args.enable_rpc { let rpc_addr = format!("127.0.0.1:{}", args.rpc_port).parse().unwrap(); - let base_dir = format!("{}/rpc_databases", args.dir); + let base_dir = args.dir.clone(); match rpc_server::start_rpc_server(rpc_addr, Arc::clone(&server), base_dir).await { Ok(handle) => { diff --git a/herodb/src/rpc.rs b/herodb/src/rpc.rs index d2aced5..1ab27e3 100644 --- a/herodb/src/rpc.rs +++ b/herodb/src/rpc.rs @@ -106,17 +106,36 @@ impl RpcServer for RpcServerImpl { } async fn create_database(&self, db_index: u64) -> RpcResult { - // Pre-create the database by accessing it through the main server - let server_guard = self.main_server.lock().await; + // Lock the main server to create the database + let mut server_guard = self.main_server.lock().await; - // We can't directly modify selected_db, but we can try to access the storage - // This will create the database file if it doesn't exist - // Note: This is a simplified approach - in practice, we'd need to modify the server to allow database pre-creation + // Save the current selected_db to restore it later + let original_db = server_guard.selected_db; - println!("Note: Database {} will be created when first accessed via Redis protocol", db_index); - println!("Use: redis-cli -p 6379, then: SELECT {}", db_index); + // Temporarily set the selected_db to the target database + server_guard.selected_db = db_index; - Ok(true) + // Call current_storage() which will create the database file if it doesn't exist + match server_guard.current_storage() { + Ok(_) => { + println!("Successfully created database at index {}", db_index); + + // Restore the original selected_db + server_guard.selected_db = original_db; + + Ok(true) + } + Err(e) => { + // Restore the original selected_db even on error + server_guard.selected_db = original_db; + + Err(jsonrpsee::types::ErrorObjectOwned::owned( + -32000, + format!("Failed to create database {}: {}", db_index, e.0), + None::<()> + )) + } + } } async fn set_database_encryption(&self, db_index: u64, encryption_key: String) -> RpcResult {