Commit d05a7b5d authored by Aziz Berkay Yesilyurt's avatar Aziz Berkay Yesilyurt
Browse files

apply new changes related to pooling

parent ae1b43b5
Showing with 35 additions and 30 deletions
+35 -30
......@@ -15,28 +15,7 @@ pub type DBConnection = rusqlite::Connection;
pub struct DBConnectionManager {
init_db: Arc<RwLock<HashMap<String, Arc<RwLock<PathBuf>>>>>,
}
pub async fn remove_db(owner: &str, init_db: &InitDb,
database_key: &DatabaseKey,) -> Result<()> {
// gather write lock, so no other thread can access the the hashmap
let mut db = init_db.write().await;
// NOTE: how do we verify owner-database_key pair is valid?
// remove the pool from the hashmap
// NOTE: readers will create a new pool as if it was never there
db.remove(owner);
let database_path = format!("{}{}", &owner, constants::DATABASE_SUFFIX);
let database_path_buf = PathBuf::from(constants::DATABASE_DIR).join(database_path);
let database_path_str = database_path_buf.to_str().unwrap();
info!("Removing database at {:?}", database_path_str);
fs::remove_file(database_path_str);
fs::remove_file(format!("{}-wal", database_path_str));
fs::remove_file(format!("{}-shm", database_path_str));
Ok(())
}
impl DBConnectionManager {
pub fn new() -> Self {
Self {
......@@ -172,6 +151,32 @@ impl DBConnectionManager {
Ok(conn)
}
pub async fn remove_db(&self, owner: &str, database_key: &DatabaseKey,) -> Result<()> {
// gather write lock, so no other thread can access the the hashmap
let mut db = self.init_db.write().await;
let database_path = format!("{}{}", &owner, constants::DATABASE_SUFFIX);
let database_path = PathBuf::from(constants::DATABASE_DIR).join(database_path);
// Verify the ownership of the database
self.make_connection(database_key, &*database_path)?;
// remove the pool from the hashmap
// NOTE: readers will create a new pool as if it was never there
db.remove(owner);
let database_path = format!("{}{}", &owner, constants::DATABASE_SUFFIX);
let database_path_buf = PathBuf::from(constants::DATABASE_DIR).join(database_path);
let database_path_str = database_path_buf.to_str().unwrap();
// info!("Removing database at {:?}", database_path_str);
fs::remove_file(database_path_str);
fs::remove_file(format!("{}-wal", database_path_str));
fs::remove_file(format!("{}-shm", database_path_str));
Ok(())
}
}
impl Default for DBConnectionManager {
......
......@@ -226,10 +226,10 @@ pub async fn run_server<S: 'static>(cli_options: CliOptions, trace_handle: Handl
.and(warp::path!(PodOwner / "delete_user"))
.and(warp::path::end())
.and(warp::body::bytes())
.and(with_init_db())
.and(with_db())
.then(
|owner: PodOwner, body: Bytes, init_db: Arc<InitDb>| async move {
warp_endpoints::delete_user(owner, init_db.deref(), body).await
|owner: PodOwner, body: Bytes, db: DBConnectionManager| async move {
warp_endpoints::delete_user(owner, db, body).await
},
)
.boxed();
......
......@@ -300,18 +300,18 @@ async fn _delete_item(owner: PodOwner, db: DBConnectionManager, body: Bytes) ->
}
#[instrument(fields(uid=trace_uid(), %owner), skip_all)]
pub async fn delete_user(owner: PodOwner, init_db: &InitDb, body: Bytes) -> Response {
let result = _delete_user(owner, init_db, body).await;
pub async fn delete_user(owner: PodOwner, db: DBConnectionManager, body: Bytes) -> Response {
let result = _delete_user(owner, db, body).await;
let result = result.map(|()| warp::reply::json(&serde_json::json!({})));
respond_with_result(result)
}
async fn _delete_user(owner: PodOwner, init_db: &InitDb, body: Bytes) -> Result<()> {
async fn _delete_user(owner: PodOwner, db: DBConnectionManager, body: Bytes) -> Result<()> {
let body = &mut serde_json::Deserializer::from_slice(body.deref());
let body: PayloadWrapper<String> = serde_path_to_error::deserialize(body)?;
let auth = body.auth;
let database_key = auth_to_database_key(auth)?;
check_owner_and_delete_db(&owner, init_db, &database_key).await?;
check_owner_and_delete_db(&owner, db, &database_key).await?;
Result::Ok(())
}
......@@ -665,11 +665,11 @@ async fn check_owner_and_initialize_db(
/// As additional failsafe to the fact that non-owners don't have the database key.
async fn check_owner_and_delete_db(
owner: &str,
init_db: &InitDb,
db: DBConnectionManager,
database_key: &DatabaseKey,
) -> Result<()> {
check_owner(owner)?;
remove_db(owner, init_db, database_key).await;
db.remove_db(owner, database_key).await;
Result::Ok(())
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment