diff --git a/crates/semantic_index/src/db.rs b/crates/semantic_index/src/db.rs index 652c2819ce..313df40674 100644 --- a/crates/semantic_index/src/db.rs +++ b/crates/semantic_index/src/db.rs @@ -58,7 +58,8 @@ impl FromSql for Sha1 { #[derive(Clone)] pub struct VectorDatabase { path: Arc, - transactions: smol::channel::Sender>, + transactions: + smol::channel::Sender>, } impl VectorDatabase { @@ -71,15 +72,16 @@ impl VectorDatabase { fs.create_dir(db_directory).await?; } - let (transactions_tx, transactions_rx) = - smol::channel::unbounded::>(); + let (transactions_tx, transactions_rx) = smol::channel::unbounded::< + Box, + >(); executor .spawn({ let path = path.clone(); async move { - let connection = rusqlite::Connection::open(&path)?; + let mut connection = rusqlite::Connection::open(&path)?; while let Ok(transaction) = transactions_rx.recv().await { - transaction(&connection); + transaction(&mut connection); } anyhow::Ok(()) @@ -99,9 +101,9 @@ impl VectorDatabase { &self.path } - fn transact(&self, transaction: F) -> impl Future> + fn transact(&self, f: F) -> impl Future> where - F: 'static + Send + FnOnce(&rusqlite::Connection) -> Result, + F: 'static + Send + FnOnce(&rusqlite::Transaction) -> Result, T: 'static + Send, { let (tx, rx) = oneshot::channel(); @@ -109,7 +111,14 @@ impl VectorDatabase { async move { if transactions .send(Box::new(|connection| { - let result = transaction(connection); + let result = connection + .transaction() + .map_err(|err| anyhow!(err)) + .and_then(|transaction| { + let result = f(&transaction)?; + transaction.commit()?; + Ok(result) + }); let _ = tx.send(result); })) .await