Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt 2: Move fastn_ds::wasm::Store to fastn_wasm::Store #2043

Merged
merged 6 commits into from
Dec 26, 2024

Conversation

Arpita-Jaiswal
Copy link
Contributor

@Arpita-Jaiswal Arpita-Jaiswal commented Dec 25, 2024

This PR moves fastn_ds::wasm::Store to fastn_wasm::Store. With moving Store, we also achieved moving fastn_ds::pg, fastn_ds::sqlite, and fastn_ds::http. This became possible by introducing StoreExt and ConnectionExt, and fastn_ds implemented their methods. In the future, other projects and their crates like ft can use it and implement their methods according to their own requirements.

pub struct Store<STORE: StoreExt> {
    pub req: ft_sys_shared::Request,
    pub clients: std::sync::Arc<async_lock::Mutex<Vec<Conn>>>,
    pub pg_pools: actix_web::web::Data<scc::HashMap<String, deadpool_postgres::Pool>>,
    pub sqlite: Option<std::sync::Arc<async_lock::Mutex<Box<dyn ConnectionExt>>>>,
    pub response: Option<ft_sys_shared::Request>,
    pub db_url: String,
    pub inner: STORE,
}
pub trait StoreExt: Send {
    fn get_db_url(&self, store_db_url: &str, db_url: &str) -> String;
    fn connection_open(
        &self,
        store_db_url: &str,
        db_url: &str,
    ) -> wasmtime::Result<Box<dyn ConnectionExt>>;
}

pub struct Conn {
    pub client: deadpool::managed::Object<deadpool_postgres::Manager>,
}

impl<STORE: StoreExt> Store<STORE> {
    pub fn new(
        req: ft_sys_shared::Request,
        pg_pools: actix_web::web::Data<scc::HashMap<String, deadpool_postgres::Pool>>,
        db_url: String,
        inner: STORE,
    ) -> Store<STORE> {
        Self {
            req,
            response: None,
            clients: Default::default(),
            pg_pools,
            db_url,
            sqlite: None,
            inner,
        }
    }
}

#[derive(Debug)]
pub enum ExecuteError {
    Rusqlite(rusqlite::Error),
    InvalidQuery(String),
}

pub trait ConnectionExt: Send {
    fn prepare(&self, sql: &str) -> Result<rusqlite::Statement, ExecuteError>;
    fn execute(&self, query: &str, binds: Vec<Value>) -> Result<usize, ExecuteError>;
    fn execute_batch(&self, query: &str) -> Result<(), ExecuteError>;
}

pub type Value = ft_sys_shared::SqliteRawValue;

@Arpita-Jaiswal Arpita-Jaiswal marked this pull request as ready for review December 26, 2024 07:44
@Arpita-Jaiswal Arpita-Jaiswal merged commit 1c44bf7 into main Dec 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant