rebuild file structur + simple sqlite db endpoint
This commit is contained in:
35
src/utils/db_pool.rs
Normal file
35
src/utils/db_pool.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::sync::Arc;
|
||||
use dashmap::DashMap;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
type HotelId = i32; // or i32 if you want numeric ids
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HotelPools {
|
||||
pools: Arc<DashMap<HotelId, Pool<SqliteConnectionManager>>>,
|
||||
}
|
||||
|
||||
impl HotelPools {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pools: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pool(&self, hotel_id: i32) -> Pool<SqliteConnectionManager> {
|
||||
if let Some(pool) = self.pools.get(&hotel_id) {
|
||||
return pool.clone();
|
||||
}
|
||||
|
||||
let db_path = format!("db/{}.sqlite", hotel_id);
|
||||
let manager = SqliteConnectionManager::file(db_path);
|
||||
let pool = Pool::builder()
|
||||
.max_size(5) // adjust based on load
|
||||
.build(manager)
|
||||
.expect("Failed to build pool");
|
||||
|
||||
self.pools.insert(hotel_id, pool.clone());
|
||||
pool
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user