This commit is contained in:
2025-08-16 09:06:33 +02:00
parent 0511dddd99
commit 5502ff4bc5
10 changed files with 151 additions and 225 deletions

View File

@@ -12,27 +12,36 @@ use crate::storage::Storage;
#[derive(Clone)]
pub struct Server {
pub storage: Arc<Storage>,
pub storages: Vec<Arc<Storage>>,
pub option: options::DBOption,
pub client_name: Option<String>,
pub selected_db: usize, // per-connection
}
impl Server {
pub async fn new(option: options::DBOption) -> Self {
// Create database file path with fixed filename
let db_file_path = PathBuf::from(option.dir.clone()).join("herodb.redb");
println!("will open db file path: {}", db_file_path.display());
// Initialize storage with redb
let storage = Storage::new(db_file_path).expect("Failed to initialize storage");
// Eagerly create N db files: <dir>/<index>.db
let mut storages = Vec::with_capacity(option.databases as usize);
for i in 0..option.databases {
let db_file_path = PathBuf::from(option.dir.clone()).join(format!("{}.db", i));
println!("will open db file path (db {}): {}", i, db_file_path.display());
let storage = Storage::new(db_file_path).expect("Failed to initialize storage");
storages.push(Arc::new(storage));
}
Server {
storage: Arc::new(storage),
storages,
option,
client_name: None,
selected_db: 0,
}
}
#[inline]
pub fn current_storage(&self) -> &Storage {
self.storages[self.selected_db].as_ref()
}
pub async fn handle(
&mut self,
mut stream: tokio::net::TcpStream,