...
This commit is contained in:
@@ -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,
|
||||
|
Reference in New Issue
Block a user